use of org.drools.model.Variable 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.Variable 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.Variable 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.Variable 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());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testFrom.
@Test
public void testFrom() throws Exception {
Variable<Result> resultV = declarationOf(Result.class);
Variable<Adult> dadV = declarationOf(Adult.class);
Variable<Child> childV = declarationOf(Child.class, from(dadV, adult -> adult.getChildren()));
Rule rule = rule("from").build(expr("exprA", childV, c -> c.getAge() > 8), on(childV, resultV).execute((c, r) -> r.setValue(c.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.insert(result);
Adult dad = new Adult("dad", 40);
dad.addChild(new Child("Alan", 10));
dad.addChild(new Child("Betty", 7));
ksession.insert(dad);
ksession.fireAllRules();
assertEquals("Alan", result.getValue());
}
Aggregations