Search in sources :

Example 6 with ExecutionResultImpl

use of org.drools.core.runtime.impl.ExecutionResultImpl in project drools by kiegroup.

the class ApplyPmmlModelCommand method execute.

@Override
public PMML4Result execute(Context context) {
    if (requestData == null) {
        throw new IllegalStateException("ApplyPmmlModelCommand requires request data (PMMLRequestData) to execute");
    }
    RegistryContext registryContext = (RegistryContext) context;
    PMML4Result toReturn = PMMLCommandExecutorFactory.get().newPMMLCommandExecutor().execute(requestData, context);
    // Needed to update the ExecutionResultImpl and the Registry context,
    // as done inside legacy implementation
    Optional<ExecutionResultImpl> execRes = Optional.ofNullable(registryContext.lookup(ExecutionResultImpl.class));
    registryContext.register(PMML4Result.class, toReturn);
    execRes.ifPresent(result -> result.setResult("results", toReturn));
    return toReturn;
}
Also used : PMML4Result(org.kie.api.pmml.PMML4Result) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) RegistryContext(org.kie.internal.command.RegistryContext)

Example 7 with ExecutionResultImpl

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

Example 8 with ExecutionResultImpl

use of org.drools.core.runtime.impl.ExecutionResultImpl 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());
}
Also used : KieBase(org.kie.api.KieBase) ExecutionResults(org.kie.api.runtime.ExecutionResults) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ExecuteCommand(org.drools.core.command.ExecuteCommand) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.kie.api.runtime.RequestContext) InsertObjectCommand(org.drools.core.command.runtime.rule.InsertObjectCommand) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) Test(org.junit.Test)

Example 9 with ExecutionResultImpl

use of org.drools.core.runtime.impl.ExecutionResultImpl in project drools by kiegroup.

the class GetGlobalCommand method execute.

public Object execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    Object object = ksession.getGlobal(identifier);
    ExecutionResultImpl results = ((RegistryContext) context).lookup(ExecutionResultImpl.class);
    if (results != null) {
        results.getResults().put((this.outIdentifier != null) ? this.outIdentifier : this.identifier, object);
    }
    return object;
}
Also used : ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.kie.internal.command.RegistryContext)

Example 10 with ExecutionResultImpl

use of org.drools.core.runtime.impl.ExecutionResultImpl in project drools by kiegroup.

the class ExecuteCommand method execute.

public ExecutionResults execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ExecutionResults kresults = ksession.execute(this.command);
    if (this.outIdentifier != null) {
        ((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, kresults);
    }
    if (disconnected) {
        ExecutionResultImpl disconnectedResults = new ExecutionResultImpl();
        HashMap<String, Object> disconnectedHandles = new HashMap<String, Object>();
        for (String key : kresults.getIdentifiers()) {
            FactHandle handle = (FactHandle) kresults.getFactHandle(key);
            if (handle != null) {
                DefaultFactHandle disconnectedHandle = ((DefaultFactHandle) handle).clone();
                disconnectedHandle.disconnect();
                disconnectedHandles.put(key, disconnectedHandle);
            }
        }
        disconnectedResults.setFactHandles(disconnectedHandles);
        disconnectedResults.setResults((HashMap) ((ExecutionResultImpl) kresults).getResults());
        return disconnectedResults;
    }
    return kresults;
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) HashMap(java.util.HashMap) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ExecutionResults(org.kie.api.runtime.ExecutionResults) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.kie.internal.command.RegistryContext)

Aggregations

ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)13 Test (org.junit.Test)6 KieSession (org.kie.api.runtime.KieSession)6 RegistryContext (org.kie.internal.command.RegistryContext)6 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)5 FactHandle (org.kie.api.runtime.rule.FactHandle)5 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)4 ExecutionResults (org.kie.api.runtime.ExecutionResults)3 List (java.util.List)2 ExecuteCommand (org.drools.core.command.ExecuteCommand)2 ContextImpl (org.drools.core.command.impl.ContextImpl)2 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)2 KieBase (org.kie.api.KieBase)2 Command (org.kie.api.command.Command)2 ExecutableRunner (org.kie.api.runtime.ExecutableRunner)2 RequestContext (org.kie.api.runtime.RequestContext)2 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)2