Search in sources :

Example 11 with Man

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

Example 12 with Man

use of org.drools.mvel.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.mvel.compiler.oopath.model.Man) InternalFactHandle(org.drools.core.common.InternalFactHandle) Child(org.drools.mvel.compiler.oopath.model.Child)

Example 13 with Man

use of org.drools.mvel.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.mvel.compiler.oopath.model.Man) InternalFactHandle(org.drools.core.common.InternalFactHandle) Child(org.drools.mvel.compiler.oopath.model.Child)

Example 14 with Man

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

Example 15 with Man

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

the class OOPathTest method testWithExists.

@Test
public void testWithExists() {
    String header = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n\n";
    String drl1 = "rule R1 when\n" + "  exists( Man( $m: /wife[age == 25] ) )\n" + "then\n" + "  list.add(\"Found\");\n" + "end\n\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, header + drl1);
    KieSession ksession = kbase.newKieSession();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Man bob = new Man("John", 25);
    bob.setWife(new Woman("Jane", 25));
    ksession.insert(bob);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("Found", list.get(0));
    list.clear();
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) Test(org.junit.Test)

Aggregations

Man (org.drools.mvel.compiler.oopath.model.Man)34 KieSession (org.kie.api.runtime.KieSession)32 ArrayList (java.util.ArrayList)31 KieBase (org.kie.api.KieBase)29 Child (org.drools.mvel.compiler.oopath.model.Child)27 Woman (org.drools.mvel.compiler.oopath.model.Woman)24 Test (org.junit.Test)24 Toy (org.drools.mvel.compiler.oopath.model.Toy)19 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 BabyBoy (org.drools.mvel.compiler.oopath.model.BabyBoy)2 BabyGirl (org.drools.mvel.compiler.oopath.model.BabyGirl)2 Collection (java.util.Collection)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ClassObjectType (org.drools.core.base.ClassObjectType)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