use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathBenchmarkTest method generateModel.
private static List<Man> generateModel(int nr) {
final List<Man> model = new ArrayList<Man>();
for (int i = 0; i < nr; i++) {
final Man man = new Man("m" + i, 40);
model.add(man);
final Woman woman = new Woman("w" + i, 35);
man.setWife(woman);
woman.setHusband(man.getName());
final Child childA = new Child("cA" + i, 12);
woman.addChild(childA);
childA.setMother(woman.getName());
final Child childB = new Child("cB" + i, 10);
woman.addChild(childB);
childB.setMother(woman.getName());
final Toy toyA = new Toy("tA" + i);
toyA.setOwner(childA.getName());
childA.addToy(toyA);
final Toy toyB = new Toy("tB" + i);
toyB.setOwner(childA.getName());
childA.addToy(toyB);
final Toy toyC = new Toy("tC" + i);
toyC.setOwner(childB.getName());
childB.addToy(toyC);
}
return model;
}
use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathBenchmarkTest method testOOPath.
public static long[] testOOPath(KieBase kbase, int n) {
final long[] result = new long[2];
final KieSession ksession = kbase.newKieSession();
final List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
final List<Man> model = generateModel(n);
final List<Child> toBeModified = getChildToBeModified(model);
long start = System.nanoTime();
insertModel(ksession, model);
ksession.fireAllRules();
result[0] = System.nanoTime() - start;
list.clear();
start = System.nanoTime();
for (Child child : toBeModified) {
child.setAge(11);
}
ksession.fireAllRules();
result[1] = System.nanoTime() - start;
Assertions.assertThat(n).isEqualTo(list.size());
ksession.dispose();
return result;
}
use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathCastTest method testInlineCast.
@Test
public void testInlineCast() {
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#BabyGirl/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 BabyBoy charlie = new BabyBoy("Charles", 12);
final BabyGirl debbie = new BabyGirl("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("doll");
}
use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.
the class OOPathReactiveTests method testReactiveOnBeta.
@Test
public void testReactiveOnBeta() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $i : Integer()\n" + " Man( $toy: /wife/children[age > $i]?/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(10);
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 testSingleFireOnReactiveChange.
@Test
public void testSingleFireOnReactiveChange() {
// DROOLS-1302
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 );\n" + "end\n";
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
final 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