use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class UnicodeTest method testJapanese.
@Test
public void testJapanese() {
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 List<Command<?>> commands = new ArrayList<>();
final List<人> 一覧 = new ArrayList<>();
commands.add(kieServices.getCommands().newSetGlobal("一覧", 一覧));
// let's create person yokozuna
final 人 横綱 = new 人();
横綱.set歳(30);
横綱.setの名前("横綱");
横綱.set既婚(true);
commands.add(kieServices.getCommands().newInsert(横綱));
commands.add(kieServices.getCommands().newFireAllRules("firedRulesCount"));
final ExecutionResults results = ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
Assertions.assertThat(results.getValue("firedRulesCount")).isEqualTo(2);
Assertions.assertThat(一覧.size()).isEqualTo(1);
Assertions.assertThat(一覧.iterator().next().getの名前()).isEqualTo("横綱");
}
use of org.kie.api.runtime.ExecutionResults 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.ExecutionResults in project drools by kiegroup.
the class RuleStatelessScenarioExecutableBuilderTest method generateCommands.
@Test
public void generateCommands() {
FactMappingValue emptyFMV = new FactMappingValue(FactIdentifier.EMPTY, ExpressionIdentifier.DESCRIPTION, null);
RuleStatelessScenarioExecutableBuilder builder = new RuleStatelessScenarioExecutableBuilder(null, null);
Command<ExecutionResults> batchCommand = builder.generateCommands(null);
assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
assertFalse(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
assertFalse(verifyCommand(batchCommand, InsertElementsCommand.class));
assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
builder.setActiveAgendaGroup("test");
batchCommand = builder.generateCommands(null);
assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
assertFalse(verifyCommand(batchCommand, InsertElementsCommand.class));
assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
builder.insert(new Object());
batchCommand = builder.generateCommands(null);
assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
assertTrue(verifyCommand(batchCommand, InsertElementsCommand.class));
assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
builder.addInternalCondition(String.class, obj -> null, new ScenarioResult(emptyFMV, null));
batchCommand = builder.generateCommands(null);
assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
assertTrue(verifyCommand(batchCommand, InsertElementsCommand.class));
assertTrue(verifyCommand(batchCommand, ValidateFactCommand.class));
}
use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class MoreBatchExecutionTest method testQuery.
@Test
public void testQuery() {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "org/drools/mvel/integrationtests/simple_query_test.drl");
KieSession ksession = kbase.newKieSession();
ksession.insert(new Cheese("stinky", 5));
ksession.insert(new Cheese("smelly", 7));
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newQuery("numStinkyCheeses", "simple query"));
Command<?> cmds = CommandFactory.newBatchExecution(commands);
ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
assertNotNull("Batch execution result is null!", result);
Object queryResultsObject = result.getValue("numStinkyCheeses");
assertTrue("Retrieved object is null or incorrect!", queryResultsObject != null && queryResultsObject instanceof QueryResults);
assertEquals(1, ((QueryResults) queryResultsObject).size());
}
use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class StatelessSessionTest method testQuery.
@Test
public void testQuery() throws Exception {
String str = "";
str += "package org.kie.test \n";
str += "import org.drools.mvel.compiler.Cheese \n";
str += "query cheeses \n";
str += " stilton : Cheese(type == 'stilton') \n";
str += " cheddar : Cheese(type == 'cheddar', price == stilton.price) \n";
str += "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
final StatelessKieSession ksession = kbase.newStatelessKieSession();
final Cheese stilton1 = new Cheese("stilton", 1);
final Cheese cheddar1 = new Cheese("cheddar", 1);
final Cheese stilton2 = new Cheese("stilton", 2);
final Cheese cheddar2 = new Cheese("cheddar", 2);
final Cheese stilton3 = new Cheese("stilton", 3);
final Cheese cheddar3 = new Cheese("cheddar", 3);
final Set set = new HashSet();
List list = new ArrayList();
list.add(stilton1);
list.add(cheddar1);
set.add(list);
list = new ArrayList();
list.add(stilton2);
list.add(cheddar2);
set.add(list);
list = new ArrayList();
list.add(stilton3);
list.add(cheddar3);
set.add(list);
final List<Command> cmds = new ArrayList<Command>();
cmds.add(CommandFactory.newInsert(stilton1));
cmds.add(CommandFactory.newInsert(stilton2));
cmds.add(CommandFactory.newInsert(stilton3));
cmds.add(CommandFactory.newInsert(cheddar1));
cmds.add(CommandFactory.newInsert(cheddar2));
cmds.add(CommandFactory.newInsert(cheddar3));
cmds.add(CommandFactory.newQuery("cheeses", "cheeses"));
final ExecutionResults batchResult = (ExecutionResults) ksession.execute(CommandFactory.newBatchExecution(cmds));
final org.kie.api.runtime.rule.QueryResults results = (org.kie.api.runtime.rule.QueryResults) batchResult.getValue("cheeses");
assertEquals(3, results.size());
assertEquals(2, results.getIdentifiers().length);
final Set newSet = new HashSet();
for (final org.kie.api.runtime.rule.QueryResultsRow result : results) {
list = new ArrayList();
list.add(result.get("stilton"));
list.add(result.get("cheddar"));
newSet.add(list);
}
assertEquals(set, newSet);
}
Aggregations