[jboss-svn-commits] JBL Code SVN: r12733 - 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
Thu Jun 21 01:22:49 EDT 2007


Author: michael.neale at jboss.com
Date: 2007-06-21 01:22:49 -0400 (Thu, 21 Jun 2007)
New Revision: 12733

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAgentTest.java
Log:
JBRULES-752 refactoring love

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java	2007-06-21 05:19:55 UTC (rev 12732)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java	2007-06-21 05:22:49 UTC (rev 12733)
@@ -1,5 +1,7 @@
 package org.drools.agent;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -22,13 +24,20 @@
  * CONFIG OPTIONS:
  *  <code>newInstance</code>: means that each time the rules are changed
  *   a new instance of the rulebase is created (as opposed to updated in place)
- *   the default is to update in place. DEFAULT: true
+ *   the default is to update in place. DEFAULT: false. If you set this to true, 
+ *   then you will need to call getRuleBase() each time you want to use it. If it is false, 
+ *   then it means you can keep your reference to the rulebase and it will be updated automatically
+ *   (as well as any stateful sessions). 
  *
+ *  <code>poll</code>The number of seconds to poll for changes. Polling 
+ *  happens in a background thread.
+ *
  *  <code>file</code>: a space seperated listing of files that make up the 
- *  packages of the rulebase. 
+ *  packages of the rulebase. Each package can only be in one file. You can't have 
+ *  packages spread across files.
  *  
  *  <code>dir</code>: a single file system directory to monitor for packages.
- *  ...
+ *  As with files, each package must be in its own file.
  * 
  * @author Michael Neale
  */
@@ -50,13 +59,14 @@
     public static Map          PACKAGE_PROVIDERS = new HashMap() {
                                                      {
                                                          put( FILES, FileScanner.class );
+                                                         put( DIRECTORY, DirectoryScanner.class);
                                                      }
                                                  };
 
     /**
      * This is true if the rulebase is created anew each time.
      */
-    private boolean            newInstance;
+    boolean                    newInstance;
 
     /**
      * The rule base that is being managed.
@@ -67,13 +77,26 @@
      * The timer that is used to monitor for changes and deal with them. 
      */
     private Timer              timer;
+    
+    /**
+     * The providers that actually do the work.
+     */
+    ArrayList                  providers;
 
+    /**
+     * Properties configured to load up packages into a rulebase (and monitor them
+     * for changes).
+     */
     public RuleBaseAgent(
                          Properties config) {
+        init( config );
+    }
+
+    private void init(Properties config) {
         boolean newInstance = Boolean.parseBoolean( config.getProperty( NEW_INSTANCE, "false" ) );
         int secondsToRefresh = Integer.parseInt( config.getProperty( POLL_INTERVAL, "-1" ) );
 
-        List providers = new ArrayList();
+        providers = new ArrayList();
 
         for ( Iterator iter = config.keySet().iterator(); iter.hasNext(); ) {
             String key = (String) iter.next();
@@ -83,9 +106,28 @@
             }
         }
 
-        init( newInstance, providers, secondsToRefresh );
+        configure( newInstance, providers, secondsToRefresh );
     }
+    
+    /**
+     * Pass in the name and full path to a config file that is on the classpath.
+     */
+    public RuleBaseAgent(String propsFileName) {
+        init(loadFromProperties( propsFileName ));        
+    }
 
+    Properties loadFromProperties(String propsFileName) {
+        InputStream in = this.getClass().getResourceAsStream( propsFileName );
+        Properties props = new Properties();
+        try {
+            props.load( in );
+            return props;
+            
+        } catch ( IOException e ) {
+            throw new RuntimeDroolsException("Unable to load properties. Needs to be the path and name of a config file on your classpath.",e);
+        }
+    }
+
     /**
      * Return a configured provider ready to go.
      */
@@ -107,14 +149,11 @@
         }
     }
 
