Search in sources :

Example 6 with NonExternalisedLambdaFoundException

use of org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException in project drools by kiegroup.

the class ExternalisedLambdaTest method testAccumulateWithBinaryExpr.

@Test
public void testAccumulateWithBinaryExpr() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + "  String( $l : length )" + "  accumulate ( $p: Person ( getName().startsWith(\"M\")); \n" + "                $sum : sum($p.getAge() * $l)  \n" + "              )                          \n" + "then\n" + "  insert(new Result($sum));\n" + "end";
    KieSession ksession = null;
    try {
        ksession = getKieSession(str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    ksession.insert("x");
    ksession.insert(new Person("Mark", 37));
    ksession.insert(new Person("Edson", 35));
    ksession.insert(new Person("Mario", 40));
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(1, results.size());
    assertEquals(77, ((Number) results.iterator().next().getValue()).intValue());
}
Also used : NonExternalisedLambdaFoundException(org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 7 with NonExternalisedLambdaFoundException

use of org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException in project drools by kiegroup.

the class ExternalisedLambdaTest method testCep.

@Test
public void testCep() throws Exception {
    String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    $b : StockTick( company == \"ACME\", timeFieldAsLong after[5,8] $a.timeFieldAsLong )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n";
    KieModuleModel kmodel = KieServices.get().newKieModuleModel();
    kmodel.newKieBaseModel("kb").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("ks").setDefault(true).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = null;
    try {
        ksession = getKieSession(kmodel, str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockTick("DROO").setTimeField(0));
    ksession.insert(new StockTick("ACME").setTimeField(6));
    assertEquals(1, ksession.fireAllRules());
    ksession.insert(new StockTick("ACME").setTimeField(10));
    assertEquals(0, ksession.fireAllRules());
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) NonExternalisedLambdaFoundException(org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 8 with NonExternalisedLambdaFoundException

use of org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException in project drools by kiegroup.

the class ExternalisedLambdaTest method testExternalizeLambdaUsingVariable.

@Test
public void testExternalizeLambdaUsingVariable() throws Exception {
    String str = "package defaultpkg;\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + "  Integer( $i : intValue )\n" + "  Person( age > $i )\n" + "then\n" + "  list.add($i);\n" + "end";
    KieSession ksession = null;
    try {
        ksession = getKieSession(str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    final List<Integer> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.insert(43);
    ksession.insert(new Person("John", 44));
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder(43);
}
Also used : NonExternalisedLambdaFoundException(org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 9 with NonExternalisedLambdaFoundException

use of org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException in project drools by kiegroup.

the class ExternalisedLambdaTest method testOOPath.

@Test
public void testOOPath() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $man: Man( /wife/children[age > 10] )\n" + "then\n" + "  list.add( $man.getName() );\n" + "end\n";
    KieSession ksession = null;
    try {
        ksession = getKieSession(str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    final Man carl = new Man("Carl", 40);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 12);
    final Child debbie = new Child("Debbie", 10);
    alice.addChild(charlie);
    alice.addChild(debbie);
    ksession.insert(bob);
    ksession.insert(carl);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
Also used : NonExternalisedLambdaFoundException(org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.modelcompiler.domain.Man) Woman(org.drools.modelcompiler.domain.Woman) Child(org.drools.modelcompiler.domain.Child) Test(org.junit.Test)

Aggregations

NonExternalisedLambdaFoundException (org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException)9 Test (org.junit.Test)9 KieSession (org.kie.api.runtime.KieSession)9 Person (org.drools.modelcompiler.domain.Person)6 ArrayList (java.util.ArrayList)5 Child (org.drools.modelcompiler.domain.Child)2 Man (org.drools.modelcompiler.domain.Man)2 Result (org.drools.modelcompiler.domain.Result)2 Woman (org.drools.modelcompiler.domain.Woman)2 StockTick (org.drools.modelcompiler.domain.StockTick)1 KieModuleModel (org.kie.api.builder.model.KieModuleModel)1 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)1