Search in sources :

Example 76 with QueryResults

use of org.kie.api.runtime.rule.QueryResults 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)

Example 77 with QueryResults

use of org.kie.api.runtime.rule.QueryResults 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)

Example 78 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class DeleteTest method deleteUpdatedFactTest.

@Test
public void deleteUpdatedFactTest() {
    FactHandle person = ksession.insert(new Person("George", 18));
    ksession.update(person, new Person("John", 21));
    QueryResults results = ksession.getQueryResults("countPerson");
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$personCount")).isEqualTo(1L);
    ksession.delete(person);
    results = ksession.getQueryResults("countPerson");
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$personCount")).isEqualTo(0L);
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Person(org.drools.core.test.model.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 79 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class DroolsAbstractPMMLTest method queryIntegerField.

protected double queryIntegerField(String target, String modelName) {
    QueryResults results = getKSession().getQueryResults(target, modelName, Variable.v);
    assertEquals(1, results.size());
    return (Integer) results.iterator().next().get("$result");
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults)

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)79 Test (org.junit.Test)67 KieSession (org.kie.api.runtime.KieSession)58 ArrayList (java.util.ArrayList)30 KieBase (org.kie.api.KieBase)25 List (java.util.List)24 Person (org.drools.modelcompiler.domain.Person)19 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)18 FactHandle (org.kie.api.runtime.rule.FactHandle)15 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)10 Assertions (org.assertj.core.api.Assertions)9 Relationship (org.drools.modelcompiler.domain.Relationship)9 Collection (java.util.Collection)8 Map (java.util.Map)8 TimeUnit (java.util.concurrent.TimeUnit)7 ClockType (org.drools.core.ClockType)7 InternationalAddress (org.drools.modelcompiler.oopathdtables.InternationalAddress)6 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)6 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)6 Global (org.drools.model.Global)5