Search in sources :

Example 6 with Child

use of org.drools.compiler.oopath.model.Child 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 7 with Child

use of org.drools.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;
}
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 8 with Child

use of org.drools.compiler.oopath.model.Child 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)

Example 9 with Child

use of org.drools.compiler.oopath.model.Child 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;
}
Also used : ArrayList(java.util.ArrayList) 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)

Example 10 with Child

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

Aggregations

Child (org.drools.compiler.oopath.model.Child)29 Man (org.drools.compiler.oopath.model.Man)27 KieSession (org.kie.api.runtime.KieSession)27 ArrayList (java.util.ArrayList)24 KieHelper (org.kie.internal.utils.KieHelper)24 Test (org.junit.Test)21 Woman (org.drools.compiler.oopath.model.Woman)18 Toy (org.drools.compiler.oopath.model.Toy)17 School (org.drools.compiler.oopath.model.School)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 Disease (org.drools.compiler.oopath.model.Disease)2 KieBase (org.kie.api.KieBase)2 Collection (java.util.Collection)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 LeftTuple (org.drools.core.reteoo.LeftTuple)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 ReactiveFromNode (org.drools.core.reteoo.ReactiveFromNode)1