use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testAccumulate2.
@Test
public void testAccumulate2() {
Result result = new Result();
Variable<Person> person = declarationOf(Person.class);
Variable<Integer> resultSum = declarationOf(Integer.class);
Variable<Double> resultAvg = declarationOf(Double.class);
Variable<Integer> age = declarationOf(Integer.class);
Rule rule = rule("accumulate").build(bind(age).as(person, Person::getAge), accumulate(expr(person, p -> p.getName().startsWith("M")), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, age).as(resultSum), accFunction(org.drools.core.base.accumulators.AverageAccumulateFunction.class, age).as(resultAvg)), on(resultSum, resultAvg).execute((sum, avg) -> result.setValue("total = " + sum + "; average = " + avg)));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("total = 77; average = 38.5", result.getValue());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testOrConditional.
@Test
public void testOrConditional() {
final org.drools.model.Global<java.util.List> var_list = globalOf(java.util.List.class, "defaultpkg", "list");
final Variable<Employee> var_$pattern_Employee$1$ = declarationOf(Employee.class, "$pattern_Employee$1$");
final Variable<Address> var_$address = declarationOf(Address.class, "$address", reactiveFrom(var_$pattern_Employee$1$, (_this) -> _this.getAddress()));
org.drools.model.Rule rule = rule("R").build(or(and(input(var_$pattern_Employee$1$), expr("$expr$2$", var_$address, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCity(), "Big City")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCity(), "Big City").reactOn("city")), and(input(var_$pattern_Employee$1$), expr("$expr$4$", var_$address, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCity(), "Small City")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCity(), "Small City").reactOn("city"))), on(var_$address, var_list).execute(($address, list) -> {
list.add($address.getCity());
}));
Model model = new ModelImpl().addRule(rule).addGlobal(var_list);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession kieSession = kieBase.newKieSession();
List<String> results = new ArrayList<>();
kieSession.setGlobal("list", results);
final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
kieSession.insert(bruno);
final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
kieSession.insert(alice);
kieSession.fireAllRules();
Assertions.assertThat(results).containsExactlyInAnyOrder("Big City", "Small City");
}
use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testNot.
@Test
public void testNot() {
Result result = new Result();
Variable<Person> oldestV = declarationOf(Person.class);
Variable<Person> otherV = declarationOf(Person.class);
Rule rule = rule("not").build(not(otherV, oldestV, (p1, p2) -> p1.getAge() > p2.getAge()), on(oldestV).execute(p -> result.setValue("Oldest person is " + p.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("Oldest person is Mario", result.getValue());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testQueryWithNamedArg.
@Test
public void testQueryWithNamedArg() {
Variable<Person> personV = declarationOf(Person.class, "$p");
Query1Def<Integer> qdef = query("olderThan", Integer.class, "ageArg");
Query query = qdef.build(expr("exprA", personV, qdef.getArg("ageArg", Integer.class), (p, a) -> p.getAge() > a));
Model model = new ModelImpl().addQuery(query);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
QueryResults results = ksession.getQueryResults("olderThan", 40);
assertEquals(1, results.size());
Person p = (Person) results.iterator().next().get("$p");
assertEquals("Mario", p.getName());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method test3Patterns.
@Test
public void test3Patterns() {
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("myrule").build(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());
}
Aggregations