use of org.apache.commons.math3.util.Pair in project drools by kiegroup.
the class AccumulateTest method testTwoAccumulatesOnPattern.
@Test
public void testTwoAccumulatesOnPattern() {
// DROOLS-5938
String str = "import " + Person.class.getCanonicalName() + ";\n" + "import " + Pair.class.getCanonicalName() + ";\n" + "import " + Collection.class.getCanonicalName() + ";\n" + "global java.util.List result;\n" + "rule R when\n" + " $acc1 : Collection() from accumulate( \n" + " Person( $pair1 : Pair.create(name, age) ), collectSet( $pair1 ) ) \n" + " $acc2 : Collection() from accumulate( \n" + " $pair2 : Pair( ) from $acc1, collectSet( $pair2 ) )\n" + "then\n" + " result.add($acc2.iterator().next());" + "end";
KieSession ksession = getKieSession(str);
List<Pair> result = new ArrayList<>();
ksession.setGlobal("result", result);
ksession.insert(new Person("Lukas", 35));
ksession.fireAllRules();
assertEquals(1, result.size());
assertEquals(Pair.create("Lukas", 35), result.get(0));
}
use of org.apache.commons.math3.util.Pair in project drools by kiegroup.
the class GroupByTest method testDecomposedGroupByKey.
@Test
public void testDecomposedGroupByKey() 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<Person> var_$p = D.declarationOf(Person.class);
final Variable<String> var_$subkeyA = D.declarationOf(String.class);
final Variable<String> var_$subkeyB = D.declarationOf(String.class);
final Rule rule1 = PatternDSL.rule("R1").build(D.groupBy(// Patterns
PatternDSL.pattern(var_$p), // Grouping Function
var_$p, var_$key, person -> Pair.create(person.getName().substring(0, 1), person.getName().substring(1, 2))), // Bindings
D.pattern(var_$key).bind(var_$subkeyA, Pair::getKey).bind(var_$subkeyB, Pair::getValue), // Consequence
D.on(var_$subkeyA, var_$subkeyB, var_results).execute(($a, $b, results) -> {
results.add($a);
results.add($b);
}));
final Model model = new ModelImpl().addRule(rule1).addGlobal(var_results);
final KieSession ksession = KieBaseBuilder.createKieBaseFromModel(model).newKieSession();
final List<String> 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(2);
Assertions.assertThat(results.get(0)).isEqualTo("M");
Assertions.assertThat(results.get(1)).isEqualTo("a");
}
use of org.apache.commons.math3.util.Pair 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.apache.commons.math3.util.Pair in project drools by kiegroup.
the class PatternDSLTest method testTwoAccumulatesWithVarBindingModel.
@Test
public void testTwoAccumulatesWithVarBindingModel() {
Variable<Person> a = PatternDSL.declarationOf(Person.class);
Variable<Pair> accSource = PatternDSL.declarationOf(Pair.class);
Variable<Collection> accResult = PatternDSL.declarationOf(Collection.class);
Variable<Collection> accResult2 = PatternDSL.declarationOf(Collection.class);
Variable<Pair> wrapped = PatternDSL.declarationOf(Pair.class, PatternDSL.from(accResult));
Variable<Object> unwrapped1 = PatternDSL.declarationOf(Object.class);
PatternDSL.PatternDef aPattern = PatternDSL.pattern(a).bind(accSource, v -> Pair.create(v.getName(), v.getAge()));
ViewItem accumulate = PatternDSL.accumulate(aPattern, DSL.accFunction(CollectSetAccumulateFunction::new, accSource).as(accResult));
PatternDSL.PatternDef secondPattern = PatternDSL.pattern(accResult);
PatternDSL.PatternDef thirdPattern = // If binding removed, test will pass.
PatternDSL.pattern(wrapped).bind(unwrapped1, Pair::getKey);
ViewItem accumulate2 = PatternDSL.accumulate(PatternDSL.and(secondPattern, thirdPattern), DSL.accFunction(CollectSetAccumulateFunction::new, wrapped).as(accResult2));
Rule rule = PatternDSL.rule("R").build(accumulate, accumulate2, PatternDSL.on(accResult2).execute(obj -> {
boolean works = obj.contains(Pair.create("Lukas", 35));
if (!works) {
throw new IllegalStateException("Why is " + obj + " not Set<" + Pair.class + ">?");
}
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession session = kieBase.newKieSession();
session.insert(new Person("Lukas", 35));
session.fireAllRules();
}
use of org.apache.commons.math3.util.Pair in project bugzy by cpunq.
the class CaseEditActivity method openImageActivity.
public void openImageActivity(View v, String filePath) {
Pair<View, String> bug = new Pair<>(v, "image");
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, bug);
Intent intent = new Intent(this, FullScreenImageActivity.class);
Bundle arg = new Bundle();
arg.putString("img_path", filePath);
intent.putExtras(arg);
ActivityCompat.startActivity(this, intent, options.toBundle());
}
Aggregations