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());
}
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();
}
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());
}
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());
}
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));
}
Aggregations