001: package org.drools.reteoo;
002:
003: import org.drools.Otherwise;
004: import org.drools.RuleBase;
005: import org.drools.RuleBaseFactory;
006: import org.drools.WorkingMemory;
007: import org.drools.base.ClassObjectType;
008: import org.drools.base.ShadowProxyFactory;
009: import org.drools.base.TestBean;
010: import org.drools.rule.Pattern;
011: import org.drools.rule.Package;
012: import org.drools.rule.Rule;
013: import org.drools.spi.Consequence;
014: import org.drools.spi.KnowledgeHelper;
015:
016: import junit.framework.TestCase;
017:
018: /**
019: * This tests the "otherwise" feature.
020: * @author Michael Neale
021: */
022: public class OtherwiseTest extends TestCase {
023:
024: public void testOneRuleFiringNoOtherwise() throws Exception {
025: final RuleBase ruleBase = RuleBaseFactory
026: .newRuleBase(RuleBase.RETEOO);
027:
028: final Package pkg = new Package("Miss Manners");
029: final Rule rule1 = getRule("rule1");
030: pkg.addRule(rule1);
031:
032: final Rule ruleOtherwise = getOtherwise("rule2");
033: pkg.addRule(ruleOtherwise);
034:
035: ruleBase.addPackage(pkg);
036:
037: final WorkingMemory workingMemory = ruleBase
038: .newStatefulSession();
039: workingMemory.insert(new TestBean());
040: workingMemory.fireAllRules();
041:
042: assertTrue(((MockConsequence) rule1.getConsequence()).fired);
043: assertFalse(((MockConsequence) ruleOtherwise.getConsequence()).fired);
044:
045: }
046:
047: public void testTwoRulesFiringNoOtherwise() throws Exception {
048: final RuleBase ruleBase = RuleBaseFactory
049: .newRuleBase(RuleBase.RETEOO);
050:
051: final Package pkg = new Package("Miss Manners");
052: final Rule rule1 = getRule("rule1");
053: pkg.addRule(rule1);
054: final Rule rule2 = getRule("rule2");
055: pkg.addRule(rule2);
056:
057: final Rule ruleOtherwise = getOtherwise("ruleOtherwise");
058: pkg.addRule(ruleOtherwise);
059:
060: ruleBase.addPackage(pkg);
061:
062: final WorkingMemory workingMemory = ruleBase
063: .newStatefulSession();
064: workingMemory.insert(new TestBean());
065: workingMemory.fireAllRules();
066:
067: assertFalse(((MockConsequence) ruleOtherwise.getConsequence()).fired);
068: assertTrue(((MockConsequence) rule1.getConsequence()).fired);
069: assertTrue(((MockConsequence) rule2.getConsequence()).fired);
070:
071: }
072:
073: /**
074: * @TODO: this is a future to be implemented in the future
075: * @throws Exception
076: */
077: public void FIXME_testOtherwiseFiringWithOneRule() throws Exception {
078: final RuleBase ruleBase = RuleBaseFactory
079: .newRuleBase(RuleBase.RETEOO);
080:
081: final Package pkg = new Package("Miss Manners");
082: final Rule rule1 = getRule("rule1");
083: pkg.addRule(rule1);
084:
085: final Rule ruleOtherwise = getOtherwise("rule2");
086: pkg.addRule(ruleOtherwise);
087:
088: ruleBase.addPackage(pkg);
089:
090: final WorkingMemory workingMemory = ruleBase
091: .newStatefulSession();
092:
093: workingMemory.fireAllRules();
094:
095: assertFalse(((MockConsequence) rule1.getConsequence()).fired);
096: assertTrue(((MockConsequence) ruleOtherwise.getConsequence()).fired);
097:
098: }
099:
100: /**
101: * @TODO: this is a future to be implemented in the future
102: * @throws Exception
103: */
104: public void FIXME_testOtherwiseFiringMultipleRules()
105: throws Exception {
106: final RuleBase ruleBase = RuleBaseFactory
107: .newRuleBase(RuleBase.RETEOO);
108:
109: final Package pkg = new Package("Miss Manners");
110: final Rule rule1 = getRule("rule1");
111: pkg.addRule(rule1);
112: final Rule rule2 = getRule("rule2");
113: pkg.addRule(rule2);
114:
115: final Rule ruleOtherwise1 = getOtherwise("other1");
116: pkg.addRule(ruleOtherwise1);
117: final Rule ruleOtherwise2 = getOtherwise("other2");
118: pkg.addRule(ruleOtherwise2);
119:
120: ruleBase.addPackage(pkg);
121:
122: final WorkingMemory workingMemory = ruleBase
123: .newStatefulSession();
124:
125: workingMemory.fireAllRules();
126:
127: assertFalse(((MockConsequence) rule1.getConsequence()).fired);
128: assertFalse(((MockConsequence) rule2.getConsequence()).fired);
129: assertTrue(((MockConsequence) ruleOtherwise1.getConsequence()).fired);
130: assertTrue(((MockConsequence) ruleOtherwise2.getConsequence()).fired);
131:
132: }
133:
134: private Rule getOtherwise(final String name) {
135: final Rule rule = new Rule(name);
136: final Pattern pat = new Pattern(0, new ClassObjectType(
137: Otherwise.class));
138: rule.addPattern(pat);
139: rule.setConsequence(new MockConsequence());
140: return rule;
141: }
142:
143: private Rule getRule(final String name) {
144: final Rule rule = new Rule(name);
145:
146: final Pattern pat = new Pattern(0, new ClassObjectType(
147: TestBean.class));
148:
149: rule.addPattern(pat);
150: rule.setConsequence(new MockConsequence());
151:
152: return rule;
153: }
154:
155: static class MockConsequence implements Consequence {
156:
157: public boolean fired = false;
158:
159: public void evaluate(final KnowledgeHelper knowledgeHelper,
160: final WorkingMemory workingMemory) throws Exception {
161: this .fired = true;
162: }
163:
164: }
165:
166: }
|