Search in sources :

Example 6 with Man

use of org.drools.compiler.oopath.model.Man 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);
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Child(org.drools.compiler.oopath.model.Child)

Example 7 with Man

use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.

the class OOPathBindTest method testBindInteger.

public void testBindInteger(final boolean fireUntilHalt) throws InterruptedException, ExecutionException {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Adult( $age: /age )\n" + "then\n" + "  list.add( $age );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    Future fireUntilHaltFuture = null;
    if (fireUntilHalt) {
        fireUntilHaltFuture = startFireUntilHaltThread(ksession);
    }
    final List<Integer> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Man bob = new Man("Bob", 40);
    ksession.insert(bob);
    if (fireUntilHalt) {
        waitForResultAndStopFireUntilHalt(list, ksession, fireUntilHaltFuture);
    } else {
        ksession.fireAllRules();
        Assertions.assertThat(list).hasSize(1);
    }
    Assertions.assertThat(list).contains(40);
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Future(java.util.concurrent.Future) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man)

Example 8 with Man

use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.

the class OOPathMultilevelTest method testClassThreeLevelPath.

@Test
public void testClassThreeLevelPath() {
    final String drl = "import org.drools.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";
    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);
    alice.addChild(charlie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) Child(org.drools.compiler.oopath.model.Child) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 9 with Man

use of org.drools.compiler.oopath.model.Man 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;
}
Also used : ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) InternalFactHandle(org.drools.core.common.InternalFactHandle) Child(org.drools.compiler.oopath.model.Child)

Example 10 with Man

use of org.drools.compiler.oopath.model.Man in project drools by kiegroup.

the class OOPathBenchmarkTest method testFrom.

public static long[] testFrom(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 = insertModel(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 * 3).isEqualTo(list.size());
    ksession.dispose();
    return result;
}
Also used : ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) InternalFactHandle(org.drools.core.common.InternalFactHandle) Child(org.drools.compiler.oopath.model.Child)

Aggregations

Man (org.drools.compiler.oopath.model.Man)34 KieSession (org.kie.api.runtime.KieSession)32 ArrayList (java.util.ArrayList)31 KieHelper (org.kie.internal.utils.KieHelper)29 Child (org.drools.compiler.oopath.model.Child)27 Woman (org.drools.compiler.oopath.model.Woman)24 Test (org.junit.Test)24 Toy (org.drools.compiler.oopath.model.Toy)18 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 BabyBoy (org.drools.compiler.oopath.model.BabyBoy)2 BabyGirl (org.drools.compiler.oopath.model.BabyGirl)2 KieBase (org.kie.api.KieBase)2 Collection (java.util.Collection)1 Future (java.util.concurrent.Future)1 Disease (org.drools.compiler.oopath.model.Disease)1 School (org.drools.compiler.oopath.model.School)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1 EntryPointNode (org.drools.core.reteoo.EntryPointNode)1 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)1