Author: tsurdilovic
Date: 2009-08-12 15:31:59 -0400 (Wed, 12 Aug 2009)
New Revision: 11370
Added:
branches/community/Seam_2_2_Drools5/examples/drools/resources/numberguesschangeset.xml
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeAgent.java
Modified:
branches/community/Seam_2_2_Drools5/examples/build.xml
branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml
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/KnowledgeBase.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/StatefulKnowledgeSession.java
Log:
Drools5 Integration
Modified: branches/community/Seam_2_2_Drools5/examples/build.xml
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/build.xml 2009-08-12 14:59:52 UTC (rev
11369)
+++ branches/community/Seam_2_2_Drools5/examples/build.xml 2009-08-12 19:31:59 UTC (rev
11370)
@@ -466,6 +466,7 @@
<include name="seam.quartz.properties" />
<include name="*.drl" />
<include name="*.properties"/>
+ <include name="*.xml"/>
<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/WEB-INF/components.xml
===================================================================
---
branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml 2009-08-12
14:59:52 UTC (rev 11369)
+++
branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml 2009-08-12
19:31:59 UTC (rev 11370)
@@ -15,7 +15,14 @@
</drools:rule-resources>
</drools:knowledge-base>
- <drools:stateful-knowledge-session name="ksession"
knowledge-base="#{kbase}"
knowledge-session-config="ksessionconfig.properties">
+ <drools:knowledge-agent name="kagent" agent-name="myAgent"
scan-resources="true" scan-directories="false"
+ monitor-change-set-events="true" scan-interval="30">
+ <drools:change-sets>
+ <value>classpath:numberguesschangeset.xml</value>
+ </drools:change-sets>
+ </drools:knowledge-agent>
+
+ <drools:stateful-knowledge-session name="ksession"
knowledge-base="#{kagent}"
knowledge-session-config="ksessionconfig.properties">
<drools:event-listeners>
<value>org.drools.event.rule.DebugAgendaEventListener</value>
<value>org.drools.event.rule.DebugWorkingMemoryEventListener</value>
Added:
branches/community/Seam_2_2_Drools5/examples/drools/resources/numberguesschangeset.xml
===================================================================
---
branches/community/Seam_2_2_Drools5/examples/drools/resources/numberguesschangeset.xml
(rev 0)
+++
branches/community/Seam_2_2_Drools5/examples/drools/resources/numberguesschangeset.xml 2009-08-12
19:31:59 UTC (rev 11370)
@@ -0,0 +1,7 @@
+<change-set
xmlns='http://drools.org/drools-5.0/change-set'
+
xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
+
xs:schemaLocation='http://drools.org/drools-5.0/change-set
drools-change-set-5.0.xsd' >
+ <add>
+ <resource source='classpath:numberguess.drl' type='DRL' />
+ </add>
+</change-set>
\ No newline at end of file
Added:
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeAgent.java
===================================================================
---
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeAgent.java
(rev 0)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeAgent.java 2009-08-12
19:31:59 UTC (rev 11370)
@@ -0,0 +1,238 @@
+package org.jboss.seam.drools;
+
+import java.io.InputStream;
+import java.util.Properties;
+import java.util.regex.Pattern;
+
+import org.drools.SystemEventListenerFactory;
+import org.drools.agent.KnowledgeAgentConfiguration;
+import org.drools.agent.KnowledgeAgentFactory;
+import org.drools.io.ResourceChangeScannerConfiguration;
+import org.drools.io.ResourceFactory;
+import org.drools.io.impl.ResourceChangeNotifierImpl;
+import org.drools.io.impl.ResourceChangeScannerImpl;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+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.ResourceLoader;
+import org.jboss.seam.core.Expressions.ValueExpression;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
+/**
+ * Manager component for a KnowledgeBase loaded from a drools KnowledgeAgent
+ *
+ * @author Tihomir Surdilovic
+ */
+(a)Scope(ScopeType.APPLICATION)
+@BypassInterceptors
+public class KnowledgeAgent
+{
+ private static final LogProvider log = Logging.getLogProvider(RuleAgent.class);
+ private static final Pattern COLON = Pattern.compile(":");
+ private static final int RESOURCE_PATH = 0;
+ private static final int RESOURCE = 1;
+ 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 org.drools.agent.KnowledgeAgent kagent;
+ private String agentName;
+ private String scanResources;
+ private String scanDirectories;
+ private String newInstance;
+ private String monitorChangeSetEvents;
+ private String knowledgeAgentConfig;
+ private String scanInterval;
+ private String[] changeSets;
+ private ValueExpression<org.drools.KnowledgeBase> knowledgeBase;
+
+ @Create
+ public void createAgent() throws Exception
+ {
+ ResourceFactory.getResourceChangeScannerService().start();
+ ResourceFactory.getResourceChangeNotifierService().start();
+
+ if(scanInterval != null) {
+ ResourceChangeScannerConfiguration sconf =
ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();
+ log.info("Setting drools.resource.scanner.interval to " +
scanInterval);
+ sconf.setProperty( "drools.resource.scanner.interval", scanInterval);
+ ResourceFactory.getResourceChangeScannerService().configure( sconf );
+ }
+
+ KnowledgeAgentConfiguration kagentConf;
+ if(knowledgeAgentConfig != null) {
+ Properties kagentProp = new Properties();
+ InputStream stream =
ResourceLoader.instance().getResourceAsStream(knowledgeAgentConfig);
+ if (stream==null) {
+ throw new IllegalStateException("could not locate knowledgeAgent
configuration: " + knowledgeAgentConfig);
+ }
+ kagentProp.load(stream);
+ log.info("loading knowledge agent config properties: " +
kagentProp.toString());
+ stream.close();
+ kagentConf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration(kagentProp);
+ } else {
+ kagentConf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
+ if(scanResources != null) {
+ log.info("Setting drools.agent.scanResources to " +
scanResources);
+ kagentConf.setProperty("drools.agent.scanResources",
scanResources);
+ }
+ if(scanDirectories != null) {
+ log.info("Setting drools.agent.scanDirectories to " +
scanDirectories);
+ kagentConf.setProperty("drools.agent.scanDirectories",
scanDirectories);
+ }
+ if(newInstance != null) {
+ log.info("Setting drools.agent.newInstance to " + newInstance);
+ kagentConf.setProperty("drools.agent.newInstance", newInstance);
+ }
+ if(monitorChangeSetEvents != null) {
+ log.info("Setting drools.agent.monitorChangeSetEvents to " +
monitorChangeSetEvents);
+ kagentConf.setProperty("drools.agent.monitorChangeSetEvents",
monitorChangeSetEvents);
+ }
+ }
+
+ if(agentName == null) {
+ log.info("Invalid knowledge agent name. Setting to default.");
+ agentName = "seamAgent";
+ }
+
+ if(knowledgeBase != null) {
+ kagent = KnowledgeAgentFactory.newKnowledgeAgent(agentName,
knowledgeBase.getValue(), kagentConf);
+ } else {
+ kagent = KnowledgeAgentFactory.newKnowledgeAgent(agentName, kagentConf);
+ }
+
+ if(changeSets != null) {
+ for(String changeSet : changeSets) {
+ applyChangeSet(kagent, changeSet);
+ }
+ }
+
+ kagent.setSystemEventListener(SystemEventListenerFactory.getSystemEventListener());
+ }
+
+ private void applyChangeSet(org.drools.agent.KnowledgeAgent kagent, String changeSet)
{
+ String[] resourceParts = COLON.split(changeSet.trim());
+
+ if(resourceParts.length != 2) {
+ log.error("Unable to use changeset: " + changeSet);
+ } else {
+ log.info("New changeset path: " + resourceParts[0]);
+ log.info("New changeset location: " + resourceParts[1]);
+
+ if(resourceParts[RESOURCE_PATH].equals(RESOURCE_TYPE_URL)) {
+
kagent.applyChangeSet(ResourceFactory.newUrlResource(resourceParts[RESOURCE]));
+ } else if(resourceParts[RESOURCE_PATH].equals(RESOURCE_TYPE_FILE)) {
+
kagent.applyChangeSet(ResourceFactory.newFileResource(resourceParts[RESOURCE]));
+ } else if(resourceParts[RESOURCE_PATH].equals(RESOURCE_TYPE_CLASSPATH)) {
+
kagent.applyChangeSet(ResourceFactory.newClassPathResource(resourceParts[RESOURCE]));
+ } else {
+ log.error("Unable to use resource path: " +
resourceParts[RESOURCE_PATH]);
+ }
+ }
+ }
+
+ @Unwrap
+ public org.drools.KnowledgeBase getKnowledgeBase()
+ {
+ return kagent.getKnowledgeBase();
+ }
+
+ @Destroy
+ public void destroy() {
+ ResourceFactory.getResourceChangeNotifierService().stop();
+ ResourceFactory.getResourceChangeScannerService().stop();
+ ((ResourceChangeNotifierImpl)
ResourceFactory.getResourceChangeNotifierService()).reset();
+ ((ResourceChangeScannerImpl)
ResourceFactory.getResourceChangeScannerService()).reset();
+ }
+
+ public String getAgentName()
+ {
+ return agentName;
+ }
+
+ public void setAgentName(String agentName)
+ {
+ this.agentName = agentName;
+ }
+
+ public String getScanResources()
+ {
+ return scanResources;
+ }
+
+ public void setScanResources(String scanResources)
+ {
+ this.scanResources = scanResources;
+ }
+
+ public String getScanDirectories()
+ {
+ return scanDirectories;
+ }
+
+ public void setScanDirectories(String scanDirectories)
+ {
+ this.scanDirectories = scanDirectories;
+ }
+
+ public String getNewInstance()
+ {
+ return newInstance;
+ }
+
+ public void setNewInstance(String newInstance)
+ {
+ this.newInstance = newInstance;
+ }
+
+ public String getMonitorChangeSetEvents()
+ {
+ return monitorChangeSetEvents;
+ }
+
+ public void setMonitorChangeSetEvents(String monitorChangeSetEvents)
+ {
+ this.monitorChangeSetEvents = monitorChangeSetEvents;
+ }
+
+ public String getKnowledgeAgentConfig()
+ {
+ return knowledgeAgentConfig;
+ }
+
+ public void setKnowledgeAgentConfig(String knowledgeAgentConfig)
+ {
+ this.knowledgeAgentConfig = knowledgeAgentConfig;
+ }
+
+ public String getScanInterval()
+ {
+ return scanInterval;
+ }
+
+ public void setScanInterval(String scanInterval)
+ {
+ this.scanInterval = scanInterval;
+ }
+
+ public void setKnowledgeBase(ValueExpression<org.drools.KnowledgeBase>
knowledgeBase)
+ {
+ this.knowledgeBase = knowledgeBase;
+ }
+
+ public String[] getChangeSets()
+ {
+ return changeSets;
+ }
+
+ public void setChangeSets(String[] changeSets)
+ {
+ this.changeSets = changeSets;
+ }
+
+
+}
Modified:
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 2009-08-12
14:59:52 UTC (rev 11369)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeBase.java 2009-08-12
19:31:59 UTC (rev 11370)
@@ -58,6 +58,7 @@
}
kbuilderProp.load(stream);
log.info("loading knowledge builder config properties: " +
kbuilderProp.toString());
+ stream.close();
kbuilderconfig =
KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(kbuilderProp, null);
} else {
kbuilderconfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
@@ -89,6 +90,7 @@
}
kbaseProp.load(stream);
log.info("loading knowledge base config properties: " +
kbaseProp.toString());
+ stream.close();
kbaseconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(kbaseProp,
null);
} else {
kbaseconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
@@ -145,13 +147,13 @@
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 {
+ log.info("New resource path: " + resourceParts[0]);
+ log.info("New resource location: " + resourceParts[1]);
+ log.info("New resource type: " + resourceParts[2]);
+
ResourceType resourceType =
ResourceType.getResourceType(resourceParts[RESOURCE_TYPE]);
if(resourceParts[RESOURCE_PATH].equals(RESOURCE_TYPE_URL)) {
kbuilder.add(ResourceFactory.newUrlResource(resourceParts[RESOURCE]),
resourceType);
Modified:
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 2009-08-12
14:59:52 UTC (rev 11369)
+++
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/StatefulKnowledgeSession.java 2009-08-12
19:31:59 UTC (rev 11370)
@@ -58,6 +58,7 @@
}
ksessionProp.load(stream);
log.info("loading knowledge session config properties: " +
ksessionProp.toString());
+ stream.close();
ksessionconfig =
KnowledgeBaseFactory.newKnowledgeSessionConfiguration(ksessionProp);
} else {
ksessionconfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
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-12
14:59:52 UTC (rev 11369)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools-2.2.xsd 2009-08-12
19:31:59 UTC (rev 11370)
@@ -98,5 +98,28 @@
<xs:attribute name="knowledge-session-config"
type="components:string"/>
</xs:attributeGroup>
<xs:element name="globals"
type="components:multiValuedProperty"/>
-
+
+ <xs:element name="knowledge-agent">
+ <xs:annotation>
+ <xs:documentation>Drools KnowledgeAgent</xs:documentation>
+ </xs:annotation>
+ <xs:complexType mixed="true">
+ <xs:choice minOccurs="0" maxOccurs="unbounded">
+ <xs:element minOccurs="0" maxOccurs="1"
ref="drools:change-sets"/>
+ </xs:choice>
+ <xs:attributeGroup ref="components:attlist.component"/>
+ <xs:attributeGroup ref="drools:attlist.KnowledgeAgent"/>
+ </xs:complexType>
+ </xs:element>
+ <xs:attributeGroup name="attlist.KnowledgeAgent">
+ <xs:attribute name="knowledge-base"
type="components:expressionType"/>
+ <xs:attribute name="agent-name"
type="components:string"/>
+ <xs:attribute name="scan-resources"
type="components:string"/>
+ <xs:attribute name="scan-directories"
type="components:string"/>
+ <xs:attribute name="new-instance"
type="components:string"/>
+ <xs:attribute name="monitor-change-set-events"
type="components:string"/>
+ <xs:attribute name="knowledge-agent-config"
type="components:string"/>
+ <xs:attribute name="scan-interval"
type="components:string"/>
+ </xs:attributeGroup>
+ <xs:element name="change-sets"
type="components:multiValuedProperty"/>
</xs:schema>