[seam-commits] Seam SVN: r11394 - in branches/community/Seam_2_2_Drools5: examples/drools/resources/WEB-INF and 3 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Aug 17 14:54:15 EDT 2009


Author: tsurdilovic
Date: 2009-08-17 14:54:14 -0400 (Mon, 17 Aug 2009)
New Revision: 11394

Added:
   branches/community/Seam_2_2_Drools5/examples/drools/resources/testtemplate.drl
   branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/Item.java
   branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/ItemCode.java
   branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestTemplateDataProvider.java
   branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestTemplateFactProvider.java
   branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/TemplateDataProvider.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/src/org/jboss/seam/example/numberguess/TestFactProvider.java
   branches/community/Seam_2_2_Drools5/examples/drools/view/simple.jsp
   branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeBase.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-17 12:17:03 UTC (rev 11393)
+++ branches/community/Seam_2_2_Drools5/examples/drools/resources/WEB-INF/components.xml	2009-08-17 18:54:14 UTC (rev 11394)
@@ -63,6 +63,25 @@
     		<value>org.drools.event.rule.DebugWorkingMemoryEventListener</value>
     	</drools:event-listeners>
     </drools:stateful-knowledge-session>
+    
+    
+    <drools:knowledge-base name="kbase4" knowledge-builder-config="kbuilderconfig.properties" knowledge-base-config="kbaseconfig.properties">
+    	<drools:rule-resources>
+    		<value>classpath:testtemplate.drl:DRL:testTemplateDataProvider</value>
+    	</drools:rule-resources>	
+    </drools:knowledge-base>
+    
+    <drools:stateful-knowledge-session name="ksession4" 
+    	  startup="false"
+    	  auto-create="true"
+          knowledge-base="#{kbase4}" 
+          knowledge-session-config="ksessionconfig.properties"
+          fact-provider="#{testTemplateFactProvider}">
+    	<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"/>
 	

Added: branches/community/Seam_2_2_Drools5/examples/drools/resources/testtemplate.drl
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/resources/testtemplate.drl	                        (rev 0)
+++ branches/community/Seam_2_2_Drools5/examples/drools/resources/testtemplate.drl	2009-08-17 18:54:14 UTC (rev 11394)
@@ -0,0 +1,17 @@
+template header
+field
+lower
+upper
+codes
+
+package org.jboss.seam.example.numbergues;
+import org.jboss.seam.example.numberguess.Item;
+import org.jboss.seam.example.numberguess.ItemCode;
+template "inRange"
+rule "is in range @{row.rowNumber}"
+when
+    Item( $name : name, $v : @{field} >= @{lower} && <= @{upper}, $code : code )
+then
+    System.out.println( "Item " + $name + " @{field} in range: " + $v + " code: " + $code );
+end
+end template
\ No newline at end of file

Added: branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/Item.java
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/Item.java	                        (rev 0)
+++ branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/Item.java	2009-08-17 18:54:14 UTC (rev 11394)
@@ -0,0 +1,58 @@
+package org.jboss.seam.example.numberguess;
+
+public class Item {
+   
+   private String name;
+   private int weight;
+   private int price;
+   private ItemCode code;
+   
+   public Item(String name, int price, int weight, ItemCode code) {
+      this.name = name;
+      this.price = price;
+      this.weight = weight;
+      this.code = code;
+   }
+
+   public String getName()
+   {
+      return name;
+   }
+
+   public void setName(String name)
+   {
+      this.name = name;
+   }
+
+   public int getWeight()
+   {
+      return weight;
+   }
+
+   public void setWeight(int weight)
+   {
+      this.weight = weight;
+   }
+
+   public int getPrice()
+   {
+      return price;
+   }
+
+   public void setPrice(int price)
+   {
+      this.price = price;
+   }
+
+   public ItemCode getCode()
+   {
+      return code;
+   }
+
+   public void setCode(ItemCode code)
+   {
+      this.code = code;
+   }
+
+   
+}

