use of org.drools.model.PatternDSL.accumulate in project drools by kiegroup.
the class PatternDSLTest method testTwoAccumulatesWithVarBindingModel.
@Test
public void testTwoAccumulatesWithVarBindingModel() {
Variable<Person> a = PatternDSL.declarationOf(Person.class);
Variable<Pair> accSource = PatternDSL.declarationOf(Pair.class);
Variable<Collection> accResult = PatternDSL.declarationOf(Collection.class);
Variable<Collection> accResult2 = PatternDSL.declarationOf(Collection.class);
Variable<Pair> wrapped = PatternDSL.declarationOf(Pair.class, PatternDSL.from(accResult));
Variable<Object> unwrapped1 = PatternDSL.declarationOf(Object.class);
PatternDSL.PatternDef aPattern = PatternDSL.pattern(a).bind(accSource, v -> Pair.create(v.getName(), v.getAge()));
ViewItem accumulate = PatternDSL.accumulate(aPattern, DSL.accFunction(CollectSetAccumulateFunction::new, accSource).as(accResult));
PatternDSL.PatternDef secondPattern = PatternDSL.pattern(accResult);
PatternDSL.PatternDef thirdPattern = // If binding removed, test will pass.
PatternDSL.pattern(wrapped).bind(unwrapped1, Pair::getKey);
ViewItem accumulate2 = PatternDSL.accumulate(PatternDSL.and(secondPattern, thirdPattern), DSL.accFunction(CollectSetAccumulateFunction::new, wrapped).as(accResult2));
Rule rule = PatternDSL.rule("R").build(accumulate, accumulate2, PatternDSL.on(accResult2).execute(obj -> {
boolean works = obj.contains(Pair.create("Lukas", 35));
if (!works) {
throw new IllegalStateException("Why is " + obj + " not Set<" + Pair.class + ">?");
}
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession session = kieBase.newKieSession();
session.insert(new Person("Lukas", 35));
session.fireAllRules();
}
Aggregations