[jboss-svn-commits] JBL Code SVN: r12724 - 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 Jun 20 21:26:45 EDT 2007
Author: michael.neale at jboss.com
Date: 2007-06-20 21:26:45 -0400 (Wed, 20 Jun 2007)
New Revision: 12724
Added:
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
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseManagerTest.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAssemblerTest.java
Log:
JBRULES-752 Rule agent
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java 2007-06-20 23:20:17 UTC (rev 12723)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java 2007-06-21 01:26:45 UTC (rev 12724)
@@ -46,13 +46,12 @@
/**
* Perform the scan, adding in any packages changed to the rulebase.
- * It will call remove package
- * @throws ClassNotFoundException
- * @throws IOException
- * @throws FileNotFoundException
+ * If there was an error reading the packages, this will not fail, it will
+ * just do nothing (as there may be a temporary IO issue).
*/
void updateRuleBase(RuleBase rb, boolean removeExistingPackages) {
Package[] changes = getChangeSet();
+ if (changes == null) return;
for ( int i = 0; i < changes.length; i++ ) {
Package p = changes[i];
if ( removeExistingPackages ) {
@@ -95,13 +94,16 @@
for ( int i = 0; i < files.length; i++ ) {
File f = files[i];
if ( hasChanged( f.getPath(), this.lastUpdated, f.lastModified() ) ) {
- list.add( readPackage( f ) );
+ Package p = readPackage( f );
+ if (p == null) return null;
+ list.add( p );
}
}
return (Package[]) list.toArray( new Package[list.size()] );
}
- public static Package readPackage(File pkgFile) {
+ private static Package readPackage(File pkgFile) {
+
Package p1_ = null;
ObjectInputStream in;
try {
@@ -109,11 +111,11 @@
p1_ = (Package) in.readObject();
in.close();
} catch ( FileNotFoundException e ) {
- throw new RuntimeDroolsException("Unable to open file: [" + pkgFile.getPath() + "]", e);
+ //throw new RuntimeDroolsException("Unable to open file: [" + pkgFile.getPath() + "]", e);
} catch ( IOException e ) {
- throw new RuntimeDroolsException("Unable to open file: [" + pkgFile.getPath() + "]", e);
+ //throw new RuntimeDroolsException("Unable to open file: [" + pkgFile.getPath() + "] ", e);
} catch ( ClassNotFoundException e ) {
- throw new RuntimeDroolsException("Unable to load package from file: [" + pkgFile.getPath() + "]", e);
+ //throw new RuntimeDroolsException("Unable to load package from file: [" + pkgFile.getPath() + "]", e);
}
return p1_;
}
@@ -134,5 +136,14 @@
}
}
+
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ for ( int i = 0; i < files.length; i++ ) {
+ File f= files[i];
+ buf.append( f.getPath() + " " );
+ }
+ return buf.toString();
+ }
}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java (from rev 12704, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java 2007-06-21 01:26:45 UTC (rev 12724)
@@ -0,0 +1,142 @@
+package org.drools.agent;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.StringTokenizer;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.util.BinaryRuleBaseLoader;
+
+/**
+ * This manages a single rulebase, based on the properties given
+ * This one does most of the actual work !
+ *
+ * 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
+ *
+ * <code>file</code>: a space seperated listing of files that make up the
+ * packages of the rulebase.
+ *
+ * ...
+ *
+ * @author Michael Neale
+ */
+public class RuleBaseAgent {
+
+ /**
+ * Following are property keys to be used in the property
+ * config file.
+ */
+ public static final String NEW_INSTANCE = "newInstance";
+ public static final String FILES = "file";
+ //public static final String DIRECTORIES = "dir";
+ //public static final String URIS = "uri";
+ public static final String POLL_INTERVAL = "poll";
+
+ /**
+ * This is true if the rulebase is created anew each time.
+ */
+ private boolean newInstance;
+
+ /**
+ * The rule base that is being managed.
+ */
+ private RuleBase ruleBase;
+ private Timer timer;
+
+ public RuleBaseAgent(Properties config) {
+ boolean newInstance = Boolean.parseBoolean( config.getProperty( NEW_INSTANCE, "false" ) );
+ int secondsToRefresh = Integer.parseInt( config.getProperty( POLL_INTERVAL , "-1") );
+ List files = list( config.getProperty( FILES ) );
+
+ init( newInstance, files, secondsToRefresh );
+ }
+
+ synchronized void init(boolean newInstance, List files, int secondsToRefresh) {
+ this.newInstance = newInstance;
+ final FileScanner fileScan = new FileScanner();
+ fileScan.setFiles( (String[]) files.toArray( new String[files.size()] ) );
+
+ //run it the first time
+ updateRuleBase( fileScan );
+
+
+ if (secondsToRefresh != -1) {
+ int interval = secondsToRefresh * 1000;
+ //now schedule it for polling
+ timer = new Timer();
+ timer.schedule( new TimerTask() {
+ public void run() {
+ updateRuleBase( fileScan );
+ }
+ }, interval, interval );
+ }
+
+ }
+
+
+ private synchronized void updateRuleBase(FileScanner fileScan) {
+ System.err.println("SCANNING FOR CHANGE " + fileScan.toString());
+
+ if (this.newInstance || this.ruleBase == null) {
+ ruleBase = RuleBaseFactory.newRuleBase();
+ }
+ fileScan.updateRuleBase( this.ruleBase, !this.newInstance );
+ }
+
+ /**
+ * Convert a space seperated list into a List of stuff.
+ * @param property
+ * @return
+ */
+ List list(String property) {
+ if ( property == null ) return Collections.EMPTY_LIST;
+ StringTokenizer st = new StringTokenizer( property,
+ "\n\r\t " );
+ List list = new ArrayList();
+ while ( st.hasMoreTokens() ) {
+ list.add( st.nextToken() );
+ }
+ return list;
+ }
+
+ /**
+ * Return a current rulebase.
+ * Depending on the configuration, this may be a new object each time
+ * the rules are updated.
+ *
+ */
+ public RuleBase getRuleBase() {
+ return this.ruleBase;
+ }
+
+ RuleBaseAgent() {
+ }
+
+ /**
+ * Stop the polling (if it is happening)
+ */
+ public void stopPolling() {
+ if (this.timer != null) timer.cancel();
+ timer = null;
+ }
+
+ boolean isNewInstance() {
+ return newInstance;
+ }
+
+ boolean isPolling() {
+ return this.timer != null;
+ }
+
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java 2007-06-20 23:20:17 UTC (rev 12723)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java 2007-06-21 01:26:45 UTC (rev 12724)
@@ -1,94 +0,0 @@
-package org.drools.agent;
-
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Properties;
-import java.util.StringTokenizer;
-
-import org.drools.RuleBase;
-import org.drools.RuleBaseFactory;
-import org.drools.util.BinaryRuleBaseLoader;
-
-/**
- * This manages a single rulebase, based on the properties given
- * This one does most of the actual work !
- *
- * 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
- *
- * <code>file</code>: a space seperated listing of files that make up the
- * packages of the rulebase.
- *
- * ...
- *
- * @author Michael Neale
- */
-public class RuleBaseAgent2 {
-
- /**
- * Following are property keys to be used in the property
- * config file.
- */
- public static final String NEW_INSTANCE = "newInstance";
- public static final String FILES = "file";
- //public static final String DIRECTORIES = "dir";
- //public static final String URIS = "uri";
- public static final String POLL_INTERVAL = "poll";
-
- /**
- * This is true if the rulebase is created anew each time.
- */
- private boolean newInstance;
-
- /**
- * The rule base that is being managed.
- */
- private RuleBase ruleBase;
-
- public RuleBaseAgent2(Properties config) {
- boolean newInstance = Boolean.getBoolean( config.getProperty( NEW_INSTANCE, "false" ) );
- List files = list( config.getProperty( FILES ) );
- init( newInstance, files );
- }
-
- synchronized void init(boolean newInstance, List files) {
- this.newInstance = newInstance;
- FileScanner fileScan = new FileScanner();
- fileScan.setFiles( (String[]) files.toArray( new String[files.size()] ) );
-
- this.ruleBase = RuleBaseFactory.newRuleBase();
- fileScan.updateRuleBase( this.ruleBase, !this.newInstance );
-
- }
-
- List list(String property) {
- if ( property == null ) return Collections.EMPTY_LIST;
- StringTokenizer st = new StringTokenizer( property,
- "\n\r\t " );
- List list = new ArrayList();
- while ( st.hasMoreTokens() ) {
- list.add( st.nextToken() );
- }
- return list;
- }
-
- /**
- * Return a current rulebase.
- * Depending on the configuration, this may be a new object each time
- * the rules are updated.
- *
- */
- public RuleBase getRuleBase() {
- return this.ruleBase;
- }
-
- RuleBaseAgent2() {
- }
-
-}
Copied: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAgentTest.java (from rev 12704, labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseManagerTest.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAgentTest.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAgentTest.java 2007-06-21 01:26:45 UTC (rev 12724)
@@ -0,0 +1,154 @@
+package org.drools.agent;
+
+import java.io.File;
+import java.util.List;
+import java.util.Properties;
+
+import org.drools.RuleBase;
+import org.drools.rule.Package;
+
+import junit.framework.TestCase;
+
+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 );
+ 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 );
+ assertNotNull(result);
+ assertEquals(0, result.size());
+ }
+
+ public void testFiles() throws Exception {
+ File dir = RuleBaseAssemblerTest.getTempDirectory();
+
+ Package p1 = new Package("p1");
+ File p1f = new File(dir, "p1.pkg");
+ RuleBaseAssemblerTest.writePackage( p1, p1f );
+
+ Package p2 = new Package("p2");
+ File p2f = new File(dir, "p2.pkg");
+ RuleBaseAssemblerTest.writePackage( p2, p2f );
+
+ String path = dir.getPath() + "/" + "p1.pkg " + dir.getPath() + "/" + "p2.pkg";
+
+ Properties props = new Properties();
+ props.setProperty( "file", path );
+ RuleBaseAgent ag = new RuleBaseAgent(props);
+ RuleBase rb = ag.getRuleBase();
+ assertNotNull(rb);
+ assertEquals(2, rb.getPackages().length);
+
+ assertFalse(ag.isPolling());
+
+ props.setProperty( "poll", "1" );
+ ag = new RuleBaseAgent(props);
+ assertTrue(ag.isPolling());
+
+ ag.stopPolling();
+ assertFalse(ag.isPolling());
+
+ }
+
+ public void testPollingFilesRuleBaseUpdate() throws Exception {
+ //RuleBaseAssemblerTest.clearTempDirectory();
+ File dir = RuleBaseAssemblerTest.getTempDirectory();
+
+ Package p1 = new Package("p1");
+ File p1f = new File(dir, "p42_.pkg");
+ RuleBaseAssemblerTest.writePackage( p1, p1f );
+
+ String path = dir.getPath() + "/" + "p42_.pkg";
+
+ Properties props = new Properties();
+ props.setProperty( "file", path );
+ props.setProperty( "poll", "1" );
+ RuleBaseAgent ag = new RuleBaseAgent(props);
+
+ assertTrue(ag.isPolling());
+ RuleBase rb = ag.getRuleBase();
+ assertEquals(1, rb.getPackages().length);
+ assertEquals(0, rb.getPackages()[0].getGlobals().size());
+
+ p1.addGlobal( "goo", String.class );
+
+ RuleBaseAssemblerTest.writePackage( p1, p1f );
+
+ RuleBase rb_ = ag.getRuleBase();
+ assertSame(rb, rb_);
+ assertEquals(1, rb.getPackages().length);
+ assertEquals(0, rb.getPackages()[0].getGlobals().size());
+
+ Thread.sleep( 2000 );
+
+ RuleBase rb2 = ag.getRuleBase();
+ assertSame(rb, rb2);
+
+ assertEquals(1, rb2.getPackages().length);
+ assertEquals(1, rb2.getPackages()[0].getGlobals().size());
+
+ //now check subsequent changes
+ p1.addGlobal( "goo2", String.class );
+ System.err.println("-->WRITING CHANGE");
+ RuleBaseAssemblerTest.writePackage( p1, p1f );
+ System.err.println("-->WROTE CHANGE");
+ RuleBase rb2_ = ag.getRuleBase();
+ assertSame(rb2_, rb2);
+ assertEquals(1, rb2_.getPackages().length);
+ assertEquals(1, rb2_.getPackages()[0].getGlobals().size());
+
+ Thread.sleep( 2000 );
+
+ RuleBase rb3 = ag.getRuleBase();
+ assertSame(rb3, rb2);
+
+ assertEquals(1, rb3.getPackages().length);
+ assertEquals(2, rb3.getPackages()[0].getGlobals().size());
+
+
+ ag.stopPolling();
+ }
+
+ public void testPollingFilesRuleBaseReplace() throws Exception {
+ File dir = RuleBaseAssemblerTest.getTempDirectory();
+
+ Package p1 = new Package("p1");
+ File p1f = new File(dir, "p43_.pkg");
+ RuleBaseAssemblerTest.writePackage( p1, p1f );
+
+ String path = dir.getPath() + "/" + "p43_.pkg";
+
+ Properties props = new Properties();
+ props.setProperty( "file", path );
+ props.setProperty( "poll", "1" );
+ props.setProperty( "newInstance", "true" );
+ RuleBaseAgent ag = new RuleBaseAgent(props);
+ assertTrue(ag.isNewInstance());
+ assertTrue(ag.isPolling());
+ RuleBase rb = ag.getRuleBase();
+ assertEquals(1, rb.getPackages().length);
+
+ RuleBase rb_ = ag.getRuleBase();
+ assertSame(rb, rb_);
+
+ RuleBaseAssemblerTest.writePackage( p1, p1f );
+
+ Thread.sleep( 2100 );
+
+ rb_ = ag.getRuleBase();
+
+ assertNotSame( rb, rb_ );
+
+ ag.stopPolling();
+
+
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAssemblerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAssemblerTest.java 2007-06-20 23:20:17 UTC (rev 12723)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseAssemblerTest.java 2007-06-21 01:26:45 UTC (rev 12724)
@@ -55,7 +55,7 @@
}
public static File getTempDirectory() {
- File f = new File("__temp_test_drools_packages");
+ File f = tempDir();
if (f.exists()) {
if (f.isFile()) {
throw new IllegalStateException("The temp directory exists as a file. Nuke it now !");
@@ -67,6 +67,10 @@
}
return f;
}
+
+ private static File tempDir() {
+ return new File("__temp_test_drools_packages");
+ }
public static boolean deleteDir(File dir) {
@@ -75,6 +79,7 @@
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
+ //throw new RuntimeException("Unable to delete !");
return false;
}
}
@@ -82,6 +87,11 @@
// The directory is now empty so delete it
return dir.delete();
+ }
+
+ public static void clearTempDirectory() {
+ deleteDir( tempDir() );
+
}
}
Deleted: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseManagerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseManagerTest.java 2007-06-20 23:20:17 UTC (rev 12723)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseManagerTest.java 2007-06-21 01:26:45 UTC (rev 12724)
@@ -1,24 +0,0 @@
-package org.drools.agent;
-
-import java.util.List;
-
-import junit.framework.TestCase;
-
-public class RuleBaseManagerTest extends TestCase {
-
- public void testLists() {
- RuleBaseAgent2 mgr = new RuleBaseAgent2();
- String s = "\tfoo.bar\n baz.bar\t whee ";
- List result = mgr.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 );
- assertNotNull(result);
- assertEquals(0, result.size());
- }
-
-}
More information about the jboss-svn-commits
mailing list