001: package org.drools.reteoo;
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.util.ArrayList;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: import org.drools.RuleBase;
025: import org.drools.RuleBaseFactory;
026: import org.drools.WorkingMemory;
027: import org.drools.base.ClassObjectType;
028: import org.drools.rule.Pattern;
029: import org.drools.rule.Declaration;
030: import org.drools.rule.Package;
031: import org.drools.rule.Rule;
032: import org.drools.spi.Consequence;
033: import org.drools.spi.KnowledgeHelper;
034: import org.drools.spi.ObjectType;
035:
036: public class CrossProductTest extends TestCase {
037: private Package pkg;
038: private WorkingMemory workingMemory;
039: private List values;
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043: final ObjectType list1ObjectType = new ClassObjectType(
044: String.class);
045: final ObjectType list2ObjectType = new ClassObjectType(
046: String.class);
047:
048: final Rule rule = new Rule("rule-1");
049:
050: final Pattern list1Pattern = new Pattern(0, list1ObjectType,
051: "s1");
052: final Pattern list2Pattern = new Pattern(1, list2ObjectType,
053: "s2");
054:
055: rule.addPattern(list1Pattern);
056: rule.addPattern(list2Pattern);
057:
058: final Declaration s1Declaration = rule.getDeclaration("s1");
059: final Declaration s2Declaration = rule.getDeclaration("s2");
060:
061: this .values = new ArrayList();
062:
063: rule.setConsequence(new Consequence() {
064:
065: /**
066: *
067: */
068: private static final long serialVersionUID = 400L;
069:
070: public void evaluate(final KnowledgeHelper knowledgeHelper,
071: final WorkingMemory workingMemory) throws Exception {
072: final String s1 = (String) knowledgeHelper
073: .get(s1Declaration);
074: final String s2 = (String) knowledgeHelper
075: .get(s2Declaration);
076: CrossProductTest.this .values
077: .add(new String[] { s1, s2 });
078: }
079:
080: });
081:
082: this .pkg = new Package("org.drools");
083: this .pkg.addRule(rule);
084: }
085:
086: public void testNotRemoveIdentities() throws Exception {
087: // Default is remove identity FALSE
088: final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
089: ruleBase.addPackage(this .pkg);
090:
091: this .workingMemory = ruleBase.newStatefulSession();
092: this .workingMemory.insert("F1");
093: this .workingMemory.insert("F2");
094: this .workingMemory.insert("F3");
095: this .workingMemory.insert("F4");
096:
097: this .workingMemory.fireAllRules();
098:
099: // A full cross product is 16, this is just 12
100: assertEquals(16, this .values.size());
101: }
102:
103: public void testRemoveIdentities() throws Exception {
104: System.setProperty("drools.removeIdentities", "true");
105: final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
106: ruleBase.addPackage(this .pkg);
107:
108: this .workingMemory = ruleBase.newStatefulSession();
109: this .workingMemory.insert("F1");
110: this .workingMemory.insert("F2");
111: this .workingMemory.insert("F3");
112: this .workingMemory.insert("F4");
113:
114: this .workingMemory.fireAllRules();
115:
116: // A full cross product is 16, this is just 12
117: assertEquals(12, this.values.size());
118: }
119:
120: }
|