use of org.drools.model.Variable 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.model.Variable in project drools by kiegroup.
the class FlowTest method testNoLoopWithModel.
@Test(timeout = 5000)
public void testNoLoopWithModel() {
Variable<Person> meV = declarationOf(Person.class);
Rule rule = rule("noloop").attribute(Rule.Attribute.NO_LOOP, true).build(expr("exprA", meV, p -> p.getAge() > 18).reactOn("age"), on(meV).execute((drools, p) -> {
p.setAge(p.getAge() + 1);
drools.update(p, "age");
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Person me = new Person("Mario", 40);
ksession.insert(me);
ksession.fireAllRules();
assertEquals(41, me.getAge());
}
use of org.drools.model.Variable 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.model.Variable in project drools by kiegroup.
the class FlowTest method testBetaWithDeclarationBeforePattern.
@Test
public void testBetaWithDeclarationBeforePattern() {
Variable<Person> markV = declarationOf(Person.class);
Variable<Integer> markAge = declarationOf(Integer.class);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(bind(markAge).as(markV, Person::getAge).reactOn("age"), expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 1, Person::getName, "Mark").reactOn(// also react on age, see RuleDescr.lookAheadFieldsOfIdentifier
"name"), expr("exprB", olderV, p -> !p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.NOT_EQUAL, 1, Person::getName, "Mark").reactOn("name"), expr("exprC", olderV, markAge, (p1, age) -> p1.getAge() > age).indexedBy(int.class, ConstraintType.GREATER_THAN, 0, Person::getAge, int.class::cast).reactOn("age"), on(olderV, markV).execute((drools, p1, p2) -> drools.insert(p1.getName() + " is older than " + p2.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
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();
Collection<String> results = getObjectsIntoList(ksession, String.class);
Assertions.assertThat(results).containsExactlyInAnyOrder("Mario is older than Mark");
ksession.delete(marioFH);
ksession.fireAllRules();
mark.setAge(34);
ksession.update(markFH, mark, "age");
ksession.fireAllRules();
results = getObjectsIntoList(ksession, String.class);
Assertions.assertThat(results).containsExactlyInAnyOrder("Mario is older than Mark", "Edson is older than Mark");
}
use of org.drools.model.Variable 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());
}
Aggregations