use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathBenchmarkTest method insertFullModel.
private static List<InternalFactHandle> insertFullModel(KieSession ksession, List<Man> model) {
final List<InternalFactHandle> toBeModified = new ArrayList<InternalFactHandle>();
for (Man man : model) {
ksession.insert(man);
ksession.insert(man.getWife());
for (Child child : man.getWife().getChildren()) {
final InternalFactHandle fh = (InternalFactHandle) ksession.insert(child);
if (child.getAge() == 10) {
toBeModified.add(fh);
}
for (Toy toy : child.getToys()) {
ksession.insert(toy);
}
}
}
return toBeModified;
}
use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathCastTest method testInlineCastWithConstraint.
@Test
public void testInlineCastWithConstraint() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( name == \"Bob\", $name: /wife/children#BabyGirl[ favoriteDollName.startsWith(\"A\") ].name )\n" + "then\n" + " list.add( $name );\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 BabyBoy charlie = new BabyBoy("Charles", 12);
final BabyGirl debbie = new BabyGirl("Debbie", 8, "Anna");
final BabyGirl elisabeth = new BabyGirl("Elisabeth", 5, "Zoe");
final BabyGirl farrah = new BabyGirl("Farrah", 3, "Agatha");
alice.addChild(charlie);
alice.addChild(debbie);
alice.addChild(elisabeth);
alice.addChild(farrah);
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("Debbie", "Farrah");
}
use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathReactiveTests method testReactiveList.
@Test
public void testReactiveList() {
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[age > 10]/toys )\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", 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.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathReactiveTests method testReactiveOnLia.
@Test
public void testReactiveOnLia() {
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[age > 10]/toys )\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", 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();
debbie.setAge(11);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathReactiveTests method testReactiveSet.
@Test
public void testReactiveSet() {
final String drl = "import org.drools.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";
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", 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");
}
Aggregations