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