Search in sources :

Example 1 with QueryResultsImpl

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());
}
Also used : QueryResultsImpl(org.drools.core.QueryResultsImpl) InsertedObject(org.drools.compiler.InsertedObject) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 2 with QueryResultsImpl

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"));
}
Also used : QueryResultsImpl(org.drools.core.QueryResultsImpl) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 3 with QueryResultsImpl

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;
}
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 4 with QueryResultsImpl

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();
    }
}
Also used : QueryRowWithSubruleIndex(org.drools.core.base.QueryRowWithSubruleIndex) QueryResultsImpl(org.drools.core.QueryResultsImpl) PropagationContext(org.drools.core.spi.PropagationContext) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) QueryTerminalNode(org.drools.core.reteoo.QueryTerminalNode) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 5 with QueryResultsImpl

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;
}
Also used : QueryResultsImpl(org.drools.core.QueryResultsImpl) Variable(org.kie.api.runtime.rule.Variable) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) QueryResults(org.kie.api.runtime.rule.QueryResults)

Aggregations

QueryResultsImpl (org.drools.core.QueryResultsImpl)5 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)3 KieSession (org.kie.api.runtime.KieSession)3 ArrayList (java.util.ArrayList)2 InsertedObject (org.drools.compiler.InsertedObject)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 Test (org.junit.Test)2 QueryResults (org.kie.api.runtime.rule.QueryResults)2 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DomainObject (org.drools.compiler.DomainObject)1 DroolsQuery (org.drools.core.base.DroolsQuery)1 QueryRowWithSubruleIndex (org.drools.core.base.QueryRowWithSubruleIndex)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 BaseNode (org.drools.core.common.BaseNode)1