Search in sources :

Example 81 with Pair

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));
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Pair(org.apache.commons.math3.util.Pair) Test(org.junit.Test)

Example 82 with Pair

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");
}
Also used : Arrays(java.util.Arrays) InternalFactHandle(org.drools.core.common.InternalFactHandle) Accumulator(org.drools.core.spi.Accumulator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Global(org.drools.model.Global) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) DSL(org.drools.model.DSL) Child(org.drools.modelcompiler.domain.Child) Match(org.kie.api.runtime.rule.Match) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Declaration(org.drools.core.rule.Declaration) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) Parent(org.drools.modelcompiler.domain.Parent) Set(java.util.Set) Index(org.drools.model.Index) ConsequenceBuilder(org.drools.model.consequences.ConsequenceBuilder) EvaluationUtil(org.drools.modelcompiler.util.EvaluationUtil) Objects(java.util.Objects) List(java.util.List) Tuple(org.drools.core.spi.Tuple) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) Person(org.drools.modelcompiler.domain.Person) DSL.from(org.drools.model.DSL.from) ModelImpl(org.drools.model.impl.ModelImpl) HashMap(java.util.HashMap) PatternDSL(org.drools.model.PatternDSL) ArrayList(java.util.ArrayList) Function1(org.drools.model.functions.Function1) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieBase(org.kie.api.KieBase) LinkedHashSet(java.util.LinkedHashSet) Model(org.drools.model.Model) IntegerMaxAccumulateFunction(org.drools.core.base.accumulators.IntegerMaxAccumulateFunction) ReteEvaluator(org.drools.core.common.ReteEvaluator) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IntegerSumAccumulateFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction) ToIntFunction(java.util.function.ToIntFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) GroupKey(org.drools.model.functions.accumulate.GroupKey) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Rule(org.drools.model.Rule) ExprViewItem(org.drools.model.view.ExprViewItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Variable(org.drools.model.Variable) ArrayList(java.util.ArrayList) Model(org.drools.model.Model) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Pair(org.apache.commons.math3.util.Pair) Test(org.junit.Test)

Example 83 with Pair

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);
}
Also used : Arrays(java.util.Arrays) InternalFactHandle(org.drools.core.common.InternalFactHandle) Accumulator(org.drools.core.spi.Accumulator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Global(org.drools.model.Global) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) DSL(org.drools.model.DSL) Child(org.drools.modelcompiler.domain.Child) Match(org.kie.api.runtime.rule.Match) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Declaration(org.drools.core.rule.Declaration) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) Parent(org.drools.modelcompiler.domain.Parent) Set(java.util.Set) Index(org.drools.model.Index) ConsequenceBuilder(org.drools.model.consequences.ConsequenceBuilder) EvaluationUtil(org.drools.modelcompiler.util.EvaluationUtil) Objects(java.util.Objects) List(java.util.List) Tuple(org.drools.core.spi.Tuple) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) Person(org.drools.modelcompiler.domain.Person) DSL.from(org.drools.model.DSL.from) ModelImpl(org.drools.model.impl.ModelImpl) HashMap(java.util.HashMap) PatternDSL(org.drools.model.PatternDSL) ArrayList(java.util.ArrayList) Function1(org.drools.model.functions.Function1) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieBase(org.kie.api.KieBase) LinkedHashSet(java.util.LinkedHashSet) Model(org.drools.model.Model) IntegerMaxAccumulateFunction(org.drools.core.base.accumulators.IntegerMaxAccumulateFunction) ReteEvaluator(org.drools.core.common.ReteEvaluator) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IntegerSumAccumulateFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction) ToIntFunction(java.util.function.ToIntFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) GroupKey(org.drools.model.functions.accumulate.GroupKey) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Rule(org.drools.model.Rule) ExprViewItem(org.drools.model.view.ExprViewItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Variable(org.drools.model.Variable) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Objects(java.util.Objects) Model(org.drools.model.Model) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Pair(org.apache.commons.math3.util.Pair) Test(org.junit.Test)

Example 84 with Pair

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();
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL(org.drools.model.DSL) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Query2Def(org.drools.model.Query2Def) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) QueryImpl(org.drools.core.rule.QueryImpl) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) PatternDSL.rule(org.drools.model.PatternDSL.rule) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Activation(org.drools.core.spi.Activation) Index(org.drools.model.Index) Predicate1(org.drools.model.functions.Predicate1) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) List(java.util.List) Query(org.drools.model.Query) PatternDSL.or(org.drools.model.PatternDSL.or) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PatternDSL.globalOf(org.drools.model.PatternDSL.globalOf) Person(org.drools.modelcompiler.domain.Person) PatternDSL.valueOf(org.drools.model.PatternDSL.valueOf) ModelImpl(org.drools.model.impl.ModelImpl) ClassObjectType(org.drools.core.base.ClassObjectType) PatternDSL.execute(org.drools.model.PatternDSL.execute) StockTick(org.drools.modelcompiler.domain.StockTick) Pattern(org.drools.core.rule.Pattern) PatternDSL(org.drools.model.PatternDSL) PatternDSL.after(org.drools.model.PatternDSL.after) ClockType(org.drools.core.ClockType) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Assert.assertSame(org.junit.Assert.assertSame) Adult(org.drools.modelcompiler.domain.Adult) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) PatternDSL.reactiveFrom(org.drools.model.PatternDSL.reactiveFrom) KieServices(org.kie.api.KieServices) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PatternDSL.and(org.drools.model.PatternDSL.and) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IOException(java.io.IOException) Test(org.junit.Test) PatternDSL.accumulate(org.drools.model.PatternDSL.accumulate) PatternDSL.not(org.drools.model.PatternDSL.not) PatternDSL.when(org.drools.model.PatternDSL.when) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) PatternDSL.accFunction(org.drools.model.PatternDSL.accFunction) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.drools.model.Rule) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) Accumulate(org.drools.core.rule.Accumulate) DSL.supply(org.drools.model.DSL.supply) Assert.assertEquals(org.junit.Assert.assertEquals) PatternDSL(org.drools.model.PatternDSL) ViewItem(org.drools.model.view.ViewItem) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Pair(org.apache.commons.math3.util.Pair) Test(org.junit.Test)

Example 85 with Pair

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());
}
Also used : Bundle(android.os.Bundle) Intent(android.content.Intent) BindView(butterknife.BindView) NachoTextView(com.hootsuite.nachos.NachoTextView) View(android.view.View) AdapterView(android.widget.AdapterView) ImagePickerSheetView(com.flipboard.bottomsheet.commons.ImagePickerSheetView) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) ActivityOptionsCompat(android.support.v4.app.ActivityOptionsCompat) Pair(android.support.v4.util.Pair)

Aggregations

Pair (android.support.v4.util.Pair)79 ArrayList (java.util.ArrayList)56 Pair (org.apache.commons.math3.util.Pair)38 List (java.util.List)30 View (android.view.View)28 Collectors (java.util.stream.Collectors)20 ActivityOptionsCompat (android.support.v4.app.ActivityOptionsCompat)19 Intent (android.content.Intent)18 Arrays (java.util.Arrays)17 Map (java.util.Map)17 IntStream (java.util.stream.IntStream)17 MaxEval (org.apache.commons.math3.optim.MaxEval)17 HashMap (java.util.HashMap)16 TextView (android.widget.TextView)15 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)15 InitialGuess (org.apache.commons.math3.optim.InitialGuess)14 PointValuePair (org.apache.commons.math3.optim.PointValuePair)14 MultivariateOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer)14 java.util (java.util)13 Collections (java.util.Collections)13