[jboss-svn-commits] JBL Code SVN: r12704 - 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 00:13:53 EDT 2007
Author: michael.neale at jboss.com
Date: 2007-06-20 00:13:53 -0400 (Wed, 20 Jun 2007)
New Revision: 12704
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgentCache.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseManagerTest.java
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.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/FileScannerTest.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 03:30:24 UTC (rev 12703)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java 2007-06-20 04:13:53 UTC (rev 12704)
@@ -51,11 +51,13 @@
* @throws IOException
* @throws FileNotFoundException
*/
- void updateRuleBase(RuleBase rb) throws FileNotFoundException, IOException, ClassNotFoundException {
+ void updateRuleBase(RuleBase rb, boolean removeExistingPackages) {
Package[] changes = getChangeSet();
for ( int i = 0; i < changes.length; i++ ) {
Package p = changes[i];
- removePackage(p.getName(), rb);
+ if ( removeExistingPackages ) {
+ removePackage( p.getName(), rb );
+ }
try {
rb.addPackage( p );
} catch ( Exception e ) {
@@ -64,12 +66,16 @@
}
}
+ /**
+ * Remove the package from the rulebase if it exists in it.
+ * If it does not, does nothing.
+ */
private void removePackage(String name, RuleBase rb) {
Package[] ps = rb.getPackages();
- if (ps == null) return;
+ if ( ps == null ) return;
for ( int i = 0; i < ps.length; i++ ) {
Package p = ps[i];
- if (p.getName().equals( name )) {
+ if ( p.getName().equals( name ) ) {
rb.removePackage( name );
return;
}
@@ -83,23 +89,32 @@
* @throws IOException
* @throws FileNotFoundException
*/
- private Package[] getChangeSet() throws FileNotFoundException, IOException, ClassNotFoundException {
+ private Package[] getChangeSet() {
+ if ( this.files == null ) return new Package[0];
List list = new ArrayList();
for ( int i = 0; i < files.length; i++ ) {
File f = files[i];
if ( hasChanged( f.getPath(), this.lastUpdated, f.lastModified() ) ) {
- list.add( readPackage(f) );
+ list.add( readPackage( f ) );
}
}
return (Package[]) list.toArray( new Package[list.size()] );
}
- public static Package readPackage(File pkgFile) throws IOException,
- FileNotFoundException,
- ClassNotFoundException {
- ObjectInputStream in = new DroolsObjectInputStream( new FileInputStream( pkgFile ) );
- Package p1_ = (Package) in.readObject();
- in.close();
+ public static Package readPackage(File pkgFile) {
+ Package p1_ = null;
+ ObjectInputStream in;
+ try {
+ in = new DroolsObjectInputStream( new FileInputStream( pkgFile ) );
+ p1_ = (Package) in.readObject();
+ in.close();
+ } catch ( FileNotFoundException e ) {
+ throw new RuntimeDroolsException("Unable to open file: [" + pkgFile.getPath() + "]", e);
+ } catch ( IOException 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);
+ }
return p1_;
}
Deleted: 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-20 03:30:24 UTC (rev 12703)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java 2007-06-20 04:13:53 UTC (rev 12704)
@@ -1,33 +0,0 @@
-package org.drools.agent;
-
-import java.util.Properties;
-
-import org.drools.RuleBase;
-
-public class RuleBaseAgent {
-
-
- private RuleBaseAgent() {
-
- }
-
- public static RuleBaseAgent instance() {
- return null;
- }
-
- /**
- * Return a rulebase by name to its config file.
- */
- public RuleBase getRuleBase(String name) {
- throw new UnsupportedOperationException("Not done yet !");
- }
-
-
- /** Pass in a pre populated properties file */
- public RuleBase getRuleBase(Properties props) {
- return null;
- }
-
-
-
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java (from rev 12681, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent2.java 2007-06-20 04:13:53 UTC (rev 12704)
@@ -0,0 +1,94 @@
+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/main/java/org/drools/agent/RuleBaseAgentCache.java (from rev 12681, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgentCache.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgentCache.java 2007-06-20 04:13:53 UTC (rev 12704)
@@ -0,0 +1,41 @@
+package org.drools.agent;
+
+import java.util.Properties;
+
+import org.drools.RuleBase;
+
+public class RuleBaseAgentCache {
+
+
+ private static final RuleBaseAgentCache INSTANCE = new RuleBaseAgentCache();
+
+ private RuleBaseAgentCache() {
+ }
+
+ public static RuleBaseAgentCache instance() {
+ return INSTANCE;
+ }
+
+ /**
+ * Return a rulebase by name.
+ * This name may be the name of a pre configured rulebase,
+ * or the name of a config properties file to be found
+ * on the classpath.
+ */
+ public RuleBase getRuleBase(String name) {
+ throw new UnsupportedOperationException("Not done yet !");
+ }
+
+
+ /**
+ * Pass in a pre populated properties file.
+ * It will then map this config to the given name for future use.
+ * @return A RuleBase ready to go.
+ */
+ public RuleBase configureRuleBase(String name, Properties props) {
+ return null;
+ }
+
+
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/FileScannerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/FileScannerTest.java 2007-06-20 03:30:24 UTC (rev 12703)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/FileScannerTest.java 2007-06-20 04:13:53 UTC (rev 12704)
@@ -31,7 +31,6 @@
}
public void testScanAndLoad() throws Exception {
-
Package p1 = new Package("p1");
Package p2 = new Package("p2");
@@ -42,38 +41,36 @@
RuleBaseAssemblerTest.writePackage( p1, p1f );
RuleBaseAssemblerTest.writePackage( p2, p2f);
-
-
-
-
FileScanner scan = new FileScanner();
scan.setFiles( new String[] {p1f.getPath(), p2f.getPath()} );
-
-
RuleBase rb = RuleBaseFactory.newRuleBase();
- scan.updateRuleBase( rb );
+ scan.updateRuleBase( rb, true );
assertEquals(2, rb.getPackages().length);
assertEquals("p1", rb.getPackages()[0].getName());
assertEquals("p2", rb.getPackages()[1].getName());
- scan.updateRuleBase( rb );
+ scan.updateRuleBase( rb, true );
assertEquals(2, rb.getPackages().length);
assertEquals("p1", rb.getPackages()[0].getName());
assertEquals("p2", rb.getPackages()[1].getName());
RuleBaseAssemblerTest.writePackage( p2, p2f );
- scan.updateRuleBase( rb );
+ scan.updateRuleBase( rb, true );
assertEquals(2, rb.getPackages().length);
assertEquals("p1", rb.getPackages()[0].getName());
assertEquals("p2", rb.getPackages()[1].getName());
-
-
-
}
+ public void testEmptyList() throws Exception {
+ FileScanner scan = new FileScanner();
+ RuleBase rb = RuleBaseFactory.newRuleBase();
+ scan.updateRuleBase( rb, true );
+ assertEquals(0, rb.getPackages().length);
+ }
+
}
Added: 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 (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseManagerTest.java 2007-06-20 04:13:53 UTC (rev 12704)
@@ -0,0 +1,24 @@
+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());
+ }
+
+}
Property changes on: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/RuleBaseManagerTest.java
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the jboss-svn-commits
mailing list