use of org.drools.core.QueryResultsImpl in project drools by kiegroup.
the class QueryTest method testQueryWithParams.
@Test
public void testQueryWithParams() throws Exception {
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_QueryWithParams.drl"));
KieSession session = createKieSession(kbase);
session.fireAllRules();
String queryName = "assertedobjquery";
String[] arguments = new String[] { "value1" };
QueryResultsImpl resultsImpl = (QueryResultsImpl) session.getQueryResults(queryName, arguments);
QueryResults results = getQueryResults(session, queryName, arguments);
assertEquals(1, results.size());
InsertedObject value = new InsertedObject("value1");
assertEquals(value, ((InternalFactHandle) results.iterator().next().getFactHandle("assertedobj")).getObject());
assertEquals(value, results.iterator().next().get("assertedobj"));
results = getQueryResults(session, "assertedobjquery", new String[] { "value3" });
assertEquals(0, results.size());
results = getQueryResults(session, "assertedobjquery2", new String[] { null, "value2" });
assertEquals(1, results.size());
assertEquals(new InsertedObject("value2"), ((InternalFactHandle) results.iterator().next().getFactHandle("assertedobj")).getObject());
results = getQueryResults(session, "assertedobjquery2", new String[] { "value3", "value2" });
assertEquals(1, results.size());
assertEquals(new InsertedObject("value2"), ((InternalFactHandle) results.iterator().next().getFactHandle("assertedobj")).getObject());
}
use of org.drools.core.QueryResultsImpl in project drools by kiegroup.
the class QueryTest method testQueriesWithVariableUnification.
@Test
public void testQueriesWithVariableUnification() throws Exception {
String str = "import " + Person.class.getCanonicalName() + ";" + "query peeps( String $name, int $age ) \n" + " $p : Person( $name := name, $age := age ) \n" + "end\n";
KieSession ksession = getKieSession(str);
Person p1 = new Person("darth", 100);
Person p2 = new Person("yoda", 300);
Person p3 = new Person("luke", 300);
Person p4 = new Person("bobba", 300);
ksession.insert(p1);
ksession.insert(p2);
ksession.insert(p3);
ksession.insert(p4);
QueryResultsImpl results = (QueryResultsImpl) ksession.getQueryResults("peeps", Variable.v, Variable.v);
assertEquals(4, results.size());
List names = new ArrayList();
for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
names.add(((Person) row.get("$p")).getName());
}
assertEquals(4, names.size());
assertTrue(names.contains("luke"));
assertTrue(names.contains("yoda"));
assertTrue(names.contains("bobba"));
assertTrue(names.contains("darth"));
results = (QueryResultsImpl) ksession.getQueryResults("peeps", Variable.v, 300);
assertEquals(3, results.size());
names = new ArrayList();
for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
names.add(((Person) row.get("$p")).getName());
}
assertEquals(3, names.size());
assertTrue(names.contains("luke"));
assertTrue(names.contains("yoda"));
assertTrue(names.contains("bobba"));
results = (QueryResultsImpl) ksession.getQueryResults("peeps", "darth", Variable.v);
assertEquals(1, results.size());
names = new ArrayList();
for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
names.add(((Person) row.get("$p")).getName());
}
assertEquals(1, names.size());
assertTrue(names.contains("darth"));
}
use of org.drools.core.QueryResultsImpl in project drools by kiegroup.
the class QueryTest method getQueryResults.
private static QueryResults getQueryResults(KieSession session, String queryName, Object... arguments) throws Exception {
QueryResultsImpl results = (QueryResultsImpl) session.getQueryResults(queryName, arguments);
FlatQueryResults flatResults = new FlatQueryResults(results);
assertEquals("Query results size", results.size(), flatResults.size());
assertEquals("Query results identifiers", results.getIdentifiers().length, flatResults.getIdentifiers().length);
Set<String> resultIds = new TreeSet<String>(Arrays.asList(results.getIdentifiers()));
Set<String> flatIds = new TreeSet<String>(Arrays.asList(flatResults.getIdentifiers()));
assertArrayEquals("Flat query results identifiers", resultIds.toArray(), flatIds.toArray());
FlatQueryResults copyFlatResults = roundTrip(flatResults);
String[] identifiers = results.getIdentifiers();
Iterator<QueryResultsRow> copyFlatIter = copyFlatResults.iterator();
for (int i = 0; i < results.size(); ++i) {
QueryResultsRow row = results.get(i);
assertTrue("Round-tripped flat query results contain less rows than original query results", copyFlatIter.hasNext());
QueryResultsRow copyRow = copyFlatIter.next();
for (String id : identifiers) {
Object obj = row.get(id);
if (obj != null) {
Object copyObj = copyRow.get(id);
assertTrue("Flat query result [" + i + "] does not contain result: '" + id + "': " + obj + "/" + copyObj, obj != null && obj.equals(copyObj));
}
FactHandle fh = row.getFactHandle(id);
FactHandle copyFh = copyRow.getFactHandle(id);
if (fh != null) {
assertNotNull("Flat query result [" + i + "] does not contain facthandle: '" + ((InternalFactHandle) fh).getId() + "'", copyFh);
String fhStr = fh.toExternalForm();
fhStr = fhStr.substring(0, fhStr.lastIndexOf(":"));
String copyFhStr = copyFh.toExternalForm();
copyFhStr = copyFhStr.substring(0, copyFhStr.lastIndexOf(":"));
assertEquals("Unequal fact handles for fact handle '" + ((InternalFactHandle) fh).getId() + "':", fhStr, copyFhStr);
}
}
}
// check identifiers
Set<String> copyFlatIds = new TreeSet<String>(Arrays.asList(copyFlatResults.getIdentifiers()));
assertArrayEquals("Flat query results identifiers", flatIds.toArray(), copyFlatIds.toArray());
return copyFlatResults;
}
use of org.drools.core.QueryResultsImpl in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method internalGetQueryResult.
protected QueryResultsImpl internalGetQueryResult(boolean calledFromRHS, String queryName, Object... arguments) {
try {
startOperation();
this.lock.lock();
this.kBase.executeQueuedActions();
// eventually enqueued by events that have been inserted when already expired
if (calledFromRHS) {
flushPropagations();
flushPropagations();
} else {
agenda.executeFlush();
agenda.executeFlush();
}
DroolsQuery queryObject = new DroolsQuery(queryName, arguments, getQueryListenerInstance(), false, null, null, null, null, null);
InternalFactHandle handle = this.handleFactory.newFactHandle(queryObject, null, this, this);
final PropagationContext pCtx = pctxFactory.createPropagationContext(getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, getEntryPoint());
BaseNode[] tnodes = evalQuery(queryName, queryObject, handle, pCtx, calledFromRHS);
List<Map<String, Declaration>> decls = new ArrayList<Map<String, Declaration>>();
if (tnodes != null) {
for (BaseNode node : tnodes) {
decls.add(((QueryTerminalNode) node).getSubRule().getOuterDeclarations());
}
}
this.handleFactory.destroyFactHandle(handle);
return new QueryResultsImpl((List<QueryRowWithSubruleIndex>) queryObject.getQueryResultCollector().getResults(), decls.toArray(new Map[decls.size()]), this, (queryObject.getQuery() != null) ? queryObject.getQuery().getParameters() : new Declaration[0]);
} finally {
this.lock.unlock();
endOperation();
}
}
use of org.drools.core.QueryResultsImpl 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