use of org.drools.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathTest method testIndexedAccess.
@Test
public void testIndexedAccess() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[0]/toys[1] )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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", 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("ball");
}
use of org.drools.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathTest method testPrimitives.
@Test
public void testPrimitives() {
// DROOLS-1266
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Adult( $x: /children.age )\n" + "then\n" + " list.add( $x );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Man bob = new Man("Bob", 40);
bob.addChild(new Child("Charles", 12));
bob.addChild(new Child("Debbie", 8));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(2);
}
use of org.drools.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathTest method testBackReferenceConstraint.
@Test
public void testBackReferenceConstraint() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children/toys[ name.length == ../name.length ] )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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("Carl", 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"));
debbie.addToy(new Toy("guitar"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("ball", "guitar");
}
use of org.drools.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathTest method testNotReactivePeer.
@Test
public void testNotReactivePeer() {
// DROOLS-1727
String drl1 = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n\n" + "rule R1 when\n" + " not String()\n" + " $a : Man( name == \"Mario\" )\n" + "then\n" + " list.add(\"Found\");\n" + " insert($a.getName());\n" + "end\n\n" + "rule R2 when\n" + " not String()\n" + " $a : Man( $c: /children[age == 6], name == \"Mario\" )\n" + "then\n" + " list.add(\"Found\");\n" + " insert($a.getName());\n" + "end\n\n";
KieSession ksession = new KieHelper().addContent(drl1, ResourceType.DRL).build().newKieSession();
List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
Man mario = new Man("Mario", 40);
mario.addChild(new Child("Sofia", 6));
ksession.insert(mario);
ksession.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathAccumulateTest method testAccumulateCollection.
private void testAccumulateCollection(final String accumulateFunction, final Integer... expectedResults) {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.Collection<Integer> globalVar\n" + "\n" + "rule R when\n" + " accumulate ( Adult( $child: /children ) ; $accumulateResult: " + accumulateFunction + "($child.getAge()) )\n" + "then\n" + " kcontext.getKieRuntime().setGlobal(\"globalVar\", $accumulateResult);\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final Man bob = new Man("Bob", 40);
bob.addChild(new Child("Charles", 12));
bob.addChild(new Child("Debbie", 8));
ksession.insert(bob);
ksession.fireAllRules();
final Collection<Integer> result = (Collection<Integer>) ksession.getGlobal("globalVar");
Assertions.assertThat(result).containsExactlyInAnyOrder(expectedResults);
}
Aggregations