001: package org.drools.decisiontable;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.StringReader;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import junit.framework.TestCase;
024:
025: import org.acme.insurance.Driver;
026: import org.acme.insurance.Policy;
027: import org.drools.RuleBase;
028: import org.drools.RuleBaseFactory;
029: import org.drools.WorkingMemory;
030: import org.drools.compiler.DroolsError;
031: import org.drools.compiler.PackageBuilder;
032: import org.drools.decisiontable.parser.ExternalSheetListener;
033: import org.drools.rule.Package;
034:
035: /**
036: * @author <a href="mailto:stevearoonie@gmail.com">Steven Williams</a> Some basic unit tests for converter utility.
037: * Note that some of this may still use the drools 2.x syntax, as it is not compiled, only tested that it
038: * generates DRL in the correct structure (not that the DRL itself is correct).
039: */
040: public class ExternalSpreadsheetCompilerIntegrationTest extends
041: TestCase {
042: public void testIntegration() throws Exception {
043: final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
044: final String drl = converter.compile(
045: "/data/IntegrationExampleTest.xls",
046: "/templates/test_integration.drl", 18, 3);
047: //COMPILE
048: final PackageBuilder builder = new PackageBuilder();
049: builder.addPackageFromDrl(new StringReader(drl));
050:
051: final Package pkg = builder.getPackage();
052: assertNotNull(pkg);
053: assertEquals(0, builder.getErrors().getErrors().length);
054:
055: //BUILD RULEBASE
056: final RuleBase rb = RuleBaseFactory.newRuleBase();
057: rb.addPackage(pkg);
058:
059: //NEW WORKING MEMORY
060: final WorkingMemory wm = rb.newStatefulSession();
061:
062: //ASSERT AND FIRE
063: wm.insert(new Cheese("stilton", 42));
064: wm.insert(new Person("michael", "stilton", 42));
065: final List list = new ArrayList();
066: wm.setGlobal("list", list);
067: wm.fireAllRules();
068: assertEquals(1, list.size());
069:
070: }
071:
072: public void testPricing() throws Exception {
073: final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
074: final List listeners = new ArrayList();
075: ExternalSheetListener l1 = new ExternalSheetListener(10, 3,
076: "/templates/test_pricing1.drl");
077: listeners.add(l1);
078: ExternalSheetListener l2 = new ExternalSheetListener(30, 3,
079: "/templates/test_pricing2.drl");
080: listeners.add(l2);
081: converter.compile("/data/ExamplePolicyPricing.xls",
082: InputType.XLS, listeners);
083: //COMPILE
084: final PackageBuilder builder = new PackageBuilder();
085: builder.addPackageFromDrl(new StringReader(l1.renderDRL()));
086: builder.addPackageFromDrl(new StringReader(l2.renderDRL()));
087:
088: final Package pkg = builder.getPackage();
089: assertNotNull(pkg);
090: DroolsError[] errors = builder.getErrors().getErrors();
091: // for (int i = 0; i < errors.length; i++) {
092: // DroolsError error = errors[i];
093: // System.out.println(error.getMessage());
094: // }
095: assertEquals(0, errors.length);
096:
097: //BUILD RULEBASE
098: final RuleBase rb = RuleBaseFactory.newRuleBase();
099: rb.addPackage(pkg);
100:
101: WorkingMemory wm = rb.newStatefulSession();
102:
103: //now create some test data
104: Driver driver = new Driver();
105: Policy policy = new Policy();
106:
107: wm.insert(driver);
108: wm.insert(policy);
109:
110: wm.fireAllRules();
111:
112: System.out.println("BASE PRICE IS: " + policy.getBasePrice());
113: System.out.println("DISCOUNT IS: "
114: + policy.getDiscountPercent());
115:
116: int basePrice = policy.getBasePrice();
117: assertEquals(120, basePrice);
118:
119: }
120:
121: }
|