Search in sources :

Example 1 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) {
            ((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 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) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) Test(org.junit.Test)

Example 3 with ExecutionResultImpl

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

the class XStreamXMLTest method testGetFactHandlesExecutionResults.

@Test
public void testGetFactHandlesExecutionResults() {
    final Message msg = new Message("Hello World!");
    final FactHandle msgHandle = new DefaultFactHandle(1, null, 10, 10, 20, msg);
    final Message msg2 = new Message("Hello World again!");
    final FactHandle msgHandle2 = new DefaultFactHandle(2, null, 10, 10, 20, msg2);
    List<FactHandle> factHandleList = new ArrayList<FactHandle>();
    factHandleList.add(msgHandle);
    factHandleList.add(msgHandle2);
    HashMap<String, Object> factHandles = new LinkedHashMap<String, Object>();
    ExecutionResultImpl executionResult = new ExecutionResultImpl();
    executionResult.setFactHandles(factHandles);
    HashMap<String, Object> results = new LinkedHashMap<String, Object>();
    results.put("facts", factHandleList);
    executionResult.setResults(results);
    String xmlString = xstream.toXML(executionResult);
    Assert.assertEquals("<execution-results>\n" + "  <result identifier=\"facts\">\n" + "    <list>\n" + "      <fact-handle external-form=\"0:1:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "      <fact-handle external-form=\"0:2:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "    </list>\n" + "  </result>\n" + "</execution-results>", xmlString);
    ExecutionResultImpl executionResult2 = (ExecutionResultImpl) xstream.fromXML(xmlString);
    Assert.assertEquals(executionResult.getFactHandles().size(), executionResult2.getFactHandles().size());
    Assert.assertEquals(executionResult.getResults().size(), executionResult2.getResults().size());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 4 with ExecutionResultImpl

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

the class XStreamXMLTest method testExecutionResults.

@Test
public void testExecutionResults() {
    final Message msg = new Message("Hello World!");
    final FactHandle msgHandle = new DefaultFactHandle(1, null, 10, 10, 20, msg);
    final Message msg2 = new Message("Hello World again!");
    final FactHandle msgHandle2 = new DefaultFactHandle(2, null, 10, 10, 20, msg2);
    HashMap<String, Object> factHandles = new LinkedHashMap<String, Object>();
    factHandles.put("first", msgHandle);
    factHandles.put("second", msgHandle2);
    ExecutionResultImpl executionResult = new ExecutionResultImpl();
    executionResult.setFactHandles(factHandles);
    HashMap<String, Object> results = new LinkedHashMap<String, Object>();
    results.put("message1", msg);
    results.put("message2", msg2);
    executionResult.setResults(results);
    String xmlString = xstream.toXML(executionResult);
    Assert.assertEquals("<execution-results>\n" + "  <result identifier=\"message1\">\n" + "    <org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "      <msg>Hello World!</msg>\n" + "    </org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "  </result>\n" + "  <result identifier=\"message2\">\n" + "    <org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "      <msg>Hello World again!</msg>\n" + "    </org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "  </result>\n" + "  <fact-handle identifier=\"first\" external-form=\"0:1:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "  <fact-handle identifier=\"second\" external-form=\"0:2:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "</execution-results>", xmlString);
    ExecutionResultImpl executionResult2 = (ExecutionResultImpl) xstream.fromXML(xmlString);
    Assert.assertEquals(executionResult.getFactHandles().size(), executionResult2.getFactHandles().size());
    Assert.assertEquals(executionResult.getResults().size(), executionResult2.getResults().size());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 5 with ExecutionResultImpl

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

the class XStreamXMLTest method testGetFactHandlesExecutionResults.

@Test
public void testGetFactHandlesExecutionResults() {
    final Message msg = new Message("Hello World!");
    final FactHandle msgHandle = new DefaultFactHandle(1, null, 10, 10, 20, msg);
    final Message msg2 = new Message("Hello World again!");
    final FactHandle msgHandle2 = new DefaultFactHandle(2, null, 10, 10, 20, msg2);
    List<FactHandle> factHandleList = new ArrayList<FactHandle>();
    factHandleList.add(msgHandle);
    factHandleList.add(msgHandle2);
    HashMap<String, Object> factHandles = new LinkedHashMap<String, Object>();
    ExecutionResultImpl executionResult = new ExecutionResultImpl();
    executionResult.setFactHandles(factHandles);
    HashMap<String, Object> results = new LinkedHashMap<String, Object>();
    results.put("facts", factHandleList);
    executionResult.setResults(results);
    String xmlString = xstream.toXML(executionResult);
    Assert.assertEquals("<execution-results>\n" + "  <result identifier=\"facts\">\n" + "    <list>\n" + "      <fact-handle external-form=\"0:1:10:10:20:null:NON_TRAIT:org.drools.xml.support.XStreamXMLTest$Message\"/>\n" + "      <fact-handle external-form=\"0:2:10:10:20:null:NON_TRAIT:org.drools.xml.support.XStreamXMLTest$Message\"/>\n" + "    </list>\n" + "  </result>\n" + "</execution-results>", xmlString);
    ExecutionResultImpl executionResult2 = (ExecutionResultImpl) xstream.fromXML(xmlString);
    Assert.assertEquals(executionResult.getFactHandles().size(), executionResult2.getFactHandles().size());
    Assert.assertEquals(executionResult.getResults().size(), executionResult2.getResults().size());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

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