use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testAccumulate1.
@Test
public void testAccumulate1() {
Result result = new Result();
Variable<Person> person = declarationOf(Person.class);
Variable<Integer> resultSum = declarationOf(Integer.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)), on(resultSum).execute(sum -> result.setValue("total = " + sum)));
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", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testForall.
@Test
public void testForall() {
Variable<Person> p1V = declarationOf(Person.class);
Variable<Person> p2V = declarationOf(Person.class);
Rule rule = rule("not").build(forall(expr("exprA", p1V, p -> p.getName().length() == 5), expr("exprB", p2V, p1V, (p2, p1) -> p2 == p1), expr("exprC", p2V, p -> p.getAge() > 40)), execute(drools -> drools.insert(new Result("ok"))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mario", 41));
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Edson", 42));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("ok", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result 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.modelcompiler.domain.Result 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.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testNotEmptyPredicate.
@Test
public void testNotEmptyPredicate() {
Rule rule = rule("R").build(not(input(declarationOf(Person.class))), execute((drools) -> drools.insert(new Result("ok"))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Person mario = new Person("Mario", 40);
ksession.insert(mario);
ksession.fireAllRules();
assertTrue(ksession.getObjects(new ClassObjectFilter(Result.class)).isEmpty());
}
Aggregations