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