@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 testForall2() throws Exception { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_Forall2.drl")), ResourceType.DRL); assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); final List<String> list = new ArrayList<String>(); ksession.setGlobal("results", list); final State state = new State("SP"); ksession.insert(state); final Person bob = new Person("Bob"); bob.setStatus(state.getState()); bob.setAlive(true); ksession.insert(bob); ksession.fireAllRules(); assertEquals(0, list.size()); final State qc = new State("QC"); ksession.insert(qc); final Person john = new Person("John"); john.setStatus(qc.getState()); john.setAlive(false); ksession.fireAllRules(); assertEquals(1, list.size()); }