Author: tsurdilovic
Date: 2009-08-11 17:06:42 -0400 (Tue, 11 Aug 2009)
New Revision: 11364
Added:
branches/community/Seam_2_2_Drools5/examples/drools/resources/kbaseconfig.properties
branches/community/Seam_2_2_Drools5/examples/drools/resources/kbuilderconfig.properties
branches/community/Seam_2_2_Drools5/examples/drools/resources/ksessionconfig.properties
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeActionHandler.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeAssignmentHandler.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeBase.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeDecisionHandler.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeHandler.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/SeamDelegate.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/StatefulKnowledgeSession.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools5-2.2.xsd
Modified:
branches/community/Seam_2_2_Drools5/build/default.build.properties
branches/community/Seam_2_2_Drools5/build/validate.xml
branches/community/Seam_2_2_Drools5/examples/build.xml
branches/community/Seam_2_2_Drools5/examples/drools/resources/.gpd.pageflow.jpdl.xml
branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml
branches/community/Seam_2_2_Drools5/examples/drools/resources/pageflow.jpdl.jpg
branches/community/Seam_2_2_Drools5/examples/drools/resources/pageflow.jpdl.xml
branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RandomNumber.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools-2.2.xsd
Log:
Drools5 integration.
Modified: branches/community/Seam_2_2_Drools5/build/default.build.properties
===================================================================
--- branches/community/Seam_2_2_Drools5/build/default.build.properties 2009-08-11 11:28:22
UTC (rev 11363)
+++ branches/community/Seam_2_2_Drools5/build/default.build.properties 2009-08-11 21:06:42
UTC (rev 11364)
@@ -12,5 +12,5 @@
#
# Other program locations
# -----------------------
-jboss.home /Applications/jboss-5.1.0.GA
+jboss.home /home/tsurdilo/support/eap-4.3.0.GA_CP06/jboss-eap-4.3/jboss-as
tomcat.home /Applications/apache-tomcat-6.0
Modified: branches/community/Seam_2_2_Drools5/build/validate.xml
===================================================================
--- branches/community/Seam_2_2_Drools5/build/validate.xml 2009-08-11 11:28:22 UTC (rev
11363)
+++ branches/community/Seam_2_2_Drools5/build/validate.xml 2009-08-11 21:06:42 UTC (rev
11364)
@@ -8,7 +8,8 @@
http://jboss.com/products/seam/bpm ${src.schema.dir}/bpm-${schema.version}.xsd
http://jboss.com/products/seam/components
${src.schema.dir}/components-${schema.version}.xsd
http://jboss.com/products/seam/core ${src.schema.dir}/core-${schema.version}.xsd
-
http://jboss.com/products/seam/drools ${src.schema.dir}/drools-${schema.version}.xsd
+
http://jboss.com/products/seam/drools ${src.schema.dir}/drools-${schema.version}.xsd
+
http://jboss.com/products/seam/drools5 ${src.schema.dir}/drools5-${schema.version}.xsd
http://jboss.com/products/seam/framework
${src.schema.dir}/framework-${schema.version}.xsd
http://jboss.com/products/seam/international
${src.schema.dir}/international-${schema.version}.xsd
http://jboss.com/products/seam/jms ${src.schema.dir}/jms-${schema.version}.xsd
Modified: branches/community/Seam_2_2_Drools5/examples/build.xml
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/build.xml 2009-08-11 11:28:22 UTC (rev
11363)
+++ branches/community/Seam_2_2_Drools5/examples/build.xml 2009-08-11 21:06:42 UTC (rev
11364)
@@ -465,6 +465,7 @@
<include name="seam.properties" />
<include name="seam.quartz.properties" />
<include name="*.drl" />
+ <include name="*.properties"/>
<include name="META-INF/persistence.xml" />
<include name="META-INF/ejb-jar.xml" />
<include name="META-INF/orm.xml" />
Modified:
branches/community/Seam_2_2_Drools5/examples/drools/resources/.gpd.pageflow.jpdl.xml
===================================================================
---
branches/community/Seam_2_2_Drools5/examples/drools/resources/.gpd.pageflow.jpdl.xml 2009-08-11
11:28:22 UTC (rev 11363)
+++
branches/community/Seam_2_2_Drools5/examples/drools/resources/.gpd.pageflow.jpdl.xml 2009-08-11
21:06:42 UTC (rev 11364)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
-<root-container name="numberGuess" width="750"
height="744">
+<root-container name="numberGuess" width="687"
height="408">
<node name="displayGuess" x="157" y="35"
width="140" height="40">
<edge>
<label x="-44" y="-10"/>
Modified:
branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml
===================================================================
---
branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml 2009-08-11
11:28:22 UTC (rev 11363)
+++
branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml 2009-08-11
21:06:42 UTC (rev 11364)
@@ -4,32 +4,45 @@
xmlns:bpm="http://jboss.com/products/seam/bpm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
- "http://jboss.com/products/seam/drools
http://jboss.com/products/seam/drools-2.2.xsd
+ "http://jboss.com/products/seam/drools
http://jboss.com/products/seam/drools-2.2.xsd
http://jboss.com/products/seam/core
http://jboss.com/products/seam/core-2.2.xsd
http://jboss.com/products/seam/components
http://jboss.com/products/seam/components-2.2.xsd
http://jboss.com/products/seam/bpm
http://jboss.com/products/seam/bpm-2.2.xsd">
-
- <drools:rule-base name="ruleBase"
rule-files="numberguess.drl"/>
+
+ <drools:knowledge-base name="kbase"
knowledge-builder-config="kbuilderconfig.properties"
knowledge-base-config="kbaseconfig.properties">
+ <drools:rule-resources>
+ <value>classpath:numberguess.drl:DRL</value>
+ </drools:rule-resources>
+ </drools:knowledge-base>
+
+ <drools:stateful-knowledge-session name="ksession"
knowledge-base="#{kbase}"
knowledge-session-config="ksessionconfig.properties">
+ <drools:event-listeners>
+ <value>org.drools.event.rule.DebugAgendaEventListener</value>
+ <value>org.drools.event.rule.DebugWorkingMemoryEventListener</value>
+ </drools:event-listeners>
+ </drools:stateful-knowledge-session>
+
+ <!-- <drools:rule-base name="ruleBase"
rule-files="numberguess.drl"/> -->
<!-- use this in order to load from decision table instead -->
- <!-- <drools:rule-base name="ruleBase"
rule-files="numberguess.xls"/> -->
+ <!-- <drools:rule-base name="ruleBase"
rule-files="numberguess.xls"/> -->
<!-- use this in order to load from a drl and also add a ruleflow -->
- <!-- <drools:rule-base name="ruleBase"
rule-files="numberguessforflow.drl, numberguess.rf"/> -->
+ <!-- <drools:rule-base name="ruleBase"
rule-files="numberguessforflow.drl, numberguess.rf"/> -->
<!-- use this in order to load from a xls and also add a ruleflow -->
<!-- <drools:rule-base name="ruleBase"
rule-files="numberguessforflow.xls, numberguess.rf"/> -->
<!-- use this if you want to register a custom consequence exception handler
-->
<!-- <drools:rule-base name="ruleBase"
rule-files="numberguess.xls"
consequence-exception-handler="#{gameConsequenceExceptionHandler}"/> -->
- <drools:managed-working-memory name="workingMemory"
rule-base="#{ruleBase}">
+ <!-- <drools:managed-working-memory name="workingMemory"
rule-base="#{ruleBase}"> -->
<!-- add WM event listeners -->
<!-- <drools:event-listeners>
<value>org.drools.event.DebugWorkingMemoryEventListener</value>
<value>org.drools.event.DebugAgendaEventListener</value>
</drools:event-listeners> -->
- </drools:managed-working-memory>
-
+ <!-- </drools:managed-working-memory> -->
<bpm:jbpm>
<bpm:pageflow-definitions>
<value>pageflow.jpdl.xml</value>
</bpm:pageflow-definitions>
</bpm:jbpm>
+
</components>
Added:
branches/community/Seam_2_2_Drools5/examples/drools/resources/kbaseconfig.properties
===================================================================
Added:
branches/community/Seam_2_2_Drools5/examples/drools/resources/kbuilderconfig.properties
===================================================================
Added:
branches/community/Seam_2_2_Drools5/examples/drools/resources/ksessionconfig.properties
===================================================================
Modified: branches/community/Seam_2_2_Drools5/examples/drools/resources/pageflow.jpdl.jpg
===================================================================
(Binary files differ)
Modified: branches/community/Seam_2_2_Drools5/examples/drools/resources/pageflow.jpdl.xml
===================================================================
---
branches/community/Seam_2_2_Drools5/examples/drools/resources/pageflow.jpdl.xml 2009-08-11
11:28:22 UTC (rev 11363)
+++
branches/community/Seam_2_2_Drools5/examples/drools/resources/pageflow.jpdl.xml 2009-08-11
21:06:42 UTC (rev 11364)
@@ -18,8 +18,8 @@
<decision name="drools">
- <handler class="org.jboss.seam.drools.DroolsDecisionHandler">
- <workingMemoryName>workingMemory</workingMemoryName>
+ <handler class="org.jboss.seam.drools.KnowledgeDecisionHandler">
+ <workingMemoryName>ksession</workingMemoryName>
<!-- if a ruleflow was added, start a process -->
<!-- <startProcessId>numberguessid</startProcessId> -->
<assertObjects>
Modified:
branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RandomNumber.java
===================================================================
---
branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RandomNumber.java 2009-08-11
11:28:22 UTC (rev 11363)
+++
branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RandomNumber.java 2009-08-11
21:06:42 UTC (rev 11364)
@@ -21,9 +21,21 @@
}
@Unwrap
- public int getValue()
+ public Integer getValue()
{
return randomNumber;
}
+
+ public int getRandomNumber()
+ {
+ return randomNumber;
+ }
+
+ public void setRandomNumber(int randomNumber)
+ {
+ this.randomNumber = randomNumber;
+ }
+
+
}
Added:
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeActionHandler.java
===================================================================
---
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeActionHandler.java
(rev 0)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeActionHandler.java 2009-08-11
21:06:42 UTC (rev 11364)
@@ -0,0 +1,33 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+
+import org.jbpm.graph.def.ActionHandler;
+import org.jbpm.graph.exe.ExecutionContext;
+
+/**
+ * A jBPM ActionHandler that delegates to a Drools StatefulKnowledgeSession
+ * held in a Seam context variable.
+ *
+ * @author Tihomir Surdilovic
+ *
+ */
+public class KnowledgeActionHandler extends KnowledgeHandler implements ActionHandler
+{
+
+ public List<String> assertObjects;
+ public List<String> retractObjects;
+ public String workingMemoryName;
+ public String startProcessId;
+
+ public void execute(ExecutionContext executionContext) throws Exception
+ {
+ org.drools.runtime.StatefulKnowledgeSession ksession =
getKnowledgeSession(workingMemoryName, assertObjects, retractObjects, executionContext);
+ if(startProcessId != null && startProcessId.trim().length() > 0 )
+ {
+ ksession.startProcess(startProcessId);
+ }
+ ksession.fireAllRules();
+ }
+
+}
Added:
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeAssignmentHandler.java
===================================================================
---
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeAssignmentHandler.java
(rev 0)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeAssignmentHandler.java 2009-08-11
21:06:42 UTC (rev 11364)
@@ -0,0 +1,33 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.taskmgmt.def.AssignmentHandler;
+import org.jbpm.taskmgmt.exe.Assignable;
+
+/**
+ * A jBPM AssignmentHandler that delegates to a Drools StatefulKnowledgeSession
+ * held in a Seam context variable.
+ *
+ * @author Tihomir Surdilovic
+ */
+public class KnowledgeAssignmentHandler extends KnowledgeHandler implements
AssignmentHandler
+{
+ public String workingMemoryName;
+ public List<String> assertObjects;
+ public List<String> retractObjects;
+ public String startProcessId;
+
+ public void assign(Assignable assignable, ExecutionContext executionContext) throws
Exception
+ {
+ org.drools.runtime.StatefulKnowledgeSession ksession =
getKnowledgeSession(workingMemoryName, assertObjects, retractObjects, executionContext);
+ ksession.setGlobal( "assignable", assignable );
+ if(startProcessId != null && startProcessId.trim().length() > 0 )
+ {
+ ksession.startProcess(startProcessId);
+ }
+ ksession.fireAllRules();
+ }
+
+}
Added:
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeBase.java
===================================================================
--- branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeBase.java
(rev 0)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeBase.java 2009-08-11
21:06:42 UTC (rev 11364)
@@ -0,0 +1,167 @@
+package org.jboss.seam.drools;
+
+import java.io.InputStream;
+import java.util.Properties;
+import java.util.regex.Pattern;
+
+import org.drools.KnowledgeBaseConfiguration;
+import org.drools.KnowledgeBaseFactory;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderConfiguration;
+import org.drools.builder.KnowledgeBuilderError;
+import org.drools.builder.KnowledgeBuilderErrors;
+import org.drools.builder.KnowledgeBuilderFactory;
+import org.drools.builder.ResourceType;
+import org.drools.io.ResourceFactory;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.core.ResourceLoader;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
+/**
+ * Manager component for a Drools KnowledgeBase
+ *
+ * @author Tihomir Surdilovic
+ *
+ */
+(a)Scope(ScopeType.APPLICATION)
+@BypassInterceptors
+public class KnowledgeBase
+{
+ private static final LogProvider log = Logging.getLogProvider(KnowledgeBase.class);
+ private static final Pattern COLON = Pattern.compile(":");
+ private static final int RESOURCE_PATH = 0;
+ private static final int RESOURCE = 1;
+ private static final int RESOURCE_TYPE = 2;
+ private static final String RESOURCE_TYPE_URL = "url";
+ private static final String RESOURCE_TYPE_FILE = "file";
+ private static final String RESOURCE_TYPE_CLASSPATH = "classpath";
+
+ private String knowledgeBuilderConfig;
+ private String knowledgeBaseConfig;
+ private String[] ruleResources;
+ private org.drools.KnowledgeBase kbase;
+
+ @Create
+ public void createKnowledgeBase() throws Exception {
+ KnowledgeBuilderConfiguration kbuilderconfig;
+ if(knowledgeBaseConfig != null) {
+ Properties kbuilderProp = new Properties();
+ InputStream stream =
ResourceLoader.instance().getResourceAsStream(knowledgeBuilderConfig);
+ if (stream==null)
+ {
+ throw new IllegalStateException("could not locate knowledgeBuilder
configuration: " + knowledgeBaseConfig);
+ }
+ kbuilderProp.load(stream);
+ log.info("loading knowledge builder config properties: " +
kbuilderProp.toString());
+ kbuilderconfig =
KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(kbuilderProp, null);
+ } else {
+ kbuilderconfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
+ }
+
+ KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderconfig);
+
+ if(ruleResources != null) {
+ for(String nextResource : ruleResources) {
+ addResource(kbuilder, nextResource);
+ }
+ }
+
+ KnowledgeBuilderErrors kbuildererrors = kbuilder.getErrors();
+ if(kbuildererrors.size() > 0) {
+ for(KnowledgeBuilderError kbuildererror : kbuildererrors) {
+ log.error(kbuildererror.getMessage());
+ }
+ }
+
+ KnowledgeBaseConfiguration kbaseconfig;
+
+ if(knowledgeBaseConfig != null) {
+ Properties kbaseProp = new Properties();
+ InputStream stream =
ResourceLoader.instance().getResourceAsStream(knowledgeBaseConfig);
+ if (stream==null)
+ {
+ throw new IllegalStateException("could not locate knowledgeBase
configuration: " + knowledgeBaseConfig);
+ }
+ kbaseProp.load(stream);
+ log.info("loading knowledge base config properties: " +
kbaseProp.toString());
+ kbaseconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(kbaseProp,
null);
+ } else {
+ kbaseconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
+ }
+
+ kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseconfig);
+ kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+
+
+ }
+
+
+ @Unwrap
+ public org.drools.KnowledgeBase getKnowledgeBase()
+ {
+ return kbase;
+ }
+
+ public String getKnowledgeBuilderConfig()
+ {
+ return knowledgeBuilderConfig;
+ }
+
+
+ public void setKnowledgeBuilderConfig(String knowledgeBuilderConfig)
+ {
+ this.knowledgeBuilderConfig = knowledgeBuilderConfig;
+ }
+
+
+ public String getKnowledgeBaseConfig()
+ {
+ return knowledgeBaseConfig;
+ }
+
+
+ public void setKnowledgeBaseConfig(String knowledgeBaseConfig)
+ {
+ this.knowledgeBaseConfig = knowledgeBaseConfig;
+ }
+
+
+ public String[] getRuleResources()
+ {
+ return ruleResources;
+ }
+
+
+ public void setRuleResources(String[] ruleResources)
+ {
+ this.ruleResources = ruleResources;
+ }
+
+ protected void addResource(KnowledgeBuilder kbuilder, String resource) {
+ String[] resourceParts = COLON.split(resource.trim());
+
+ log.info("New resource path: " + resourceParts[0]);
+ log.info("New resource location: " + resourceParts[1]);
+ log.info("New resource type: " + resourceParts[2]);
+
+ if(resourceParts.length != 3) {
+ log.error("Unable to use resource: " + resource);
+ } else {
+ ResourceType resourceType =
ResourceType.getResourceType(resourceParts[RESOURCE_TYPE]);
+ if(resourceParts[RESOURCE_PATH].equals(RESOURCE_TYPE_URL)) {
+ kbuilder.add(ResourceFactory.newUrlResource(resourceParts[RESOURCE]),
resourceType);
+ } else if(resourceParts[RESOURCE_PATH].equals(RESOURCE_TYPE_FILE)) {
+ kbuilder.add(ResourceFactory.newFileResource(resourceParts[RESOURCE]),
resourceType);
+ } else if(resourceParts[RESOURCE_PATH].equals(RESOURCE_TYPE_CLASSPATH)) {
+ kbuilder.add(ResourceFactory.newClassPathResource(resourceParts[RESOURCE]),
resourceType);
+ } else {
+ log.error("Unable to use resource path: " +
resourceParts[RESOURCE_PATH]);
+ }
+ }
+ }
+}
\ No newline at end of file
Added:
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeDecisionHandler.java
===================================================================
---
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeDecisionHandler.java
(rev 0)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeDecisionHandler.java 2009-08-11
21:06:42 UTC (rev 11364)
@@ -0,0 +1,35 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.graph.node.DecisionHandler;
+
+/**
+ * A jBPM DecisionHandler that delegates to a Drools StatefulKnowledgeSession
+ * held in a Seam context variable. The decision outcome is returned
+ * by setting the outcome attribute of the global named "decision".
+ *
+ * @author Tihomir Surdilovic
+ *
+ */
+public class KnowledgeDecisionHandler extends KnowledgeHandler implements
DecisionHandler
+{
+ public List<String> assertObjects;
+ public List<String> retractObjects;
+ public String workingMemoryName;
+ public String startProcessId;
+
+ public String decide(ExecutionContext executionContext) throws Exception
+ {
+ org.drools.runtime.StatefulKnowledgeSession ksession =
getKnowledgeSession(workingMemoryName, assertObjects, retractObjects, executionContext);
+ ksession.setGlobal("decision", new Decision());
+ if(startProcessId != null && startProcessId.trim().length() > 0 )
+ {
+ ksession.startProcess(startProcessId);
+ }
+ ksession.fireAllRules();
+ return ( (Decision) ksession.getGlobal("decision") ).getOutcome();
+ }
+
+}
Added:
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeHandler.java
===================================================================
---
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeHandler.java
(rev 0)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeHandler.java 2009-08-11
21:06:42 UTC (rev 11364)
@@ -0,0 +1,92 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+
+import org.drools.runtime.rule.FactHandle;
+import org.jboss.seam.Component;
+import org.jboss.seam.bpm.Actor;
+import org.jboss.seam.core.Expressions;
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.jpdl.el.ELException;
+
+/**
+ * Common functionality for jBPM handlers for Drools5.
+ *
+ * @author Tihomir Surdilovic
+ *
+ */
+public class KnowledgeHandler
+{
+ protected org.drools.runtime.StatefulKnowledgeSession getKnowledgeSession(String
knowledgeSessionName, List<String> expressions, List<String> retractions,
ExecutionContext executionContext) throws ELException
+ {
+ org.drools.runtime.StatefulKnowledgeSession ksession =
(org.drools.runtime.StatefulKnowledgeSession) Component.getInstance(knowledgeSessionName,
true);
+
+ if(expressions != null && expressions.size() > 0)
+ {
+ for (String objectName: expressions)
+ {
+ Object object =
Expressions.instance().createValueExpression(objectName).getValue();
+ //Object object = new SeamVariableResolver().resolveVariable(objectName);
+ // assert the object into the rules engine
+ if (object instanceof Iterable)
+ {
+ for (Object element: (Iterable) object)
+ {
+ assertObject(ksession, element);
+ }
+ }
+ else
+ {
+ assertObject(ksession, object);
+ }
+ }
+ }
+
+ if(retractions != null && retractions.size() > 0)
+ {
+ for (String objectName: retractions)
+ {
+ Object object =
Expressions.instance().createValueExpression(objectName).getValue();
+ //Object object = new SeamVariableResolver().resolveVariable(objectName);
+ // retract the object from the rules engine
+ if (object instanceof Iterable)
+ {
+ for (Object element: (Iterable) object)
+ {
+ retractObject(ksession, element);
+ }
+ }
+ else
+ {
+ retractObject(ksession, object);
+ }
+ }
+ }
+
+ ksession.insert(Actor.instance());
+
+ return ksession;
+ }
+
+ private void assertObject(org.drools.runtime.StatefulKnowledgeSession ksession, Object
element)
+ {
+ FactHandle fact = ksession.getFactHandle(element);
+ if (fact==null)
+ {
+ ksession.insert(element);
+ }
+ else
+ {
+ ksession.update(fact, element);
+ }
+ }
+
+ private void retractObject(org.drools.runtime.StatefulKnowledgeSession ksession,
Object element)
+ {
+ FactHandle fact = ksession.getFactHandle(element);
+ if (fact != null)
+ {
+ ksession.retract(fact);
+ }
+ }
+}
Added:
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/SeamDelegate.java
===================================================================
--- branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/SeamDelegate.java
(rev 0)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/SeamDelegate.java 2009-08-11
21:06:42 UTC (rev 11364)
@@ -0,0 +1,51 @@
+package org.jboss.seam.drools;
+
+import org.drools.runtime.Globals;
+import org.jboss.seam.Component;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.core.Init;
+
+/**
+ * Resolves Seam context variables as Drools globals
+ *
+ * @author Tihomir Surdilovic
+ *
+ */
+public class SeamDelegate implements Globals
+{
+ private Globals delegate;
+
+ public Object get(String name)
+ {
+ if ( !Contexts.isApplicationContextActive() )
+ {
+ return delegate.get(name);
+ }
+ else
+ {
+ Object instance = Component.getInstance(name);
+ if (instance==null)
+ {
+ instance = delegate.get(name);
+ return instance==null ?
+ Init.instance().getRootNamespace().getChild(name) :
+ instance;
+ }
+ else
+ {
+ return instance;
+ }
+ }
+ }
+
+ public void set(String name, Object value)
+ {
+ delegate.set(name, value);
+ }
+
+ public void setDelegate(Globals delegate)
+ {
+ this.delegate = delegate;
+ }
+
+}
Added:
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/StatefulKnowledgeSession.java
===================================================================
---
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/StatefulKnowledgeSession.java
(rev 0)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/StatefulKnowledgeSession.java 2009-08-11
21:06:42 UTC (rev 11364)
@@ -0,0 +1,174 @@
+package org.jboss.seam.drools;
+
+import java.io.InputStream;
+import java.io.Serializable;
+import java.util.Properties;
+import java.util.regex.Pattern;
+
+import org.drools.KnowledgeBaseFactory;
+import org.drools.runtime.KnowledgeSessionConfiguration;
+import org.drools.spi.GlobalResolver;
+import org.drools.event.process.ProcessEventListener;
+import org.drools.event.rule.AgendaEventListener;
+import org.drools.event.rule.WorkingMemoryEventListener;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Destroy;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.core.Expressions;
+import org.jboss.seam.core.Mutable;
+import org.jboss.seam.core.ResourceLoader;
+import org.jboss.seam.core.Expressions.ValueExpression;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
+/**
+ * A conversation-scoped Drools StatefulKnowledgeSession for a named KnowledgeBase
+ *
+ * @author Tihomir Surdilovic
+ *
+ */
+(a)Scope(ScopeType.CONVERSATION)
+@BypassInterceptors
+public class StatefulKnowledgeSession implements Mutable, Serializable
+{
+ private static final LogProvider log =
Logging.getLogProvider(org.jboss.seam.drools.StatefulKnowledgeSession.class);
+ private static final Pattern COLON = Pattern.compile(":");
+ private static final int GLOBAL_NAME = 0;
+ private static final int GLOBAL_OBJ = 1;
+ private static final int GLOBAL_TYPE = 2;
+
+ private ValueExpression<org.drools.KnowledgeBase> knowledgeBase;
+ private String[] globals;
+ private String[] eventListeners;
+ private String knowledgeSessionConfig;
+
+ private org.drools.runtime.StatefulKnowledgeSession ksession;
+
+ @Unwrap
+ public org.drools.runtime.StatefulKnowledgeSession getStatefulKnowledgeSession()
throws Exception {
+ if(ksession == null) {
+ KnowledgeSessionConfiguration ksessionconfig;
+ if(knowledgeSessionConfig != null) {
+ Properties ksessionProp = new Properties();
+ InputStream stream =
ResourceLoader.instance().getResourceAsStream(knowledgeSessionConfig);
+ if (stream==null) {
+ throw new IllegalStateException("could not locate knowledgeSession
configuration: " + knowledgeSessionConfig);
+ }
+ ksessionProp.load(stream);
+ log.info("loading knowledge session config properties: " +
ksessionProp.toString());
+ ksessionconfig =
KnowledgeBaseFactory.newKnowledgeSessionConfiguration(ksessionProp);
+ } else {
+ ksessionconfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
+ }
+
+ ksession = knowledgeBase.getValue().newStatefulKnowledgeSession(ksessionconfig,
null);
+
+ ksession.getGlobals().setDelegate(new SeamDelegate());
+
+ if(eventListeners != null) {
+ for(String eventListener : eventListeners) {
+ setEventListener(ksession, eventListener);
+ }
+ }
+
+ if(globals != null) {
+ for(String nextGlobal : globals) {
+ String[] globalParts = COLON.split(nextGlobal.trim());
+ if(globalParts.length == 3) {
+ log.info("setting global: " + globalParts[GLOBAL_NAME] +
" - " + globalParts[GLOBAL_OBJ]);
+ try {
+ Class ctype = Class.forName(globalParts[GLOBAL_TYPE]);
+ ksession.setGlobal(globalParts[GLOBAL_NAME],
Expressions.instance().createValueExpression(globalParts[GLOBAL_OBJ], ctype).getValue());
+ } catch (Exception e) {
+ log.error("Error setting global: " +
globalParts[GLOBAL_NAME] + " - " + globalParts[GLOBAL_OBJ] + " Exception :
" + e.getMessage());
+ }
+ } else {
+ log.warn("Invalid global entry: " + nextGlobal);
+ }
+ }
+ }
+ }
+
+ return ksession;
+ }
+
+ protected GlobalResolver createGlobalResolver(GlobalResolver delegate)
+ {
+ return new SeamGlobalResolver(delegate);
+ }
+
+ protected void setEventListener(org.drools.runtime.StatefulKnowledgeSession ksession,
String eventListener) {
+ try {
+ Class eventListenerClass = Class.forName(eventListener);
+ Object eventListenerObject = eventListenerClass.newInstance();
+
+ if(eventListenerObject instanceof WorkingMemoryEventListener) {
+ ksession.addEventListener((WorkingMemoryEventListener) eventListenerObject);
+ } else if(eventListenerObject instanceof AgendaEventListener) {
+ ksession.addEventListener((AgendaEventListener) eventListenerObject);
+ } else if(eventListenerObject instanceof ProcessEventListener) {
+ ksession.addEventListener((ProcessEventListener) eventListenerObject);
+ } else {
+ log.warn("event Listener " + eventListener + " is not of valid
type");
+ }
+ } catch(Exception e) {
+ log.warn("error adding event listener " + eventListener);
+ }
+ }
+
+ public boolean clearDirty()
+ {
+ return true;
+ }
+
+ @Destroy
+ public void destroy()
+ {
+ if(ksession != null) {
+ ksession.dispose();
+ }
+ }
+
+ public ValueExpression<org.drools.KnowledgeBase> getKnowledgeBase()
+ {
+ return knowledgeBase;
+ }
+
+ public void setKnowledgeBase(ValueExpression<org.drools.KnowledgeBase>
knowledgeBase)
+ {
+ this.knowledgeBase = knowledgeBase;
+ }
+
+ public String[] getGlobals()
+ {
+ return globals;
+ }
+
+ public void setGlobals(String[] globals)
+ {
+ this.globals = globals;
+ }
+
+ public String[] getEventListeners()
+ {
+ return eventListeners;
+ }
+
+ public void setEventListeners(String[] eventListeners)
+ {
+ this.eventListeners = eventListeners;
+ }
+
+ public String getKnowledgeSessionConfig()
+ {
+ return knowledgeSessionConfig;
+ }
+
+ public void setKnowledgeSessionConfig(String knowledgeSessionConfig)
+ {
+ this.knowledgeSessionConfig = knowledgeSessionConfig;
+ }
+
+}
Modified: branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools-2.2.xsd
===================================================================
--- branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools-2.2.xsd 2009-08-11
11:28:22 UTC (rev 11363)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools-2.2.xsd 2009-08-11
21:06:42 UTC (rev 11364)
@@ -59,5 +59,44 @@
<xs:attribute name="rule-base"
type="components:expressionType"/>
</xs:attributeGroup>
<xs:element name="event-listeners"
type="components:multiValuedProperty"/>
+
+ <xs:element name="knowledge-base">
+ <xs:annotation>
+ <xs:documentation>Drools KnowledgeBase</xs:documentation>
+ </xs:annotation>
+ <xs:complexType mixed="true">
+ <xs:choice minOccurs="0" maxOccurs="unbounded">
+ <xs:element minOccurs="0" maxOccurs="1"
ref="drools:rule-resources"/>
+ </xs:choice>
+ <xs:attributeGroup ref="components:attlist.component"/>
+ <xs:attributeGroup ref="drools:attlist.KnowledgeBase"/>
+ </xs:complexType>
+ </xs:element>
+ <xs:element name="rule-resources"
type="components:multiValuedProperty"/>
+ <xs:attributeGroup name="attlist.KnowledgeBase">
+ <xs:attribute name="knowledge-builder-config"
type="components:string"/>
+ <xs:attribute name="knowledge-base-config"
type="components:string"/>
+ </xs:attributeGroup>
+
+ <xs:element name="stateful-knowledge-session">
+ <xs:annotation>
+ <xs:documentation>Drools
StatefulKnowledgeSession</xs:documentation>
+ </xs:annotation>
+ <xs:complexType mixed="true">
+ <xs:choice minOccurs="0" maxOccurs="unbounded">
+ <xs:element minOccurs="0" maxOccurs="1"
ref="drools:event-listeners"/>
+ <xs:element minOccurs="0" maxOccurs="1"
ref="drools:globals"/>
+ </xs:choice>
+ <xs:attributeGroup ref="components:attlist.component"/>
+ <xs:attributeGroup
ref="drools:attlist.StatefulKnowledgeSession"/>
+ </xs:complexType>
+ </xs:element>
+ <xs:attributeGroup name="attlist.StatefulKnowledgeSession">
+ <xs:attribute name="knowledge-base"
type="components:expressionType"/>
+ <xs:attribute name="globals"
type="components:string"/>
+ <xs:attribute name="event-listeners"
type="components:string"/>
+ <xs:attribute name="knowledge-session-config"
type="components:string"/>
+ </xs:attributeGroup>
+ <xs:element name="globals"
type="components:multiValuedProperty"/>
</xs:schema>
Added: branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools5-2.2.xsd
===================================================================
--- branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools5-2.2.xsd
(rev 0)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools5-2.2.xsd 2009-08-11
21:06:42 UTC (rev 11364)
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
+
targetNamespace="http://jboss.com/products/seam/drools"
xmlns:drools="http://jboss.com/products/seam/drools"
+
xmlns:components="http://jboss.com/products/seam/components"
attributeFormDefault="unqualified">
+
+ <xs:import
namespace="http://jboss.com/products/seam/components"
schemaLocation="components-2.2.xsd"/>
+
+ <xs:import
namespace="http://jboss.com/products/seam/components"
schemaLocation="components-2.2.xsd"/>
+
+ <xs:element name="knowledge-base">
+ <xs:annotation>
+ <xs:documentation>Drools KnowledgeBase</xs:documentation>
+ </xs:annotation>
+ <xs:complexType mixed="true">
+ <xs:sequence minOccurs="1" maxOccurs="1">
+ <xs:element name="knowledge-builder-config"
type="components:string" minOccurs="0" maxOccurs="1"/>
+ <xs:element name="knowledge-base-config"
type="components:string" minOccurs="0" maxOccurs="1"/>
+ <xs:element name="rule-resources"
type="components:multiValuedProperty" minOccurs="1"
maxOccurs="1"/>
+ </xs:sequence>
+ <xs:attributeGroup ref="components:attlist.component"/>
+ <xs:attributeGroup ref="drools:attlist.KnowledgeBase"/>
+ </xs:complexType>
+ </xs:element>
+ <xs:attributeGroup name="attlist.KnowledgeBase">
+ <xs:attribute name="knowledge-builder-config"
type="components:string"/>
+ <xs:attribute name="knowledge-base-config"
type="components:string"/>
+ </xs:attributeGroup>
+</xs:schema>
\ No newline at end of file