Added: branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/ItemCode.java
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/ItemCode.java	                        (rev 0)
+++ branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/ItemCode.java	2009-08-17 18:54:14 UTC (rev 11394)
@@ -0,0 +1,7 @@
+package org.jboss.seam.example.numberguess;
+
+public enum ItemCode {
+   LOCK,
+   STOCK,
+   BARREL;
+}

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-17 12:17:03 UTC (rev 11393)
+++ branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/RulesTest.java	2009-08-17 18:54:14 UTC (rev 11394)
@@ -18,6 +18,7 @@
 {
    @In(create=true) ExecutionResults batch1;
    @In(create=true) Map<String, org.drools.runtime.rule.QueryResults> queries1;
+   @In(create=false) org.drools.runtime.StatefulKnowledgeSession ksession4;
    
    private static final LogProvider log = Logging.getLogProvider(RulesTest.class);
    private String result;
@@ -49,5 +50,9 @@
       this.queryResult = queryResult;
    }
    
+   public String runTemplate() {
+      ksession4.fireAllRules();
+      return null;
+   }
    
 }

Modified: 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	2009-08-17 12:17:03 UTC (rev 11393)
+++ branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestFactProvider.java	2009-08-17 18:54:14 UTC (rev 11394)
@@ -6,6 +6,7 @@
 import java.util.Map;
 
 import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Begin;
 import org.jboss.seam.annotations.Create;
 import org.jboss.seam.annotations.Name;
 import org.jboss.seam.annotations.Scope;
@@ -19,6 +20,7 @@
    private Map<String, Object> allGlobals = new HashMap<String, Object>();
    
    @Create
+   @Begin(join=true)
    public void init() {
       Message m1 = new Message();
       m1.setMessage("message1");
@@ -48,7 +50,7 @@
 
    public Map<String, Object> getGlobals()
    {
-      return null;
+      return allGlobals;
    }
 
    public void setFacts(List<Object> allFacts)
@@ -56,9 +58,9 @@
      this.allFacts = allFacts;
    }
 
-   public void setGlobals(Map<String, Object> globals)
+   public void setGlobals(Map<String, Object> allGlobals)
    {
-      
+      this.allGlobals = allGlobals;
    }
    
 }

Added: branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestTemplateDataProvider.java
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestTemplateDataProvider.java	                        (rev 0)
+++ branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestTemplateDataProvider.java	2009-08-17 18:54:14 UTC (rev 11394)
@@ -0,0 +1,56 @@
+package org.jboss.seam.example.numberguess;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Begin;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.drools.TemplateDataProvider;
+
+ at Name("testTemplateDataProvider")
+ at Scope(ScopeType.CONVERSATION)
+public class TestTemplateDataProvider implements TemplateDataProvider
+{
+
+   Collection<Map<String, Object>> templateData = new ArrayList<Map<String,Object>>();
+   
+   @Create
+   @Begin(join=true)
+   public void init()
+   {
+      Map<String, Object> m1 = new HashMap<String, Object>();
+      m1.put("field", "weight");
+      m1.put("lower", 10);
+      m1.put("upper", 99);
+      m1.put("codes", EnumSet.of( ItemCode.LOCK, ItemCode.STOCK ) );
+      
+      Map<String, Object> m2 = new HashMap<String, Object>();
+      m2.put("field", "price");
+      m2.put("lower", 10);
+      m2.put("upper", 50);
+      m2.put("codes", EnumSet.of( ItemCode.BARREL ) );
+      
+      templateData.add(m1);
+      templateData.add(m2);
+      
+   }
+
+   public Collection<Map<String, Object>> getTemplateData()
+   {
+      return templateData;
+   }
+
+   public void setTemplateData(Collection<Map<String, Object>> templateData)
+   {
+      this.templateData = templateData;
+   }
+   
+   
+   
+}

