[jboss-svn-commits] JBL Code SVN: r21130 - in labs/jbossrules/trunk/drools-examples/drools-examples-drl: src/main/java/org/drools/examples and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Jul 19 01:47:40 EDT 2008
Author: stevearoonie
Date: 2008-07-19 01:47:40 -0400 (Sat, 19 Jul 2008)
New Revision: 21130
Added:
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PricingRuleTemplateExample.java
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/SimpleRuleTemplateExample.java
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/Cheese.java
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/Person.java
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/BasePricing.drt
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/Cheese.drt
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/ExampleCheese.xls
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PromotionalPricing.drt
Modified:
labs/jbossrules/trunk/drools-examples/drools-examples-drl/.classpath
labs/jbossrules/trunk/drools-examples/drools-examples-drl/.project
labs/jbossrules/trunk/drools-examples/drools-examples-drl/pom.xml
Log:
Added examples for Spreadsheet-driven rule templates
Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/.classpath
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/.classpath 2008-07-19 05:39:57 UTC (rev 21129)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/.classpath 2008-07-19 05:47:40 UTC (rev 21130)
@@ -1,13 +1,21 @@
-<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="src" path="src/main/java"/>
- <classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/>
- <classpathentry excluding="**/*.java" kind="src" path="src/main/rules"/>
- <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
- <classpathentry excluding="**/*.java" kind="src" output="target/test-classes" path="src/test/resources"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="lib" path="lib/foxtrot-2.0.jar"/>
- <classpathentry kind="lib" path="lib/jgoodies-forms-1.0.4.jar"/>
- <classpathentry kind="con" path="DROOLS/Drools"/>
- <classpathentry kind="output" path="target/classes"/>
-</classpath>
+ <classpathentry kind="src" path="src/main/java"/>
+ <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
+ <classpathentry kind="src" path="src/main/rules" excluding="**/*.java"/>
+ <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
+ <classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
+ <classpathentry kind="output" path="target/classes"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="var" path="M2_REPO/com/jgoodies/forms/1.0.7/forms-1.0.7.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/drools/drools-templates/5.0.0.SNAPSHOT/drools-templates-5.0.0.SNAPSHOT.jar" sourcepath="M2_REPO/org/drools/drools-templates/5.0.0.SNAPSHOT/drools-templates-5.0.0.SNAPSHOT-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/jexcelapi/jxl/2.4.2/jxl-2.4.2.jar"/>
+ <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/eclipse/jdt/core/3.2.3.v_686_R32x/core-3.2.3.v_686_R32x.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/drools/drools-decisiontables/5.0.0.SNAPSHOT/drools-decisiontables-5.0.0.SNAPSHOT.jar" sourcepath="M2_REPO/org/drools/drools-decisiontables/5.0.0.SNAPSHOT/drools-decisiontables-5.0.0.SNAPSHOT-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/foxtrot/foxtrot/2.0/foxtrot-2.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/mvel/mvel/2.0-dp4/mvel-2.0-dp4.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/drools/drools-compiler/5.0.0.SNAPSHOT/drools-compiler-5.0.0.SNAPSHOT.jar" sourcepath="M2_REPO/org/drools/drools-compiler/5.0.0.SNAPSHOT/drools-compiler-5.0.0.SNAPSHOT-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/drools/drools-core/5.0.0.SNAPSHOT/drools-core-5.0.0.SNAPSHOT.jar" sourcepath="M2_REPO/org/drools/drools-core/5.0.0.SNAPSHOT/drools-core-5.0.0.SNAPSHOT-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/antlr/antlr-runtime/3.0/antlr-runtime-3.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/janino/janino/2.5.10/janino-2.5.10.jar"/>
+</classpath>
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/.project
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/.project 2008-07-19 05:39:57 UTC (rev 21129)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/.project 2008-07-19 05:47:40 UTC (rev 21130)
@@ -1,22 +1,16 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>drools-examples-drl</name>
- <comment>A rule production system</comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.jdt.core.javabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.drools.eclipse.droolsbuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.jdt.core.javanature</nature>
- </natures>
-</projectDescription>
+<projectDescription>
+ <name>drools-examples</name>
+ <comment>A rule production system</comment>
+ <projects/>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ </buildCommand>
+ <buildCommand>
+ <name>org.drools.eclipse.droolsbuilder</name>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/pom.xml
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/pom.xml 2008-07-19 05:39:57 UTC (rev 21129)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/pom.xml 2008-07-19 05:47:40 UTC (rev 21130)
@@ -30,6 +30,10 @@
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-templates</artifactId>
+ </dependency>
<!-- External dependencies -->
<dependency>
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PricingRuleTemplateExample.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PricingRuleTemplateExample.java (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PricingRuleTemplateExample.java 2008-07-19 05:47:40 UTC (rev 21130)
@@ -0,0 +1,101 @@
+package org.drools.examples;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.WorkingMemory;
+import org.drools.compiler.DroolsParserException;
+import org.drools.compiler.PackageBuilder;
+import org.drools.decisiontable.ExternalSpreadsheetCompiler;
+import org.drools.decisiontable.InputType;
+import org.drools.examples.decisiontable.Driver;
+import org.drools.examples.decisiontable.Policy;
+import org.drools.template.parser.DataListener;
+import org.drools.template.parser.TemplateDataListener;
+
+/**
+ * This shows off a rule template where the data provider is a spreadsheet.
+ * This example uses the same spreadsheet as the Decision table example ({@link PricingRuleDTExample})
+ * so that you can see the difference between the two.
+ *
+ * Note that even though they use the same spreadsheet, this example is just
+ * concerned with the data cells and does not use any of the Decision Table data.
+ * @author Steve
+ *
+ */
+public class PricingRuleTemplateExample {
+ public static void main(String[] args) throws Exception {
+ PricingRuleTemplateExample launcher = new PricingRuleTemplateExample();
+ launcher.executeExample();
+ }
+
+ private int executeExample() throws Exception {
+
+ //first we compile the decision table into a whole lot of rules.
+ final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
+ final List<DataListener> listeners = new ArrayList<DataListener>();
+ TemplateDataListener l1 = new TemplateDataListener(10, 3, getBasePricingRulesStream());
+ listeners.add(l1);
+ TemplateDataListener l2 = new TemplateDataListener(30, 3, getPromotionalPricingRulesStream());
+ listeners.add(l2);
+ converter.compile(getSpreadsheetStream(), InputType.XLS, listeners);
+
+ String baseRules = l1.renderDRL();
+ //Uncomment to see the base pricing rules
+ //System.out.println(baseRules);
+ String promotionalRules = l2.renderDRL();
+ //Uncomment to see the promotional pricing rules
+ //System.out.println(promotionalRules);
+ //BUILD RULEBASE
+ final RuleBase rb = buildRuleBase(baseRules, promotionalRules);
+
+ WorkingMemory wm = rb.newStatefulSession();
+
+ //now create some test data
+ Driver driver = new Driver();
+ Policy policy = new Policy();
+
+ wm.insert(driver);
+ wm.insert(policy);
+
+ wm.fireAllRules();
+
+ System.out.println("BASE PRICE IS: " + policy.getBasePrice());
+ System.out.println("DISCOUNT IS: " + policy.getDiscountPercent( ));
+
+ return policy.getBasePrice();
+ }
+
+ /** Build the rule base from the generated DRL */
+ private RuleBase buildRuleBase(String... drls) throws DroolsParserException, IOException, Exception {
+ //now we build the rule package and rulebase, as if they are normal rules
+ PackageBuilder builder = new PackageBuilder();
+ for ( String drl : drls ) {
+ builder.addPackageFromDrl( new StringReader( drl ) );
+ }
+
+ //add the package to a rulebase (deploy the rule package).
+ RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ ruleBase.addPackage( builder.getPackage() );
+ return ruleBase;
+ }
+
+ private InputStream getSpreadsheetStream() {
+ return this.getClass().getResourceAsStream("ExamplePolicyPricing.xls");
+ }
+
+ private InputStream getBasePricingRulesStream() {
+ return this.getClass().getResourceAsStream("BasePricing.drt");
+ }
+
+ private InputStream getPromotionalPricingRulesStream() {
+ return this.getClass().getResourceAsStream("PromotionalPricing.drt");
+ }
+
+
+}
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/SimpleRuleTemplateExample.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/SimpleRuleTemplateExample.java (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/SimpleRuleTemplateExample.java 2008-07-19 05:47:40 UTC (rev 21130)
@@ -0,0 +1,82 @@
+package org.drools.examples;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.WorkingMemory;
+import org.drools.compiler.DroolsParserException;
+import org.drools.compiler.PackageBuilder;
+import org.drools.decisiontable.ExternalSpreadsheetCompiler;
+import org.drools.examples.templates.Cheese;
+import org.drools.examples.templates.Person;
+
+/**
+ * This shows off a very simple rule template where the data provider is a spreadsheet.
+ * @author Steve
+ *
+ */
+public class SimpleRuleTemplateExample {
+ public static void main(String[] args) throws Exception {
+ SimpleRuleTemplateExample launcher = new SimpleRuleTemplateExample();
+ launcher.executeExample();
+ }
+
+ private void executeExample() throws Exception {
+
+ //first we compile the spreadsheet with the template
+ //to create a whole lot of rules.
+ final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
+ //the data we are interested in starts at row 2, column 2 (e.g. B2)
+ final String drl = converter.compile(getSpreadsheetStream(), getRulesStream(), 2, 2);
+
+ //Uncomment to see rules
+ //System.out.println( drl );
+ //BUILD RULEBASE
+ final RuleBase rb = buildRuleBase(drl);
+
+ WorkingMemory wm = rb.newStatefulSession();
+
+ //now create some test data
+ wm.insert( new Cheese( "stilton",
+ 42 ) );
+ wm.insert( new Person( "michael",
+ "stilton",
+ 42 ) );
+ final List<String> list = new ArrayList<String>();
+ wm.setGlobal( "list",
+ list );
+
+ wm.fireAllRules();
+
+ System.out.println(list);
+
+ }
+
+ /** Build the rule base from the generated DRL */
+ private RuleBase buildRuleBase(String... drls) throws DroolsParserException, IOException, Exception {
+ //now we build the rule package and rulebase, as if they are normal rules
+ PackageBuilder builder = new PackageBuilder();
+ for ( String drl : drls ) {
+ builder.addPackageFromDrl( new StringReader( drl ) );
+ }
+
+ //add the package to a rulebase (deploy the rule package).
+ RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ ruleBase.addPackage( builder.getPackage() );
+ return ruleBase;
+ }
+
+ private InputStream getSpreadsheetStream() {
+ return this.getClass().getResourceAsStream("ExampleCheese.xls");
+ }
+
+ private InputStream getRulesStream() {
+ return this.getClass().getResourceAsStream("Cheese.drt");
+ }
+
+}
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/Cheese.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/Cheese.java (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/Cheese.java 2008-07-19 05:47:40 UTC (rev 21130)
@@ -0,0 +1,45 @@
+package org.drools.examples.templates;
+
+/*
+ * Copyright 2005 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class Cheese {
+ private String type;
+ private int price;
+
+ public Cheese() {
+
+ }
+ public Cheese(final String type,
+ final int price) {
+ super();
+ this.type = type;
+ this.price = price;
+ }
+
+ public int getPrice() {
+ return this.price;
+ }
+
+ public String getType() {
+ return this.type;
+ }
+
+ public void setPrice(final int price) {
+ this.price = price;
+ }
+
+}
\ No newline at end of file
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/Person.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/Person.java (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/Person.java 2008-07-19 05:47:40 UTC (rev 21130)
@@ -0,0 +1,93 @@
+package org.drools.examples.templates;
+
+/*
+ * Copyright 2005 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class Person {
+ private String name;
+ private String likes;
+ private int age;
+
+ private char sex;
+
+ private boolean alive;
+
+ private String status;
+
+ public Person() {
+
+ }
+ public Person(final String name) {
+ this( name,
+ "",
+ 0 );
+ }
+
+ public Person(final String name,
+ final String likes) {
+ this( name,
+ likes,
+ 0 );
+ }
+
+ public Person(final String name,
+ final String likes,
+ final int age) {
+ this.name = name;
+ this.likes = likes;
+ this.age = age;
+ }
+
+ public String getStatus() {
+ return this.status;
+ }
+
+ public void setStatus(final String status) {
+ this.status = status;
+ }
+
+ public String getLikes() {
+ return this.likes;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public int getAge() {
+ return this.age;
+ }
+
+ public boolean isAlive() {
+ return this.alive;
+ }
+
+ public void setAlive(final boolean alive) {
+ this.alive = alive;
+ }
+
+ public char getSex() {
+ return this.sex;
+ }
+
+ public void setSex(final char sex) {
+ this.sex = sex;
+ }
+
+ public String toString() {
+ return "[Person name='" + this.name + "']";
+ }
+}
\ No newline at end of file
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/BasePricing.drt
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/BasePricing.drt (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/BasePricing.drt 2008-07-19 05:47:40 UTC (rev 21130)
@@ -0,0 +1,28 @@
+template header
+age[]
+profile
+priorClaims
+policyType
+base
+reason
+
+package org.drools.examples.decisiontable;
+
+template "Pricing bracket"
+age
+policyType
+base
+
+rule "Pricing bracket_@{row.rowNumber}"
+
+ when
+ Driver(age >= @{age0}, age <= @{age1}
+ , priorClaims == "@{priorClaims}"
+ , locationRiskProfile == "@{profile}"
+ )
+ policy: Policy(type == "@{policyType}")
+ then
+ policy.setBasePrice(@{base});
+ System.out.println("@{reason}");
+end
+end template
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/Cheese.drt
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/Cheese.drt (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/Cheese.drt 2008-07-19 05:47:40 UTC (rev 21130)
@@ -0,0 +1,19 @@
+template header
+age
+type
+log
+
+package org.drools.examples.templates;
+
+global java.util.List list;
+
+template "cheesefans"
+
+rule "Cheese fans_@{row.rowNumber}"
+ when
+ Person(age == @{age})
+ Cheese(type == "@{type}")
+ then
+ list.add("@{log}");
+end
+end template
\ No newline at end of file
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/ExampleCheese.xls
===================================================================
(Binary files differ)
Property changes on: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/ExampleCheese.xls
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PromotionalPricing.drt
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PromotionalPricing.drt (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PromotionalPricing.drt 2008-07-19 05:47:40 UTC (rev 21130)
@@ -0,0 +1,23 @@
+template header
+age[]
+priorClaims
+policyType
+discount
+
+package org.drools.examples.decisiontable;
+
+template "discounts"
+age
+priorClaims
+policyType
+discount
+
+rule "Discounts_@{row.rowNumber}"
+
+ when
+ Driver(age >= @{age0}, age <= @{age1}, priorClaims == "@{priorClaims}")
+ policy: Policy(type == "@{policyType}")
+ then
+ policy.applyDiscount(@{discount});
+end
+end template
More information about the jboss-svn-commits
mailing list