Search in sources :

Example 21 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class FireAllRulesCommandTest method fiveRulesFiredTest.

@Test
public void fiveRulesFiredTest() {
    String str = "";
    str += "package org.drools.mvel.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";
    StatelessKieSession ksession = getSession(str);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(new Cheese("stilton")));
    commands.add(CommandFactory.newInsert(new Cheese("gruyere")));
    commands.add(CommandFactory.newInsert(new Cheese("cheddar")));
    commands.add(CommandFactory.newInsert(new Cheese("stinky")));
    commands.add(CommandFactory.newInsert(new Cheese("limburger")));
    commands.add(CommandFactory.newFireAllRules("num-rules-fired"));
    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
    assertEquals(5, fired);
}
Also used : Command(org.kie.api.command.Command) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 22 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class FireAllRulesCommandTest method oneRuleFiredWithDefinedMaxTest.

@Test
public void oneRuleFiredWithDefinedMaxTest() {
    String str = "";
    str += "package org.drools.mvel.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";
    StatelessKieSession ksession = getSession(str);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(new Cheese("stilton")));
    FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
    farc.setOutIdentifier("num-rules-fired");
    commands.add(farc);
    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
    assertEquals(1, fired);
}
Also used : Command(org.kie.api.command.Command) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 23 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults 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)

Example 24 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class SimpleBatchExecutionTest method testGetObjectCommand.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectCommand() throws Exception {
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    FactHandle handle_1 = ksession.insert(expected_1);
    FactHandle handle_2 = ksession.insert(expected_2);
    ksession.fireAllRules();
    Object fact = ksession.getObject(handle_1);
    assertNotNull(fact);
    assertEquals(expected_1, fact);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newGetObject(handle_1, "out_1"));
    commands.add(CommandFactory.newGetObject(handle_2, "out_2"));
    Command cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("GetObjectCommand result is null!", result);
    assertEquals(expected_1, result.getValue("out_1"));
    assertEquals(expected_2, result.getValue("out_2"));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 25 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class SimpleBatchExecutionTest method testInsertObjectCommand.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertObjectCommand() throws Exception {
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(expected_1, "out_1"));
    commands.add(CommandFactory.newInsert(expected_2, "out_2"));
    Command cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    Object fact_1 = result.getValue("out_1");
    assertNotNull(fact_1);
    Object fact_2 = result.getValue("out_2");
    assertNotNull(fact_2);
    ksession.fireAllRules();
    Object[] expectedArr = { expected_1, expected_2 };
    List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr));
    Collection<? extends Object> factList = ksession.getObjects();
    assertTrue("Expected " + expectedList.size() + " objects but retrieved " + factList.size(), factList.size() == expectedList.size());
    for (Object fact : factList) {
        if (expectedList.contains(fact)) {
            expectedList.remove(fact);
        }
    }
    assertTrue("Retrieved object list did not contain expected objects.", expectedList.isEmpty());
}
Also used : Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ExecutionResults (org.kie.api.runtime.ExecutionResults)41 Test (org.junit.Test)39 ArrayList (java.util.ArrayList)37 Command (org.kie.api.command.Command)35 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)17 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)11 Cheese (org.drools.compiler.Cheese)10 Cheese (org.drools.mvel.compiler.Cheese)10 List (java.util.List)9 KieSession (org.kie.api.runtime.KieSession)8 KieBase (org.kie.api.KieBase)7 FactHandle (org.kie.api.runtime.rule.FactHandle)5 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)4 QueryResults (org.kie.api.runtime.rule.QueryResults)4 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)3 KieServices (org.kie.api.KieServices)3 ExecutableCommand (org.kie.api.command.ExecutableCommand)3 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)3