Search in sources :

Example 6 with QueryResults

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

the class BasicUpdateTest method verifyFactsWithQuery.

private <T> void verifyFactsWithQuery(final Class<T> expectedClassOfFacts, final String queryToGetFacts, final T... factsToVerify) {
    final QueryResults results = ksession.getQueryResults(queryToGetFacts);
    assertThat(results).isNotEmpty();
    final QueryResultsRow resultsRow = results.iterator().next();
    assertThat(resultsRow.get("$" + queryToGetFacts)).isInstanceOf(List.class);
    final List<Object> objects = (List<Object>) resultsRow.get("$" + queryToGetFacts);
    assertThat(objects).hasSize(factsToVerify.length);
    assertThat(objects).hasOnlyElementsOfType(expectedClassOfFacts);
    assertThat(objects).containsAll(Arrays.asList(factsToVerify));
}
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 7 with QueryResults

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

the class DeleteTest method deleteFactTest.

@Test
public void deleteFactTest() {
    ksession.insert(new Person("Petr", 25));
    FactHandle george = ksession.insert(new Person("George", 19));
    QueryResults results = ksession.getQueryResults("informationAboutPersons");
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$countOfPerson")).isEqualTo(2L);
    ksession.delete(george);
    results = ksession.getQueryResults("informationAboutPersons");
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$countOfPerson")).isEqualTo(1L);
}
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 8 with QueryResults

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

the class DeleteTest method deleteFactTwiceTest.

@Test
public void deleteFactTwiceTest() {
    FactHandle george = ksession.insert(new Person("George", 19));
    QueryResults results = ksession.getQueryResults("countPerson");
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$personCount")).isEqualTo(1L);
    ksession.delete(george);
    results = ksession.getQueryResults("countPerson");
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$personCount")).isEqualTo(0L);
    ksession.delete(george);
    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 9 with QueryResults

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

the class QueryTest method testQueriesWithVariableUnification.

@Test
public void testQueriesWithVariableUnification() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, String $likes, int $age ) \n";
    str += "    $p : Person( $name := name, $likes := likes, $age := age ) \n";
    str += "end\n";
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    KieSession ksession = createKieSession(kbase);
    Person p1 = new Person("darth", "stilton", 100);
    Person p2 = new Person("yoda", "stilton", 300);
    Person p3 = new Person("luke", "brie", 300);
    Person p4 = new Person("bobba", "cheddar", 300);
    ksession.insert(p1);
    ksession.insert(p2);
    ksession.insert(p3);
    ksession.insert(p4);
    QueryResults results = getQueryResults(ksession, "peeps", new Object[] { Variable.v, 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 = getQueryResults(ksession, "peeps", new Object[] { Variable.v, 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 = getQueryResults(ksession, "peeps", new Object[] { Variable.v, "stilton", 300 });
    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("yoda"));
    results = ksession.getQueryResults("peeps", new Object[] { Variable.v, "stilton", Variable.v });
    assertEquals(2, results.size());
    names = new ArrayList();
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        names.add(((Person) row.get("$p")).getName());
    }
    assertEquals(2, names.size());
    assertTrue(names.contains("yoda"));
    assertTrue(names.contains("darth"));
    results = ksession.getQueryResults("peeps", new Object[] { "darth", Variable.v, 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 : QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) DomainObject(org.drools.compiler.DomainObject) InsertedObject(org.drools.compiler.InsertedObject) Person(org.drools.compiler.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 10 with QueryResults

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

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)80 Test (org.junit.Test)67 KieSession (org.kie.api.runtime.KieSession)59 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 Collection (java.util.Collection)9 Assertions (org.assertj.core.api.Assertions)9 Relationship (org.drools.modelcompiler.domain.Relationship)9 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