use of org.kie.api.command.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.kie.api.command.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) {
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) context.lookup(ExecutionResultImpl.class);
} else {
return (T) o;
}
} finally {
((StatefulKnowledgeSessionImpl) ksession).endBatchExecution();
dispose(ksession);
}
}
use of org.kie.api.command.ExecutableCommand in project drools by kiegroup.
the class RuleStatefulScenarioExecutableBuilderTest method testBuilder.
@Test
public void testBuilder() {
when(executableBuilderMock.newApplicationContext(anyString())).thenReturn(executableBuilderMock);
when(executableBuilderMock.setKieContainer(any())).thenReturn(kieContainerFluent);
when(kieContainerFluent.newSessionCustomized(any(), any())).thenReturn(kieSessionFluentMock);
when(kieSessionFluentMock.dispose()).thenReturn(executableBuilderMock);
when(kieSessionFluentMock.addCommand(any())).thenReturn(kieSessionFluentMock);
when(executableRunnerMock.execute(Mockito.<Executable>any())).thenReturn(requestContextMock);
when(requestContextMock.getOutputs()).thenReturn(Collections.emptyMap());
RuleStatefulScenarioExecutableBuilder builder = new RuleStatefulScenarioExecutableBuilder(null, null) {
@Override
protected ExecutableBuilder createExecutableBuilder() {
return executableBuilderMock;
}
@Override
protected ExecutableRunner<RequestContext> createExecutableRunner() {
return executableRunnerMock;
}
};
Object toInsert = new Object();
String agendaGroup = "agendaGroup";
String ruleFlowGroup = "ruleFlowGroup";
FactMappingValue indexFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, null);
builder.setActiveAgendaGroup(agendaGroup);
verify(kieSessionFluentMock, times(1)).setActiveAgendaGroup(eq(agendaGroup));
reset(kieContainerFluent);
builder.setActiveRuleFlowGroup(ruleFlowGroup);
verify(kieSessionFluentMock, times(1)).setActiveAgendaGroup(eq(agendaGroup));
reset(kieContainerFluent);
builder.insert(toInsert);
verify(kieSessionFluentMock, times(1)).insert(eq(toInsert));
reset(kieContainerFluent);
builder.addInternalCondition(String.class, obj -> null, new ScenarioResult(indexFMV, null));
Map<String, Object> result = builder.run();
verify(kieSessionFluentMock, times(1)).fireAllRules();
verify(kieSessionFluentMock, times(3)).addCommand(commandArgumentCaptor.capture());
List<ExecutableCommand<?>> allAddCommands = commandArgumentCaptor.getAllValues();
assertTrue(allAddCommands.stream().map(Object::getClass).anyMatch(ValidateFactCommand.class::isAssignableFrom));
assertTrue(allAddCommands.stream().map(Object::getClass).anyMatch(AddCoverageListenerCommand.class::isAssignableFrom));
assertTrue(result.containsKey(RuleScenarioExecutableBuilder.COVERAGE_LISTENER));
verify(kieSessionFluentMock, times(1)).out(eq(RULES_AVAILABLE));
}
use of org.kie.api.command.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 = CoreComponentsBuilder.get().getMVELExecutor().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) CoreComponentsBuilder.get().getMVELExecutor().eval("expected != actual", vars))) {
throw new AssertionError(format(this.message, expectedObject, actualObject));
}
return null;
}
use of org.kie.api.command.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