Search in sources :

Example 46 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class PatternDSLTest method testQueryInvokingQuery2.

@Test
public void testQueryInvokingQuery2() {
    final org.drools.model.Query2Def<java.lang.String, java.lang.String> queryDef_isRelatedTo2 = query("isRelatedTo2", java.lang.String.class, "x", java.lang.String.class, "y");
    final org.drools.model.Query2Def<java.lang.String, java.lang.String> queryDef_isRelatedTo = query("isRelatedTo", java.lang.String.class, "x", java.lang.String.class, "y");
    org.drools.model.Query isRelatedTo_build = queryDef_isRelatedTo.build(queryDef_isRelatedTo2.call(true, queryDef_isRelatedTo.getArg1(), queryDef_isRelatedTo.getArg2()));
    final org.drools.model.Variable<org.drools.modelcompiler.domain.Relationship> var_$pattern_Relationship$4$ = declarationOf(org.drools.modelcompiler.domain.Relationship.class, "$pattern_Relationship$4$");
    org.drools.model.Query isRelatedTo2_build = queryDef_isRelatedTo2.build(pattern(var_$pattern_Relationship$4$).expr("$expr$63$", queryDef_isRelatedTo2.getArg1(), (_this, x) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getStart(), x), betaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getStart(), x -> x), reactOn("start")).expr("$expr$64$", queryDef_isRelatedTo2.getArg2(), (_this, y) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getEnd(), y), betaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 1, _this -> _this.getEnd(), y -> y), reactOn("end")));
    Model model = new ModelImpl().addQuery(isRelatedTo_build).addQuery(isRelatedTo2_build);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    ksession.insert(new Relationship("A", "B"));
    ksession.insert(new Relationship("B", "C"));
    QueryResults results = ksession.getQueryResults("isRelatedTo", "A", "B");
    assertEquals(1, results.size());
    String paramName = ((QueryImpl) ksession.getKieBase().getQuery("defaultpkg", "isRelatedTo")).getParameters()[1].getIdentifier();
    assertEquals("B", results.iterator().next().get(paramName));
}
Also used : 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) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) 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) PatternDSL.rule(org.drools.model.PatternDSL.rule) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Index(org.drools.model.Index) 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) 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) BitMask(org.drools.model.BitMask) PatternDSL.execute(org.drools.model.PatternDSL.execute) StockTick(org.drools.modelcompiler.domain.StockTick) 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) 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) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PatternDSL.and(org.drools.model.PatternDSL.and) Variable(org.drools.model.Variable) 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) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) Assert.assertEquals(org.junit.Assert.assertEquals) QueryResults(org.kie.api.runtime.rule.QueryResults) Query(org.drools.model.Query) QueryImpl(org.drools.core.rule.QueryImpl) KieBase(org.kie.api.KieBase) Relationship(org.drools.modelcompiler.domain.Relationship) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) Test(org.junit.Test)

Example 47 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class QueryTest method testNonPositionalQuery.

@Test
public void testNonPositionalQuery() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "query findPerson( String $n, int $a )\n" + "    $p : Person(name == $n, age == $a)\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    QueryResults results = ksession.getQueryResults("findPerson", "Mario", 41);
    assertEquals(1, results.size());
    Person p = (Person) results.iterator().next().get("$p");
    assertEquals("Mario", p.getName());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 48 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class QueryTest method testQueryOneArgument.

@Test
public void testQueryOneArgument() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "query olderThan( int $age )\n" + "    $p : Person(age > $age)\n" + "end ";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    QueryResults results = ksession.getQueryResults("olderThan", 40);
    assertEquals(1, results.size());
    Person p = (Person) results.iterator().next().get("$p");
    assertEquals("Mario", p.getName());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 49 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class QueryTest method testPositionalRecursiveQueryWithUnification.

@Test
public void testPositionalRecursiveQueryWithUnification() {
    String str = "import " + Relationship.class.getCanonicalName() + ";" + "query isRelatedTo(String x, String y)\n" + "    Relationship (x, y;)\n" + "    or\n" + "    ( Relationship (z, y;) and ?isRelatedTo(x, z;))\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Relationship("A", "B"));
    ksession.insert(new Relationship("B", "C"));
    QueryResults results = ksession.getQueryResults("isRelatedTo", "A", "C");
    assertEquals(1, results.size());
    final QueryResultsRow firstResult = results.iterator().next();
    Object resultDrlx = firstResult.get("z");
    assertTrue("B".equals(resultDrlx));
}
Also used : QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) Relationship(org.drools.modelcompiler.domain.Relationship) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 50 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class QueryTest method testQueryCalling2Queries.

@Test
public void testQueryCalling2Queries() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "query isPersonOlderThan(Person p, int ageFrom)\n" + "    Person(this == p, age > ageFrom)\n" + "end\n" + "\n" + "query isPersonYoungerThan(Person p, int ageTo)\n" + "    Person(this == p, age < ageTo)\n" + "end\n" + "query getPersonsBetween(int ageFrom, int ageTo) \n" + "    p : Person()\n" + "    isPersonOlderThan(p, ageFrom;) and isPersonYoungerThan(p, ageTo;)\n" + "end\n" + "\n";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    QueryResults results = ksession.getQueryResults("getPersonsBetween", 40, 50);
    assertEquals(1, results.size());
    Person p = (Person) results.iterator().next().get("p");
    assertEquals("Mario", p.getName());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)79 Test (org.junit.Test)67 KieSession (org.kie.api.runtime.KieSession)58 ArrayList (java.util.ArrayList)30 KieBase (org.kie.api.KieBase)25 List (java.util.List)24 Person (org.drools.modelcompiler.domain.Person)19 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)18 FactHandle (org.kie.api.runtime.rule.FactHandle)15 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)10 Assertions (org.assertj.core.api.Assertions)9 Relationship (org.drools.modelcompiler.domain.Relationship)9 Collection (java.util.Collection)8 Map (java.util.Map)8 TimeUnit (java.util.concurrent.TimeUnit)7 ClockType (org.drools.core.ClockType)7 InternationalAddress (org.drools.modelcompiler.oopathdtables.InternationalAddress)6 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)6 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)6 Global (org.drools.model.Global)5