public void testForall() throws Exception { RuleBase ruleBase = loadRuleBase(new InputStreamReader(getClass().getResourceAsStream("test_Forall.drl"))); StatefulSession workingMemory = ruleBase.newStatefulSession(); List list = new ArrayList(); workingMemory.setGlobal("results", list); final State state = new State("SP"); workingMemory.insert(state); final Person bob = new Person("Bob"); bob.setStatus(state.getState()); bob.setLikes("stilton"); workingMemory.insert(bob); ruleBase = serializeRuleBase(ruleBase); workingMemory = serializeWorkingMemory(ruleBase, workingMemory); list = (List) workingMemory.getGlobal("results"); workingMemory.fireAllRules(); assertEquals(0, list.size()); workingMemory.insert(new Cheese(bob.getLikes(), 10)); ruleBase = serializeRuleBase(ruleBase); workingMemory = serializeWorkingMemory(ruleBase, workingMemory); list = (List) workingMemory.getGlobal("results"); workingMemory.fireAllRules(); assertEquals(1, list.size()); }
public void testCollectModify() throws Exception { // read in the source final Reader reader = new InputStreamReader(getClass().getResourceAsStream("test_Collect.drl")); RuleBase ruleBase = loadRuleBase(reader); StatefulSession wm = ruleBase.newStatefulSession(); List results = new ArrayList(); wm.setGlobal("results", results); final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 2), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 16), new Cheese("provolone", 8) }; final Person bob = new Person("Bob", "stilton"); final FactHandle[] cheeseHandles = new FactHandle[cheese.length]; for (int i = 0; i < cheese.length; i++) { cheeseHandles[i] = wm.insert(cheese[i]); } FactHandle bobHandle = wm.insert(bob); ruleBase = serializeRuleBase(ruleBase); wm = serializeWorkingMemory(ruleBase, wm); results = (List) wm.getGlobal("results"); updateHandles(wm, cheeseHandles); bobHandle = updateHandle(wm, (InternalFactHandle) bobHandle); // ---------------- 1st scenario int fireCount = 0; wm.fireAllRules(); Assert.assertEquals(++fireCount, results.size()); Assert.assertEquals(3, ((Collection) results.get(fireCount - 1)).size()); Assert.assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName()); // ---------------- 2nd scenario final int index = 1; cheese[index].setPrice(9); wm.update(cheeseHandles[index], cheese[index]); wm.fireAllRules(); Assert.assertEquals(++fireCount, results.size()); Assert.assertEquals(3, ((Collection) results.get(fireCount - 1)).size()); Assert.assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName()); // ---------------- 3rd scenario bob.setLikes("brie"); wm.update(bobHandle, bob); wm.fireAllRules(); Assert.assertEquals(fireCount, results.size()); // ---------------- 4th scenario wm.retract(cheeseHandles[3]); wm.fireAllRules(); // should not have fired as per constraint Assert.assertEquals(fireCount, results.size()); }