@Test public void testCollectResultConstraints() throws Exception { // read in the source final Reader reader = new InputStreamReader(getClass().getResourceAsStream("test_CollectResultConstraints.drl")); RuleBase ruleBase = loadRuleBase(reader); StatefulSession wm = ruleBase.newStatefulSession(); List results = new ArrayList(); wm.setGlobal("results", results); wm.insert(new Cheese("stilton", 10)); wm = SerializationHelper.getSerialisedStatefulSession(wm); results = (List) wm.getGlobal("results"); wm.fireAllRules(); assertEquals(1, results.size()); assertEquals(1, ((Collection) results.get(0)).size()); wm.insert(new Cheese("stilton", 7)); wm.insert(new Cheese("stilton", 8)); wm.fireAllRules(); wm = SerializationHelper.getSerialisedStatefulSession(wm); results = (List) wm.getGlobal("results"); assertEquals(1, results.size()); // It's 3 as while the rule does not fire, it does continue to evaluate and update the // collection assertEquals(3, ((Collection) results.get(0)).size()); assertEquals(ArrayList.class.getName(), results.get(0).getClass().getName()); }
@Test public void testCollectWithNestedFromWithParams() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_CollectWithNestedFrom.drl"))); final Package pkg = builder.getPackage(); RuleBase ruleBase = getSinglethreadRuleBase(); ruleBase.addPackage(pkg); ruleBase = SerializationHelper.serializeObject(ruleBase); final WorkingMemory workingMemory = ruleBase.newStatefulSession(); final List results = new ArrayList(); workingMemory.setGlobal("results", results); final Person bob = new Person("bob", "stilton"); Cheesery cheesery = new Cheesery(); cheesery.addCheese(new Cheese("stilton", 10)); cheesery.addCheese(new Cheese("brie", 20)); cheesery.addCheese(new Cheese("muzzarela", 8)); cheesery.addCheese(new Cheese("stilton", 5)); cheesery.addCheese(new Cheese("provolone", 1)); workingMemory.insert(bob); workingMemory.insert(cheesery); workingMemory.fireAllRules(); assertEquals(1, results.size()); List cheeses = (List) results.get(0); assertEquals(2, cheeses.size()); assertEquals(bob.getLikes(), ((Cheese) cheeses.get(0)).getType()); assertEquals(bob.getLikes(), ((Cheese) cheeses.get(1)).getType()); }
@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()); }
@Test public void testCollectResultBetaConstraint() throws Exception { // read in the source final Reader reader = new InputStreamReader( getClass().getResourceAsStream("test_CollectResultsBetaConstraint.drl")); RuleBase ruleBase = loadRuleBase(reader); StatefulSession wm = ruleBase.newStatefulSession(); List results = new ArrayList(); wm.setGlobal("results", results); wm.insert(new Double(10)); wm.insert(new Integer(2)); // ruleBase = SerializationHelper.serializeObject( ruleBase ); // wm = serializeWorkingMemory( ruleBase, // wm ); // results = (List) wm.getGlobal( "results" ); wm.fireAllRules(); assertEquals(0, results.size()); wm.insert(new Double(15)); wm.fireAllRules(); assertEquals(2, results.size()); assertEquals("collect", results.get(0)); assertEquals("accumulate", results.get(1)); }
@Test public void testCollectFromMVELAfterOr() throws Exception { // read in the source final Reader reader = new InputStreamReader(getClass().getResourceAsStream("test_CollectFromMVELAfterOr.drl")); RuleBase ruleBase = loadRuleBase(reader); StatefulSession wm = ruleBase.newStatefulSession(); List results = new ArrayList(); wm.setGlobal("results", results); Person jill = new Person("jill"); Person bob = new Person("bob"); List addresses = new ArrayList(); addresses.add(new Address("a")); addresses.add(new Address("b")); addresses.add(new Address("c")); bob.setAddresses(addresses); wm.insert(jill); wm.insert(bob); wm = SerializationHelper.getSerialisedStatefulSession(wm); results = (List) wm.getGlobal("results"); wm.fireAllRules(); assertEquals(2, results.size()); assertEquals(3, ((Collection) results.get(0)).size()); }
@Test public void testCollect() 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); wm.insert(new Cheese("stilton", 10)); wm.insert(new Cheese("stilton", 7)); wm.insert(new Cheese("stilton", 8)); wm.insert(new Cheese("brie", 5)); wm.insert(new Cheese("provolone", 150)); wm = SerializationHelper.getSerialisedStatefulSession(wm); results = (List) wm.getGlobal("results"); wm.insert(new Cheese("provolone", 20)); wm.insert(new Person("Bob", "stilton")); wm.insert(new Person("Mark", "provolone")); wm = SerializationHelper.getSerialisedStatefulSession(wm); results = (List) wm.getGlobal("results"); wm.fireAllRules(); wm = SerializationHelper.getSerialisedStatefulSession(wm); results = (List) wm.getGlobal("results"); assertEquals(1, results.size()); assertEquals(3, ((Collection) results.get(0)).size()); assertEquals(ArrayList.class.getName(), results.get(0).getClass().getName()); }
@Test public void testForallSinglePatternWithExists() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream("test_ForallSinglePatternWithExists.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); workingMemory.insert(new Cheese("stilton", 10)); workingMemory.insert(new Cheese("brie", 10)); workingMemory.insert(new Cheese("brie", 10)); workingMemory.insert(new Order(1, "bob")); workingMemory.insert(new Person("bob", "stilton", 10)); workingMemory.insert(new Person("mark", "stilton")); workingMemory.fireAllRules(); assertEquals(1, list.size()); }
@Test public void testNestedCorelatedRulesWithForall() throws Exception { PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader( FirstOrderLogicTest.class.getResourceAsStream( "test_NestedCorrelatedRulesWithForall.drl"))); RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(builder.getPackage()); ruleBase = SerializationHelper.serializeObject(ruleBase); StatefulSession session = ruleBase.newStatefulSession(); List list1 = new ArrayList(); List list2 = new ArrayList(); List list3 = new ArrayList(); List list4 = new ArrayList(); session.setGlobal("list1", list1); session.setGlobal("list2", list2); session.setGlobal("list3", list3); session.setGlobal("list4", list4); SpecialString first42 = new SpecialString("42"); SpecialString second42 = new SpecialString("42"); SpecialString world = new SpecialString("World"); // System.out.println( "Inserting ..." ); session.insert(world); session.insert(first42); session.insert(second42); // System.out.println( "Done." ); // System.out.println( "Firing rules ..." ); // check all lists are empty assertTrue(list1.isEmpty()); assertTrue(list2.isEmpty()); assertTrue(list3.isEmpty()); assertTrue(list4.isEmpty()); session.fireAllRules(); // System.out.println( "Done." ); // check first list is populated correctly assertEquals(0, list1.size()); // check second list is populated correctly assertEquals(0, list2.size()); // check third list is populated correctly assertEquals(1, list3.size()); // check fourth list is populated correctly assertEquals(0, list4.size()); }
@Test public void testFromInsideNotAndExists() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_FromInsideNotAndExists.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 Cheese cheddar = new Cheese("cheddar", 7); final Cheese provolone = new Cheese("provolone", 5); final Cheesery cheesery = new Cheesery(); cheesery.addCheese(cheddar); cheesery.addCheese(provolone); FactHandle handle = workingMemory.insert(cheesery); workingMemory.fireAllRules(); assertEquals(0, list.size()); cheesery.addCheese(new Cheese("stilton", 10)); cheesery.removeCheese(cheddar); workingMemory.update(handle, cheesery); workingMemory.fireAllRules(); assertEquals(2, list.size()); }
@Test public void testOr() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_OrNesting.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 Cheese cheddar = new Cheese("cheddar", 7); final Cheese provolone = new Cheese("provolone", 5); final Cheese brie = new Cheese("brie", 15); final Person mark = new Person("mark", "stilton"); FactHandle ch = workingMemory.insert(cheddar); FactHandle ph = workingMemory.insert(provolone); FactHandle bh = workingMemory.insert(brie); FactHandle markh = workingMemory.insert(mark); workingMemory.fireAllRules(); assertEquals(1, list.size()); }
@Test public void testMVELCollect() throws Exception { // read in the source final Reader reader = new InputStreamReader(getClass().getResourceAsStream("test_MVELCollect.drl")); final RuleBase ruleBase = loadRuleBase(reader); final WorkingMemory wm = ruleBase.newStatefulSession(); final List results = new ArrayList(); wm.setGlobal("results", results); wm.insert(new Cheese("stilton", 10)); wm.insert(new Cheese("stilton", 7)); wm.insert(new Cheese("stilton", 8)); wm.insert(new Cheese("brie", 5)); wm.insert(new Cheese("provolone", 150)); wm.insert(new Cheese("provolone", 20)); wm.insert(new Person("Bob", "stilton")); wm.insert(new Person("Mark", "provolone")); wm.fireAllRules(); assertEquals(1, results.size()); assertEquals(6, ((List) results.get(0)).size()); }
@Test public void testCollectNodeSharing() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_collectNodeSharing.drl"))); final Package pkg = builder.getPackage(); RuleBase ruleBase = getSinglethreadRuleBase(); ruleBase.addPackage(pkg); ruleBase = SerializationHelper.serializeObject(ruleBase); StatefulSession workingMemory = ruleBase.newStatefulSession(); List results = new ArrayList(); workingMemory.setGlobal("results", results); workingMemory = SerializationHelper.getSerialisedStatefulSession(workingMemory); results = (List) workingMemory.getGlobal("results"); workingMemory.insert(new Cheese("stilton", 10)); workingMemory = SerializationHelper.getSerialisedStatefulSession(workingMemory); results = (List) workingMemory.getGlobal("results"); workingMemory.insert(new Cheese("brie", 15)); workingMemory.fireAllRules(); workingMemory = SerializationHelper.getSerialisedStatefulSession(workingMemory); results = (List) workingMemory.getGlobal("results"); assertEquals(1, results.size()); assertEquals(2, ((List) results.get(0)).size()); }
@Test public void testNot() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("not_rule_test.drl"))); if (builder.hasErrors()) { fail(builder.getErrors().toString()); } final Package pkg = builder.getPackage(); final RuleBase ruleBase = getSinglethreadRuleBase(); ruleBase.addPackage(pkg); final WorkingMemory workingMemory = ruleBase.newStatefulSession(); final List list = new ArrayList(); workingMemory.setGlobal("list", list); final Cheese stilton = new Cheese("stilton", 5); final FactHandle stiltonHandle = workingMemory.insert(stilton); final Cheese cheddar = new Cheese("cheddar", 7); final FactHandle cheddarHandle = workingMemory.insert(cheddar); workingMemory.fireAllRules(); assertEquals(0, list.size()); workingMemory.retract(stiltonHandle); workingMemory.fireAllRules(); assertEquals(4, list.size()); assertTrue(list.contains(new Integer(5))); assertTrue(list.contains(new Integer(6))); assertTrue(list.contains(new Integer(7))); assertTrue(list.contains(new Integer(8))); }
@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 testExists() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("exists_rule_test.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("list", list); final Cheese cheddar = new Cheese("cheddar", 7); final FactHandle cheddarHandle = workingMemory.insert(cheddar); workingMemory.fireAllRules(); assertEquals(0, list.size()); final Cheese stilton = new Cheese("stilton", 5); final FactHandle stiltonHandle = workingMemory.insert(stilton); workingMemory.fireAllRules(); assertEquals(1, list.size()); final Cheese brie = new Cheese("brie", 5); final FactHandle brieHandle = workingMemory.insert(brie); workingMemory.fireAllRules(); assertEquals(1, list.size()); }
@Test public void testNotWithBindings() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("not_with_bindings_rule_test.drl"))); final Package pkg = builder.getPackage(); final Rule rule = pkg.getRules()[0]; assertTrue(rule.isValid()); assertEquals(0, builder.getErrors().getErrors().length); RuleBase ruleBase = getSinglethreadRuleBase(); ruleBase.addPackage(pkg); ruleBase = SerializationHelper.serializeObject(ruleBase); final WorkingMemory workingMemory = ruleBase.newStatefulSession(); final List list = new ArrayList(); workingMemory.setGlobal("list", list); final Cheese stilton = new Cheese("stilton", 5); final FactHandle stiltonHandle = workingMemory.insert(stilton); final Cheese cheddar = new Cheese("cheddar", 7); final FactHandle cheddarHandle = workingMemory.insert(cheddar); final PersonInterface paul = new Person("paul", "stilton", 12); workingMemory.insert(paul); workingMemory.fireAllRules(); assertEquals(0, list.size()); workingMemory.retract(stiltonHandle); workingMemory.fireAllRules(); assertEquals(1, list.size()); }
@Test @Ignore("This test requires us to fix the propagation order") public void testForallSinglePattern() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_ForallSinglePattern.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); int fired = 0; // no cheeses, so should fire workingMemory.fireAllRules(); assertEquals(++fired, list.size()); // only stilton, so should not fire again FactHandle stilton1 = workingMemory.insert(new Cheese("stilton", 10)); workingMemory.fireAllRules(); assertEquals(fired, list.size()); // only stilton, so should not fire again FactHandle stilton2 = workingMemory.insert(new Cheese("stilton", 11)); workingMemory.fireAllRules(); assertEquals(fired, list.size()); // still only stilton, so should not fire workingMemory.retract(stilton1); workingMemory.fireAllRules(); assertEquals( ++fired, // we need to fix forall to not fire in this situation list.size()); // there is a brie, so should not fire FactHandle brie = workingMemory.insert(new Cheese("brie", 10)); workingMemory.fireAllRules(); assertEquals(fired, list.size()); // no brie anymore, so should fire workingMemory.retract(brie); workingMemory.fireAllRules(); assertEquals(++fired, list.size()); // no more cheese, but since it already fired, should not fire again workingMemory.retract(stilton2); workingMemory.fireAllRules(); assertEquals(fired, list.size()); }
@Test public void testCollectModifyAlphaRestriction() throws Exception { // read in the source final Reader reader = new InputStreamReader(getClass().getResourceAsStream("test_CollectAlphaRestriction.drl")); RuleBase ruleBase = loadRuleBase(reader); final WorkingMemory wm = ruleBase.newStatefulSession(); final 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 FactHandle[] cheeseHandles = new FactHandle[cheese.length]; for (int i = 0; i < cheese.length; i++) { cheeseHandles[i] = wm.insert(cheese[i]); } // ---------------- 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].setType("brie"); wm.update(cheeseHandles[index], cheese[index]); wm.fireAllRules(); assertEquals(++fireCount, results.size()); assertEquals(2, ((Collection) results.get(fireCount - 1)).size()); assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName()); // ---------------- 3rd scenario wm.retract(cheeseHandles[2]); wm.fireAllRules(); assertEquals(++fireCount, results.size()); assertEquals(1, ((Collection) results.get(fireCount - 1)).size()); assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName()); }
@Test public void testRemoveIdentitiesSubNetwork() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream("test_removeIdentitiesSubNetwork.drl"))); final Package pkg = builder.getPackage(); final RuleBaseConfiguration config = new RuleBaseConfiguration(); config.setRemoveIdentities(true); RuleBase ruleBase = getRuleBase(config); ruleBase.addPackage(pkg); ruleBase = SerializationHelper.serializeObject(ruleBase); final WorkingMemory workingMemory = ruleBase.newStatefulSession(); final List list = new ArrayList(); workingMemory.setGlobal("results", list); final Person bob = new Person("bob", "stilton"); workingMemory.insert(bob); final Person mark = new Person("mark", "stilton"); workingMemory.insert(mark); final Cheese stilton1 = new Cheese("stilton", 6); final FactHandle stilton1Handle = workingMemory.insert(stilton1); final Cheese stilton2 = new Cheese("stilton", 7); final FactHandle stilton2Handle = workingMemory.insert(stilton2); workingMemory.fireAllRules(); assertEquals(0, list.size()); workingMemory.retract(stilton1Handle); workingMemory.fireAllRules(); assertEquals(1, list.size()); assertEquals(mark, list.get(0)); workingMemory.retract(stilton2Handle); workingMemory.fireAllRules(); assertEquals(2, list.size()); assertEquals(bob, list.get(1)); }
@Test public void testCollectWithContainsOperators() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_CollectContainsOperator.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 Order order1 = new Order(1, "bob"); final OrderItem item11 = new OrderItem(order1, 1); final OrderItem item12 = new OrderItem(order1, 2); final Order order2 = new Order(2, "mark"); final OrderItem item21 = new OrderItem(order2, 1); final OrderItem item22 = new OrderItem(order2, 2); workingMemory.insert(order1); workingMemory.insert(item11); workingMemory.insert(item12); workingMemory.insert(order2); workingMemory.insert(item21); workingMemory.insert(item22); workingMemory.fireAllRules(); int index = 0; assertEquals(8, list.size()); assertSame(order1, list.get(index++)); assertSame(item11, list.get(index++)); assertSame(order2, list.get(index++)); assertSame(item21, list.get(index++)); assertSame(order1, list.get(index++)); assertSame(item11, list.get(index++)); assertSame(order2, list.get(index++)); assertSame(item21, list.get(index++)); }
@Test public void testExistsWithBinding() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_ExistsWithBindings.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 Cheese c = new Cheese("stilton", 10); final Person p = new Person("Mark", "stilton"); workingMemory.insert(c); workingMemory.insert(p); workingMemory.fireAllRules(); assertTrue(list.contains(c.getType())); assertEquals(1, list.size()); }
@Test public void testExists2() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader(getClass().getResourceAsStream("test_exists.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("list", list); final Cheese cheddar = new Cheese("cheddar", 7); final Cheese provolone = new Cheese("provolone", 5); final Person edson = new Person("Edson", "cheddar"); final Person bob = new Person("Bob", "muzzarela"); workingMemory.insert(cheddar); workingMemory.fireAllRules(); assertEquals(0, list.size()); workingMemory.insert(provolone); workingMemory.fireAllRules(); assertEquals(0, list.size()); workingMemory.insert(edson); workingMemory.fireAllRules(); assertEquals(1, list.size()); workingMemory.insert(bob); workingMemory.fireAllRules(); assertEquals(1, list.size()); }
@Test public void test3() throws Exception { String process1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"com.sample.ruleflow\" package-name=\"com.sample\" >\n" + "\n" + " <header>\n" + " <imports>\n" + " <import name=\"org.jbpm.integrationtests.test.Person\" />\n" + " </imports>\n" + " <swimlanes>\n" + " <swimlane name=\"swimlane\" />\n" + " </swimlanes>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <end id=\"4\" name=\"End\" />\n" + " <split id=\"5\" name=\"AND\" type=\"1\" />\n" + " <subProcess id=\"6\" name=\"SubProcess\" processId=\"com.sample.subflow\" />\n" + " <actionNode id=\"7\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Executing action 1\");</action>\n" + " </actionNode>\n" + " <join id=\"8\" name=\"AND\" type=\"1\" />\n" + " <actionNode id=\"9\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Executing action 2\");</action>\n" + " </actionNode>\n" + " <ruleSet id=\"10\" name=\"RuleSet\" ruleFlowGroup=\"flowgroup\" />\n" + " <milestone id=\"11\" name=\"Event Wait\" >\n" + " <constraint type=\"rule\" dialect=\"mvel\" >Person( )</constraint>\n" + " </milestone>\n" + " <workItem id=\"12\" name=\"Log\" >\n" + " <work name=\"Log\" >\n" + " <parameter name=\"Message\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>This is a log message</value>\n" + " </parameter>\n" + " </work>\n" + " </workItem>\n" + " <composite id=\"13\" name=\"CompositeNode\" >\n" + " <variables>\n" + " <variable name=\"x\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>x-value</value>\n" + " </variable>\n" + " </variables>\n" + " <nodes>\n" + " <humanTask id=\"1\" name=\"Human Task\" swimlane=\"swimlane\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>John Doe</value>\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something !</value>\n" + " </parameter>\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " </work>\n" + " </humanTask>\n" + " <humanTask id=\"2\" name=\"Human Task\" swimlane=\"swimlane\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something else !</value>\n" + " </parameter>\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " </work>\n" + " <mapping type=\"in\" from=\"x\" to=\"Priority\" />\n" + " </humanTask>\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " </connections>\n" + " <in-ports>\n" + " <in-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeInType=\"DROOLS_DEFAULT\" />\n" + " </in-ports>\n" + " <out-ports>\n" + " <out-port type=\"DROOLS_DEFAULT\" nodeId=\"2\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + " </out-ports>\n" + " </composite>\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"9\" to=\"4\" />\n" + " <connection from=\"1\" to=\"5\" />\n" + " <connection from=\"5\" to=\"6\" />\n" + " <connection from=\"5\" to=\"7\" />\n" + " <connection from=\"7\" to=\"8\" />\n" + " <connection from=\"6\" to=\"8\" />\n" + " <connection from=\"10\" to=\"8\" />\n" + " <connection from=\"11\" to=\"8\" />\n" + " <connection from=\"12\" to=\"8\" />\n" + " <connection from=\"13\" to=\"8\" />\n" + " <connection from=\"8\" to=\"9\" />\n" + " <connection from=\"5\" to=\"10\" />\n" + " <connection from=\"5\" to=\"11\" />\n" + " <connection from=\"5\" to=\"12\" />\n" + " <connection from=\"5\" to=\"13\" />\n" + " </connections>\n" + "\n" + "</process>\n"; final PackageBuilder builder = new PackageBuilder(); builder.addProcessFromXml(new StringReader(process1)); String process2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"flow\" id=\"com.sample.subflow\" package-name=\"com.sample\" >\n" + "\n" + " <header>\n" + " <imports>\n" + " <import name=\"org.jbpm.integrationtests.test.Person\" />\n" + " </imports>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <milestone id=\"2\" name=\"Event Wait\" >\n" + " <constraint type=\"rule\" dialect=\"mvel\" >Person( )</constraint>\n" + " </milestone>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>\n"; builder.addProcessFromXml(new StringReader(process2)); String rule = "package com.sample\n" + "import org.jbpm.integrationtests.test.Person;\n" + "rule \"Hello\" ruleflow-group \"flowgroup\"\n" + " when\n" + " then\n" + " System.out.println( \"Hello\" );\n" + "end"; builder.addPackageFromDrl(new StringReader(rule)); final Package pkg = builder.getPackage(); final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(pkg); StatefulSession session = ruleBase.newStatefulSession(); TestWorkItemHandler handler1 = new TestWorkItemHandler(); session.getWorkItemManager().registerWorkItemHandler("Log", handler1); TestWorkItemHandler handler2 = new TestWorkItemHandler(); session.getWorkItemManager().registerWorkItemHandler("Human Task", handler2); session.startProcess("com.sample.ruleflow"); assertEquals(2, session.getProcessInstances().size()); assertTrue(handler1.getWorkItem() != null); long workItemId = handler2.getWorkItem().getId(); assertTrue(workItemId != -1); session = getSerialisedStatefulSession(session); session.getWorkItemManager().registerWorkItemHandler("Human Task", handler2); assertEquals(2, session.getProcessInstances().size()); handler2.reset(); session.getWorkItemManager().completeWorkItem(workItemId, null); assertTrue(handler2.getWorkItem() != null); assertEquals("John Doe", handler2.getWorkItem().getParameter("ActorId")); assertEquals("x-value", handler2.getWorkItem().getParameter("Priority")); session.getWorkItemManager().completeWorkItem(handler1.getWorkItem().getId(), null); session.getWorkItemManager().completeWorkItem(handler2.getWorkItem().getId(), null); session.insert(new Person()); session.fireAllRules(); assertEquals(0, session.getProcessInstances().size()); }
@Test public void test5() throws Exception { String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"com.sample.ruleflow\" package-name=\"com.sample\" >\n" + "\n" + " <header>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <timerNode id=\"4\" name=\"Timer\" delay=\"200\" />\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"4\" />\n" + " <connection from=\"4\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>\n"; final PackageBuilder builder = new PackageBuilder(); builder.addProcessFromXml(new StringReader(process)); final Package pkg = builder.getPackage(); final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(pkg); final StatefulSession session = ruleBase.newStatefulSession(); new Thread( new Runnable() { public void run() { session.fireUntilHalt(); } }) .start(); session.startProcess("com.sample.ruleflow", null); assertEquals(1, session.getProcessInstances().size()); session.halt(); final StatefulSession session2 = getSerialisedStatefulSession(session); new Thread( new Runnable() { public void run() { session2.fireUntilHalt(); } }) .start(); Thread.sleep(400); assertEquals(0, session2.getProcessInstances().size()); session2.halt(); }
@Test public void test6() throws Exception { String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"com.sample.ruleflow\" package-name=\"com.sample\" >\n" + "\n" + " <header>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <timerNode id=\"4\" name=\"Timer\" delay=\"200\" />\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"4\" />\n" + " <connection from=\"4\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>\n"; final PackageBuilder builder = new PackageBuilder(); builder.addProcessFromXml(new StringReader(process)); final Package pkg = builder.getPackage(); final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(pkg); final StatefulSession session = ruleBase.newStatefulSession(); new Thread( new Runnable() { public void run() { session.fireUntilHalt(); } }) .start(); session.startProcess("com.sample.ruleflow", null); assertEquals(1, session.getProcessInstances().size()); StatefulKnowledgeSession ksession = new StatefulKnowledgeSessionImpl((ReteooWorkingMemory) session); Marshaller marshaller = MarshallerFactory.newMarshaller(ksession.getKieBase()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshall(baos, ksession); byte[] b1 = baos.toByteArray(); session.halt(); session.dispose(); Thread.sleep(400); ByteArrayInputStream bais = new ByteArrayInputStream(b1); final StatefulSession session2 = (StatefulSession) ((StatefulKnowledgeSessionImpl) marshaller.unmarshall(bais)).session; new Thread( new Runnable() { public void run() { session2.fireUntilHalt(); } }) .start(); Thread.sleep(100); assertEquals(0, session2.getProcessInstances().size()); session2.halt(); }
@Test public void testVariablePersistenceMarshallingStrategies() throws Exception { String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + " <header>\n" + " <variables>\n" + " <variable name=\"myVariable\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>OldValue</value>\n" + " </variable>\n" + " <variable name=\"myPerson\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.integrationtests.test.Person\"/>\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <workItem id=\"2\" name=\"Email\" >\n" + " <work name=\"Report\" >\n" + " <parameter name=\"Subject\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Mail</value>\n" + " </parameter>\n" + " <parameter name=\"Subject\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Mail</value>\n" + " </parameter>\n" + " </work>\n" + " </workItem>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\"/>\n" + " <connection from=\"2\" to=\"3\"/>\n" + " </connections>\n" + "</process>"; final PackageBuilder builder = new PackageBuilder(); builder.addProcessFromXml(new StringReader(process)); final Package pkg = builder.getPackage(); final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(pkg); StatefulSession session = ruleBase.newStatefulSession(); TestWorkItemHandler handler = new TestWorkItemHandler(); session.getWorkItemManager().registerWorkItemHandler("Report", handler); Map<String, Object> variables = new HashMap<String, Object>(); variables.put("myVariable", "ThisIsMyValue"); Person myPerson = new Person("Nikola Tesla", 156); variables.put("myPerson", myPerson); session.startProcess("org.test.ruleflow", variables); assertEquals(1, session.getProcessInstances().size()); assertTrue(handler.getWorkItem() != null); session = getSerialisedStatefulSession(session); assertEquals(1, session.getProcessInstances().size()); VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstance) session.getProcessInstances().iterator().next()) .getContextInstance(VariableScope.VARIABLE_SCOPE); assertEquals("ThisIsMyValue", variableScopeInstance.getVariable("myVariable")); assertEquals(myPerson, variableScopeInstance.getVariable("myPerson")); session.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null); assertEquals(0, session.getProcessInstances().size()); }
@Test public void test4() throws Exception { String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"com.sample.ruleflow\" package-name=\"com.sample\" >\n" + "\n" + " <header>\n" + " <variables>\n" + " <variable name=\"list\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.ObjectDataType\" className=\"java.util.List\" />\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <forEach id=\"4\" name=\"ForEach\" variableName=\"item\" collectionExpression=\"list\" >\n" + " <nodes>\n" + " <humanTask id=\"1\" name=\"Human Task\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something: #{item}</value>\n" + " </parameter>\n" + " </work>\n" + " </humanTask>\n" + " <humanTask id=\"2\" name=\"Human Task Again\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something else: #{item}</value>\n" + " </parameter>\n" + " </work>\n" + " </humanTask>\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " </connections>\n" + " <in-ports>\n" + " <in-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeInType=\"DROOLS_DEFAULT\" />\n" + " </in-ports>\n" + " <out-ports>\n" + " <out-port type=\"DROOLS_DEFAULT\" nodeId=\"2\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + " </out-ports>\n" + " </forEach>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"4\" />\n" + " <connection from=\"4\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>\n"; final PackageBuilder builder = new PackageBuilder(); builder.addProcessFromXml(new StringReader(process)); final Package pkg = builder.getPackage(); final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(pkg); StatefulSession session = ruleBase.newStatefulSession(); TestListWorkItemHandler handler = new TestListWorkItemHandler(); session.getWorkItemManager().registerWorkItemHandler("Human Task", handler); List<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("list", list); session.startProcess("com.sample.ruleflow", parameters); assertEquals(1, session.getProcessInstances().size()); assertEquals(3, handler.getWorkItems().size()); // session = getSerialisedStatefulSession( session ); // session.getWorkItemManager().registerWorkItemHandler("Human Task", handler); List<WorkItem> workItems = new ArrayList<WorkItem>(handler.getWorkItems()); handler.reset(); for (WorkItem workItem : workItems) { session.getWorkItemManager().completeWorkItem(workItem.getId(), null); } assertEquals(1, session.getProcessInstances().size()); assertEquals(3, handler.getWorkItems().size()); session = getSerialisedStatefulSession(session); for (WorkItem workItem : handler.getWorkItems()) { session.getWorkItemManager().completeWorkItem(workItem.getId(), null); } assertEquals(0, session.getProcessInstances().size()); }