use of org.kie.api.command.Command in project drools by kiegroup.
the class PseudoClockRunner method executeQueue.
private void executeQueue(RequestContext ctx) {
while (!queue.isEmpty()) {
Batch batch = queue.remove();
long timeNow = startTime + batch.getDistance();
for (KieSession ksession : ksessions) {
// make sure all sessions are set to timeNow
updateKieSessionTime(timeNow, batch.getDistance(), ksession);
}
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(timeNow, batch.getDistance(), ksession);
ksessions.add((KieSession) returned);
}
}
}
}
}
use of org.kie.api.command.Command in project drools by kiegroup.
the class StatelessKnowledgeSessionImpl method execute.
public <T> T execute(Command<T> command) {
StatefulKnowledgeSession ksession = newWorkingMemory();
RegistryContext context = new ContextImpl().register(KieSession.class, ksession);
try {
if (command instanceof BatchExecutionCommand) {
((RegistryContext) context).register(ExecutionResultImpl.class, new ExecutionResultImpl());
}
((StatefulKnowledgeSessionImpl) ksession).startBatchExecution();
Object o = ((ExecutableCommand) command).execute(context);
// did the user take control of fireAllRules, if not we will auto execute
boolean autoFireAllRules = true;
if (command instanceof FireAllRulesCommand) {
autoFireAllRules = false;
} else if (command instanceof BatchExecutionCommandImpl) {
for (Command nestedCmd : ((BatchExecutionCommandImpl) command).getCommands()) {
if (nestedCmd instanceof FireAllRulesCommand) {
autoFireAllRules = false;
break;
}
}
}
if (autoFireAllRules) {
ksession.fireAllRules();
}
if (command instanceof BatchExecutionCommand) {
return (T) ((RegistryContext) context).lookup(ExecutionResultImpl.class);
} else {
return (T) o;
}
} finally {
((StatefulKnowledgeSessionImpl) ksession).endBatchExecution();
dispose(ksession);
}
}
use of org.kie.api.command.Command in project drools by kiegroup.
the class IncrementalCompilationTest method testUpdateVersionWithKSessionLogger.
@Test
public void testUpdateVersionWithKSessionLogger() {
// DROOLS-790
String drl1 = "import java.util.List\n" + "import java.util.ArrayList\n" + "\n" + "rule \"Test1\"\n" + "\n" + "when\n" + " $a : Integer()\n" + "then\n" + " insert(new ArrayList());\n" + "end\n";
String drl2 = "rule \"Test2\"\n" + "when\n" + " $b : List()\n" + " then\n" + " $b.isEmpty();\n" + "end";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, drl1);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
StatelessKieSession statelessKieSession = kc.newStatelessKieSession();
KieRuntimeLogger kieRuntimeLogger = ks.getLoggers().newConsoleLogger(statelessKieSession);
List<Command> cmds = new ArrayList<Command>();
cmds.add(CommandFactory.newInsertElements(new ArrayList()));
FireAllRulesCommand fireAllRulesCommand = (FireAllRulesCommand) CommandFactory.newFireAllRules();
cmds.add(fireAllRulesCommand);
cmds.add(CommandFactory.newGetObjects("returnedObjects"));
BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(cmds);
statelessKieSession.execute(batchExecutionCommand);
kieRuntimeLogger.close();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl1 + drl2);
kc.updateToVersion(km.getReleaseId());
}
use of org.kie.api.command.Command in project drools by kiegroup.
the class StatelessSessionTest method testSetGlobal.
@Test
public void testSetGlobal() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import org.drools.compiler.Cheese \n";
str += "global java.util.List list1 \n";
str += "global java.util.List list2 \n";
str += "global java.util.List list3 \n";
str += "rule rule1 \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " \n";
str += " then \n";
str += " $c.setPrice( 30 ); \n";
str += " list1.add( $c ); \n";
str += " list2.add( $c ); \n";
str += " list3.add( $c ); \n";
str += "end\n";
final Cheese stilton = new Cheese("stilton", 5);
final List list1 = new ArrayList();
List list2 = new ArrayList();
List list3 = new ArrayList();
final StatelessKieSession ksession = getSession2(ResourceFactory.newByteArrayResource(str.getBytes()));
final Command setGlobal1 = CommandFactory.newSetGlobal("list1", list1);
final Command setGlobal2 = CommandFactory.newSetGlobal("list2", list2, true);
final Command setGlobal3 = CommandFactory.newSetGlobal("list3", list3, "outList3");
final Command insert = CommandFactory.newInsert(stilton);
final List cmds = new ArrayList();
cmds.add(setGlobal1);
cmds.add(setGlobal2);
cmds.add(setGlobal3);
cmds.add(insert);
final ExecutionResults result = (ExecutionResults) ksession.execute(CommandFactory.newBatchExecution(cmds));
assertEquals(30, stilton.getPrice());
assertNull(result.getValue("list1"));
list2 = (List) result.getValue("list2");
assertEquals(1, list2.size());
assertSame(stilton, list2.get(0));
list3 = (List) result.getValue("outList3");
assertEquals(1, list3.size());
assertSame(stilton, list3.get(0));
}
use of org.kie.api.command.Command in project drools by kiegroup.
the class FireAllRulesCommandTest method zeroRulesFiredTest.
@Test
public void zeroRulesFiredTest() {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " System.out.println($c); \n";
str += "end \n";
StatelessKieSession ksession = getSession(str);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert("not cheese"));
commands.add(CommandFactory.newFireAllRules("num-rules-fired"));
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
assertEquals(0, fired);
}
Aggregations