001: package org.drools.reteoo;
002:
003: import java.util.HashMap;
004: import java.util.Iterator;
005: import java.util.Map;
006:
007: import junit.framework.TestCase;
008:
009: import org.drools.Cheese;
010: import org.drools.FactHandle;
011: import org.drools.Person;
012: import org.drools.RuleBase;
013: import org.drools.RuleBaseFactory;
014: import org.drools.StatefulSession;
015: import org.drools.base.MapGlobalResolver;
016: import org.drools.common.EqualityKey;
017: import org.drools.common.InternalWorkingMemory;
018: import org.drools.common.TruthMaintenanceSystem;
019: import org.drools.examples.manners.Guest;
020: import org.drools.spi.GlobalResolver;
021:
022: public class ReteooWorkingMemoryTest extends TestCase {
023: /*
024: * @see JBRULES-356
025: */
026: public void testBasicWorkingMemoryActions() {
027: final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) RuleBaseFactory
028: .newRuleBase().newStatefulSession();
029: final TruthMaintenanceSystem tms = workingMemory
030: .getTruthMaintenanceSystem();
031: final String string = "test";
032: FactHandle fd = workingMemory.insert(string);
033:
034: assertEquals(1, tms.getAssertMap().size());
035: EqualityKey key = tms.get(string);
036: assertSame(fd, key.getFactHandle());
037: assertNull(key.getOtherFactHandle());
038:
039: workingMemory.update(fd, string);
040:
041: assertEquals(1, tms.getAssertMap().size());
042: key = tms.get(string);
043: assertSame(fd, key.getFactHandle());
044: assertNull(key.getOtherFactHandle());
045:
046: workingMemory.retract(fd);
047:
048: assertEquals(0, tms.getAssertMap().size());
049: key = tms.get(string);
050: assertNull(key);
051:
052: fd = workingMemory.insert(string);
053:
054: assertEquals(1, tms.getAssertMap().size());
055:
056: assertEquals(1, tms.getAssertMap().size());
057: key = tms.get(string);
058: assertSame(fd, key.getFactHandle());
059: assertNull(key.getOtherFactHandle());
060: }
061:
062: public void testId() {
063: final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
064: .newRuleBase();
065: InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase
066: .newStatefulSession();
067: assertEquals(0, workingMemory.getId());
068: workingMemory = (InternalWorkingMemory) ruleBase
069: .newStatefulSession();
070: assertEquals(1, workingMemory.getId());
071: }
072:
073: public void testGlobalResolver() {
074: final Map map = new HashMap();
075: map.put("global1", "value1");
076: map.put("global2", "value2");
077: final GlobalResolver resolver = new MapGlobalResolver(map);
078: final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
079: .newRuleBase();
080: final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase
081: .newStatefulSession();
082: workingMemory.setGlobalResolver(resolver);
083: assertEquals("value1", workingMemory.getGlobal("global1"));
084: assertEquals("value2", workingMemory.getGlobal("global2"));
085: }
086:
087: public void testObjectIterator() {
088: final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
089: final StatefulSession session = ruleBase.newStatefulSession();
090:
091: session.insert(new Person("bob", 35));
092: session.insert(new Cheese("stilton", 35));
093: session.insert(new Cheese("brie", 35));
094: session.insert(new Person("steve", 55));
095: session.insert(new Person("tom", 100));
096:
097: int i = 0;
098: for (Iterator it = session.iterateFactHandles(); it.hasNext();) {
099: Object object = it.next();
100: if (i++ > 5) {
101: fail("should not iterate for than 3 times");
102: }
103: }
104:
105: i = 0;
106: for (Iterator it = session.iterateObjects(); it.hasNext();) {
107: Object object = it.next();
108: if (i++ > 5) {
109: fail("should not iterate for than 3 times");
110: }
111: }
112: }
113: }
|