use of org.drools.core.command.impl.ContextImpl 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.ContextImpl in project drools by kiegroup.
the class GetAllDMNContextCommandTest method execute.
@Test
public void execute() {
RegistryContext registryContext = new ContextImpl();
GetAllDMNContextCommand getAllDMNContextCommand = new GetAllDMNContextCommand();
assertThatThrownBy(() -> getAllDMNContextCommand.execute(registryContext)).isInstanceOf(IllegalStateException.class).hasMessage("There is not DMNResult available");
DMNResultImpl dmnResult = new DMNResultImpl(null);
dmnResult.setContext(new DMNContextImpl());
registryContext.register(DMNResult.class, dmnResult);
Map<String, Object> result = getAllDMNContextCommand.execute(registryContext);
assertEquals(0, result.size());
}
use of org.drools.core.command.impl.ContextImpl in project drools by kiegroup.
the class GetDMNMessagesCommandTest method execute.
@Test
public void execute() {
RegistryContext registryContext = new ContextImpl();
GetDMNMessagesCommand getDMNMessagesCommand = new GetDMNMessagesCommand();
assertThatThrownBy(() -> getDMNMessagesCommand.execute(registryContext)).isInstanceOf(IllegalStateException.class).hasMessage("There is no DMNResult available");
DMNResultImpl dmnResult = new DMNResultImpl(null);
dmnResult.setContext(new DMNContextImpl());
registryContext.register(DMNResult.class, dmnResult);
List<DMNMessage> result = getDMNMessagesCommand.execute(registryContext);
assertEquals(0, result.size());
}
use of org.drools.core.command.impl.ContextImpl in project drools by kiegroup.
the class SetDMNInputCommandTest method execute.
@Test
public void execute() {
RegistryContext registryContext = new ContextImpl();
String testVariable = "testVariable";
SetDMNInputCommand setDMNInputCommand = new SetDMNInputCommand(testVariable, 10);
setDMNInputCommand.execute(registryContext);
DMNContext lookup = registryContext.lookup(DMNContext.class);
assertTrue(lookup.isDefined(testVariable));
}
use of org.drools.core.command.impl.ContextImpl in project drools by kiegroup.
the class ExecuteDMNCommandTest method execute.
@Test
public void execute() {
RegistryContext registryContext = new ContextImpl();
ExecuteDMNCommand executeDMNCommand = new ExecuteDMNCommand();
assertThatThrownBy(() -> executeDMNCommand.execute(registryContext)).isInstanceOf(IllegalStateException.class).hasMessage("No DMN active model defined");
registryContext.register(DMNModel.class, new DMNModelImpl(null));
assertThatThrownBy(() -> executeDMNCommand.execute(registryContext)).isInstanceOf(IllegalStateException.class).hasMessage("No DMNRuntime available");
DMNContext dmnContext = DMNFactory.newContext();
dmnContext.set("example", 10);
registryContext.register(DMNRuntime.class, new DMNRuntimeImpl(new VoidDMNRuntimeKB()));
registryContext.register(DMNContext.class, dmnContext);
DMNResult result = executeDMNCommand.execute(registryContext);
assertNotNull(result);
DMNContext newDmnContext = registryContext.lookup(DMNContext.class);
assertEquals(1, dmnContext.getAll().size());
assertEquals(0, newDmnContext.getAll().size());
}
Aggregations