[jboss-svn-commits] JBL Code SVN: r14108 - in labs/jbossrules/trunk/drools-core/src: test/java/org/drools/agent and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Aug 8 21:52:30 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-08-08 21:52:29 -0400 (Wed, 08 Aug 2007)
New Revision: 14108

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/MockRuleAgent.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleAgentTest.java
Log:
JBRULES-1067 Allow RuleAgent to take a RuleBaseConfiguration to configure the RuleBase it creates

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java	2007-08-08 23:02:42 UTC (rev 14107)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java	2007-08-09 01:52:29 UTC (rev 14108)
@@ -16,6 +16,7 @@
 import java.util.TimerTask;
 
 import org.drools.RuleBase;
+import org.drools.RuleBaseConfiguration;
 import org.drools.RuleBaseFactory;
 import org.drools.RuntimeDroolsException;
 import org.drools.rule.Package;
@@ -103,6 +104,11 @@
      * The rule base that is being managed.
      */
     private RuleBase           ruleBase;
+    
+    /**
+     * the configuration for the RuleBase
+     */
+    private RuleBaseConfiguration ruleBaseConf;
 
     /**
      * The timer that is used to monitor for changes and deal with them. 
@@ -136,21 +142,37 @@
      * for changes).
      */
     public static RuleAgent newRuleAgent(Properties config) {
-        RuleAgent agent = new RuleAgent();
-        agent.init( config );
-        return agent;
+        return newRuleAgent(config, null, null);
     }
     
     /**
+     * Properties configured to load up packages into a rulebase with the provided
+     * configuration (and monitor them for changes). 
+     */
+    public static RuleAgent newRuleAgent(Properties config, RuleBaseConfiguration ruleBaseConf) {
+        return newRuleAgent(config, null, ruleBaseConf);
+    }    
+    
+    /**
      * This allows an optional listener to be passed in.
      * The default one prints some stuff out to System.err only when really needed.
      */
     public static RuleAgent newRuleAgent(Properties config, AgentEventListener listener) {
-        RuleAgent agent = new RuleAgent();
-        agent.listener = listener;
+        return newRuleAgent(config, listener, null);
+    }
+    
+    /**
+     * This allows an optional listener to be passed in.
+     * The default one prints some stuff out to System.err only when really needed.
+     */
+    public static RuleAgent newRuleAgent(Properties config, AgentEventListener listener, RuleBaseConfiguration ruleBaseConf) {
+        RuleAgent agent = new RuleAgent(ruleBaseConf);
+        if ( listener != null ) {
+            agent.listener = listener;
+        }
         agent.init(config);
         return agent;
-    }
+    }    
 
 
 
@@ -185,27 +207,35 @@
     }
 
     /**
-     * Pass in the name and full path to a config file that is on the classpath.
+     * Pass in the name and full path to a config file that is on the classpath. 
      */
     public static RuleAgent newRuleAgent(String propsFileName) {
-        RuleAgent agent = new RuleAgent();
-        agent.init( agent.loadFromProperties( propsFileName ) );
-        return agent;
+        return newRuleAgent( loadFromProperties( propsFileName ) );
     }
     
     /**
-     * This takes in an optional listener.
-     * Listener must not be null in this case.
+     * Pass in the name and full path to a config file that is on the classpath. 
+     */    
+    public static RuleAgent newRuleAgent(String propsFileName, RuleBaseConfiguration ruleBaseConfiguration) {
+        return newRuleAgent( loadFromProperties( propsFileName ), ruleBaseConfiguration );
+    }    
+    
+    /**
+     * This takes in an optional listener. Listener must not be null in this case.
      */
     public static RuleAgent newRuleAgent(String propsFileName, AgentEventListener listener) {
-        RuleAgent ag = new RuleAgent();
-        ag.listener = listener;
-        ag.init( ag.loadFromProperties( propsFileName ) );
-        return ag;
+        return newRuleAgent( loadFromProperties( propsFileName ), listener );
     }
+    
+    /**
+     * This takes in an optional listener and RuleBaseConfiguration. Listener must not be null in this case.
+     */    
+    public static RuleAgent newRuleAgent(String propsFileName, AgentEventListener listener, RuleBaseConfiguration ruleBaseConfiguration) {
+        return newRuleAgent( loadFromProperties( propsFileName ), listener, ruleBaseConfiguration );
+    }    
 
