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));
}
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));
}
Aggregations