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());
}
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");
}
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());
}
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");
}
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");
}
Aggregations