Search in sources :

Example 31 with QueryResults

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

the class QueryTest method testQueryWithOOPathTransformedToFromInsideAcc.

@Test
public void testQueryWithOOPathTransformedToFromInsideAcc() {
    String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "query listSafeCities\n" + "$cities : List() from accumulate (" + "    $p : Person() and\n" + "    $a : InternationalAddress(state == \"Safecountry\") from $p.address and\n" + "    $city : String() from $a.city, collectList($city))\n" + "end";
    KieSession ksession = getKieSession(str);
    org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
    person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
    ksession.insert(person2);
    org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
    person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
    ksession.insert(person);
    QueryResults results = ksession.getQueryResults("listSafeCities");
    List cities = (List) results.iterator().next().get("$cities");
    assertEquals(1, cities.size());
    assertEquals("Milan", cities.get(0));
}
Also used : InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) QueryResults(org.kie.api.runtime.rule.QueryResults) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 32 with QueryResults

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

the class QueryTest method testQueryWithOOPathTransformedToFrom.

@Test
public void testQueryWithOOPathTransformedToFrom() {
    String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "query listSafeCities\n" + "$p  : Person()\n" + "$a  : InternationalAddress(state == \"Safecountry\") from $p.address\n" + "$cities : List() from accumulate ($city : String() from $a.city, collectList($city))\n" + "end";
    KieSession ksession = getKieSession(str);
    org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
    person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
    ksession.insert(person);
    org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
    person2.setAddress(new 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 : InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.modelcompiler.domain.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 33 with QueryResults

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

the class QueryTest method testQueryCallingQuery.

@Test
public void testQueryCallingQuery() {
    String str = "import " + Relationship.class.getCanonicalName() + ";" + "query isRelatedTo(String x, String y)\n" + "    isRelatedTo2(x, y;)\n" + "end\n" + "query isRelatedTo2(String x, String y)\n" + "    Relationship(x, y;)\n" + "end";
    KieSession ksession = getKieSession(str);
    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 : QueryImpl(org.drools.core.rule.QueryImpl) Relationship(org.drools.modelcompiler.domain.Relationship) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 34 with QueryResults

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

the class DroolsAbstractPMMLTest method queryDoubleField.

protected double queryDoubleField(String target, String modelName) {
    QueryResults results = getKSession().getQueryResults(target, modelName, Variable.v);
    assertEquals(1, results.size());
    return (Double) results.iterator().next().get("$result");
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults)

Example 35 with QueryResults

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

the class PMMLUsageDemoTest method invokePmmlWithRawData.

@Test
public void invokePmmlWithRawData() {
    KieSession kSession = getModelSession(pmmlSource, false);
    // One entry-point per input field
    // field name "xyz" => entry point name "in_Xyz"
    kSession.getEntryPoint("in_Temp").insert(22.0);
    kSession.fireAllRules();
    // Query results
    // output field name   --> query name
    // model name          --> first arg
    // value               --> second arg ( Variable.v for output, any value for testing )
    QueryResults qrs = kSession.getQueryResults("Cold", "MockCold", Variable.v);
    assertTrue(qrs.iterator().hasNext());
    Object val = qrs.iterator().next().get("$result");
    assertEquals(0.56, val);
    QueryResults qrs2 = kSession.getQueryResults("Cold", "MockCold", 0.56);
    assertTrue(qrs2.iterator().hasNext());
    QueryResults qrs3 = kSession.getQueryResults("Cold", "MockCold", 0.99);
    assertFalse(qrs3.iterator().hasNext());
}
Also used : KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)80 Test (org.junit.Test)67 KieSession (org.kie.api.runtime.KieSession)59 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 Collection (java.util.Collection)9 Assertions (org.assertj.core.api.Assertions)9 Relationship (org.drools.modelcompiler.domain.Relationship)9 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