use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryTest method testQueriesWithVariableUnificationOnNestedFields.
@Test
public void testQueriesWithVariableUnificationOnNestedFields() throws Exception {
String str = "";
str += "package org.drools.compiler.test \n";
str += "import org.drools.compiler.Person \n";
str += "query peeps( String $name, String $likes, String $street) \n";
str += " $p : Person( $name := name, $likes := likes, $street := address.street ) \n";
str += "end\n";
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
KieSession ksession = createKieSession(kbase);
Person p1 = new Person("darth", "stilton", 100);
p1.setAddress(new Address("s1"));
Person p2 = new Person("yoda", "stilton", 300);
p2.setAddress(new Address("s2"));
ksession.insert(p1);
ksession.insert(p2);
QueryResults results = getQueryResults(ksession, "peeps", new Object[] { Variable.v, Variable.v, Variable.v });
assertEquals(2, results.size());
List names = new ArrayList();
for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
names.add(((Person) row.get("$p")).getName());
}
assertTrue(names.contains("yoda"));
assertTrue(names.contains("darth"));
results = getQueryResults(ksession, "peeps", new Object[] { Variable.v, Variable.v, "s1" });
assertEquals(1, results.size());
names = new ArrayList();
for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
names.add(((Person) row.get("$p")).getName());
}
assertTrue(names.contains("darth"));
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryTest method testTwoQuerries.
@Test
public void testTwoQuerries() throws Exception {
// @see JBRULES-410 More than one Query definition causes an incorrect
// Rete network to be built.
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_TwoQuerries.drl"));
KieSession session = createKieSession(kbase);
final Cheese stilton = new Cheese("stinky", 5);
session.insert(stilton);
final Person per1 = new Person("stinker", "smelly feet", 70);
final Person per2 = new Person("skunky", "smelly armpits", 40);
session.insert(per1);
session.insert(per2);
QueryResults results = getQueryResults(session, "find stinky cheeses");
assertEquals(1, results.size());
results = getQueryResults(session, "find pensioners");
assertEquals(1, results.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class AbductionTest method testQueryAPIs.
@Test
public void testQueryAPIs() {
String droolsSource = "package org.drools.abductive.test; \n" + "import " + Abducible.class.getName() + "; \n" + "" + "declare Foo " + " @Abducible " + " id : String " + "end " + "query foo( String $s ) " + " @Abductive( target=Foo.class ) \n" + "end \n " + "query bar( String $s, Foo $foo ) " + " $foo := Foo() " + "end \n " + "rule MoveAround " + "when " + " $s : String() " + " $f : foo( $s ; ) " + " bar( $s, $f ; ) " + "then " + " delete( $s ); " + " System.out.println( 'Foo ' + $f ); " + "end " + "";
// ///////////////////////////////////
KieSession session = getSessionFromString(droolsSource);
session.insert("faa");
session.fireAllRules();
for (Object o : session.getObjects()) {
System.out.println(">>> " + o);
}
assertEquals(1, session.getObjects().size());
Query q1 = session.getKieBase().getQuery("org.drools.abductive.test", "foo");
Query q2 = session.getKieBase().getQuery("org.drools.abductive.test", "bar");
assertNotNull(q1);
assertNotNull(q2);
QueryResults q10res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("foo", "foo"));
QueryResults q11res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("foo", "foo", Variable.v));
QueryResults q20res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("bar", "foo", Variable.v));
assertEquals(1, q10res.size());
assertEquals(1, q11res.size());
assertEquals(1, q20res.size());
QueryResultsRow row10 = q10res.iterator().next();
QueryResultsRow row11 = q11res.iterator().next();
QueryResultsRow row20 = q20res.iterator().next();
assertEquals("foo", row10.get("$s"));
assertEquals("foo", row11.get("$s"));
assertEquals("foo", row20.get("$s"));
Object foo = row20.get("$foo");
assertSame(foo, session.getObjects().iterator().next());
// the implicit return argument, the abduced/retrieved fact, is hidden
assertNull(row11.get(""));
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class MoreBatchExecutionTest method testQuery.
@Test
public void testQuery() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/simple_query_test.drl"), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ksession = createKnowledgeSession(kbase);
ksession.insert(new Cheese("stinky", 5));
ksession.insert(new Cheese("smelly", 7));
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newQuery("numStinkyCheeses", "simple query"));
Command<?> cmds = CommandFactory.newBatchExecution(commands);
ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
assertNotNull("Batch execution result is null!", result);
Object queryResultsObject = result.getValue("numStinkyCheeses");
assertTrue("Retrieved object is null or incorrect!", queryResultsObject != null && queryResultsObject instanceof QueryResults);
assertEquals(1, ((QueryResults) queryResultsObject).size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryCommand method execute.
public QueryResults execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
if (this.arguments == null || this.arguments.isEmpty()) {
this.arguments = Collections.emptyList();
}
for (int j = 0; j < arguments.size(); j++) {
if (arguments.get(j) instanceof Variable) {
arguments.set(j, Variable.v);
}
}
QueryResults results = ksession.getQueryResults(name, this.arguments.toArray());
if (this.outIdentifier != null) {
((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, new FlatQueryResults((QueryResultsImpl) results));
}
return results;
}
Aggregations