use of org.drools.core.base.accumulators.CollectListAccumulateFunction in project drools by kiegroup.
the class GroupByTest method testDecomposedGroupByKeyAnd2Accumulates.
@Test
public void testDecomposedGroupByKeyAnd2Accumulates() throws Exception {
// DROOLS-6031
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<Pair> var_$accumulate = D.declarationOf(Pair.class);
final Variable<Person> var_$p = D.declarationOf(Person.class);
final Variable<Person> var_$p2 = D.declarationOf(Person.class);
final Variable<String> var_$subkeyA = D.declarationOf(String.class);
final Variable<String> var_$subkeyB = D.declarationOf(String.class);
final Variable<List> var_$accresult = D.declarationOf(List.class);
final Variable<List> var_$accresult2 = D.declarationOf(List.class);
final Rule rule1 = PatternDSL.rule("R1").build(D.groupBy(// Patterns
D.and(D.pattern(var_$p), D.pattern(var_$p2).bind(var_$accumulate, var_$p, Pair::create)), // Grouping Function
var_$p, var_$key, person -> Pair.create(person.getName().substring(0, 1), person.getName().substring(1, 2)), D.accFunction(CollectListAccumulateFunction::new, var_$accumulate).as(var_$accresult), D.accFunction(CollectListAccumulateFunction::new, var_$accumulate).as(var_$accresult2)), // Bindings
D.pattern(var_$key).bind(var_$subkeyA, Pair::getKey).bind(var_$subkeyB, Pair::getValue), D.pattern(var_$accresult), // 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");
}
use of org.drools.core.base.accumulators.CollectListAccumulateFunction in project drools by kiegroup.
the class GroupByTest method testNestedGroupBy1a.
@Test
public void testNestedGroupBy1a() throws Exception {
// DROOLS-6045
final Global<List> var_results = D.globalOf(List.class, "defaultpkg", "results");
final Variable<Object> var_$key = D.declarationOf(Object.class);
final Variable<Person> var_$p = D.declarationOf(Person.class);
final Variable<Object> var_$accresult = D.declarationOf(Object.class);
final Rule rule1 = PatternDSL.rule("R1").build(D.accumulate(D.and(D.groupBy(// Patterns
D.pattern(var_$p), // Grouping Function
var_$p, var_$key, Person::getAge), // Bindings
D.pattern(var_$key).expr(k -> ((Integer) k) > 0)), D.accFunction(CollectListAccumulateFunction::new, var_$key).as(var_$accresult)), // Consequence
D.on(var_$accresult, var_results).execute(($accresult, results) -> {
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(new Person("Mark", 42));
assertThat(ksession.fireAllRules()).isEqualTo(1);
System.out.println(results);
Assertions.assertThat(results).containsOnly(Collections.singletonList(42));
}
use of org.drools.core.base.accumulators.CollectListAccumulateFunction in project drools by kiegroup.
the class GroupByTest method testNestedGroupBy2.
@Test
public void testNestedGroupBy2() throws Exception {
// DROOLS-6045
final Global<List> var_results = D.globalOf(List.class, "defaultpkg", "results");
final Variable<Object> var_$key = D.declarationOf(Object.class);
final Variable<Object> var_$keyOuter = D.declarationOf(Object.class);
final Variable<Person> var_$p = D.declarationOf(Person.class);
final Variable<Object> var_$accresult = D.declarationOf(Object.class);
final Rule rule1 = PatternDSL.rule("R1").build(D.groupBy(D.and(D.groupBy(// Patterns
D.pattern(var_$p), // Grouping Function
var_$p, var_$key, Person::getAge), // Bindings
D.pattern(var_$key).expr(k -> ((Integer) k) > 0)), var_$key, var_$keyOuter, k -> ((Integer) k) * 2, D.accFunction(CollectListAccumulateFunction::new, var_$keyOuter).as(var_$accresult)), // Consequence
D.on(var_$keyOuter, var_$accresult, var_results).execute(($outerKey, $accresult, results) -> {
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);
}
use of org.drools.core.base.accumulators.CollectListAccumulateFunction in project drools by kiegroup.
the class GroupByTest method testNestedGroupBy1b.
@Test
public void testNestedGroupBy1b() throws Exception {
// DROOLS-6045
final Global<List> var_results = D.globalOf(List.class, "defaultpkg", "results");
final Variable<Object> var_$key = D.declarationOf(Object.class);
final Variable<Person> var_$p = D.declarationOf(Person.class);
final Variable<Object> var_$accresult = D.declarationOf(Object.class);
final Rule rule1 = PatternDSL.rule("R1").build(D.accumulate(D.and(D.groupBy(// Patterns
D.pattern(var_$p), // Grouping Function
var_$p, var_$key, Person::getAge), // Bindings
D.pattern(var_$key)), D.accFunction(CollectListAccumulateFunction::new, var_$key).as(var_$accresult)), // Consequence
D.on(var_$accresult, var_results).execute(($accresult, results) -> {
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(new Person("Mark", 42));
assertThat(ksession.fireAllRules()).isEqualTo(1);
Assertions.assertThat(results).containsOnly(Collections.singletonList(42));
}
use of org.drools.core.base.accumulators.CollectListAccumulateFunction in project drools by kiegroup.
the class GroupByTest method testDecomposedGroupByKeyAnd2AccumulatesInConsequence.
@Test
public void testDecomposedGroupByKeyAnd2AccumulatesInConsequence() throws Exception {
// DROOLS-6031
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<Pair> var_$accumulate = D.declarationOf(Pair.class);
final Variable<Person> var_$p = D.declarationOf(Person.class);
final Variable<Person> var_$p2 = D.declarationOf(Person.class);
final Variable<String> var_$subkeyA = D.declarationOf(String.class);
final Variable<String> var_$subkeyB = D.declarationOf(String.class);
final Variable<List> var_$accresult = D.declarationOf(List.class);
final Variable<List> var_$accresult2 = D.declarationOf(List.class);
final Rule rule1 = PatternDSL.rule("R1").build(D.groupBy(// Patterns
D.and(D.pattern(var_$p), D.pattern(var_$p2).bind(var_$accumulate, var_$p, Pair::create)), // Grouping Function
var_$p, var_$key, person -> Pair.create(person.getName().substring(0, 1), person.getName().substring(1, 2)), D.accFunction(CollectListAccumulateFunction::new, var_$accumulate).as(var_$accresult), D.accFunction(CollectListAccumulateFunction::new, var_$accumulate).as(var_$accresult2)), // Bindings
D.pattern(var_$accresult2), // Consequence
D.on(var_$key, var_$accresult, var_$accresult2, var_results).execute(($key, $accresult, $accresult2, results) -> {
results.add($key);
}));
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(1);
}
Aggregations