Search in sources :

Example 1 with Man

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

Example 2 with Man

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

the class OOPathTest method testWith2Peers.

@Test
public void testWith2Peers() {
    // DROOLS-1589
    String header = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n\n";
    String drl1 = "rule R1 when\n" + "  Man( $m: /wife[age == 25] )\n" + "then\n" + "  list.add($m.getName());\n" + "end\n\n";
    String drl2 = "rule R2 when\n" + "  Man( $m: /wife[age == 26] )\n" + "then\n" + "  list.add($m.getName());\n" + "end\n\n";
    String drl3 = "rule R3 when\n" + "  Man( $m: /wife[age == 27] )\n" + "then\n" + "  list.add($m.getName());\n" + "end\n\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, header + drl1 + drl2 + drl3);
    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("Jane", list.get(0));
    list.clear();
    bob.getWife().setAge(26);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("Jane", list.get(0));
    list.clear();
    bob.getWife().setAge(27);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("Jane", list.get(0));
    list.clear();
    bob.getWife().setAge(28);
    ksession.fireAllRules();
    assertEquals(0, 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) Woman(org.drools.mvel.compiler.oopath.model.Woman) Test(org.junit.Test)

Example 3 with Man

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

the class OOPathTest method testPrimitives.

@Test
public void testPrimitives() {
    // DROOLS-1266
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Adult( $x : /children[$y : age]/toys[$t : name] )\n" + "then\n" + "  list.add( $x.getName() + \":\" + $y + \":\" + $t );\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 Man bob = new Man("Bob", 40);
    Child charles = new Child("Charles", 12);
    charles.addToy(new Toy("t1"));
    charles.addToy(new Toy("t2"));
    bob.addChild(charles);
    Child deb = new Child("Debbie", 8);
    deb.addToy(new Toy("t3"));
    deb.addToy(new Toy("t4"));
    bob.addChild(deb);
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).hasSize(4);
    Assert.assertEquals(Arrays.asList(new String[] { "t2:12:t2", "t1:12:t1", "t4:8:t4", "t3:8:t3" }), list);
}
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) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 4 with Man

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

the class OOPathReactiveTest method testReactiveOnBeta.

@Test
public void testReactiveOnBeta() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  $i : Integer()\n" + "  Man( $toy: /wife/children[age > $i]?/toys )\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", 10);
    alice.addChild(charlie);
    alice.addChild(debbie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    ksession.insert(10);
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
    list.clear();
    debbie.setAge(11);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
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 5 with Man

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

the class OOPathReactiveTest method testReactiveOnLia.

@Test
public void testReactiveOnLia() {
    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[age > 10]/toys )\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", 10);
    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("car", "ball");
    list.clear();
    debbie.setAge(11);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
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

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