Search in sources :

Example 1 with FireAllRulesCommand

use of org.drools.core.command.runtime.rule.FireAllRulesCommand 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 FireAllRulesCommand

use of org.drools.core.command.runtime.rule.FireAllRulesCommand in project drools by kiegroup.

the class FireAllRulesCommandTest method infiniteLoopTerminatesAtMaxTest.

@Test
public void infiniteLoopTerminatesAtMaxTest() {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " update($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(10, 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.compiler.Cheese) Test(org.junit.Test)

Example 3 with FireAllRulesCommand

use of org.drools.core.command.runtime.rule.FireAllRulesCommand in project drools by kiegroup.

the class FireAllRulesCommandTest method oneRuleFiredWithDefinedMaxTest.

@Test
public void oneRuleFiredWithDefinedMaxTest() {
    String str = "";
    str += "package org.drools.compiler.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.compiler.Cheese) Test(org.junit.Test)

Example 4 with FireAllRulesCommand

use of org.drools.core.command.runtime.rule.FireAllRulesCommand in project jbpm by kiegroup.

the class ETransactionTest method testRuleFlowGroup.

@Test
public void testRuleFlowGroup() throws Exception {
    TrackingAgendaEventListener agenda = new TrackingAgendaEventListener();
    RuntimeManager manager = deploymentService.getRuntimeManager(kieJar);
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    engine.getKieSession().addEventListener(agenda);
    Long processInstanceId = startProcessInstance(PROCESS_ID);
    UserTransaction ut = InitialContext.doLookup(USER_TRANSACTION_NAME);
    ut.begin();
    processService.signalProcessInstance(processInstanceId, "start", "rfg");
    Assertions.assertThat(hasNodeLeft(processInstanceId, "rfg")).isTrue();
    ut.rollback();
    agenda.clear();
    processService.execute(kieJar, new FireAllRulesCommand());
    Assertions.assertThat(agenda.isRuleFired("dummyRule")).isFalse();
    agenda.clear();
    ut = InitialContext.doLookup(USER_TRANSACTION_NAME);
    ut.begin();
    processService.signalProcessInstance(processInstanceId, "start", "rfg");
    ut.commit();
    Assertions.assertThat(hasNodeLeft(processInstanceId, "rfg")).isTrue();
    processService.execute(kieJar, new FireAllRulesCommand());
    processService.signalProcessInstance(processInstanceId, "finish", null);
    Assertions.assertThat(agenda.isRuleFired("dummyRule")).isTrue();
    Assertions.assertThat(hasProcessInstanceCompleted(processInstanceId)).isTrue();
}
Also used : UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TrackingAgendaEventListener(org.jbpm.test.container.listeners.TrackingAgendaEventListener) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) AbstractRuntimeEJBServicesTest(org.jbpm.test.container.AbstractRuntimeEJBServicesTest) Test(org.junit.Test)

Example 5 with FireAllRulesCommand

use of org.drools.core.command.runtime.rule.FireAllRulesCommand in project jbpm by kiegroup.

the class AgendaFilterTest method testActivationCancelled.

@Test
public void testActivationCancelled() {
    // JBRULES-3376
    String drl = "package org.jboss.qa.brms.agendafilter\n" + "declare CancelFact\n" + "   cancel : boolean = true\n" + "end\n" + "rule NoCancel\n" + "   ruleflow-group \"rfg\"\n" + "   when\n" + "       $fact : CancelFact ( cancel == false )\n" + "   then\n" + "       System.out.println(\"No cancel...\");\n" + "       modify ($fact) {\n" + "           setCancel(true);\n" + "       }\n" + "end\n" + "rule PresenceOfBothFacts\n" + "   ruleflow-group \"rfg\"\n" + "   salience -1\n" + "   when\n" + "       $fact1 : CancelFact( cancel == false )\n" + "       $fact2 : CancelFact( cancel == true )\n" + "   then\n" + "       System.out.println(\"Both facts!\");\n" + "end\n" + "rule PresenceOfFact\n" + "   ruleflow-group \"rfg\"\n" + "   when\n" + "       $fact : CancelFact( )\n" + "   then\n" + "       System.out.println(\"We have a \" + ($fact.isCancel() ? \"\" : \"non-\") + \"cancelling fact!\");\n" + "end\n" + "rule Cancel\n" + "   ruleflow-group \"rfg\"\n" + "   when\n" + "       $fact : CancelFact ( cancel == true )\n" + "   then\n" + "       System.out.println(\"Cancel!\");\n" + "end";
    String rf = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"bz761715\" package-name=\"org.jboss.qa.brms.agendafilter\" >\n" + "  <header>\n" + "  </header>\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" x=\"16\" y=\"16\" width=\"48\" height=\"48\" />\n" + "    <ruleSet id=\"2\" name=\"Rule\" x=\"208\" y=\"16\" width=\"80\" height=\"48\" ruleFlowGroup=\"rfg\" />\n" + "    <actionNode id=\"3\" name=\"Script\" x=\"320\" y=\"16\" width=\"80\" height=\"48\" >\n" + "        <action type=\"expression\" dialect=\"java\" >System.out.println(\"Finishing process...\");</action>\n" + "    </actionNode>\n" + "    <end id=\"4\" name=\"End\" x=\"432\" y=\"16\" width=\"48\" height=\"48\" />\n" + "    <actionNode id=\"5\" name=\"Script\" x=\"96\" y=\"16\" width=\"80\" height=\"48\" >\n" + "        <action type=\"expression\" dialect=\"java\" >System.out.println(\"Starting process...\");</action>\n" + "    </actionNode>\n" + "  </nodes>\n" + "  <connections>\n" + "    <connection from=\"5\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "    <connection from=\"3\" to=\"4\" />\n" + "    <connection from=\"1\" to=\"5\" />\n" + "  </connections>\n" + "</process>";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(rf.getBytes()), ResourceType.DRF);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    ksession.addEventListener(new DebugAgendaEventListener());
    ksession.addEventListener(new DebugProcessEventListener());
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(newCancelFact(ksession, false)));
    commands.add(CommandFactory.newInsert(newCancelFact(ksession, true)));
    commands.add(CommandFactory.newStartProcess("bz761715"));
    commands.add(new FireAllRulesCommand(new CancelAgendaFilter()));
    commands.add(new FireAllRulesCommand(new CancelAgendaFilter()));
    commands.add(new FireAllRulesCommand(new CancelAgendaFilter()));
    ksession.execute(CommandFactory.newBatchExecution(commands));
}
Also used : DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) DebugProcessEventListener(org.drools.core.event.DebugProcessEventListener) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) Command(org.kie.api.command.Command) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)11 Test (org.junit.Test)9 Command (org.kie.api.command.Command)9 ArrayList (java.util.ArrayList)7 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)5 ExecutionResults (org.kie.api.runtime.ExecutionResults)4 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)3 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)3 Cheese (org.drools.compiler.Cheese)2 RuleNameEndsWithAgendaFilter (org.drools.core.base.RuleNameEndsWithAgendaFilter)2 ContextImpl (org.drools.core.command.impl.ContextImpl)2 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)2 Cheese (org.drools.mvel.compiler.Cheese)2 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 UserTransaction (javax.transaction.UserTransaction)1