Search in sources :

Example 11 with Child

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

Example 12 with Child

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

Example 13 with Child

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

the class OOPathTest method testBackReferenceConstraint.

@Test
public void testBackReferenceConstraint() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children/toys[ name.length == ../name.length ] )\n" + "then\n" + "  list.add( $toy.getName() );\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("Carl", 12);
    final Child debbie = new Child("Debbie", 8);
    alice.addChild(charlie);
    alice.addChild(debbie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    debbie.addToy(new Toy("guitar"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("ball", "guitar");
}
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) Child(org.drools.mvel.compiler.oopath.model.Child) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 14 with Child

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

the class OOPathTest method testNotReactivePeer.

@Test
public void testNotReactivePeer() {
    // DROOLS-1727
    String drl1 = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n\n" + "rule R1 when\n" + "  not String()\n" + "  $a : Man( name == \"Mario\" )\n" + "then\n" + "  list.add(\"Found\");\n" + "  insert($a.getName());\n" + "end\n\n" + "rule R2 when\n" + "  not String()\n" + "  $a : Man( $c: /children[age == 6], name == \"Mario\" )\n" + "then\n" + "  list.add(\"Found\");\n" + "  insert($a.getName());\n" + "end\n\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl1);
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    Man mario = new Man("Mario", 40);
    mario.addChild(new Child("Sofia", 6));
    ksession.insert(mario);
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
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) Child(org.drools.mvel.compiler.oopath.model.Child) Test(org.junit.Test)

Example 15 with Child

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

the class OOPathTest method testIndexedAccess.

@Test
public void testIndexedAccess() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children[0]/toys[1] )\n" + "then\n" + "  list.add( $toy.getName() );\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);
    final Child debbie = new Child("Debbie", 11);
    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("ball");
}
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) Child(org.drools.mvel.compiler.oopath.model.Child) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Aggregations

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