Search in sources :

Example 1 with FlatQueryResults

use of org.drools.core.runtime.rule.impl.FlatQueryResults in project drools by kiegroup.

the class QueryTest method testQueryWithMultipleResultsOnKnowledgeApi.

@Test
public void testQueryWithMultipleResultsOnKnowledgeApi() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Cheese \n";
    str += "query cheeses \n";
    str += "    stilton : Cheese(type == 'stilton') \n";
    str += "    cheddar : Cheese(type == 'cheddar', price == stilton.price) \n";
    str += "end\n";
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    KieSession session = createKieSession(kbase);
    Cheese stilton1 = new Cheese("stilton", 1);
    Cheese cheddar1 = new Cheese("cheddar", 1);
    Cheese stilton2 = new Cheese("stilton", 2);
    Cheese cheddar2 = new Cheese("cheddar", 2);
    Cheese stilton3 = new Cheese("stilton", 3);
    Cheese cheddar3 = new Cheese("cheddar", 3);
    Set set = new HashSet();
    List list = new ArrayList();
    list.add(stilton1);
    list.add(cheddar1);
    set.add(list);
    list = new ArrayList();
    list.add(stilton2);
    list.add(cheddar2);
    set.add(list);
    list = new ArrayList();
    list.add(stilton3);
    list.add(cheddar3);
    set.add(list);
    session.insert(stilton1);
    session.insert(stilton2);
    session.insert(stilton3);
    session.insert(cheddar1);
    session.insert(cheddar2);
    session.insert(cheddar3);
    org.kie.api.runtime.rule.QueryResults results = getQueryResults(session, "cheeses");
    assertEquals(3, results.size());
    assertEquals(2, results.getIdentifiers().length);
    Set newSet = new HashSet();
    for (org.kie.api.runtime.rule.QueryResultsRow result : results) {
        list = new ArrayList();
        list.add(result.get("stilton"));
        list.add(result.get("cheddar"));
        newSet.add(list);
    }
    assertEquals(set, newSet);
    FlatQueryResults flatResults = new FlatQueryResults(((StatefulKnowledgeSessionImpl) session).getQueryResults("cheeses"));
    newSet = new HashSet();
    for (org.kie.api.runtime.rule.QueryResultsRow result : flatResults) {
        list = new ArrayList();
        list.add(result.get("stilton"));
        list.add(result.get("cheddar"));
        newSet.add(list);
    }
    assertEquals(set, newSet);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) QueryResults(org.kie.api.runtime.rule.QueryResults) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with FlatQueryResults

use of org.drools.core.runtime.rule.impl.FlatQueryResults 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 3 with FlatQueryResults

use of org.drools.core.runtime.rule.impl.FlatQueryResults 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 4 with FlatQueryResults

use of org.drools.core.runtime.rule.impl.FlatQueryResults 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)

Example 5 with FlatQueryResults

use of org.drools.core.runtime.rule.impl.FlatQueryResults in project drools by kiegroup.

the class XStreamXMLTest method testQueryResultsConverter.

@Test
public void testQueryResultsConverter() {
    final Message msg = new Message("Hello World!");
    final FactHandle msgHandle = new DefaultFactHandle(1, null, 10, 10, 20, msg);
    Set<String> identifiers = new HashSet<String>() {

        {
            add("greeting");
        }
    };
    ArrayList<Map<String, FactHandle>> idFactHandleMaps = new ArrayList<Map<String, FactHandle>>() {

        {
            add(new HashMap<String, FactHandle>() {

                {
                    put("greeting", msgHandle);
                }
            });
        }
    };
    ArrayList<Map<String, Object>> factHandleResultMap = new ArrayList<Map<String, Object>>() {

        {
            add(new HashMap<String, Object>() {

                {
                    put("greeting", msg);
                }
            });
        }
    };
    final String EXPECTED_XML = "<query-results>\n" + "  <identifiers>\n" + "    <identifier>greeting</identifier>\n" + "  </identifiers>\n" + "  <row>\n" + "    <identifier id=\"greeting\">\n" + "      <org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "        <msg>Hello World!</msg>\n" + "      </org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "      <fact-handle external-form=\"0:1:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "    </identifier>\n" + "  </row>\n" + "</query-results>";
    QueryResults results = new FlatQueryResults(identifiers, idFactHandleMaps, factHandleResultMap);
    String xmlString = xstream.toXML(results);
    Assert.assertEquals(EXPECTED_XML, xmlString);
    QueryResults results2 = (QueryResults) xstream.fromXML(xmlString);
    Assert.assertEquals(results, results2);
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)5 QueryResults (org.kie.api.runtime.rule.QueryResults)4 Test (org.junit.Test)3 KieSession (org.kie.api.runtime.KieSession)3 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 TreeSet (java.util.TreeSet)2 QueryResultsImpl (org.drools.core.QueryResultsImpl)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Cheese (org.drools.compiler.Cheese)1 DomainObject (org.drools.compiler.DomainObject)1 InsertedObject (org.drools.compiler.InsertedObject)1 Abducible (org.drools.core.beliefsystem.abductive.Abducible)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1