Search in sources :

Example 1 with IntegerSumAccumulateFunction

use of org.drools.core.base.accumulators.IntegerSumAccumulateFunction in project drools by kiegroup.

the class GroupByTest method testNestedRewrite.

// These two test are commented out, until we figure out the correct way to do this and limitations.
// If no correct way can be found, the tests can be deleted.
// @Test
// public void testErrorOnCompositeBind() {
// Global<Map> var_results = D.globalOf(Map.class, "defaultpkg", "results");
// 
// Variable<String> var_$key_1 = D.declarationOf(String.class);
// Variable<Person> var_$p = D.declarationOf(Person.class);
// Variable<Integer> var_$age = D.declarationOf(Integer.class);
// Variable<Integer> var_$sumOfAges = D.declarationOf(Integer.class);
// Variable<Group> var_$g1 = D.declarationOf(Group.class);
// Variable<Integer> var_$g1_value = D.declarationOf(Integer.class);
// Variable<String> var_$key_2 = D.declarationOf(String.class);
// Variable<Integer> var_$maxOfValues = D.declarationOf(Integer.class);
// 
// Rule rule1 = D.rule("R1").build(
// D.groupBy(
// D.pattern(var_$p).bind(var_$age, person -> person.getAge()),
// var_$p, var_$key_1, groupResult -> var_$p.getName().substring(0, 2),
// D.accFunction( IntegerMaxAccumulateFunction::new, var_$age).as(var_$maxOfValues)),
// D.pattern(var_$key_1).bind(var_$g1, var_$maxOfValues, (k, s) -> new Group(k, s)),
// D.on(var_$key_1)
// .execute($key -> { System.out.println($key); }));
// 
// try {
// Model      model    = new ModelImpl().addRule(rule1).addGlobal(var_results);
// KieSession ksession = KieBaseBuilder.createKieBaseFromModel(model).newKieSession();
// fail("Composite Bindings are not allowed");
// } catch(Exception e) {
// 
// }
// }
// 
// @Test
// public void testErrorOnNestedCompositeBind() {
// Global<Map> var_results = D.globalOf(Map.class, "defaultpkg", "results");
// 
// Variable<String> var_$key_1 = D.declarationOf(String.class);
// Variable<Person> var_$p = D.declarationOf(Person.class);
// Variable<Integer> var_$age = D.declarationOf(Integer.class);
// Variable<Integer> var_$sumOfAges = D.declarationOf(Integer.class);
// Variable<Group> var_$g1 = D.declarationOf(Group.class);
// Variable<Integer> var_$g1_value = D.declarationOf(Integer.class);
// Variable<String> var_$key_2 = D.declarationOf(String.class);
// Variable<Integer> var_$maxOfValues = D.declarationOf(Integer.class);
// 
// Rule rule1 = D.rule("R1").build(
// D.groupBy(
// D.and(
// D.groupBy(
// D.pattern(var_$p).bind(var_$age, person -> person.getAge()),
// var_$p, var_$key_1, person -> person.getName().substring(0, 3),
// D.accFunction( IntegerSumAccumulateFunction::new, var_$age).as(var_$sumOfAges)),
// D.pattern(var_$key_1).bind(var_$g1, var_$sumOfAges, (k, s) -> new Group(k, s))), // this should fail, due to two declarations
// var_$g1, var_$key_2, groupResult -> ((String)groupResult.getKey()).substring(0, 2),
// D.accFunction( IntegerMaxAccumulateFunction::new, var_$g1_value).as(var_$maxOfValues)),
// D.on(var_$key_1)
// .execute($key -> { System.out.println($key); }));
// 
// try {
// Model      model    = new ModelImpl().addRule(rule1).addGlobal(var_results);
// KieSession ksession = KieBaseBuilder.createKieBaseFromModel(model).newKieSession();
// fail("Composite Bindings are not allowed");
// } catch(Exception e) {
// 
// }
// }
@Test
public void testNestedRewrite() {
    // DROOLS-5697
    Global<List> var_results = D.globalOf(List.class, "defaultpkg", "results");
    Variable<Person> var_$p = D.declarationOf(Person.class);
    Variable<Integer> var_$age = D.declarationOf(Integer.class);
    Variable<Integer> var_$sumOfAges = D.declarationOf(Integer.class);
    Variable<Integer> var_$g1 = D.declarationOf(Integer.class);
    Variable<Integer> var_$maxOfValues = D.declarationOf(Integer.class);
    Rule rule1 = D.rule("R1").build(D.accumulate(D.and(D.accumulate(D.pattern(var_$p).bind(var_$age, person -> person.getAge()), D.accFunction(IntegerSumAccumulateFunction::new, var_$age).as(var_$sumOfAges)), D.pattern(var_$sumOfAges).bind(var_$g1, (s) -> s + 1)), D.accFunction(IntegerMaxAccumulateFunction::new, var_$g1).as(var_$maxOfValues)), D.on(var_results, var_$maxOfValues).execute((results, $maxOfValues) -> {
        System.out.println($maxOfValues);
        results.add($maxOfValues);
    }));
    Model model = new ModelImpl().addRule(rule1).addGlobal(var_results);
    KieSession ksession = KieBaseBuilder.createKieBaseFromModel(model).newKieSession();
    List results = new ArrayList();
    ksession.setGlobal("results", results);
    FactHandle fhMark = ksession.insert(new Person("Mark", 42));
    FactHandle fhEdoardo = ksession.insert(new Person("Edoardo", 33));
    ksession.fireAllRules();
    assertTrue(results.contains(76));
    ksession.insert(new Person("Edson", 38));
    ksession.fireAllRules();
    assertTrue(results.contains(114));
    ksession.delete(fhEdoardo);
    ksession.fireAllRules();
    assertTrue(results.contains(81));
    ksession.update(fhMark, new Person("Mark", 45));
    ksession.fireAllRules();
    assertTrue(results.contains(84));
}
Also used : Arrays(java.util.Arrays) InternalFactHandle(org.drools.core.common.InternalFactHandle) Accumulator(org.drools.core.spi.Accumulator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Global(org.drools.model.Global) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) DSL(org.drools.model.DSL) Child(org.drools.modelcompiler.domain.Child) Match(org.kie.api.runtime.rule.Match) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Declaration(org.drools.core.rule.Declaration) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) Parent(org.drools.modelcompiler.domain.Parent) Set(java.util.Set) Index(org.drools.model.Index) ConsequenceBuilder(org.drools.model.consequences.ConsequenceBuilder) EvaluationUtil(org.drools.modelcompiler.util.EvaluationUtil) Objects(java.util.Objects) List(java.util.List) Tuple(org.drools.core.spi.Tuple) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) Person(org.drools.modelcompiler.domain.Person) DSL.from(org.drools.model.DSL.from) ModelImpl(org.drools.model.impl.ModelImpl) HashMap(java.util.HashMap) PatternDSL(org.drools.model.PatternDSL) ArrayList(java.util.ArrayList) Function1(org.drools.model.functions.Function1) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieBase(org.kie.api.KieBase) LinkedHashSet(java.util.LinkedHashSet) Model(org.drools.model.Model) IntegerMaxAccumulateFunction(org.drools.core.base.accumulators.IntegerMaxAccumulateFunction) ReteEvaluator(org.drools.core.common.ReteEvaluator) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IntegerSumAccumulateFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction) ToIntFunction(java.util.function.ToIntFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) GroupKey(org.drools.model.functions.accumulate.GroupKey) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Rule(org.drools.model.Rule) ExprViewItem(org.drools.model.view.ExprViewItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Model(org.drools.model.Model) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 2 with IntegerSumAccumulateFunction

use of org.drools.core.base.accumulators.IntegerSumAccumulateFunction in project drools by kiegroup.

the class GroupByTest method testFilterOnAccumulateResultWithDecomposedGroupByKey.

@Test
public void testFilterOnAccumulateResultWithDecomposedGroupByKey() throws Exception {
    // DROOLS-6045
    final Global<List> var_results = D.globalOf(List.class, "defaultpkg", "results");
    final Variable<Pair<String, String>> var_$key = (Variable) D.declarationOf(Pair.class);
    final Variable<Person> var_$p = D.declarationOf(Person.class);
    final Variable<Integer> var_$pAge = D.declarationOf(Integer.class);
    final Variable<Object> var_$subkeyA = D.declarationOf(Object.class);
    final Variable<Object> var_$subkeyB = D.declarationOf(Object.class);
    final Variable<Integer> var_$accresult = D.declarationOf(Integer.class);
    final Rule rule1 = PatternDSL.rule("R1").build(D.groupBy(// Patterns
    PatternDSL.pattern(var_$p).bind(var_$pAge, Person::getAge).expr(Objects::nonNull), // Grouping Function
    var_$p, var_$key, person -> Pair.create(person.getName().substring(0, 1), person.getName().substring(1, 2)), D.accFunction(IntegerSumAccumulateFunction::new, var_$pAge).as(var_$accresult)), // Bindings
    D.pattern(var_$key).bind(var_$subkeyA, Pair::getKey).bind(var_$subkeyB, Pair::getValue), D.pattern(var_$accresult).expr("Some expr", var_$subkeyA, var_$subkeyB, (a, b, c) -> true), // Consequence
    D.on(var_$subkeyA, var_$subkeyB, var_$accresult, var_results).execute(($a, $b, $accResult, results) -> {
        results.add($a);
        results.add($b);
        results.add($accResult);
    }));
    final Model model = new ModelImpl().addRule(rule1).addGlobal(var_results);
    final KieSession ksession = KieBaseBuilder.createKieBaseFromModel(model).newKieSession();
    final List<Object> results = new ArrayList<>();
    ksession.setGlobal("results", results);
    ksession.insert("A");
    ksession.insert("test");
    ksession.insert(new Person("Mark", 42));
    assertThat(ksession.fireAllRules()).isEqualTo(1);
    Assertions.assertThat(results.size()).isEqualTo(3);
    Assertions.assertThat(results.get(0)).isEqualTo("M");
    Assertions.assertThat(results.get(1)).isEqualTo("a");
    Assertions.assertThat(results.get(2)).isEqualTo(42);
}
Also used : Arrays(java.util.Arrays) InternalFactHandle(org.drools.core.common.InternalFactHandle) Accumulator(org.drools.core.spi.Accumulator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Global(org.drools.model.Global) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) DSL(org.drools.model.DSL) Child(org.drools.modelcompiler.domain.Child) Match(org.kie.api.runtime.rule.Match) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Declaration(org.drools.core.rule.Declaration) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) Parent(org.drools.modelcompiler.domain.Parent) Set(java.util.Set) Index(org.drools.model.Index) ConsequenceBuilder(org.drools.model.consequences.ConsequenceBuilder) EvaluationUtil(org.drools.modelcompiler.util.EvaluationUtil) Objects(java.util.Objects) List(java.util.List) Tuple(org.drools.core.spi.Tuple) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) Person(org.drools.modelcompiler.domain.Person) DSL.from(org.drools.model.DSL.from) ModelImpl(org.drools.model.impl.ModelImpl) HashMap(java.util.HashMap) PatternDSL(org.drools.model.PatternDSL) ArrayList(java.util.ArrayList) Function1(org.drools.model.functions.Function1) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieBase(org.kie.api.KieBase) LinkedHashSet(java.util.LinkedHashSet) Model(org.drools.model.Model) IntegerMaxAccumulateFunction(org.drools.core.base.accumulators.IntegerMaxAccumulateFunction) ReteEvaluator(org.drools.core.common.ReteEvaluator) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IntegerSumAccumulateFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction) ToIntFunction(java.util.function.ToIntFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) GroupKey(org.drools.model.functions.accumulate.GroupKey) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Rule(org.drools.model.Rule) ExprViewItem(org.drools.model.view.ExprViewItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Variable(org.drools.model.Variable) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Objects(java.util.Objects) Model(org.drools.model.Model) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Pair(org.apache.commons.math3.util.Pair) Test(org.junit.Test)

Example 3 with IntegerSumAccumulateFunction

use of org.drools.core.base.accumulators.IntegerSumAccumulateFunction in project drools by kiegroup.

the class GroupByTest method testTwoGroupByUsingBindings.

@Test
// FIXME This does not work, because Declaration only works with function1
@Ignore
public void testTwoGroupByUsingBindings() {
    // DROOLS-5697
    Global<Map> var_results = D.globalOf(Map.class, "defaultpkg", "results");
    Variable<String> var_$key_1 = D.declarationOf(String.class);
    Variable<Person> var_$p = D.declarationOf(Person.class);
    Variable<Integer> var_$age = D.declarationOf(Integer.class);
    Variable<Integer> var_$sumOfAges = D.declarationOf(Integer.class);
    // "$g1", D.from(var_$key_1, var_$sumOfAges, ($k, $v) -> new Group($k, $v)));
    Variable<Group> var_$g1 = D.declarationOf(Group.class);
    Variable<Integer> var_$g1_value = D.declarationOf(Integer.class);
    Variable<String> var_$key_2 = D.declarationOf(String.class);
    Variable<Integer> var_$maxOfValues = D.declarationOf(Integer.class);
    Rule rule1 = D.rule("R1").build(D.groupBy(D.and(D.groupBy(D.pattern(var_$p).bind(var_$age, person -> person.getAge()), var_$p, var_$key_1, person -> person.getName().substring(0, 3), D.accFunction(IntegerSumAccumulateFunction::new, var_$age).as(var_$sumOfAges)), // Currently this does not work
    D.pattern(var_$key_1).bind(var_$g1, var_$sumOfAges, ($k, $v) -> new Group($k, $v)), D.pattern(var_$g1).bind(var_$g1_value, group -> (Integer) group.getValue())), var_$g1, var_$key_2, groupResult -> ((String) groupResult.getKey()).substring(0, 2), D.accFunction(IntegerMaxAccumulateFunction::new, var_$g1_value).as(var_$maxOfValues)), D.on(var_$key_2, var_results, var_$maxOfValues).execute(($key, results, $maxOfValues) -> {
        System.out.println($key + " -> " + $maxOfValues);
        results.put($key, $maxOfValues);
    }));
    Model model = new ModelImpl().addRule(rule1).addGlobal(var_results);
    KieSession ksession = KieBaseBuilder.createKieBaseFromModel(model).newKieSession();
    Map results = new HashMap();
    ksession.setGlobal("results", results);
    ksession.insert(new Person("Mark", 42));
    ksession.insert(new Person("Edoardo", 33));
    FactHandle meFH = ksession.insert(new Person("Mario", 45));
    ksession.insert(new Person("Maciej", 39));
    ksession.insert(new Person("Edson", 38));
    FactHandle geoffreyFH = ksession.insert(new Person("Geoffrey", 35));
    ksession.fireAllRules();
    System.out.println("-----");
    /*
         * In the first groupBy:
         *   Mark+Mario become "(Mar, 87)"
         *   Maciej becomes "(Mac, 39)"
         *   Geoffrey becomes "(Geo, 35)"
         *   Edson becomes "(Eds, 38)"
         *   Edoardo becomes "(Edo, 33)"
         *
         * Then in the second groupBy:
         *   "(Mar, 87)" and "(Mac, 39)" become "(Ma, 87)"
         *   "(Eds, 38)" and "(Edo, 33)" become "(Ed, 38)"
         *   "(Geo, 35)" becomes "(Ge, 35)"
         */
    assertEquals(3, results.size());
    assertEquals(87, results.get("Ma"));
    assertEquals(38, results.get("Ed"));
    assertEquals(35, results.get("Ge"));
    results.clear();
    ksession.delete(meFH);
    ksession.fireAllRules();
    System.out.println("-----");
    // No Mario anymore, so "(Mar, 42)" instead of "(Mar, 87)".
    // Therefore "(Ma, 42)".
    assertEquals(1, results.size());
    assertEquals(42, results.get("Ma"));
    results.clear();
    // "(Geo, 35)" is gone.
    // "(Mat, 38)" is added, but Mark still wins, so "(Ma, 42)" stays.
    ksession.delete(geoffreyFH);
    ksession.insert(new Person("Matteo", 38));
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(42, results.get("Ma"));
}
Also used : Arrays(java.util.Arrays) InternalFactHandle(org.drools.core.common.InternalFactHandle) Accumulator(org.drools.core.spi.Accumulator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Global(org.drools.model.Global) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) DSL(org.drools.model.DSL) Child(org.drools.modelcompiler.domain.Child) Match(org.kie.api.runtime.rule.Match) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Declaration(org.drools.core.rule.Declaration) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) Parent(org.drools.modelcompiler.domain.Parent) Set(java.util.Set) Index(org.drools.model.Index) ConsequenceBuilder(org.drools.model.consequences.ConsequenceBuilder) EvaluationUtil(org.drools.modelcompiler.util.EvaluationUtil) Objects(java.util.Objects) List(java.util.List) Tuple(org.drools.core.spi.Tuple) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) Person(org.drools.modelcompiler.domain.Person) DSL.from(org.drools.model.DSL.from) ModelImpl(org.drools.model.impl.ModelImpl) HashMap(java.util.HashMap) PatternDSL(org.drools.model.PatternDSL) ArrayList(java.util.ArrayList) Function1(org.drools.model.functions.Function1) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieBase(org.kie.api.KieBase) LinkedHashSet(java.util.LinkedHashSet) Model(org.drools.model.Model) IntegerMaxAccumulateFunction(org.drools.core.base.accumulators.IntegerMaxAccumulateFunction) ReteEvaluator(org.drools.core.common.ReteEvaluator) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IntegerSumAccumulateFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction) ToIntFunction(java.util.function.ToIntFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) GroupKey(org.drools.model.functions.accumulate.GroupKey) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Rule(org.drools.model.Rule) ExprViewItem(org.drools.model.view.ExprViewItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) Person(org.drools.modelcompiler.domain.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with IntegerSumAccumulateFunction

use of org.drools.core.base.accumulators.IntegerSumAccumulateFunction in project drools by kiegroup.

the class GroupByTest method testTwoGroupBy.

@Test
public void testTwoGroupBy() {
    // DROOLS-5697
    Global<Map> var_results = D.globalOf(Map.class, "defaultpkg", "results");
    Variable<String> var_$key_1 = D.declarationOf(String.class);
    Variable<Person> var_$p = D.declarationOf(Person.class);
    Variable<Integer> var_$age = D.declarationOf(Integer.class);
    Variable<Integer> var_$sumOfAges = D.declarationOf(Integer.class);
    Variable<Group> var_$g1 = D.declarationOf(Group.class, "$g1", D.from(var_$key_1, var_$sumOfAges, ($k, $v) -> new Group($k, $v)));
    Variable<Integer> var_$g1_value = D.declarationOf(Integer.class);
    Variable<String> var_$key_2 = D.declarationOf(String.class);
    Variable<Integer> var_$maxOfValues = D.declarationOf(Integer.class);
    Rule rule1 = D.rule("R1").build(D.groupBy(D.and(D.groupBy(D.pattern(var_$p).bind(var_$age, person -> person.getAge()), var_$p, var_$key_1, person -> person.getName().substring(0, 3), D.accFunction(IntegerSumAccumulateFunction::new, var_$age).as(var_$sumOfAges)), D.pattern(var_$g1).bind(var_$g1_value, group -> (Integer) group.getValue())), var_$g1, var_$key_2, groupResult -> ((String) groupResult.getKey()).substring(0, 2), D.accFunction(IntegerMaxAccumulateFunction::new, var_$g1_value).as(var_$maxOfValues)), D.on(var_$key_2, var_results, var_$maxOfValues).execute(($key, results, $maxOfValues) -> {
        System.out.println($key + " -> " + $maxOfValues);
        results.put($key, $maxOfValues);
    }));
    Model model = new ModelImpl().addRule(rule1).addGlobal(var_results);
    KieSession ksession = KieBaseBuilder.createKieBaseFromModel(model).newKieSession();
    Map results = new HashMap();
    ksession.setGlobal("results", results);
    ksession.insert(new Person("Mark", 42));
    ksession.insert(new Person("Edoardo", 33));
    FactHandle meFH = ksession.insert(new Person("Mario", 45));
    ksession.insert(new Person("Maciej", 39));
    ksession.insert(new Person("Edson", 38));
    FactHandle geoffreyFH = ksession.insert(new Person("Geoffrey", 35));
    ksession.fireAllRules();
    System.out.println("-----");
    /*
         * In the first groupBy:
         *   Mark+Mario become "(Mar, 87)"
         *   Maciej becomes "(Mac, 39)"
         *   Geoffrey becomes "(Geo, 35)"
         *   Edson becomes "(Eds, 38)"
         *   Edoardo becomes "(Edo, 33)"
         *
         * Then in the second groupBy:
         *   "(Mar, 87)" and "(Mac, 39)" become "(Ma, 87)"
         *   "(Eds, 38)" and "(Edo, 33)" become "(Ed, 38)"
         *   "(Geo, 35)" becomes "(Ge, 35)"
         */
    assertEquals(3, results.size());
    assertEquals(87, results.get("Ma"));
    assertEquals(38, results.get("Ed"));
    assertEquals(35, results.get("Ge"));
    results.clear();
    ksession.delete(meFH);
    ksession.fireAllRules();
    System.out.println("-----");
    // No Mario anymore, so "(Mar, 42)" instead of "(Mar, 87)".
    // Therefore "(Ma, 42)".
    assertEquals(1, results.size());
    assertEquals(42, results.get("Ma"));
    results.clear();
    // "(Geo, 35)" is gone.
    // "(Mat, 38)" is added, but Mark still wins, so "(Ma, 42)" stays.
    ksession.delete(geoffreyFH);
    ksession.insert(new Person("Matteo", 38));
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(42, results.get("Ma"));
}
Also used : Arrays(java.util.Arrays) InternalFactHandle(org.drools.core.common.InternalFactHandle) Accumulator(org.drools.core.spi.Accumulator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Global(org.drools.model.Global) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) DSL(org.drools.model.DSL) Child(org.drools.modelcompiler.domain.Child) Match(org.kie.api.runtime.rule.Match) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Declaration(org.drools.core.rule.Declaration) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) Parent(org.drools.modelcompiler.domain.Parent) Set(java.util.Set) Index(org.drools.model.Index) ConsequenceBuilder(org.drools.model.consequences.ConsequenceBuilder) EvaluationUtil(org.drools.modelcompiler.util.EvaluationUtil) Objects(java.util.Objects) List(java.util.List) Tuple(org.drools.core.spi.Tuple) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) Person(org.drools.modelcompiler.domain.Person) DSL.from(org.drools.model.DSL.from) ModelImpl(org.drools.model.impl.ModelImpl) HashMap(java.util.HashMap) PatternDSL(org.drools.model.PatternDSL) ArrayList(java.util.ArrayList) Function1(org.drools.model.functions.Function1) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieBase(org.kie.api.KieBase) LinkedHashSet(java.util.LinkedHashSet) Model(org.drools.model.Model) IntegerMaxAccumulateFunction(org.drools.core.base.accumulators.IntegerMaxAccumulateFunction) ReteEvaluator(org.drools.core.common.ReteEvaluator) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IntegerSumAccumulateFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction) ToIntFunction(java.util.function.ToIntFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) GroupKey(org.drools.model.functions.accumulate.GroupKey) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Rule(org.drools.model.Rule) ExprViewItem(org.drools.model.view.ExprViewItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Map (java.util.Map)4 Objects (java.util.Objects)4 Set (java.util.Set)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ToIntFunction (java.util.function.ToIntFunction)4 Pair (org.apache.commons.math3.util.Pair)4 Assertions (org.assertj.core.api.Assertions)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 CollectListAccumulateFunction (org.drools.core.base.accumulators.CollectListAccumulateFunction)4 CountAccumulateFunction (org.drools.core.base.accumulators.CountAccumulateFunction)4 IntegerMaxAccumulateFunction (org.drools.core.base.accumulators.IntegerMaxAccumulateFunction)4 IntegerSumAccumulateFunction (org.drools.core.base.accumulators.IntegerSumAccumulateFunction)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 ReteEvaluator (org.drools.core.common.ReteEvaluator)4