-    synchronized void init(boolean newInstance, final List providers, int secondsToRefresh) {
+    synchronized void configure(boolean newInstance, final List providers, int secondsToRefresh) {
         this.newInstance = newInstance;
 
         //run it the first time for each.
-        for ( Iterator iter = providers.iterator(); iter.hasNext(); ) {
-            PackageProvider prov = (PackageProvider) iter.next();
-            updateRuleBase( prov );
-        }
+        refreshRuleBase(  );
 
         if ( secondsToRefresh != -1 ) {
             int interval = secondsToRefresh * 1000;
@@ -122,16 +161,21 @@
             timer = new Timer( true );
             timer.schedule( new TimerTask() {
                 public void run() {
-                    for ( Iterator iter = providers.iterator(); iter.hasNext(); ) {
-                        PackageProvider prov = (PackageProvider) iter.next();
-                        updateRuleBase( prov );
-                    }
+                    refreshRuleBase();
+
                 }
             }, interval, interval );
         }
 
     }
 
+    public void refreshRuleBase() {
+        for ( Iterator iter = providers.iterator(); iter.hasNext(); ) {
+            PackageProvider prov = (PackageProvider) iter.next();
+            updateRuleBase( prov );
+        }
+    }
+
     private synchronized void updateRuleBase(PackageProvider prov) {
         System.err.println( "SCANNING FOR CHANGE " + prov.toString() );
         if ( this.newInstance || this.ruleBase == null ) {

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAgentTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAgentTest.java	2007-06-21 05:19:55 UTC (rev 12732)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAgentTest.java	2007-06-21 05:22:49 UTC (rev 12733)
@@ -12,16 +12,15 @@
 public class RuleBaseAgentTest extends TestCase {
 
     public void testLists() {
-        RuleBaseAgent mgr = new RuleBaseAgent();
         String s = "\tfoo.bar\n baz.bar\t whee ";
-        List result = mgr.list( s );
+        List result = RuleBaseAgent.list( s );
         assertEquals(3, result.size());
         assertEquals("foo.bar", result.get( 0 ));
         assertEquals("baz.bar", result.get(1));
         assertEquals("whee", result.get(2));
         
         s = null;
-        result = mgr.list( s );
+        result = RuleBaseAgent.list( s );
         assertNotNull(result);
         assertEquals(0, result.size());
     }
@@ -151,4 +150,44 @@
          
     }
     
+    
+    public void testDirectory() throws Exception {
+        File dir = RuleBaseAssemblerTest.getTempDirectory();
+        
+        Package p1 = new Package("p1");
+        File p1f = new File(dir, "p43_.pkg");
+        RuleBaseAssemblerTest.writePackage( p1, p1f );
+
+        Properties props = new Properties();
+        props.setProperty( RuleBaseAgent.DIRECTORY, dir.getPath() );
+        RuleBaseAgent ag = new RuleBaseAgent(props);
+        
+        ag.refreshRuleBase();
+        
+        RuleBase rb = ag.getRuleBase();
+        assertNotNull(rb);
+        assertEquals(1, rb.getPackages().length);
+        
+    }
+    
+    public void testLoadSampleConfig() {
+        RuleBaseAgent ag = new RuleBaseAgent();
+        Properties props = ag.loadFromProperties( "/sample-agent-config.properties" );
+        assertEquals("10", props.getProperty( RuleBaseAgent.POLL_INTERVAL ));
+        assertEquals("/home/packages", props.getProperty( RuleBaseAgent.DIRECTORY ));
+        assertEquals("true", props.getProperty( RuleBaseAgent.NEW_INSTANCE ));
+        assertEqualsIgnoreWhitespace( "/foo/bar.pkg /wee/waa.pkg /wee/waa2.pkg", props.getProperty( RuleBaseAgent.FILES ));
+    }
+    
+    private void assertEqualsIgnoreWhitespace(final String expected,
+                                              final String actual) {
+        final String cleanExpected = expected.replaceAll( "\\s+",
+                                                          "" );
+        final String cleanActual = actual.replaceAll( "\\s+",
+                                                      "" );
+
+        assertEquals( cleanExpected,
+                      cleanActual );
+    }
+    
 }




More information about the jboss-svn-commits mailing list