Search in sources :

Example 1 with NonExternalisedLambdaFoundException

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

the class ExternalisedLambdaTest method testEval.

@Test
public void testEval() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person()\n" + "  eval( $p.getAge() == 40 )\n" + "then\n" + "  insert(new Result($p.getName()));\n" + "end";
    KieSession ksession = null;
    try {
        ksession = getKieSession(str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    ksession.insert(new Person("Mario", 40));
    ksession.insert(new Person("Mark", 37));
    ksession.insert(new Person("Edson", 35));
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(1, results.size());
    assertEquals("Mario", results.iterator().next().getValue());
}
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 2 with NonExternalisedLambdaFoundException

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

the class ExternalisedLambdaTest method testExternalizeLambdaPredicate.

@Test
public void testExternalizeLambdaPredicate() throws Exception {
    String str = "package defaultpkg;\n" + "import " + Person.class.getCanonicalName() + ";" + "global java.util.List list;\n" + "rule R when\n" + "  $p : Person(name == \"Mario\")\n" + "then\n" + "  list.add($p.getName());\n" + "end";
    KieSession ksession = null;
    try {
        ksession = getKieSession(str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    Person me = new Person("Mario", 40);
    ksession.insert(me);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Mario");
}
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 3 with NonExternalisedLambdaFoundException

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

the class ExternalisedLambdaTest method testConsequenceNoVariable.

@Test
public void testConsequenceNoVariable() throws Exception {
    // DROOLS-4924
    String str = "package defaultpkg;\n" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person(name == \"Mario\")\n" + "then\n" + // don't remove this line
    "  System.out.println(\"Hello\");\n" + "end";
    KieSession ksession = null;
    try {
        ksession = getKieSession(str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    Person me = new Person("Mario", 40);
    ksession.insert(me);
    assertEquals(1, ksession.fireAllRules());
}
Also used : NonExternalisedLambdaFoundException(org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 4 with NonExternalisedLambdaFoundException

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

the class ExternalisedLambdaTest method testExternalizeBindingVariableLambda.

@Test
public void testExternalizeBindingVariableLambda() throws Exception {
    String str = "package defaultpkg;\n" + "import " + Person.class.getCanonicalName() + ";" + "global java.util.List list;\n" + "rule R when\n" + "  $p : Person($n : name == \"Mario\")\n" + "then\n" + "  list.add($n);\n" + "end";
    KieSession ksession = null;
    try {
        ksession = getKieSession(str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    Person me = new Person("Mario", 40);
    ksession.insert(me);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Mario");
}
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 5 with NonExternalisedLambdaFoundException

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

the class ExternalisedLambdaTest method testFromExpression.

@Test
public void testFromExpression() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $wife : wife )\n" + " $child: Child( age > 10 ) from $wife.children\n" + "then\n" + "  list.add( $child.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);
    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.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Charles");
}
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