use of org.drools.model.impl.ModelImpl 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.model.impl.ModelImpl 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.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testIn.
@Test
public void testIn() {
final Variable<String> var_$pattern_String$1$ = declarationOf(String.class, "$pattern_String$1$");
org.drools.model.Rule rule = rule("R").build(expr("$expr$1$", var_$pattern_String$1$, (_this) -> eval(InOperator.INSTANCE, _this, "a", "b")), execute(() -> {
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert("b");
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.model.impl.ModelImpl 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());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testBeta.
@Test
public void testBeta() {
Result result = new Result();
Variable<Person> markV = declarationOf(Person.class);
Variable<Person> olderV = declarationOf(Person.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).execute((p1, p2) -> result.setValue(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();
assertEquals("Mario is older than Mark", result.getValue());
result.setValue(null);
ksession.delete(marioFH);
ksession.fireAllRules();
assertNull(result.getValue());
mark.setAge(34);
ksession.update(markFH, mark, "age");
ksession.fireAllRules();
assertEquals("Edson is older than Mark", result.getValue());
}
Aggregations