Search in sources :

Example 21 with Man

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

the class OOPathBindTest method testScenarioBindString.

private void testScenarioBindString(final String drl, final String... expectedResults) {
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Man bob = new Man("Bob", 40);
    final Woman alice = new Woman("Alice", 38);
    ksession.insert(bob);
    ksession.insert(alice);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder(expectedResults);
}
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)

Example 22 with Man

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

the class OOPathBindTest method testBindListWithConstraint.

@Test
public void testBindListWithConstraint() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toys: /wife/children[age > 10].toys )\n" + "then\n" + "  list.add( $toys.size() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final List<Integer> 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", 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(2);
}
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 23 with Man

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

the class OOPathBindTest method testBindObjectFromList.

@Test
public void testBindObjectFromList() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Adult( $child: /children )\n" + "then\n" + "  list.add( $child.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 Man bob = new Man("Bob", 40);
    bob.addChild(new Child("Charles", 12));
    bob.addChild(new Child("Debbie", 8));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Charles", "Debbie");
}
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) Child(org.drools.compiler.oopath.model.Child) Test(org.junit.Test)

Example 24 with Man

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

the class OOPathMultilevelTest method testScenarioTwoLevelPathWithConstraint.

private void testScenarioTwoLevelPathWithConstraint(final String drl) {
    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("Charlie", 12);
    alice.addChild(charlie);
    final Child debbie = new Child("Debbie", 8);
    alice.addChild(debbie);
    final Child eric = new Child("Eric", 15);
    alice.addChild(eric);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    eric.addToy(new Toy("bike"));
    ksession.insert(5);
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
}
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)

Example 25 with Man

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

the class OOPathMultilevelTest method testClassTwoLevelPath.

@Test
public void testClassTwoLevelPath() {
    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/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", 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("car", "ball", "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)

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