use of org.drools.model.consequences.ConsequenceBuilder in project drools by kiegroup.
the class GroupByTest method testWithGroupByAfterExistsWithFrom.
@Test
public void testWithGroupByAfterExistsWithFrom() {
Global<Map> groupResultVar = D.globalOf(Map.class, "defaultPkg", "glob");
Variable<Integer> patternVar = D.declarationOf(Integer.class);
Variable<String> existsVar = D.declarationOf(String.class);
Variable<Integer> keyVar = D.declarationOf(Integer.class);
Variable<Long> resultVar = D.declarationOf(Long.class);
Variable<Integer> mappedResultVar = D.declarationOf(Integer.class);
D.PatternDef<Integer> pattern = D.pattern(patternVar);
D.PatternDef<String> exist = D.pattern(existsVar);
ViewItem patternAndExists = D.and(pattern, D.exists(exist));
ViewItem groupBy = D.groupBy(patternAndExists, patternVar, keyVar, Math::abs, DSL.accFunction(CountAccumulateFunction::new).as(resultVar));
PatternDSL.PatternDef mappedResult = D.pattern(resultVar).bind(mappedResultVar, Long::intValue);
ConsequenceBuilder._3 consequence = D.on(keyVar, mappedResultVar, groupResultVar).execute((key, count, result) -> {
result.put(key, count);
});
Rule rule = D.rule("R").build(groupBy, mappedResult, consequence);
Model model = new ModelImpl().addRule(rule).addGlobal(groupResultVar);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession session = kieBase.newKieSession();
Map<Integer, Integer> global = new HashMap<>();
session.setGlobal("glob", global);
session.insert("Something");
session.insert(-1);
session.insert(1);
session.insert(2);
session.fireAllRules();
assertEquals(2, global.size());
// -1 and 1 will map to the same key, and count twice.
assertEquals(2, (int) global.get(1));
// 2 maps to a key, and counts once.
assertEquals(1, (int) global.get(2));
}
use of org.drools.model.consequences.ConsequenceBuilder in project drools by kiegroup.
the class GroupByTest method testWithGroupByAfterExists.
@Test
public void testWithGroupByAfterExists() {
Global<Map> groupResultVar = D.globalOf(Map.class, "defaultPkg", "glob");
Variable<Integer> patternVar = D.declarationOf(Integer.class);
Variable<String> existsVar = D.declarationOf(String.class);
Variable<Integer> keyVar = D.declarationOf(Integer.class);
Variable<Long> resultVar = D.declarationOf(Long.class);
D.PatternDef<Integer> pattern = D.pattern(patternVar);
D.PatternDef<String> exist = D.pattern(existsVar);
ViewItem patternAndExists = D.and(pattern, D.exists(exist));
ViewItem groupBy = D.groupBy(patternAndExists, patternVar, keyVar, Math::abs, DSL.accFunction(CountAccumulateFunction::new).as(resultVar));
ConsequenceBuilder._3 consequence = D.on(keyVar, resultVar, groupResultVar).execute((key, count, result) -> {
result.put(key, count.intValue());
});
Rule rule = D.rule("R").build(groupBy, consequence);
Model model = new ModelImpl().addRule(rule).addGlobal(groupResultVar);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession session = kieBase.newKieSession();
Map<Integer, Integer> global = new HashMap<>();
session.setGlobal("glob", global);
session.insert("Something");
session.insert(-1);
session.insert(1);
session.insert(2);
session.fireAllRules();
assertEquals(2, global.size());
// -1 and 1 will map to the same key, and count twice.
assertEquals(2, (int) global.get(1));
// 2 maps to a key, and counts once.
assertEquals(1, (int) global.get(2));
}
Aggregations