Search in sources :

Example 26 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)

Example 27 with QueryResults

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

the class NaiveBayesTest method testNaiveBayes.

@Test
public void testNaiveBayes() throws Exception {
    KieSession kieSession = getModelSession(source1, VERBOSE);
    setKSession(kieSession);
    // init model
    kieSession.fireAllRules();
    kieSession.getEntryPoint("in_Gender").insert("male");
    kieSession.getEntryPoint("in_NoOfClaims").insert("2");
    kieSession.getEntryPoint("in_AgeOfCar").insert(1.0);
    kieSession.fireAllRules();
    QueryResults q1 = kieSession.getQueryResults("ProbabilityOf500", "NaiveBayesInsurance", Variable.v);
    assertEquals(1, q1.size());
    Object a1 = q1.iterator().next().get("$result");
    assertTrue(a1 instanceof Double);
    assertEquals(0.034, (Double) a1, 4);
    QueryResults q2 = kieSession.getQueryResults("ChosenClass", "NaiveBayesInsurance", Variable.v);
    assertEquals(1, q2.size());
    Object a2 = q2.iterator().next().get("$result");
    assertTrue(a2 instanceof Integer);
    assertEquals(100, a2);
    checkGeneratedRules();
}
Also used : KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 28 with QueryResults

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

the class QueryTest method testPositionalQuery.

@Test
public void testPositionalQuery() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "query findPerson( String $n, int $a )\n" + "    $p : Person($n, $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 29 with QueryResults

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

the class QueryTest method testQueryZeroArgs.

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

Example 30 with QueryResults

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

the class QueryTest method testQueryWithOOPath.

@Test
public void testQueryWithOOPath() {
    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 (Person ( $city: /address#InternationalAddress[state == \"Safecountry\"]/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)

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