01: package org.acme.insurance.launcher;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.io.StringReader;
06:
07: import org.acme.insurance.Driver;
08: import org.acme.insurance.Policy;
09: import org.drools.RuleBase;
10: import org.drools.RuleBaseFactory;
11: import org.drools.WorkingMemory;
12: import org.drools.compiler.DroolsParserException;
13: import org.drools.compiler.PackageBuilder;
14: import org.drools.decisiontable.InputType;
15: import org.drools.decisiontable.SpreadsheetCompiler;
16:
17: /**
18: * This is a sample file to launch a rule package from a rule source file.
19: */
20: public class PricingRuleLauncher {
21:
22: public static final void main(String[] args) throws Exception {
23: PricingRuleLauncher launcher = new PricingRuleLauncher();
24: launcher.executeExample();
25: }
26:
27: public int executeExample() throws Exception {
28:
29: //first we compile the decision table into a whole lot of rules.
30: SpreadsheetCompiler compiler = new SpreadsheetCompiler();
31: String drl = compiler.compile(getSpreadsheetStream(),
32: InputType.XLS);
33:
34: //UNCOMMENT ME TO SEE THE DRL THAT IS GENERATED
35: //System.out.println(drl);
36:
37: RuleBase ruleBase = buildRuleBase(drl);
38:
39: WorkingMemory wm = ruleBase.newStatefulSession();
40:
41: //now create some test data
42: Driver driver = new Driver();
43: Policy policy = new Policy();
44:
45: wm.insert(driver);
46: wm.insert(policy);
47:
48: wm.fireAllRules();
49:
50: System.out.println("BASE PRICE IS: " + policy.getBasePrice());
51: System.out.println("DISCOUNT IS: "
52: + policy.getDiscountPercent());
53:
54: return policy.getBasePrice();
55:
56: }
57:
58: /** Build the rule base from the generated DRL */
59: private RuleBase buildRuleBase(String drl)
60: throws DroolsParserException, IOException, Exception {
61: //now we build the rule package and rulebase, as if they are normal rules
62: PackageBuilder builder = new PackageBuilder();
63: builder.addPackageFromDrl(new StringReader(drl));
64:
65: //add the package to a rulebase (deploy the rule package).
66: RuleBase ruleBase = RuleBaseFactory.newRuleBase();
67: ruleBase.addPackage(builder.getPackage());
68: return ruleBase;
69: }
70:
71: private InputStream getSpreadsheetStream() {
72: return this .getClass().getResourceAsStream(
73: "/data/ExamplePolicyPricing.xls");
74: }
75:
76: }
|