use of org.drools.mvel.compiler.oopath.model.Woman in project drools by kiegroup.
the class OOPathMultilevelTest method testClassTwoLevelPath.
@Test
public void testClassTwoLevelPath() {
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/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", 8);
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");
}
use of org.drools.mvel.compiler.oopath.model.Woman in project drools by kiegroup.
the class OOPathReactiveTest method testReactiveList.
@Test
public void testReactiveList() {
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("gun"));
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("gun");
}
use of org.drools.mvel.compiler.oopath.model.Woman in project drools by kiegroup.
the class OOPathReactiveTest method testReactiveMap.
@Test
public void testReactiveMap() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $bodyMeasurement: /wife/bodyMeasurementsMap/entrySet )\n" + "then\n" + " list.add( $bodyMeasurement.getValue() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final List<Integer> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Man bob = new Man("Bob", 40);
final Woman alice = new Woman("Alice", 38);
alice.putBodyMeasurement(CHEST, 80);
bob.setWife(alice);
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder(80);
list.clear();
alice.putBodyMeasurement(RIGHT_FOREARM, 38);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder(38, 80);
}
use of org.drools.mvel.compiler.oopath.model.Woman in project drools by kiegroup.
the class OOPathReactiveTest method testSingleFireOnReactiveChange.
@Test
public void testSingleFireOnReactiveChange() {
// DROOLS-1302
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 );\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);
ksession.insert(bob);
ksession.fireAllRules();
list.clear();
final Child eleonor = new Child("Eleonor", 10);
alice.addChild(eleonor);
final Toy toy = new Toy("eleonor toy 1");
eleonor.addToy(toy);
eleonor.setAge(11);
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(1);
list.clear();
toy.setName("eleonor toy 2");
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(1);
}
Aggregations