Search in sources :

Example 16 with QueryResultsRow

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;
}
Also used : QueryResultsImpl(org.drools.core.QueryResultsImpl) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) TreeSet(java.util.TreeSet) DomainObject(org.drools.compiler.DomainObject) InsertedObject(org.drools.compiler.InsertedObject) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults)

Example 17 with QueryResultsRow

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(""));
}
Also used : Query(org.kie.api.definition.rule.Query) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieSession(org.kie.api.runtime.KieSession) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Abducible(org.drools.core.beliefsystem.abductive.Abducible) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 18 with QueryResultsRow

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();
}
Also used : QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ArrayList(java.util.ArrayList) List(java.util.List) QueryResults(org.kie.api.runtime.rule.QueryResults)

Aggregations

QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)18 QueryResults (org.kie.api.runtime.rule.QueryResults)16 KieSession (org.kie.api.runtime.KieSession)13 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 List (java.util.List)8 KieBase (org.kie.api.KieBase)7 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)3 Cheese (org.drools.compiler.Cheese)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 Person (org.drools.modelcompiler.domain.Person)2 KieServices (org.kie.api.KieServices)2 Command (org.kie.api.command.Command)2 FactType (org.kie.api.definition.type.FactType)2 ExecutionResults (org.kie.api.runtime.ExecutionResults)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1