-    Properties loadFromProperties(String propsFileName) {
-        InputStream in = this.getClass().getResourceAsStream( propsFileName );
+    static Properties loadFromProperties(String propsFileName) {
+        InputStream in = RuleAgent.class.getResourceAsStream( propsFileName );
         Properties props = new Properties();
         try {
             props.load( in );
@@ -274,7 +304,7 @@
             if (this.newInstance) {
                 listener.info( "Creating a new rulebase as per settings." );
                 //blow away old
-                this.ruleBase = RuleBaseFactory.newRuleBase();
+                this.ruleBase = RuleBaseFactory.newRuleBase( this.ruleBaseConf );
                 
                 //need to store ALL packages
                 for ( Iterator iter = changedPackages.iterator(); iter.hasNext(); ) {
@@ -293,7 +323,7 @@
 
     private synchronized Package[] checkForChanges(PackageProvider prov) {
         listener.debug( "SCANNING FOR CHANGE " + prov.toString() );
-        if (this.ruleBase == null) ruleBase = RuleBaseFactory.newRuleBase();
+        if (this.ruleBase == null) ruleBase = RuleBaseFactory.newRuleBase( this.ruleBaseConf );
         Package[] changes = prov.loadPackageChanges();
         return changes;
     }
@@ -324,7 +354,12 @@
         return this.ruleBase;
     }
 
-    RuleAgent() {
+    RuleAgent(RuleBaseConfiguration ruleBaseConf) {
+        if ( ruleBaseConf == null ) {
+            this.ruleBaseConf = new RuleBaseConfiguration();
+        } else {
+            this.ruleBaseConf = ruleBaseConf;
+        }
     }
 
     /**

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/MockRuleAgent.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/MockRuleAgent.java	2007-08-08 23:02:42 UTC (rev 14107)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/MockRuleAgent.java	2007-08-09 01:52:29 UTC (rev 14108)
@@ -2,13 +2,17 @@
 
 import java.util.Properties;
 
+import org.drools.RuleBaseConfiguration;
+
 public class MockRuleAgent extends RuleAgent {
 
     
     public boolean refreshCalled = false;
     
+    public MockRuleAgent() {
+        super( new RuleBaseConfiguration() );
+    }
 
-
     public void refreshRuleBase() {
         refreshCalled = true;
         

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleAgentTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleAgentTest.java	2007-08-08 23:02:42 UTC (rev 14107)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleAgentTest.java	2007-08-09 01:52:29 UTC (rev 14108)
@@ -9,6 +9,8 @@
 import java.util.Random;
 
 import org.drools.RuleBase;
+import org.drools.RuleBaseConfiguration;
+import org.drools.common.InternalRuleBase;
 import org.drools.rule.Package;
 
 import junit.framework.TestCase;
@@ -212,12 +214,38 @@
         
         RuleBase rb = ag.getRuleBase();
         assertNotNull(rb);
-        assertEquals(1, rb.getPackages().length);
+        assertEquals(1, rb.getPackages().length);        
+    }
+    
+    public void testCustomRuleBaseConfiguration() throws Exception {
+        final File dir = RuleBaseAssemblerTest.getTempDirectory();
         
+        Random rnd = new Random(System.currentTimeMillis());
+        
+        final Package p1 = new Package("p1");
+        final File p1f = new File(dir, rnd.nextLong() + ".pkg");
+        RuleBaseAssemblerTest.writePackage( p1, p1f );
+        
+        String path = p1f.getPath();
+        
+        Properties props = new Properties();
+        props.setProperty( "file", path );
+        
+        // Check a default value for the RuleBase's RuleBaseConfiguration        
+        RuleAgent agent = RuleAgent.newRuleAgent( props );
+        RuleBaseConfiguration conf = ((InternalRuleBase) agent.getRuleBase()).getConfiguration();
+        assertEquals( false, conf.isSequential() );
+        
+        // Pass in a RuleBaseConfiguration and make sure the RuleBase was created with it
+        conf = new RuleBaseConfiguration();
+        conf.setSequential( true );
+        agent = RuleAgent.newRuleAgent( props, conf );
+        conf = ((InternalRuleBase) agent.getRuleBase()).getConfiguration();
+        assertEquals( true, conf.isSequential() );        
     }
     
     public void testLoadSampleConfig() {
-        RuleAgent ag = new RuleAgent();
+        RuleAgent ag = new RuleAgent( new RuleBaseConfiguration()  );
         Properties props = ag.loadFromProperties( "/sample-agent-config.properties" );
         assertEquals("10", props.getProperty( RuleAgent.POLL_INTERVAL ));
         assertEquals("/home/packages", props.getProperty( RuleAgent.DIRECTORY ));
@@ -237,7 +265,7 @@
     }
     
     public void testEventListenerSetup() throws Exception {
-        RuleAgent ag = new RuleAgent();
+        RuleAgent ag = new RuleAgent( new RuleBaseConfiguration() );
         assertNotNull(ag.listener);
 
         final String[] name = new String[1];
@@ -272,9 +300,7 @@
 
         assertEquals(list, ag.listener);
         assertEquals("poo", name[0]);
-        ag.stopPolling();
-        
-        
+        ag.stopPolling();                
     }
     
     
@@ -353,6 +379,10 @@
         public int secondsToRefresh;
         public List provs;
         public boolean newInstance;
+        
+        public AnotherRuleAgentMock() {
+            super( new RuleBaseConfiguration() );
+        }        
 
         synchronized void configure(boolean newInstance, List provs, int secondsToRefresh) {
             this.newInstance = newInstance;




More information about the jboss-svn-commits mailing list