use of org.drools.core.base.accumulators.IntegerMaxAccumulateFunction 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.IntegerMaxAccumulateFunction 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.IntegerMaxAccumulateFunction 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