Search in sources :

Example 6 with Man

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

the class OOPathCastTest method testInlineCast.

@Test
public void testInlineCast() {
    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#BabyGirl/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 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 : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) BabyBoy(org.drools.mvel.compiler.oopath.model.BabyBoy) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) BabyGirl(org.drools.mvel.compiler.oopath.model.BabyGirl) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 7 with Man

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

the class OOPathAccumulateTest method testAccumulate.

private void testAccumulate(final String accumulateFunction, final Number expectedResult) {
    // DROOLS-1265
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.lang.Object globalVar\n" + "\n" + "rule R when\n" + "  accumulate ( Adult( $child: /children ) ; $accumulateResult: " + accumulateFunction + "($child.getAge()) )\n" + "then\n" + "  kcontext.getKieRuntime().setGlobal(\"globalVar\", $accumulateResult);\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession();
    final Man bob = new Man("Bob", 40);
    bob.addChild(new Child("Charles", 12));
    bob.addChild(new Child("Debbie", 8));
    ksession.insert(bob);
    ksession.fireAllRules();
    final Number result = (Number) ksession.getGlobal("globalVar");
    if (result instanceof Double) {
        Assertions.assertThat(expectedResult.doubleValue()).isEqualTo(result.doubleValue());
    } else {
        Assertions.assertThat(expectedResult.longValue()).isEqualTo(result.longValue());
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Child(org.drools.mvel.compiler.oopath.model.Child)

Example 8 with Man

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

the class OOPathAccumulateTest method testAccumulateCollection.

private void testAccumulateCollection(final String accumulateFunction, final Integer... expectedResults) {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.Collection<Integer> globalVar\n" + "\n" + "rule R when\n" + "  accumulate ( Adult( $child: /children ) ; $accumulateResult: " + accumulateFunction + "($child.getAge()) )\n" + "then\n" + "  kcontext.getKieRuntime().setGlobal(\"globalVar\", $accumulateResult);\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession();
    final Man bob = new Man("Bob", 40);
    bob.addChild(new Child("Charles", 12));
    bob.addChild(new Child("Debbie", 8));
    ksession.insert(bob);
    ksession.fireAllRules();
    final Collection<Integer> result = (Collection<Integer>) ksession.getGlobal("globalVar");
    Assertions.assertThat(result).containsExactlyInAnyOrder(expectedResults);
}
Also used : KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Child(org.drools.mvel.compiler.oopath.model.Child)

Example 9 with Man

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

the class OOPathBindTest method testScenarioBindString.

private void testScenarioBindString(final String drl, final String... expectedResults) {
    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);
    final Woman alice = new Woman("Alice", 38);
    ksession.insert(bob);
    ksession.insert(alice);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder(expectedResults);
}
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)

Example 10 with Man

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

the class OOPathMultilevelTest method testClassThreeLevelPath.

@Test
public void testClassThreeLevelPath() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toyName: /wife/children/toys/name )\n" + "then\n" + "  list.add( $toyName );\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);
    alice.addChild(charlie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "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

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