[jboss-svn-commits] JBL Code SVN: r26506 - in labs/jbossrules/trunk: drools-api/src/main/java/org/drools/runtime/rule and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed May 13 13:10:13 EDT 2009
Author: mark.proctor at jboss.com
Date: 2009-05-13 13:10:13 -0400 (Wed, 13 May 2009)
New Revision: 26506
Modified:
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/runtime/help/BatchExecutionHelper.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/runtime/rule/QueryResultsRow.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/agent/KnowledgeAgentTest.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/impl/StatelessKnowledgeSessionImpl.java
Log:
JBRULES-2090 StatelessKnowledgeSessions created by KnowledgeAgent causes NullPointerException when used in a pipeline
-now checks agent, to make sure latest rulebase is set.
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/runtime/help/BatchExecutionHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/runtime/help/BatchExecutionHelper.java 2009-05-13 16:49:12 UTC (rev 26505)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/runtime/help/BatchExecutionHelper.java 2009-05-13 17:10:13 UTC (rev 26506)
@@ -12,7 +12,7 @@
* </p>
*
* <p>
- * This is very useful for scripting stateless of stateful knowledge sessions, especially when services are involed.
+ * This is very useful for scripting stateless of stateful knowledge sessions, especially when services are involved.
* </p>
*
* <p>
@@ -38,7 +38,10 @@
* </batch-execution>
* </pre>
* <p>
- * The insert element supports an 'out-identifier' attribute, this means the insert object will also be returned as part of the <batch-execution-results> payload.
+ * The insert element supports an 'out-identifier' attribute, this means the inserted object's FactHandle will be returned
+ * and optionally the object itself as part of the <batch-execution-results> payload. To return the object use the attribute 'return-object' which takes a boolean
+ * 'true'|'false' value, by default this is true.
+ * The
* </p>
* <pre>
* <batch-execution >
@@ -49,7 +52,9 @@
* </pre>
*
* <p>
- * It's also possible to insert a collection of objects using the <insert-elements> element, however this command does not support an out-identifier.
+ * It's also possible to insert a collection of objects using the <insert-elements> element, here each element is inserted in turn. This command also support's an
+ * 'out-identifier' attribute which returns the FactHandle's in a Collection, of the same order that the objects where inserted. 'return-object' is also supported to optionally
+ * return the inserted objects, again they will be in a collection of the same order they where inserted.
* The org.domain.UserClass is just an illustrative user object that xstream would serialise.
* </p>
* <pre>
@@ -110,6 +115,13 @@
* </batch-execution>
* </pre>
*
+ * <p>Specific objects can be retrieved using the FactHandle:</p>
+ * <pre>
+ * <batch-execution>
+ * <get-object out-identifier='outStilton' factHandle='" + factHandle.toExternalForm() + "' />
+ * </batch-execution>
+ * </pre>
+ *
* <p>
* While the 'out' attribute is useful in returning specific instances as a result payload, we often wish to run actual querries. Both parameter
* and parameterless querries are supported. The 'name' attribute is the name of the query to be called, and the 'out-identifier' is the identifier
@@ -140,6 +152,27 @@
* </batch-execution>
* </pre>
*
+ * <p>SignelEvent</p>
+ * <pre>
+ * <signal-event process-instance-id='1' event-type='MyEvent'>
+ * <string>MyValue</string>
+ * </signal-event>
+ * </pre>
+ *
+ * <p>CompleteWorkItem</p>
+ * <pre>
+ * <complete-work-item id='" + workItem.getId() + "' >
+ * <result identifier='Result'>
+ * <string>SomeOtherString</string>
+ * </result>
+ * </complete-work-item>
+ * </pre>
+ *
+ * <p>AbortWorkItem</p>
+ * <pre>
+ * <abort-work-item id='21' />
+ * </pre>
+ *
* <p>
* Support for more commands will be added over time.
* </p>
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/runtime/rule/QueryResultsRow.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/runtime/rule/QueryResultsRow.java 2009-05-13 16:49:12 UTC (rev 26505)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/runtime/rule/QueryResultsRow.java 2009-05-13 17:10:13 UTC (rev 26506)
@@ -13,5 +13,10 @@
*/
public Object get(String identifier);
+ /**
+ * Return the FactHandle associated with the given identifier
+ * @param identifier
+ * @return
+ */
public FactHandle getFactHandle(String identifier);
}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/agent/KnowledgeAgentTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/agent/KnowledgeAgentTest.java 2009-05-13 16:49:12 UTC (rev 26505)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/agent/KnowledgeAgentTest.java 2009-05-13 17:10:13 UTC (rev 26506)
@@ -20,6 +20,10 @@
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
+import org.drools.agent.KnowledgeAgent;
+import org.drools.agent.KnowledgeAgentConfiguration;
+import org.drools.agent.KnowledgeAgentFactory;
+import org.drools.agent.KnowledgeAgentTest.ResultHandlerImpl;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
@@ -31,6 +35,11 @@
import org.drools.io.impl.ResourceChangeScannerImpl;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.StatelessKnowledgeSession;
+import org.drools.runtime.pipeline.Action;
+import org.drools.runtime.pipeline.KnowledgeRuntimeCommand;
+import org.drools.runtime.pipeline.Pipeline;
+import org.drools.runtime.pipeline.PipelineFactory;
+import org.drools.runtime.pipeline.ResultHandler;
import org.drools.util.DroolsStreamUtils;
import org.drools.util.FileManager;
import org.mortbay.jetty.Server;
@@ -812,6 +821,89 @@
kagent.monitorResourceChangeEvents( false );
}
+ public void testStatelessWithPipeline() throws Exception {
+ String rule1 = "";
+ rule1 += "package org.drools.test\n";
+ rule1 += "global java.util.List list\n";
+ rule1 += "rule rule1\n";
+ rule1 += "when\n";
+ rule1 += "then\n";
+ rule1 += "list.add( drools.getRule().getName() );\n";
+ rule1 += "end\n";
+ File f1 = fileManager.newFile( "rule1.drl" );
+
+ Writer output = new BufferedWriter( new FileWriter( f1 ) );
+ output.write( rule1 );
+ output.close();
+
+ String rule2 = "";
+ rule2 += "package org.drools.test\n";
+ rule2 += "global java.util.List list\n";
+ rule2 += "rule rule2\n";
+ rule2 += "when\n";
+ rule2 += "then\n";
+ rule2 += "list.add( drools.getRule().getName() );\n";
+ rule2 += "end\n";
+ File f2 = fileManager.newFile( "rule2.drl" );
+ output = new BufferedWriter( new FileWriter( f2 ) );
+ output.write( rule2 );
+ output.close();
+
+ String xml = "";
+ xml += "<change-set xmlns='http://drools.org/drools-5.0/change-set'";
+ xml += " xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";
+ xml += " xs:schemaLocation='http://drools.org/drools-5.0/change-set drools-change-set-5.0.xsd' >";
+ xml += " <add> ";
+ xml += " <resource source='" + f1.getParentFile().toURI().toURL() + "' type='DRL' />";
+ xml += " </add> ";
+ xml += "</change-set>";
+ File newDir = fileManager.newFile( "changeset" );
+ newDir.mkdir();
+ File fxml = fileManager.newFile( newDir,
+ "changeset.xml" );
+ output = new BufferedWriter( new FileWriter( fxml ) );
+ output.write( xml );
+ output.close();
+
+ KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
+ aconf.setProperty( "drools.agent.scanDirectories",
+ "true" );
+ aconf.setProperty( "drools.agent.newInstance",
+ "true" );
+
+ KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "test agent",
+ aconf );
+ kagent.applyChangeSet( ResourceFactory.newUrlResource( fxml.toURI().toURL() ) );
+
+ Thread.sleep( 3000 ); // give it 2 seconds to detect and build the changes
+ StatelessKnowledgeSession ksession = kagent.newStatelessKnowledgeSession();
+
+ List list = new ArrayList();
+ ksession.setGlobal( "list",
+ list );
+
+ Action executeResultHandler = PipelineFactory.newExecuteResultHandler();
+
+ Action assignResult = PipelineFactory.newAssignObjectAsResult();
+ assignResult.setReceiver( executeResultHandler );
+
+ KnowledgeRuntimeCommand batchExecution = PipelineFactory.newCommandExecutor();
+ batchExecution.setReceiver( assignResult );
+
+ KnowledgeRuntimeCommand insertStage = PipelineFactory.newInsertObjectCommand();
+ insertStage.setReceiver( batchExecution );
+
+ Pipeline pipeline = PipelineFactory.newStatelessKnowledgeSessionPipeline( ksession );
+ pipeline.setReceiver( insertStage );
+
+ ResultHandlerImpl resultHandler = new ResultHandlerImpl();
+ pipeline.insert( "hello", resultHandler );
+
+ assertEquals( 2, list.size() );
+ assertTrue( list.contains( "rule1" ) );
+ assertTrue( list.contains( "rule2" ) );
+ }
+
private static void writePackage(Object pkg,
File p1file) throws IOException,
FileNotFoundException {
@@ -823,4 +915,16 @@
out.close();
}
}
+
+ public static class ResultHandlerImpl implements ResultHandler {
+ Object object;
+
+ public void handleResult(Object object) {
+ this.object = object;
+ }
+
+ public Object getObject() {
+ return this.object;
+ }
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/impl/StatelessKnowledgeSessionImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/impl/StatelessKnowledgeSessionImpl.java 2009-05-13 16:49:12 UTC (rev 26505)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/impl/StatelessKnowledgeSessionImpl.java 2009-05-13 17:10:13 UTC (rev 26506)
@@ -82,6 +82,10 @@
}
public InternalRuleBase getRuleBase() {
+ if ( this.kagent != null ) {
+ // if we have an agent always get the rulebase from there
+ this.ruleBase = ( InternalRuleBase ) ((KnowledgeBaseImpl)this.kagent.getKnowledgeBase()).ruleBase;
+ }
return this.ruleBase;
}
More information about the jboss-svn-commits
mailing list