Search in sources :

Example 1 with QueryDef

use of org.drools.model.QueryDef in project drools by kiegroup.

the class OOPathFlowTest method testQueryOOPathAccumulate.

@Test
public void testQueryOOPathAccumulate() {
    QueryDef queryDef_listSafeCities = query("listSafeCities");
    Variable<java.util.List> var_$cities = declarationOf(java.util.List.class, "$cities");
    Variable<org.drools.modelcompiler.oopathdtables.Person> var_$p = declarationOf(org.drools.modelcompiler.oopathdtables.Person.class, "$p");
    Variable<org.drools.modelcompiler.oopathdtables.InternationalAddress> var_$a = declarationOf(org.drools.modelcompiler.oopathdtables.InternationalAddress.class, "$a", from(var_$p, (_this) -> _this.getAddress()));
    Variable<String> var_$city = declarationOf(String.class, "$city", from(var_$a, (_this) -> _this.getCity()));
    org.drools.model.Query listSafeCities_build = queryDef_listSafeCities.build(accumulate(and(input(var_$p), expr("$expr$2$", var_$a, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getState(), "Safecountry")).indexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getState(), "Safecountry").reactOn("state"), expr(var_$city)), accFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction.class, var_$city).as(var_$cities)));
    Model model = new ModelImpl().addQuery(listSafeCities_build);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
    person.setAddress(new org.drools.modelcompiler.oopathdtables.InternationalAddress("", 1, "Milan", "Safecountry"));
    ksession.insert(person);
    org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
    person2.setAddress(new org.drools.modelcompiler.oopathdtables.InternationalAddress("", 1, "Rome", "Unsafecountry"));
    ksession.insert(person2);
    QueryResults results = ksession.getQueryResults("listSafeCities");
    List cities = (List) results.iterator().next().get("$cities");
    assertEquals(1, cities.size());
    assertEquals("Milan", cities.get(0));
}
Also used : FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) QueryDef(org.drools.model.QueryDef) Child(org.drools.modelcompiler.domain.Child) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) Variable(org.drools.model.Variable) FlowDSL.query(org.drools.model.FlowDSL.query) FlowDSL.on(org.drools.model.FlowDSL.on) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) List(java.util.List) Rule(org.drools.model.Rule) Assert.assertEquals(org.junit.Assert.assertEquals) ModelImpl(org.drools.model.impl.ModelImpl) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) QueryDef(org.drools.model.QueryDef) QueryResults(org.kie.api.runtime.rule.QueryResults) Model(org.drools.model.Model) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Assertions (org.assertj.core.api.Assertions)1 FlowDSL.accFunction (org.drools.model.FlowDSL.accFunction)1 FlowDSL.accumulate (org.drools.model.FlowDSL.accumulate)1 FlowDSL.and (org.drools.model.FlowDSL.and)1 FlowDSL.declarationOf (org.drools.model.FlowDSL.declarationOf)1 FlowDSL.expr (org.drools.model.FlowDSL.expr)1 FlowDSL.from (org.drools.model.FlowDSL.from)1 FlowDSL.globalOf (org.drools.model.FlowDSL.globalOf)1 FlowDSL.input (org.drools.model.FlowDSL.input)1 FlowDSL.on (org.drools.model.FlowDSL.on)1 FlowDSL.query (org.drools.model.FlowDSL.query)1 FlowDSL.reactiveFrom (org.drools.model.FlowDSL.reactiveFrom)1 FlowDSL.rule (org.drools.model.FlowDSL.rule)1 Global (org.drools.model.Global)1 Model (org.drools.model.Model)1 QueryDef (org.drools.model.QueryDef)1 Rule (org.drools.model.Rule)1 Variable (org.drools.model.Variable)1