Search in sources :

Example 26 with Man

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

Example 27 with Man

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

the class OOPathCastTest method testInlineCastWithConstraint.

@Test
public void testInlineCastWithConstraint() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( name == \"Bob\", $name: /wife/children#BabyGirl[ favoriteDollName.startsWith(\"A\") ].name )\n" + "then\n" + "  list.add( $name );\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, "Anna");
    final BabyGirl elisabeth = new BabyGirl("Elisabeth", 5, "Zoe");
    final BabyGirl farrah = new BabyGirl("Farrah", 3, "Agatha");
    alice.addChild(charlie);
    alice.addChild(debbie);
    alice.addChild(elisabeth);
    alice.addChild(farrah);
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Debbie", "Farrah");
}
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) Test(org.junit.Test)

Example 28 with Man

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

the class OOPathReactiveTests method testReactiveList.

@Test
public void testReactiveList() {
    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.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(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
    list.clear();
    charlie.addToy(new Toy("gun"));
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("gun");
}
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 29 with Man

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

the class OOPathReactiveTests method testReactiveOnLia.

@Test
public void testReactiveOnLia() {
    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.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(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 30 with Man

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

the class OOPathReactiveTests method testReactiveSet.

@Test
public void testReactiveSet() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $disease: /wife/children[age > 10]/diseases )\n" + "then\n" + "  list.add( $disease.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.addDisease(new Disease("flu"));
    charlie.addDisease(new Disease("asthma"));
    debbie.addDisease(new Disease("diabetes"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("flu", "asthma");
    list.clear();
    charlie.addDisease(new Disease("epilepsy"));
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("epilepsy");
}
Also used : Disease(org.drools.compiler.oopath.model.Disease) 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) 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