@Test public void testForall() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_Forall.drl"))); final Package pkg = builder.getPackage(); RuleBase ruleBase = getSinglethreadRuleBase(); ruleBase.addPackage(pkg); ruleBase = SerializationHelper.serializeObject(ruleBase); final WorkingMemory workingMemory = ruleBase.newStatefulSession(); final 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); workingMemory.fireAllRules(); assertEquals(0, list.size()); workingMemory.insert(new Cheese(bob.getLikes(), 10)); workingMemory.fireAllRules(); assertEquals(1, list.size()); }
@Test 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]); } final FactHandle bobHandle = wm.insert(bob); // ---------------- 1st scenario int fireCount = 0; wm.fireAllRules(); assertEquals(++fireCount, results.size()); assertEquals(3, ((Collection) results.get(fireCount - 1)).size()); 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(); assertEquals(++fireCount, results.size()); assertEquals(3, ((Collection) results.get(fireCount - 1)).size()); assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName()); // ---------------- 3rd scenario bob.setLikes("brie"); wm.update(bobHandle, bob); wm.fireAllRules(); assertEquals(fireCount, results.size()); // ---------------- 4th scenario wm.retract(cheeseHandles[3]); wm.fireAllRules(); // should not have fired as per constraint assertEquals(fireCount, results.size()); }