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));
}
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);
}
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);
}
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"));
}
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());
}
Aggregations