Search in sources :

Example 1 with ConsequenceBuilder

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));
}
Also used : D(org.drools.modelcompiler.dsl.pattern.D) HashMap(java.util.HashMap) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) ConsequenceBuilder(org.drools.model.consequences.ConsequenceBuilder) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) PatternDSL(org.drools.model.PatternDSL) ViewItem(org.drools.model.view.ViewItem) ExprViewItem(org.drools.model.view.ExprViewItem) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Model(org.drools.model.Model) Rule(org.drools.model.Rule) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with ConsequenceBuilder

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));
}
Also used : D(org.drools.modelcompiler.dsl.pattern.D) ViewItem(org.drools.model.view.ViewItem) ExprViewItem(org.drools.model.view.ExprViewItem) HashMap(java.util.HashMap) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsequenceBuilder(org.drools.model.consequences.ConsequenceBuilder) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CountAccumulateFunction (org.drools.core.base.accumulators.CountAccumulateFunction)2 Model (org.drools.model.Model)2 Rule (org.drools.model.Rule)2 ConsequenceBuilder (org.drools.model.consequences.ConsequenceBuilder)2 ModelImpl (org.drools.model.impl.ModelImpl)2 ExprViewItem (org.drools.model.view.ExprViewItem)2 ViewItem (org.drools.model.view.ViewItem)2 D (org.drools.modelcompiler.dsl.pattern.D)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 KieSession (org.kie.api.runtime.KieSession)2 PatternDSL (org.drools.model.PatternDSL)1