use of org.kie.api.runtime.rule.QueryResultsRow 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.kie.api.runtime.rule.QueryResultsRow 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.QueryResultsRow in project drools by kiegroup.
the class BasicUpdateTest method verifyWithQueryNoPersonsPresentInFacts.
private void verifyWithQueryNoPersonsPresentInFacts() {
QueryResults results = ksession.getQueryResults("persons");
assertThat(results).isNotEmpty();
results = ksession.getQueryResults("persons");
assertThat(results).isNotEmpty();
final QueryResultsRow resultsRow = results.iterator().next();
assertThat(resultsRow.get("$persons")).isInstanceOf(List.class);
final List<Object> persons = (List<Object>) resultsRow.get("$persons");
assertThat(persons).isEmpty();
}
Aggregations