use of org.drools.model.Variable in project drools by kiegroup.
the class PatternDSLTest method testAccumulate.
@Test
public void testAccumulate() {
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(accumulate(pattern(person).expr(p -> p.getName().startsWith("M")).bind(age, Person::getAge), 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 RuleUnitTest method testRuleUnitWithVarBinding.
@Test
public void testRuleUnitWithVarBinding() {
Variable<AdultUnit> unit = declarationOf(AdultUnit.class);
Variable<Person> adult = declarationOf(Person.class, unitData("persons"));
Rule rule = rule("org.drools.retebuilder", "Adult").unit(RuleUnitTest.AdultUnit.class).build(expr("$expr$1$", adult, unit, (p, u) -> p.getAge() > u.getAdultAge()), on(adult, unitData(List.class, "results")).execute((p, r) -> {
System.out.println(p.getName());
r.add(p.getName());
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kieBase);
executor.newDataSource("persons", new Person("Mario", 43), new Person("Marilena", 44), new Person("Sofia", 5));
AdultUnit ruleUnit = new AdultUnit();
executor.run(ruleUnit);
assertTrue(ruleUnit.getResults().containsAll(asList("Mario", "Marilena")));
}
use of org.drools.model.Variable 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.model.Variable in project drools by kiegroup.
the class FlowTest method testPositionalRecursiveQueryWithUnification.
@Test
public void testPositionalRecursiveQueryWithUnification() {
Variable<Relationship> var_$pattern_Relationship$1$ = declarationOf(Relationship.class);
Variable<Relationship> var_$pattern_Relationship$2$ = declarationOf(Relationship.class);
Variable<String> var_$unificationExpr$1$ = declarationOf(String.class);
Query2Def<String, String> queryDef_isRelatedTo = query("isRelatedTo", String.class, String.class);
Query query = queryDef_isRelatedTo.build(or(and(expr("exprA", var_$pattern_Relationship$1$, queryDef_isRelatedTo.getArg1(), (r, s) -> r.getStart().equals(s)), expr("exprB", var_$pattern_Relationship$1$, queryDef_isRelatedTo.getArg2(), (r, e) -> r.getEnd().equals(e))), and(and(bind(var_$unificationExpr$1$).as(var_$pattern_Relationship$2$, relationship -> relationship.getStart()), expr("exprD", var_$pattern_Relationship$2$, queryDef_isRelatedTo.getArg2(), (r, e) -> r.getEnd().equals(e))), queryDef_isRelatedTo.call(queryDef_isRelatedTo.getArg1(), var_$unificationExpr$1$))));
Model model = new ModelImpl().addQuery(query);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Relationship("A", "B"));
ksession.insert(new Relationship("B", "C"));
QueryResults results = ksession.getQueryResults("isRelatedTo", "A", "C");
assertEquals(1, results.size());
assertEquals("B", results.iterator().next().get(var_$unificationExpr$1$.getName()));
}
use of org.drools.model.Variable 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());
}
Aggregations