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 QueryBadResultTest method testAccessToNotExistingVariable.
@Test(expected = IllegalArgumentException.class)
public void testAccessToNotExistingVariable() {
final KieBase kieBase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "query.drl");
final KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Petr"));
final QueryResults results = ksession.getQueryResults("simple query with no parameters");
results.iterator().next().get("bad");
}
use of org.kie.api.runtime.rule.QueryResults 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<>();
// 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<>();
for (final 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.QueryResults in project drools by kiegroup.
the class PatternDSLTest method testQueryInvokingQuery2.
@Test
public void testQueryInvokingQuery2() {
final org.drools.model.Query2Def<java.lang.String, java.lang.String> queryDef_isRelatedTo2 = query("isRelatedTo2", java.lang.String.class, "x", java.lang.String.class, "y");
final org.drools.model.Query2Def<java.lang.String, java.lang.String> queryDef_isRelatedTo = query("isRelatedTo", java.lang.String.class, "x", java.lang.String.class, "y");
org.drools.model.Query isRelatedTo_build = queryDef_isRelatedTo.build(queryDef_isRelatedTo2.call(true, queryDef_isRelatedTo.getArg1(), queryDef_isRelatedTo.getArg2()));
final org.drools.model.Variable<org.drools.modelcompiler.domain.Relationship> var_$pattern_Relationship$4$ = declarationOf(org.drools.modelcompiler.domain.Relationship.class, "$pattern_Relationship$4$");
org.drools.model.Query isRelatedTo2_build = queryDef_isRelatedTo2.build(pattern(var_$pattern_Relationship$4$).expr("$expr$63$", queryDef_isRelatedTo2.getArg1(), (_this, x) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getStart(), x), betaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getStart(), x -> x), reactOn("start")).expr("$expr$64$", queryDef_isRelatedTo2.getArg2(), (_this, y) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getEnd(), y), betaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 1, _this -> _this.getEnd(), y -> y), reactOn("end")));
Model model = new ModelImpl().addQuery(isRelatedTo_build).addQuery(isRelatedTo2_build);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Relationship("A", "B"));
ksession.insert(new Relationship("B", "C"));
QueryResults results = ksession.getQueryResults("isRelatedTo", "A", "B");
assertEquals(1, results.size());
String paramName = ((QueryImpl) ksession.getKieBase().getQuery("defaultpkg", "isRelatedTo")).getParameters()[1].getIdentifier();
assertEquals("B", results.iterator().next().get(paramName));
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class PatternDSLTest method testQueryInvokingQuery.
@Test
public void testQueryInvokingQuery() {
Variable<Relationship> relV = DSL.declarationOf(Relationship.class);
Query2Def<String, String> query1Def = query("isRelatedTo1", String.class, String.class);
Query2Def<String, String> query2Def = query("isRelatedTo2", String.class, String.class);
Query query2 = query2Def.build(pattern(relV).expr("exprA", query2Def.getArg1(), (r, s) -> r.getStart().equals(s)).expr("exprB", query2Def.getArg2(), (r, e) -> r.getEnd().equals(e)));
Query query1 = query1Def.build(query2Def.call(query1Def.getArg1(), query1Def.getArg2()));
Model model = new ModelImpl().addQuery(query2).addQuery(query1);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Relationship("A", "B"));
ksession.insert(new Relationship("B", "C"));
QueryResults results = ksession.getQueryResults("isRelatedTo1", "A", "B");
assertEquals(1, results.size());
assertEquals("B", results.iterator().next().get(query1Def.getArg2().getName()));
}
Aggregations