use of org.kie.api.command.BatchExecutionCommand 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.BatchExecutionCommand 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.BatchExecutionCommand in project drools by kiegroup.
the class ExecuteCommandDisconnectedTest method executeDisconnected.
@Test
public void executeDisconnected() {
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = kbase.newKieSession();
ExecutionResultImpl localKresults = new ExecutionResultImpl();
RequestContext context = RequestContext.create().with(ksession);
ExecutableRunner runner = ExecutableRunner.create();
List cmds = new ArrayList();
cmds.add(new InsertObjectCommand(new String("Hi!"), "handle"));
BatchExecutionCommand batchCmd = CommandFactory.newBatchExecution(cmds, "kresults");
ExecuteCommand execCmd = new ExecuteCommand(batchCmd, true);
ExecutionResults results = execCmd.execute(context);
assertNotNull(results);
assertNotNull(results.getFactHandle("handle"));
assertTrue(((DefaultFactHandle) results.getFactHandle("handle")).isDisconnected());
cmds = new ArrayList();
cmds.add(new InsertObjectCommand(new String("Hi!"), "handle"));
batchCmd = CommandFactory.newBatchExecution(cmds, "kresults");
execCmd = new ExecuteCommand(batchCmd);
results = execCmd.execute(context);
assertNotNull(results);
assertNotNull(results.getFactHandle("handle"));
assertFalse(((DefaultFactHandle) results.getFactHandle("handle")).isDisconnected());
}
use of org.kie.api.command.BatchExecutionCommand in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method execute.
public <T> T execute(Command<T> command) {
ExecutableRunner<RequestContext> runner = ExecutableRunner.create();
RequestContext context = runner.createContext().with(this.kBase).with(this);
if (!(command instanceof BatchExecutionCommand)) {
return runner.execute(command, context);
}
try {
startBatchExecution();
return runner.execute(command, context);
} finally {
endBatchExecution();
if (kBase.flushModifications()) {
fireAllRules();
}
}
}
Aggregations