Search in sources :

Example 1 with ContextImpl

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);
    }
}
Also used : FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ContextImpl(org.drools.core.command.impl.ContextImpl) BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand)

Example 2 with ContextImpl

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());
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) RegistryContext(org.kie.internal.command.RegistryContext) ContextImpl(org.drools.core.command.impl.ContextImpl) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl) Test(org.junit.Test)

Example 3 with ContextImpl

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());
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) DMNMessage(org.kie.dmn.api.core.DMNMessage) RegistryContext(org.kie.internal.command.RegistryContext) ContextImpl(org.drools.core.command.impl.ContextImpl) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl) Test(org.junit.Test)

Example 4 with ContextImpl

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));
}
Also used : DMNContext(org.kie.dmn.api.core.DMNContext) RegistryContext(org.kie.internal.command.RegistryContext) ContextImpl(org.drools.core.command.impl.ContextImpl) Test(org.junit.Test)

Example 5 with ContextImpl

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());
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) DMNContext(org.kie.dmn.api.core.DMNContext) RegistryContext(org.kie.internal.command.RegistryContext) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) ContextImpl(org.drools.core.command.impl.ContextImpl) VoidDMNRuntimeKB(org.kie.dmn.core.impl.VoidDMNRuntimeKB) Test(org.junit.Test)

Aggregations

ContextImpl (org.drools.core.command.impl.ContextImpl)12 RegistryContext (org.kie.internal.command.RegistryContext)7 Test (org.junit.Test)6 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)3 DMNContextImpl (org.kie.dmn.core.impl.DMNContextImpl)3 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)3 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)2 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)2 Before (org.junit.Before)2 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)2 Command (org.kie.api.command.Command)2 DMNContext (org.kie.dmn.api.core.DMNContext)2 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)2 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 ExecutableCommand (org.kie.api.command.ExecutableCommand)1 Context (org.kie.api.runtime.Context)1 DMNDecisionResult (org.kie.dmn.api.core.DMNDecisionResult)1 DMNMessage (org.kie.dmn.api.core.DMNMessage)1