use of org.drools.mvel.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathAccumulateTest method testAccumulate.
private void testAccumulate(final String accumulateFunction, final Number expectedResult) {
// DROOLS-1265
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.lang.Object 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";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.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 Number result = (Number) ksession.getGlobal("globalVar");
if (result instanceof Double) {
Assertions.assertThat(expectedResult.doubleValue()).isEqualTo(result.doubleValue());
} else {
Assertions.assertThat(expectedResult.longValue()).isEqualTo(result.longValue());
}
}
use of org.drools.mvel.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.mvel.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";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.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);
}
use of org.drools.mvel.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathMultilevelTest method testClassThreeLevelPath.
@Test
public void testClassThreeLevelPath() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toyName: /wife/children/toys/name )\n" + "then\n" + " list.add( $toyName );\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);
alice.addChild(charlie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
}
use of org.drools.mvel.compiler.oopath.model.Child 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.mvel.compiler.oopath.model.Child in project drools by kiegroup.
the class OOPathBenchmarkTest method testRelational.
public static long[] testRelational(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();
final List<InternalFactHandle> fhs = insertFullModel(ksession, model);
ksession.fireAllRules();
result[0] = System.nanoTime() - start;
list.clear();
start = System.nanoTime();
for (Child child : toBeModified) {
child.setAge(11);
}
for (InternalFactHandle fh : fhs) {
ksession.update(fh, fh.getObject());
}
ksession.fireAllRules();
result[1] = System.nanoTime() - start;
Assertions.assertThat(n).isEqualTo(list.size());
ksession.dispose();
return result;
}
Aggregations