Search in sources :

Example 1 with PatternDSL.accumulate

use of org.drools.model.PatternDSL.accumulate 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)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Pair (org.apache.commons.math3.util.Pair)1 Assertions (org.assertj.core.api.Assertions)1 ClockType (org.drools.core.ClockType)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 CollectSetAccumulateFunction (org.drools.core.base.accumulators.CollectSetAccumulateFunction)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 Accumulate (org.drools.core.rule.Accumulate)1 Pattern (org.drools.core.rule.Pattern)1 QueryImpl (org.drools.core.rule.QueryImpl)1 Activation (org.drools.core.spi.Activation)1 DSL (org.drools.model.DSL)1 DSL.supply (org.drools.model.DSL.supply)1 Global (org.drools.model.Global)1