Search in sources :

Example 1 with Parent

use of org.drools.modelcompiler.domain.Parent in project drools by kiegroup.

the class AccumulateTest method testGroupBy3WithExists2.

@Test
public void testGroupBy3WithExists2() {
    String str = "import java.util.*;\n" + "import " + GroupKey.class.getCanonicalName() + ";\n" + "import " + Parent.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "global List results;\n" + "rule R1 when\n" + "    $p : Parent()\n" + "    exists( Child( this == $p.child) )\n " + "    not( GroupKey(topic ==\"a\", key == $p.getChild() ) )\n" + "then\n" + "    insert( new GroupKey( \"a\", $p.getChild() ) );\n" + "end\n" + "\n" + "rule R2 when\n" + "    $group: GroupKey( topic ==\"a\", $key : key )\n" + "    not( $p : Parent() and exists( Child( this == $key ) ) )\n" + "then\n" + "    delete( $group );\n" + "end\n" + "\n" + "rule R3 when\n" + "    GroupKey( topic ==\"a\", $k : key )\n" + "    accumulate (\n" + "            $p : Parent() and exists( Child( this == $p.getChild()) );\n" + "            $count : count($p)\n" + "         )\n" + "then\n" + "    results.add(java.util.Arrays.asList($k, $count));\n" + "end";
    KieSession ksession = getKieSession(str);
    List results = new ArrayList();
    ksession.setGlobal("results", results);
    Child child1 = new Child("Child1", 1);
    Parent parent1 = new Parent("Parent1", child1);
    Child child2 = new Child("Child2", 2);
    Parent parent2 = new Parent("Parent2", child2);
    ksession.insert(parent1);
    ksession.insert(parent2);
    FactHandle toRemove = ksession.insert(child1);
    ksession.insert(child2);
    ksession.fireAllRules();
    Assertions.assertThat(results).containsOnly(Arrays.asList(child1, 2L), Arrays.asList(child2, 2L));
    // Remove child1, therefore it does not exist, therefore there should be no groupBy matches for the child.
    results.clear();
    ksession.delete(toRemove);
    // Yet, we still get (Child2, 0).
    ksession.fireAllRules();
    Assertions.assertThat(results).containsOnly(Arrays.asList(child2, 1L));
}
Also used : Parent(org.drools.modelcompiler.domain.Parent) FactHandle(org.kie.api.runtime.rule.FactHandle) GroupKey(org.drools.model.functions.accumulate.GroupKey) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Child(org.drools.modelcompiler.domain.Child) Test(org.junit.Test)

Example 2 with Parent

use of org.drools.modelcompiler.domain.Parent in project drools by kiegroup.

the class GroupByTest method testUnexpectedRuleMatch.

@Test
public void testUnexpectedRuleMatch() {
    final Global<List> var_results = D.globalOf(List.class, "defaultpkg", "results");
    // $a: Parent()
    Variable<Parent> patternVar = D.declarationOf(Parent.class);
    PatternDSL.PatternDef<Parent> pattern = D.pattern(patternVar);
    // exists Child($a.getChild() == this)
    Variable<Child> existsPatternVar = D.declarationOf(Child.class);
    PatternDSL.PatternDef<Child> existsPattern = D.pattern(existsPatternVar).expr(patternVar, (child, parent) -> Objects.equals(parent.getChild(), child));
    // count(Parent::getChild)
    Variable<Child> groupKeyVar = D.declarationOf(Child.class);
    Variable<Long> accumulateResult = D.declarationOf(Long.class);
    ExprViewItem groupBy = PatternDSL.groupBy(D.and(pattern, D.exists(existsPattern)), patternVar, groupKeyVar, Parent::getChild, DSL.accFunction(CountAccumulateFunction::new).as(accumulateResult));
    Rule rule1 = D.rule("R1").build(groupBy, D.on(var_results, groupKeyVar, accumulateResult).execute((results, $child, $count) -> results.add(Arrays.asList($child, $count))));
    Model model = new ModelImpl().addRule(rule1).addGlobal(var_results);
    KieSession ksession = KieBaseBuilder.createKieBaseFromModel(model).newKieSession();
    List results = new ArrayList();
    ksession.setGlobal("results", results);
    Child child1 = new Child("Child1", 1);
    Parent parent1 = new Parent("Parent1", child1);
    Child child2 = new Child("Child2", 2);
    Parent parent2 = new Parent("Parent2", child2);
    ksession.insert(parent1);
    ksession.insert(parent2);
    FactHandle toRemove = ksession.insert(child1);
    ksession.insert(child2);
    // Remove child1, therefore it does not exist, therefore there should be no groupBy matches for the child.
    ksession.delete(toRemove);
    // Yet, we still get (Child1, 0).
    ksession.fireAllRules();
    Assertions.assertThat(results).containsOnly(Arrays.asList(child2, 1L));
}
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) ExprViewItem(org.drools.model.view.ExprViewItem) PatternDSL(org.drools.model.PatternDSL) Parent(org.drools.modelcompiler.domain.Parent) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) 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) Child(org.drools.modelcompiler.domain.Child) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 GroupKey (org.drools.model.functions.accumulate.GroupKey)2 Child (org.drools.modelcompiler.domain.Child)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ToIntFunction (java.util.function.ToIntFunction)1 Pair (org.apache.commons.math3.util.Pair)1 Assertions (org.assertj.core.api.Assertions)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 CollectListAccumulateFunction (org.drools.core.base.accumulators.CollectListAccumulateFunction)1 CountAccumulateFunction (org.drools.core.base.accumulators.CountAccumulateFunction)1 IntegerMaxAccumulateFunction (org.drools.core.base.accumulators.IntegerMaxAccumulateFunction)1 IntegerSumAccumulateFunction (org.drools.core.base.accumulators.IntegerSumAccumulateFunction)1