use of org.drools.mvel.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathReactiveTest method testReactiveSet.
@Test
public void testReactiveSet() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $disease: /wife/children[age > 10]/diseases )\n" + "then\n" + " list.add( $disease.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 10);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addDisease(new Disease("flu"));
charlie.addDisease(new Disease("asthma"));
debbie.addDisease(new Disease("diabetes"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("flu", "asthma");
list.clear();
charlie.addDisease(new Disease("epilepsy"));
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("epilepsy");
}
use of org.drools.mvel.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathReactiveTest method testReactiveDeleteOnLia.
@Test
public void testReactiveDeleteOnLia() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final EntryPointNode epn = ((InternalKnowledgeBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
final ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Man.class));
final LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
final ReactiveFromNode from1 = (ReactiveFromNode) lian.getSinkPropagator().getSinks()[0];
final ReactiveFromNode from2 = (ReactiveFromNode) from1.getSinkPropagator().getSinks()[0];
final ReactiveFromNode from3 = (ReactiveFromNode) from2.getSinkPropagator().getSinks()[0];
final BetaMemory betaMemory = ((InternalWorkingMemory) ksession).getNodeMemory(from3).getBetaMemory();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 11);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball", "doll");
final TupleMemory tupleMemory = betaMemory.getLeftTupleMemory();
Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(2);
Iterator<LeftTuple> it = tupleMemory.iterator();
for (LeftTuple next = it.next(); next != null; next = it.next()) {
final Object obj = next.getFactHandle().getObject();
Assertions.assertThat(obj == charlie || obj == debbie).isTrue();
}
list.clear();
debbie.setAge(10);
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(0);
;
Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(1);
it = tupleMemory.iterator();
for (LeftTuple next = it.next(); next != null; next = it.next()) {
final Object obj = next.getFactHandle().getObject();
Assertions.assertThat(obj == charlie).isTrue();
}
}
use of org.drools.mvel.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathReactiveTest method testNonReactivePart.
@Test
public void testNonReactivePart() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[age > 10]?/toys )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 10);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
list.clear();
charlie.addToy(new Toy("robot"));
ksession.fireAllRules();
Assertions.assertThat(list).isEmpty();
}
use of org.drools.mvel.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathReactiveTest method testReactivitySettingAttributeInDrl.
@Test
public void testReactivitySettingAttributeInDrl() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "\n" + "rule R when\n" + " Man( $child: /wife/children[age >= 10] )\n" + "then\n" + "end\n" + "rule R2 when\n" + " Man( $child: /wife/children[age < 10] )\n" + "then\n" + "$child.setAge(12);" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final Man bob = new Man("Bob", 40);
final Woman alice = new Woman("Alice", 38);
final Child charlie = new Child("Charles", 9);
final Child debbie = new Child("Debbie", 8);
bob.setWife(alice);
alice.addChild(charlie);
alice.addChild(debbie);
ksession.insert(bob);
Assertions.assertThat(ksession.fireAllRules()).isEqualTo(4);
}
use of org.drools.mvel.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathReactiveTest method testReactive2Rules.
@Test
public void testReactive2Rules() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List toyList\n" + "global java.util.List teenagers\n" + "\n" + "rule R1 when\n" + " $i : Integer()\n" + " Man( $toy: /wife/children[age >= $i]/toys )\n" + "then\n" + " toyList.add( $toy.getName() );\n" + "end\n" + "rule R2 when\n" + " School( $child: /children[age >= 13] )\n" + "then\n" + " teenagers.add( $child.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final List<String> toyList = new ArrayList<>();
ksession.setGlobal("toyList", toyList);
final List<String> teenagers = new ArrayList<>();
ksession.setGlobal("teenagers", teenagers);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 15);
final Child debbie = new Child("Debbie", 12);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
final School school = new School("Da Vinci");
school.addChild(charlie);
school.addChild(debbie);
ksession.insert(13);
ksession.insert(bob);
ksession.insert(school);
ksession.fireAllRules();
Assertions.assertThat(toyList).containsExactlyInAnyOrder("car", "ball");
Assertions.assertThat(teenagers).containsExactlyInAnyOrder("Charles");
toyList.clear();
debbie.setAge(13);
ksession.fireAllRules();
Assertions.assertThat(toyList).containsExactlyInAnyOrder("doll");
Assertions.assertThat(teenagers).containsExactlyInAnyOrder("Charles", "Debbie");
}
Aggregations