Author: mic...@jb... Date: 2006-05-23 02:00:27 -0400 (Tue, 23 May 2006) New Revision: 4363 Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.classpath labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.project labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/Instructions.txt labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/lib/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/lib/drools-decisiontables.jar labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Driver.java labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Policy.java labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/launcher/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/launcher/PricingRuleLauncher.java labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/rules/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/rules/decisiontables/ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/rules/decisiontables/PolicyPricing.xls Log: lab 4 Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.classpath =================================================================== --- labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.classpath 2006-05-23 05:28:34 UTC (rev 4362) +++ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.classpath 2006-05-23 06:00:27 UTC (rev 4363) @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src/java"/> + <classpathentry kind="src" path="src/rules"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="DROOLS/JBoss Rules"/> + <classpathentry kind="lib" path="lib/drools-decisiontables.jar"/> + <classpathentry kind="output" path="bin"/> +</classpath> Property changes on: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.classpath ___________________________________________________________________ Name: svn:eol-style + native Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.project =================================================================== --- labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.project 2006-05-23 05:28:34 UTC (rev 4362) +++ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.project 2006-05-23 06:00:27 UTC (rev 4363) @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>lab-4-rule-formats</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.drools.ide.droolsbuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> Property changes on: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/.project ___________________________________________________________________ Name: svn:eol-style + native Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/Instructions.txt =================================================================== --- labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/Instructions.txt 2006-05-23 05:28:34 UTC (rev 4362) +++ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/Instructions.txt 2006-05-23 06:00:27 UTC (rev 4363) @@ -0,0 +1,50 @@ +--------------------------------------------- +JBoss Rules Essentual Lab Excercise 4 + - Alternative rule formats - +--------------------------------------------- + +Instructions: + This excercise is a self contained Eclipse project. + + FOR DECISION TABLES: + + Step 1: Import the project into eclipse. + Step 2: Note the location of PolicyPricing.xls decision table. Note that it is in a classpath + accessible location (seperate to the source code). + Step 3: Right click on the PolicyPricing.xls spreadsheet, and choose the Open With menu item + followed by "System Editor". This will open up the decision table in the spreadsheet app. + + Step 4: Find the org.acme.insurance package, and note the domain object in there (they have default + values for fields). Open the PricingRuleLauncher class in the launcher sub package. + Step 5: Fire the launcher (it has a main method). Run menu, Run-as, Java application + You should see some results - one item being the base price, another the discount. You + can also see messages being logged by the rules (one of the action columns). + Step 6: Change some values in the message being logged, and/or the values of discount or price. + You can also change the attributes of the driver, to see other rules fired. + Step 7: See if you can find any inconsitencies/gaps in the rule rows (where a price may not be found, or + where there is a "conflict" and 2 rules may match). + - Note: if there is a conflict, either add more conditions to make it unique, + or, use the "sequential" flag to force the rules to have a top to bottom order. + You can then have a "hidden" action that does "drools.clearAgenda()" to stop for the first match + found. This is what we call a "dirty hack", but can be useful from time to time. + Step 8: Uncomment the line that prints out the DRL from the spreadsheet, inspect. + + Step 9: Make a grammatical error in the "rule template" row, check out the errors being reported (ie how to debug). + Step 10: For advanced folks, you can try the excel feature for "locking" cells so you can allow non + techie folk to edit in relative safety. + (http://support.microsoft.com/?kbid=214081) + + + + NOTE: when you make a change to the spreadsheet, you will need to refresh the eclipse project to pick up + the latest (of course, if you change it to load direct from a file system, not the classpath, + then you won't need to do this). + + + + +Pre-requisites: + Eclipse 3.2 must be installed + JBoss Rules plug in installed into Eclipse + (including the decision tables module). + Microsoft Excel or Open Office 2.0 should be installed. \ No newline at end of file Property changes on: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/Instructions.txt ___________________________________________________________________ Name: svn:eol-style + native Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/lib/drools-decisiontables.jar =================================================================== (Binary files differ) Property changes on: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/lib/drools-decisiontables.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Driver.java =================================================================== --- labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Driver.java 2006-05-23 05:28:34 UTC (rev 4362) +++ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Driver.java 2006-05-23 06:00:27 UTC (rev 4363) @@ -0,0 +1,41 @@ +package org.acme.insurance; + +/** + * This represents obviously a driver who is applying for an insurance Policy. + * @author Michael Neale + * + */ +public class Driver { + + private String name = "Mr Joe Blogs"; + private Integer age = new Integer(30); + private Integer priorClaims = new Integer(0); + private String locationRiskProfile = "LOW"; + + public Integer getAge() { + return age; + } + public void setAge(Integer age) { + this.age = age; + } + public String getLocationRiskProfile() { + return locationRiskProfile; + } + public void setLocationRiskProfile(String locationRiskProfile) { + this.locationRiskProfile = locationRiskProfile; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public Integer getPriorClaims() { + return priorClaims; + } + public void setPriorClaims(Integer priorClaims) { + this.priorClaims = priorClaims; + } + + +} Property changes on: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Driver.java ___________________________________________________________________ Name: svn:eol-style + native Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Policy.java =================================================================== --- labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Policy.java 2006-05-23 05:28:34 UTC (rev 4362) +++ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Policy.java 2006-05-23 06:00:27 UTC (rev 4363) @@ -0,0 +1,49 @@ +package org.acme.insurance; + +/** + * This represents a policy that a driver is applying for. + * + * Obviously in the real world, there are actuaries to mess things up, but lets just pretend there is + * some simple base price and discount that we can calculate with relatively simple rules ! + * + * @author Michael Neale + */ +public class Policy { + + private String type = "COMPREHENSIVE"; + private boolean approved = true; + private int discountPercent = 0; + private int basePrice; + + public boolean isApproved() { + return approved; + } + public void setApproved(boolean approved) { + this.approved = approved; + } + public int getDiscountPercent() { + return discountPercent; + } + public void setDiscountPercent(int discountPercent) { + this.discountPercent = discountPercent; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public void applyDiscount(int discount) { + discountPercent += discount; + } + public int getBasePrice() { + return basePrice; + } + public void setBasePrice(int basePrice) { + this.basePrice = basePrice; + } + +} Property changes on: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/Policy.java ___________________________________________________________________ Name: svn:eol-style + native Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/launcher/PricingRuleLauncher.java =================================================================== --- labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/launcher/PricingRuleLauncher.java 2006-05-23 05:28:34 UTC (rev 4362) +++ labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/launcher/PricingRuleLauncher.java 2006-05-23 06:00:27 UTC (rev 4363) @@ -0,0 +1,137 @@ +package org.acme.insurance.launcher; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; + +import org.acme.insurance.Driver; +import org.acme.insurance.Policy; +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.InputType; +import org.drools.decisiontable.SpreadsheetCompiler; +import org.drools.rule.Package; + +/** + * This is a sample file to launch a rule package from a rule source file. + */ +public class PricingRuleLauncher { + + public static final void main(String[] args) throws Exception { + PricingRuleLauncher launcher = new PricingRuleLauncher(); + launcher.executeExample(); + } + + + public void executeExample() throws Exception { + + //first we compile the decision table into a whole lot of rules. + SpreadsheetCompiler compiler = new SpreadsheetCompiler(); + String drl = compiler.compile(getSpreadsheetStream(), InputType.XLS); + + //UNCOMMENT ME TO SEE THE DRL THAT IS GENERATED + //System.out.println(drl); + + RuleBase ruleBase = buildRuleBase(drl); + + WorkingMemory wm = ruleBase.newWorkingMemory(); + + //now create some test data + Driver driver = new Driver(); + Policy policy = new Policy(); + + wm.assertObject(driver); + wm.assertObject(policy); + + wm.fireAllRules(); + + System.out.println("BASE PRICE IS: " + policy.getBasePrice()); + System.out.println("DISCOUNT IS: " + policy.getDiscountPercent()); + + + } + + + private RuleBase buildRuleBase(String drl) throws DroolsParserException, IOException, Exception { + //now we build the rule package and rulebase, as if they are normal rules + PackageBuilder builder = new PackageBuilder(); + 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; + } + + + + /** + * Need to get an input stream for a spreadsheet. + * In this case, as it is in the projects source directory, it can be grabbed + * from the classpath, but it could be anywhere. + * + * Try changing it to load from a hard coded location on disk. + */ + private InputStream getSpreadsheetStream() { + return this.getClass().getResourceAsStream("/decisiontables/PolicyPricing.xls"); + } + + + /** + * Please note that this is the "low level" rule assembly API. + */ + private static RuleBase readRule() throws Exception { + + //read in the source + Reader source = new InputStreamReader( PricingRuleLauncher.class.getResourceAsStream( "/Sample.drl" ) ); + + + PackageBuilder builder = new PackageBuilder(); + + //this wil parse and compile in one step + //NOTE: There are 2 methods here, the one argument one is for normal DRL. + builder.addPackageFromDrl( source ); + + //Use the following instead of above if you are using a DSL: + //builder.addPackageFromDrl( source, dsl ); + + //get the compiled package (which is serializable) + Package pkg = builder.getPackage(); + + //add the package to a rulebase (deploy the rule package). + RuleBase ruleBase = RuleBaseFactory.newRuleBase(); + ruleBase.addPackage( pkg ); + return ruleBase; + } + + public static class Message { + public static final int HELLO = 0; + public static final int GOODBYE = 1; + + private String message; + + private int status; + + public String getMessage() { + return this.message; + } + + public void setMessage(String message) { + this.message = message; + } + + public int getStatus() { + return this.status; + } + + public void setStatus( int status ) { + this.status = status; + } + } + +} Property changes on: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/java/org/acme/insurance/launcher/PricingRuleLauncher.java ___________________________________________________________________ Name: svn:eol-style + native Added: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/rules/decisiontables/PolicyPricing.xls =================================================================== (Binary files differ) Property changes on: labs/jbossrules/trunk/documentation/training/developers-course/lab-4-rule-formats/src/rules/decisiontables/PolicyPricing.xls ___________________________________________________________________ Name: svn:mime-type + application/octet-stream |