use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class QueryTest method testPositionalRecursiveQueryWithUnification.
@Test
public void testPositionalRecursiveQueryWithUnification() {
String str = "import " + Relationship.class.getCanonicalName() + ";" + "query isRelatedTo(String x, String y)\n" + " Relationship (x, y;)\n" + " or\n" + " ( Relationship (z, y;) and ?isRelatedTo(x, z;))\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Relationship("A", "B"));
ksession.insert(new Relationship("B", "C"));
QueryResults results = ksession.getQueryResults("isRelatedTo", "A", "C");
assertEquals(1, results.size());
final QueryResultsRow firstResult = results.iterator().next();
Object resultDrlx = firstResult.get("z");
assertTrue("B".equals(resultDrlx));
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class QueryTest method testRecursiveQueryWithBatchCommand.
@Test
public void testRecursiveQueryWithBatchCommand() throws Exception {
String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + "query isContainedIn(String x, String y)\n" + " Location (x, y;)\n" + " or\n" + " ( Location (z, y;) and ?isContainedIn(x, z;))\n" + "end\n" + "declare Location\n" + " thing : String\n" + " location : String\n" + "end";
KieServices kieServices = KieServices.Factory.get();
KieSession ksession = getKieSession(str);
FactType locationType = ksession.getKieBase().getFactType("org.test", "Location");
// a pear is in the kitchen
final Object pear = locationType.newInstance();
locationType.set(pear, "thing", "pear");
locationType.set(pear, "location", "kitchen");
// a desk is in the office
final Object desk = locationType.newInstance();
locationType.set(desk, "thing", "desk");
locationType.set(desk, "location", "office");
// a flashlight is on the desk
final Object flashlight = locationType.newInstance();
locationType.set(flashlight, "thing", "flashlight");
locationType.set(flashlight, "location", "desk");
// an envelope is on the desk
final Object envelope = locationType.newInstance();
locationType.set(envelope, "thing", "envelope");
locationType.set(envelope, "location", "desk");
// a key is in the envelope
final Object key = locationType.newInstance();
locationType.set(key, "thing", "key");
locationType.set(key, "location", "envelope");
// create working memory objects
final List<Command<?>> commands = new ArrayList<Command<?>>();
// Location instances
commands.add(kieServices.getCommands().newInsert(pear));
commands.add(kieServices.getCommands().newInsert(desk));
commands.add(kieServices.getCommands().newInsert(flashlight));
commands.add(kieServices.getCommands().newInsert(envelope));
commands.add(kieServices.getCommands().newInsert(key));
// fire all rules
final String queryAlias = "myQuery";
commands.add(kieServices.getCommands().newQuery(queryAlias, "isContainedIn", new Object[] { Variable.v, "office" }));
final ExecutionResults results = ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
final QueryResults qResults = (QueryResults) results.getValue(queryAlias);
final List<String> l = new ArrayList<String>();
for (QueryResultsRow r : qResults) {
l.add((String) r.get("x"));
}
// items in the office should be the following
Assertions.assertThat(l.size()).isEqualTo(4);
Assertions.assertThat(l.contains("desk")).isTrue();
Assertions.assertThat(l.contains("flashlight")).isTrue();
Assertions.assertThat(l.contains("envelope")).isTrue();
Assertions.assertThat(l.contains("key")).isTrue();
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class JpaPersistenceTraitTest method testTraitWithJPAOnFreshKieBase.
@Test
public void testTraitWithJPAOnFreshKieBase() {
// DROOLS-904
String str = "package org.drools.trait.test; " + "global java.util.List list; " + "declare TBean2 " + " @propertyReactive " + " @Traitable " + "end " + "declare trait Mask " + " @propertyReactive " + "end " + "query getTraits( Mask $m ) " + " $m := Mask() " + "end " + "rule Init when then don( new TBean2(), Mask.class ); end " + "rule Trig when String() then don( new TBean2(), Mask.class ); end " + "rule Main when $m : Mask() then list.add( $m ); end ";
List list = new ArrayList();
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
ksession.setGlobal("list", list);
ksession.fireAllRules();
long id = ksession.getIdentifier();
InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
TraitFactory.setMode(VirtualPropertyMode.MAP, kbase);
kbase2.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase2, null, env);
ksession.setGlobal("list", list);
ksession2.insert("go");
ksession2.fireAllRules();
assertEquals(2, list.size());
Class<?> oldProxyClass = list.get(0).getClass();
Class<?> newProxyClass = list.get(1).getClass();
assertNotSame(oldProxyClass, newProxyClass);
QueryResults qry = ksession2.getQueryResults("getTraits", Variable.v);
assertEquals(2, qry.size());
java.util.Iterator<QueryResultsRow> iter = qry.iterator();
int j = 0;
while (iter.hasNext()) {
QueryResultsRow row = iter.next();
Object entry = row.get("$m");
assertNotNull(entry);
assertSame(newProxyClass, entry.getClass());
j++;
}
assertEquals(2, j);
for (Object o : ksession2.getObjects()) {
if (o.getClass().getName().contains("Mask")) {
assertSame(newProxyClass, o.getClass());
}
}
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class UnicodeTest method testQueryCallFromJava.
// test queries in Czech language
@Test
public void testQueryCallFromJava() throws InstantiationException, IllegalAccessException {
final KieServices kieServices = KieServices.Factory.get();
final Resource resource = kieServices.getResources().newClassPathResource("unicode.drl", getClass());
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
final KieSession ksession = kbase.newKieSession();
final FactType locationType = kbase.getFactType(TestConstants.PACKAGE_FUNCTIONAL, "Umístění");
// a pear is in the kitchen
final Object hruška = locationType.newInstance();
locationType.set(hruška, "věc", "hruška");
locationType.set(hruška, "místo", "kuchyně");
// a desk is in the office
final Object stůl = locationType.newInstance();
locationType.set(stůl, "věc", "stůl");
locationType.set(stůl, "místo", "kancelář");
// a flashlight is on the desk
final Object svítilna = locationType.newInstance();
locationType.set(svítilna, "věc", "svítilna");
locationType.set(svítilna, "místo", "stůl");
// an envelope is on the desk
final Object obálka = locationType.newInstance();
locationType.set(obálka, "věc", "obálka");
locationType.set(obálka, "místo", "stůl");
// a key is in the envelope
final Object klíč = locationType.newInstance();
locationType.set(klíč, "věc", "klíč");
locationType.set(klíč, "místo", "obálka");
// create working memory objects
final List<Command<?>> commands = new ArrayList<Command<?>>();
// Location instances
commands.add(kieServices.getCommands().newInsert(hruška));
commands.add(kieServices.getCommands().newInsert(stůl));
commands.add(kieServices.getCommands().newInsert(svítilna));
commands.add(kieServices.getCommands().newInsert(obálka));
commands.add(kieServices.getCommands().newInsert(klíč));
// fire all rules
final String queryAlias = "obsaženo";
commands.add(kieServices.getCommands().newQuery(queryAlias, "jeObsažen", new Object[] { Variable.v, "kancelář" }));
final ExecutionResults results = ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
final QueryResults qResults = (QueryResults) results.getValue(queryAlias);
final List<String> l = new ArrayList<String>();
for (QueryResultsRow r : qResults) {
l.add((String) r.get("x"));
}
// items in the office should be the following
Assertions.assertThat(l.size()).isEqualTo(4);
Assertions.assertThat(l.contains("stůl")).isTrue();
Assertions.assertThat(l.contains("svítilna")).isTrue();
Assertions.assertThat(l.contains("obálka")).isTrue();
Assertions.assertThat(l.contains("klíč")).isTrue();
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class ConcurrentBasesParallelTest method testQueries.
@Test(timeout = 20000)
public void testQueries() throws InterruptedException {
final int numberOfObjects = 100;
final TestExecutor exec = counter -> {
final String query = "import " + BeanA.class.getCanonicalName() + ";\n" + "query Query " + " bean : BeanA( seed == " + counter + " ) " + "end";
final KieBase base = getKieBase(query);
final KieSession session = base.newKieSession();
try {
final BeanA bean = new BeanA(counter);
session.insert(bean);
for (int i = 0; i < numberOfObjects; i++) {
if (i != counter) {
session.insert(new BeanA(i));
}
}
final QueryResults results = session.getQueryResults("Query");
Assertions.assertThat(results).hasSize(1);
for (final QueryResultsRow row : results) {
Assertions.assertThat(row.get("bean")).isEqualTo(bean);
}
return true;
} finally {
session.dispose();
}
};
parallelTest(NUMBER_OF_THREADS, exec);
}
Aggregations