01: package org.drools.decisiontable.parser;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: import junit.framework.TestCase;
07:
08: import org.drools.rule.GroupElement;
09: import org.drools.rule.LiteralConstraint;
10: import org.drools.rule.Package;
11: import org.drools.rule.Rule;
12:
13: public class DefaultTemplateRuleBaseTest extends TestCase {
14:
15: public void testSimpleTemplate() throws Exception {
16: TemplateContainer tc = new TemplateContainer() {
17:
18: public Column[] getColumns() {
19: return new Column[] { new Column("column1"),
20: new Column("column2") };
21: }
22:
23: public String getHeader() {
24: return null;
25: }
26:
27: public Map getTemplates() {
28: Map templates = new HashMap();
29: RuleTemplate ruleTemplate = new RuleTemplate(
30: "template1");
31: ruleTemplate.addColumn("column1");
32: ruleTemplate.addColumn("column2");
33: templates.put("template1", ruleTemplate);
34: return templates;
35: }
36:
37: };
38: DefaultTemplateRuleBase ruleBase = new DefaultTemplateRuleBase(
39: tc);
40: Package[] packages = ruleBase.newStatefulSession()
41: .getRuleBase().getPackages();
42: assertEquals(1, packages.length);
43: Map globals = packages[0].getGlobals();
44: assertEquals(DefaultGenerator.class, globals.get("generator"));
45: Rule[] rules = packages[0].getRules();
46: assertEquals(1, rules.length);
47: assertEquals("template1", rules[0].getName());
48: GroupElement lhs = rules[0].getLhs();
49: //when
50: // r : Row()
51: // Cell(row == r, column == "column1")
52: // Cell(row == r, column == "column2")
53: assertEquals(3, lhs.getChildren().size());
54: org.drools.rule.Pattern pattern = (org.drools.rule.Pattern) lhs
55: .getChildren().get(1);
56: LiteralConstraint constraint = (LiteralConstraint) pattern
57: .getConstraints().get(1);
58: assertEquals("column1", constraint.getField().getValue());
59: pattern = (org.drools.rule.Pattern) lhs.getChildren().get(2);
60: constraint = (LiteralConstraint) pattern.getConstraints()
61: .get(1);
62: assertEquals("column2", constraint.getField().getValue());
63: }
64: }
|