use of org.kie.api.command.Command in project drools by kiegroup.
the class PseudoClockRunner method executeBatch.
private void executeBatch(Batch batch, RequestContext ctx) {
// everything else must be handled by a priority queue and timer afterwards.
for (Command cmd : batch.getCommands()) {
Object returned = ((ExecutableCommand) cmd).execute(ctx);
if (returned != null) {
ctx.setResult(returned);
if (returned instanceof KieSession) {
KieSession ksession = (KieSession) returned;
// make sure all sessions are set to timeNow
updateKieSessionTime(startTime, 0, ksession);
ksessions.add((KieSession) returned);
}
}
}
}
use of org.kie.api.command.Command in project drools by kiegroup.
the class UnicodeInCSVTest method testUnicodeCSVDecisionTable.
@Test
public void testUnicodeCSVDecisionTable() throws FileNotFoundException {
DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
dtconf.setInputType(DecisionTableInputType.CSV);
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("unicode.csv", getClass()), ResourceType.DTABLE, dtconf);
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors().toString());
System.out.println(DecisionTableFactory.loadFromInputStream(getClass().getResourceAsStream("unicode.xls"), dtconf));
fail("Cannot build CSV decision table containing utf-8 characters\n" + kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
List<Command<?>> commands = new ArrayList<Command<?>>();
List<Člověk> dospělí = new ArrayList<Člověk>();
commands.add(CommandFactory.newSetGlobal("dospělí", dospělí));
Člověk Řehoř = new Člověk();
Řehoř.setVěk(30);
Řehoř.setJméno("Řehoř");
commands.add(CommandFactory.newInsert(Řehoř));
commands.add(CommandFactory.newFireAllRules());
ksession.execute(CommandFactory.newBatchExecution(commands));
// people with age greater than 18 should be added to list of adults
assertNotNull(kbase.getRule("org.drools.decisiontable", "přidej k dospělým"));
assertEquals(dospělí.size(), 5);
assertEquals(dospělí.iterator().next().getJméno(), "Řehoř");
assertNotNull(kbase.getRule("org.drools.decisiontable", "привет мир"));
assertNotNull(kbase.getRule("org.drools.decisiontable", "你好世界"));
assertNotNull(kbase.getRule("org.drools.decisiontable", "hallå världen"));
assertNotNull(kbase.getRule("org.drools.decisiontable", "مرحبا العالم"));
ksession.dispose();
}
use of org.kie.api.command.Command 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.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";
final 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());
kbase = SerializationHelper.serializeObject(kbase);
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