use of org.drools.core.command.impl.ExecutableCommand 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.drools.core.command.impl.ExecutableCommand 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.drools.core.command.impl.ExecutableCommand in project drools by kiegroup.
the class StatelessSessionTest method testInsertObject.
@Test
public void testInsertObject() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import org.drools.compiler.Cheese \n";
str += "rule rule1 \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " \n";
str += " then \n";
str += " $c.setPrice( 30 ); \n";
str += "end\n";
Cheese stilton = new Cheese("stilton", 5);
final StatelessKieSession ksession = getSession2(ResourceFactory.newByteArrayResource(str.getBytes()));
final ExecutableCommand cmd = (ExecutableCommand) CommandFactory.newInsert(stilton, "outStilton");
final BatchExecutionCommandImpl batch = new BatchExecutionCommandImpl(Arrays.asList(new ExecutableCommand<?>[] { cmd }));
final ExecutionResults result = (ExecutionResults) ksession.execute(batch);
stilton = (Cheese) result.getValue("outStilton");
assertEquals(30, stilton.getPrice());
}
use of org.drools.core.command.impl.ExecutableCommand in project drools by kiegroup.
the class AssertEquals method execute.
public Void execute(Context context) {
Object actualObject = ((ExecutableCommand) command).execute(context);
if (this.mvelString != null) {
actualObject = MVELSafeHelper.getEvaluator().eval(this.mvelString, actualObject);
}
if (this.expectedIdentifier != null) {
this.expectedObject = context.get(this.expectedIdentifier);
}
Map vars = new HashMap();
vars.put("expected", expectedObject);
vars.put("actual", actualObject);
if (((Boolean) MVELSafeHelper.getEvaluator().eval("expected != actual", vars))) {
throw new AssertionError(format(this.message, expectedObject, actualObject));
}
return null;
}
use of org.drools.core.command.impl.ExecutableCommand 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);
}
}
}
}
Aggregations