@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 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 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 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 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 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 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 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 @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 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 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()); }