Added: branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestTemplateFactProvider.java
===================================================================
--- branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestTemplateFactProvider.java	                        (rev 0)
+++ branches/community/Seam_2_2_Drools5/examples/drools/src/org/jboss/seam/example/numberguess/TestTemplateFactProvider.java	2009-08-17 18:54:14 UTC (rev 11394)
@@ -0,0 +1,59 @@
+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.Begin;
+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("testTemplateFactProvider")
+ at Scope(ScopeType.CONVERSATION)
+public class TestTemplateFactProvider implements FactProvider
+{
+   private List<Object> allFacts = new ArrayList<Object>();
+   private Map<String, Object> allGlobals = new HashMap<String, Object>();
+   
+   @Create
+   @Begin(join=true)
+   public void init() {
+      Item item1 = new Item( "A", 130,  42, ItemCode.LOCK );
+      Item item2 = new Item( "B",  44, 100, ItemCode.STOCK );
+      Item item3 = new Item( "C", 123, 180, ItemCode.BARREL );
+      Item item4 = new Item( "D",  85,   9, ItemCode.LOCK );
+      
+      allFacts.add(item1);
+      allFacts.add(item2);
+      allFacts.add(item3);
+      allFacts.add(item4);
+      
+   }
+
+   public List<Object> getFacts()
+   {
+      return allFacts;
+   }
+
+   public Map<String, Object> getGlobals()
+   {
+      return allGlobals;
+   }
+
+   public void setFacts(List<Object> allFacts)
+   {
+      this.allFacts = allFacts;
+      
+   }
+
+   public void setGlobals(Map<String, Object> allGlobals)
+   {
+     this.allGlobals = allGlobals;
+      
+   }
+   
+}

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-17 12:17:03 UTC (rev 11393)
+++ branches/community/Seam_2_2_Drools5/examples/drools/view/simple.jsp	2009-08-17 18:54:14 UTC (rev 11394)
@@ -15,6 +15,11 @@
 	    	<h:outputLabel value="Query Result: " for="output2" />
 	    	<h:outputText id="output2" value="#{rulesTest.queryResult}"/>
 	    </h:panelGrid>
+	</h:form> 
+	<h:form id="simple2">
+		<h:panelGrid columns="2">
+			<h:commandButton action="#{rulesTest.runTemplate}" value="Run template..." />
+		</h:panelGrid>
 	</h:form>
 </f:view>
 </body>

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-17 12:17:03 UTC (rev 11393)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/KnowledgeBase.java	2009-08-17 18:54:14 UTC (rev 11394)
@@ -1,6 +1,10 @@
 package org.jboss.seam.drools;
 
 import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Collection;
+import java.util.Map;
 import java.util.Properties;
 import java.util.regex.Pattern;
 
@@ -13,11 +17,14 @@
 import org.drools.builder.KnowledgeBuilderFactory;
 import org.drools.builder.ResourceType;
 import org.drools.io.ResourceFactory;
+import org.drools.template.ObjectDataCompiler;
+import org.jboss.seam.Component;
 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.Expressions;
 import org.jboss.seam.core.ResourceLoader;
 import org.jboss.seam.log.LogProvider;
 import org.jboss.seam.log.Logging;
@@ -26,7 +33,7 @@
  * Manager component for a Drools KnowledgeBase
  * 
  * @author Tihomir Surdilovic
- *
+ * 
  */
 @Scope(ScopeType.APPLICATION)
 @BypassInterceptors
@@ -37,6 +44,7 @@
    private static final int RESOURCE_PATH = 0;
    private static final int RESOURCE = 1;
    private static final int RESOURCE_TYPE = 2;
+   private static final int RESOURCE_TEMPLATE_DATA = 3;
    private static final String RESOURCE_TYPE_URL = "url";
    private static final String RESOURCE_TYPE_FILE = "file";
    private static final String RESOURCE_TYPE_CLASSPATH = "classpath";
@@ -47,12 +55,14 @@
    private org.drools.KnowledgeBase kbase;
    
    @Create
