use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testQueryInvokedWithGlobal.
@Test
public void testQueryInvokedWithGlobal() {
Global<Integer> ageG = globalOf(Integer.class, "defaultpkg", "ageG");
Variable<Person> personV = declarationOf(Person.class);
Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
Query query = qdef.build(expr("exprA", qdef.getArg1(), qdef.getArg2(), (_this, $age) -> _this.getAge() > $age));
Rule rule = rule("R").build(expr("exprB", personV, p -> p.getName().startsWith("M")), qdef.call(personV, ageG), on(personV).execute((drools, p) -> drools.insert(new Result(p.getName()))));
Model model = new ModelImpl().addGlobal(ageG).addQuery(query).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.setGlobal("ageG", 40);
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.insert(new Person("Edson", 41));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testOr.
@Test
public void testOr() {
Result result = new Result();
Variable<Person> personV = declarationOf(Person.class);
Variable<Person> markV = declarationOf(Person.class);
Variable<String> nameV = declarationOf(String.class);
Rule rule = rule("or").build(or(expr("exprA", personV, p -> p.getName().equals("Mark")), and(expr("exprA", markV, p -> p.getName().equals("Mark")), expr("exprB", personV, markV, (p1, p2) -> p1.getAge() > p2.getAge()))), expr("exprC", nameV, personV, (s, p) -> s.equals(p.getName())), on(nameV).execute(result::setValue));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert("Mario");
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("Mario", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testGlobalInConstraint.
@Test
public void testGlobalInConstraint() {
Variable<Person> markV = declarationOf(Person.class);
Global<Result> resultG = globalOf(Result.class, "org.mypkg");
Global<String> nameG = globalOf(String.class, "org.mypkg");
Rule rule = rule("org.mypkg", "global").build(expr("exprA", markV, nameG, (p, n) -> p.getName().equals(n)).reactOn("name"), on(markV, resultG).execute((p, r) -> r.setValue(p.getName() + " is " + p.getAge())));
Model model = new ModelImpl().addRule(rule).addGlobal(nameG).addGlobal(resultG);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.setGlobal(nameG.getName(), "Mark");
Result result = new Result();
ksession.setGlobal(resultG.getName(), result);
Person mark = new Person("Mark", 37);
Person edson = new Person("Edson", 35);
Person mario = new Person("Mario", 40);
ksession.insert(mark);
ksession.insert(edson);
ksession.insert(mario);
ksession.fireAllRules();
assertEquals("Mark is 37", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testBetaWithResult.
@Test
public void testBetaWithResult() {
Variable<Person> markV = declarationOf(Person.class);
Variable<Person> olderV = declarationOf(Person.class);
Variable<Result> resultV = declarationOf(Result.class);
Rule rule = rule("beta").build(expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 1, Person::getName, "Mark").reactOn("name", // also react on age, see RuleDescr.lookAheadFieldsOfIdentifier
"age"), expr("exprB", olderV, p -> !p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.NOT_EQUAL, 1, Person::getName, "Mark").reactOn("name"), expr("exprC", olderV, markV, (p1, p2) -> p1.getAge() > p2.getAge()).indexedBy(int.class, ConstraintType.GREATER_THAN, 0, Person::getAge, Person::getAge).reactOn("age"), on(olderV, markV, resultV).execute((p1, p2, r) -> r.setValue(p1.getName() + " is older than " + p2.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.insert(result);
Person mark = new Person("Mark", 37);
Person edson = new Person("Edson", 35);
Person mario = new Person("Mario", 40);
FactHandle markFH = ksession.insert(mark);
FactHandle edsonFH = ksession.insert(edson);
FactHandle marioFH = ksession.insert(mario);
ksession.fireAllRules();
assertEquals("Mario is older than Mark", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testBindWithEval.
@Test
public void testBindWithEval() {
Variable<Person> var_$p1 = declarationOf(Person.class, "$p1");
Variable<Integer> var_$a1 = declarationOf(Integer.class, "$a1");
org.drools.model.Rule rule = rule("R").build(bind(var_$a1).as(var_$p1, (_this) -> _this.getAge()).reactOn("age"), expr("$expr$2$", var_$a1, (_this) -> _this > 39), on(var_$p1).execute((drools, $p1) -> {
drools.insert(new Result($p1.getName()));
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mario", 40));
ksession.insert(new Person("Mark", 38));
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());
}
Aggregations