[seam-commits] Seam SVN: r11388 - in branches/community/Seam_2_2_Drools5: examples/drools/resources/WEB-INF and 4 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Fri Aug 14 18:49:06 EDT 2009
Author: tsurdilovic
Date: 2009-08-14 18:49:05 -0400 (Fri, 14 Aug 2009)
New Revision: 11388
Added:
branches/community/Seam_2_2_Drools5/examples/drools/resources/test2.drl
branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestFactProvider.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/FactProvider.java
branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/QueryResults.java
Modified:
branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml
branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RulesTest.java
branches/community/Seam_2_2_Drools5/examples/drools/view/simple.jsp
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/StatefulKnowledgeSession.java
Log:
Drools5 integration
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-14 21:09:04 UTC (rev 11387)
+++ branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml 2009-08-14 22:49:05 UTC (rev 11388)
@@ -47,7 +47,25 @@
<drools:batch-execution name="batch1" ksession="#{ksession2}"
batch-xml-config="testbatch.xml"/>
+
+ <drools:knowledge-base name="kbase3" knowledge-builder-config="kbuilderconfig.properties" knowledge-base-config="kbaseconfig.properties">
+ <drools:rule-resources>
+ <value>classpath:test2.drl:DRL</value>
+ </drools:rule-resources>
+ </drools:knowledge-base>
+
+ <drools:stateful-knowledge-session name="ksession3"
+ knowledge-base="#{kbase3}"
+ knowledge-session-config="ksessionconfig.properties"
+ fact-provider="#{testFactProvider}">
+ <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:query-results name="queries1" ksession="#{ksession3}" query-names="number_of_messages"/>
+
<!-- <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"/> -->
Added: branches/community/Seam_2_2_Drools5/examples/drools/resources/test2.drl
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/resources/test2.drl (rev 0)
+++ branches/community/Seam_2_2_Drools5/examples/drools/resources/test2.drl 2009-08-14 22:49:05 UTC (rev 11388)
@@ -0,0 +1,7 @@
+package org.jboss.seam.example.numberguess
+
+import org.jboss.seam.example.numberguess.Message;
+
+query "number_of_messages"
+ message : Message()
+end
\ No newline at end of file
Modified: branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RulesTest.java
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RulesTest.java 2009-08-14 21:09:04 UTC (rev 11387)
+++ branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RulesTest.java 2009-08-14 22:49:05 UTC (rev 11388)
@@ -1,8 +1,11 @@
package org.jboss.seam.example.numberguess;
+import java.util.Map;
+
import org.drools.runtime.ExecutionResults;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
+import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@@ -13,11 +16,19 @@
@Scope(ScopeType.CONVERSATION)
public class RulesTest
{
- @In(create=true) ExecutionResults batch1;
+ @In(create=true) ExecutionResults batch1;
+ @In(create=true) Map<String, org.drools.runtime.rule.QueryResults> queries1;
+
private static final LogProvider log = Logging.getLogProvider(RulesTest.class);
private String result;
+ private String queryResult;
+ @Create
@Begin(join=true)
+ public void init() {
+
+ }
+
public String getResult()
{
return ((Message) batch1.getValue( "message" )).getMessage();
@@ -27,4 +38,16 @@
{
this.result = result;
}
+
+ public String getQueryResult()
+ {
+ return String.valueOf(queries1.get("number_of_messages").size());
+ }
+
+ public void setQueryResult(String queryResult)
+ {
+ this.queryResult = queryResult;
+ }
+
+
}
Added: branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestFactProvider.java
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestFactProvider.java (rev 0)
+++ branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestFactProvider.java 2009-08-14 22:49:05 UTC (rev 11388)
@@ -0,0 +1,64 @@
+package org.jboss.seam.example.numberguess;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.drools.FactProvider;
+
+ at Name("testFactProvider")
+ at Scope(ScopeType.CONVERSATION)
+public class TestFactProvider implements FactProvider
+{
+ private List<Object> allFacts = new ArrayList<Object>();
+ private Map<String, Object> allGlobals = new HashMap<String, Object>();
+
+ @Create
+ public void init() {
+ Message m1 = new Message();
+ m1.setMessage("message1");
+ allFacts.add(m1);
+
+ Message m2 = new Message();
+ m2.setMessage("message2");
+ allFacts.add(m2);
+
+ Message m3 = new Message();
+ m3.setMessage("message3");
+ allFacts.add(m3);
+
+ Message m4 = new Message();
+ m4.setMessage("message4");
+ allFacts.add(m4);
+
+ Message m5 = new Message();
+ m5.setMessage("message5");
+ allFacts.add(m5);
+ }
+
+ public List<Object> getFacts()
+ {
+ return allFacts;
+ }
+
+ public Map<String, Object> getGlobals()
+ {
+ return null;
+ }
+
+ public void setFacts(List<Object> allFacts)
+ {
+ this.allFacts = allFacts;
+ }
+
+ public void setGlobals(Map<String, Object> globals)
+ {
+
+ }
+
+}
Modified: branches/community/Seam_2_2_Drools5/examples/drools/view/simple.jsp
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/view/simple.jsp 2009-08-14 21:09:04 UTC (rev 11387)
+++ branches/community/Seam_2_2_Drools5/examples/drools/view/simple.jsp 2009-08-14 22:49:05 UTC (rev 11388)
@@ -8,8 +8,13 @@
<h1>Simple Test</h1>
<f:view>
<h:form id="simples">
- <h:outputLabel value="Rules Result" for="output" />
- <h:outputText id="output" value="#{rulesTest.result}"/>
+ <h:panelGrid columns="2">
+ <h:outputLabel value="Rules Result: " for="output" />
+ <h:outputText id="output" value="#{rulesTest.result}"/>
+
+ <h:outputLabel value="Query Result: " for="output2" />
+ <h:outputText id="output2" value="#{rulesTest.queryResult}"/>
+ </h:panelGrid>
</h:form>
</f:view>
</body>
Added: branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/FactProvider.java
===================================================================
--- branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/FactProvider.java (rev 0)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/FactProvider.java 2009-08-14 22:49:05 UTC (rev 11388)
@@ -0,0 +1,20 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Interface for fact providers.
+ *
+ * @author Tihomir Surdilovic
+ *
+ */
+public interface FactProvider
+{
+ public List<Object> getFacts();
+ public void setFacts(List<Object> facts);
+
+ public Map<String, Object> getGlobals();
+ public void setGlobals(Map<String, Object> globals);
+
+}
Added: branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/QueryResults.java
===================================================================
--- branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/QueryResults.java (rev 0)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/QueryResults.java 2009-08-14 22:49:05 UTC (rev 11388)
@@ -0,0 +1,39 @@
+package org.jboss.seam.drools;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.core.Expressions.ValueExpression;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
+/**
+ * A conversation-scoped component that performs a query lookup against
+ * StatefulKnowledgeSession.
+ *
+ * @author Tihomir Surdilovic
+ *
+ */
+public class QueryResults
+{
+ private static final LogProvider log = Logging.getLogProvider(QueryResults.class);
+
+ private ValueExpression<org.drools.runtime.StatefulKnowledgeSession> ksession;
+ private String[] queryNames;
+
+ @Unwrap
+ public Map<String, org.drools.runtime.rule.QueryResults> getQueryResults() throws Exception {
+ Map<String, org.drools.runtime.rule.QueryResults> queryResultsMap = new HashMap<String, org.drools.runtime.rule.QueryResults>();
+ if(queryNames != null && ksession != null) {
+ for(String queryName : queryNames) {
+ org.drools.runtime.rule.QueryResults results = ksession.getValue().getQueryResults( queryName );
+ queryResultsMap.put(queryName, results);
+ }
+ } else {
+ log.warn("Invalid kseession or queries.");
+ }
+
+ return queryResultsMap;
+ }
+}
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-14 21:09:04 UTC (rev 11387)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/StatefulKnowledgeSession.java 2009-08-14 22:49:05 UTC (rev 11388)
@@ -2,6 +2,7 @@
import java.io.InputStream;
import java.io.Serializable;
+import java.util.Map;
import java.util.Properties;
import org.drools.KnowledgeBaseFactory;
@@ -34,6 +35,7 @@
private static final LogProvider log = Logging.getLogProvider(org.jboss.seam.drools.StatefulKnowledgeSession.class);
private ValueExpression<org.drools.KnowledgeBase> knowledgeBase;
+ private ValueExpression<org.jboss.seam.drools.FactProvider> factProvider;
private String[] eventListeners;
private String knowledgeSessionConfig;
@@ -61,6 +63,22 @@
ksession.getGlobals().setDelegate(new SeamDelegate());
+ if(factProvider != null) {
+ FactProvider fp = factProvider.getValue();
+ if(fp.getFacts() != null) {
+ for(Object nextFact : fp.getFacts()) {
+ log.info("Inserting next fact : " + nextFact);
+ ksession.insert(nextFact);
+ }
+ }
+ if(fp.getGlobals() != null) {
+ for(Map.Entry<String, Object> nextGlobal : fp.getGlobals().entrySet()) {
+ log.info("Setting next global: " + nextGlobal.getKey() + " with value: " + nextGlobal.getValue());
+ ksession.setGlobal(nextGlobal.getKey(), nextGlobal.getValue());
+ }
+ }
+ }
+
if(eventListeners != null) {
for(String eventListener : eventListeners) {
setEventListener(ksession, eventListener);
@@ -137,5 +155,15 @@
{
this.knowledgeSessionConfig = knowledgeSessionConfig;
}
-
+
+ public ValueExpression<org.jboss.seam.drools.FactProvider> getFactProvider()
+ {
+ return factProvider;
+ }
+
+ public void setFactProvider(ValueExpression<org.jboss.seam.drools.FactProvider> factProvider)
+ {
+ this.factProvider = factProvider;
+ }
+
}
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-14 21:09:04 UTC (rev 11387)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools-2.2.xsd 2009-08-14 22:49:05 UTC (rev 11388)
@@ -100,6 +100,7 @@
<xs:attribute name="knowledge-base" type="components:expressionType" />
<xs:attribute name="event-listeners" type="components:string" />
<xs:attribute name="knowledge-session-config" type="components:string" />
+ <xs:attribute name="fact-provider" type="components:expressionType"/>
</xs:attributeGroup>
<xs:element name="globals" type="components:multiValuedProperty" />
@@ -141,4 +142,22 @@
<xs:attribute name="batch-xml" type="components:expressionType" />
<xs:attribute name="batch-xml-config" type="components:string" />
</xs:attributeGroup>
+
+ <xs:element name="query-results">
+ <xs:annotation>
+ <xs:documentation>Query Results Component</xs:documentation>
+ </xs:annotation>
+ <xs:complexType mixed="true">
+ <xs:choice minOccurs="0" maxOccurs="unbounded">
+ <xs:element minOccurs="0" maxOccurs="1" ref="drools:query-names" />
+ </xs:choice>
+ <xs:attributeGroup ref="components:attlist.component" />
+ <xs:attributeGroup ref="drools:attlist.QueryResults" />
+ </xs:complexType>
+ </xs:element>
+ <xs:attributeGroup name="attlist.QueryResults">
+ <xs:attribute name="ksession" type="components:expressionType" />
+ <xs:attribute name="query-names" type="components:string"/>
+ </xs:attributeGroup>
+ <xs:element name="query-names" type="components:multiValuedProperty" />
</xs:schema>
More information about the seam-commits
mailing list