-   public void createKnowledgeBase() throws Exception {
+   public void createKnowledgeBase() throws Exception
+   {
       KnowledgeBuilderConfiguration kbuilderconfig;
-      if(knowledgeBaseConfig != null) {
+      if (knowledgeBaseConfig != null)
+      {
          Properties kbuilderProp = new Properties();
          InputStream stream = ResourceLoader.instance().getResourceAsStream(knowledgeBuilderConfig);
-         if (stream==null)
+         if (stream == null)
          {
             throw new IllegalStateException("could not locate knowledgeBuilder configuration: " + knowledgeBaseConfig);
          }
@@ -60,31 +70,38 @@
          log.info("loading knowledge builder config properties: " + kbuilderProp.toString());
          stream.close();
          kbuilderconfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(kbuilderProp, null);
-      } else {
+      }
+      else
+      {
          kbuilderconfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
       }
       
       KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderconfig);
       
-      if(ruleResources != null) {
-         for(String nextResource : ruleResources) {
+      if (ruleResources != null)
+      {
+         for (String nextResource : ruleResources)
+         {
             addResource(kbuilder, nextResource);
          }
       }
-         
+      
       KnowledgeBuilderErrors kbuildererrors = kbuilder.getErrors();
-      if(kbuildererrors.size() > 0) {
-         for(KnowledgeBuilderError kbuildererror : kbuildererrors) {
+      if (kbuildererrors.size() > 0)
+      {
+         for (KnowledgeBuilderError kbuildererror : kbuildererrors)
+         {
             log.error(kbuildererror.getMessage());
          }
-      }  
+      }
       
       KnowledgeBaseConfiguration kbaseconfig;
       
-      if(knowledgeBaseConfig != null) {
+      if (knowledgeBaseConfig != null)
+      {
          Properties kbaseProp = new Properties();
          InputStream stream = ResourceLoader.instance().getResourceAsStream(knowledgeBaseConfig);
-         if (stream==null)
+         if (stream == null)
          {
             throw new IllegalStateException("could not locate knowledgeBase configuration: " + knowledgeBaseConfig);
          }
@@ -92,17 +109,17 @@
          log.info("loading knowledge base config properties: " + kbaseProp.toString());
          stream.close();
          kbaseconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(kbaseProp, null);
-      } else {
+      }
+      else
+      {
          kbaseconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
       }
-           
+      
       kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseconfig);
       kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
       
-      
    }
    
-   
    @Unwrap
    public org.drools.KnowledgeBase getKnowledgeBase()
    {
@@ -113,57 +130,85 @@
    {
       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) {
+   protected void addResource(KnowledgeBuilder kbuilder, String resource)
+   {
       String[] resourceParts = COLON.split(resource.trim());
       
-      if(resourceParts.length != 3) {
+      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]);
+      }
+      else
+      {
+         log.info("New resource path: " + resourceParts[RESOURCE_PATH]);
+         log.info("New resource location: " + resourceParts[RESOURCE]);
+         log.info("New resource type: " + resourceParts[RESOURCE_TYPE]);
          
          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]);
+         
+         if (resourceParts.length == 4)
+         {
+            log.info("New template data provider: " + resourceParts[RESOURCE_TEMPLATE_DATA]);
+            TemplateDataProvider templateDataProvider = (TemplateDataProvider) Component.getInstance(resourceParts[RESOURCE_TEMPLATE_DATA], true);   
+            
+            InputStream templateStream = ResourceLoader.instance().getResourceAsStream(resourceParts[RESOURCE]);
+            if (templateStream==null)
+            {
+               throw new IllegalStateException("Could not locate rule file: " + resourceParts[RESOURCE]);
+            }
+            
+            ObjectDataCompiler converter = new ObjectDataCompiler();
+            String drl = converter.compile( templateDataProvider.getTemplateData(), templateStream );
+            log.info("Generated DRL from template: " + drl);
+            Reader rdr = new StringReader( drl );
+            
+            kbuilder.add( ResourceFactory.newReaderResource( rdr ), resourceType );
          }
+         else
+         {    
+            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/TemplateDataProvider.java
===================================================================
--- branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/TemplateDataProvider.java	                        (rev 0)
+++ branches/community/Seam_2_2_Drools5/src/main/org/jboss/seam/drools/TemplateDataProvider.java	2009-08-17 18:54:14 UTC (rev 11394)
@@ -0,0 +1,15 @@
+package org.jboss.seam.drools;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Interface for template data providers.
+ * 
+ * @author Tihomir Surdilovic
+ * 
+ */
+public interface TemplateDataProvider
+{
+   public Collection<Map<String,Object>> getTemplateData();
+}



More information about the seam-commits mailing list