Search in sources :

Example 11 with Man

use of org.drools.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.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 12 with Man

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

Example 13 with Man

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

the class OOPathCastTest method testInlineCast.

@Test
public void testInlineCast() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children#BabyGirl/toys )\n" + "then\n" + "  list.add( $toy.getName() );\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 BabyBoy charlie = new BabyBoy("Charles", 12);
    final BabyGirl debbie = new BabyGirl("Debbie", 8);
    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("doll");
}
Also used : ArrayList(java.util.ArrayList) BabyBoy(org.drools.compiler.oopath.model.BabyBoy) 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) BabyGirl(org.drools.compiler.oopath.model.BabyGirl) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 14 with Man

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

the class OOPathReactiveTests method testReactiveOnBeta.

@Test
public void testReactiveOnBeta() {
    final String drl = "import org.drools.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";
    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);
    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 : 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 15 with Man

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

the class OOPathReactiveTests method testSingleFireOnReactiveChange.

@Test
public void testSingleFireOnReactiveChange() {
    // DROOLS-1302
    final String drl = "import org.drools.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 );\n" + "end\n";
    final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    final 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);
    ksession.insert(bob);
    ksession.fireAllRules();
    list.clear();
    final Child eleonor = new Child("Eleonor", 10);
    alice.addChild(eleonor);
    final Toy toy = new Toy("eleonor toy 1");
    eleonor.addToy(toy);
    eleonor.setAge(11);
    ksession.fireAllRules();
    Assertions.assertThat(list).hasSize(1);
    list.clear();
    toy.setName("eleonor toy 2");
    ksession.fireAllRules();
    Assertions.assertThat(list).hasSize(1);
}
Also used : KieBase(org.kie.api.KieBase) 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)

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