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