Search in sources :

Example 11 with CommandBasedStatefulKnowledgeSession

use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project drools by kiegroup.

the class AgendaRuleFlowGroupsTest method testAgendaGroupAndRuleFlowGroup.

@Test
public void testAgendaGroupAndRuleFlowGroup() throws Exception {
    CommandBasedStatefulKnowledgeSession ksession = createSession(-1, "agenda-groups.drl", "ruleflow-groups.drl");
    org.drools.core.spi.AgendaGroup[] groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroupsManager().getAgendaGroups();
    // only main is available
    assertEquals(1, groups.length);
    assertEquals("MAIN", groups[0].getName());
    long id = ksession.getIdentifier();
    List<String> list = new ArrayList<String>();
    list.add("Test");
    ksession.insert(list);
    ksession.execute(new ActivateAgendaGroupCommand("agenda-group"));
    ksession.execute(new ActivateRuleFlowCommand("ruleflow-group"));
    ksession.dispose();
    ksession = createSession(id, "agenda-groups.drl", "ruleflow-groups.drl");
    groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroupsManager().getAgendaGroups();
    // main and agenda group is now on the agenda
    assertEquals(3, groups.length);
    assertEquals("MAIN", groups[0].getName());
    assertEquals("ruleflow-group", groups[1].getName());
    assertEquals("agenda-group", groups[2].getName());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) ArrayList(java.util.ArrayList) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) Test(org.junit.Test)

Example 12 with CommandBasedStatefulKnowledgeSession

use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project jbpm by kiegroup.

the class DynamicUtils method internalAddDynamicSubProcess.

public static long internalAddDynamicSubProcess(final WorkflowProcessInstance processInstance, final DynamicNodeInstance dynamicContext, KieRuntime ksession, final String processId, final Map<String, Object> parameters) {
    final SubProcessNodeInstance subProcessNodeInstance = new SubProcessNodeInstance();
    subProcessNodeInstance.setNodeInstanceContainer(dynamicContext == null ? processInstance : dynamicContext);
    subProcessNodeInstance.setProcessInstance(processInstance);
    subProcessNodeInstance.setMetaData("NodeType", "SubProcessNode");
    if (ksession instanceof StatefulKnowledgeSessionImpl) {
        return executeSubProcess((StatefulKnowledgeSessionImpl) ksession, processId, parameters, processInstance, subProcessNodeInstance);
    } else if (ksession instanceof CommandBasedStatefulKnowledgeSession) {
        ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
        return commandService.execute(new ExecutableCommand<Long>() {

            private static final long serialVersionUID = 5L;

            public Long execute(Context context) {
                StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
                WorkflowProcessInstance realProcessInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstance.getId());
                subProcessNodeInstance.setProcessInstance(realProcessInstance);
                if (dynamicContext == null) {
                    subProcessNodeInstance.setNodeInstanceContainer(realProcessInstance);
                } else {
                    DynamicNodeInstance realDynamicContext = findDynamicContext(realProcessInstance, dynamicContext.getUniqueId());
                    subProcessNodeInstance.setNodeInstanceContainer(realDynamicContext);
                }
                return executeSubProcess((StatefulKnowledgeSessionImpl) ksession, processId, parameters, processInstance, subProcessNodeInstance);
            }
        });
    } else {
        throw new IllegalArgumentException("Unsupported ksession: " + (ksession == null ? "null" : ksession.getClass().getName()));
    }
}
Also used : RegistryContext(org.kie.internal.command.RegistryContext) Context(org.kie.api.runtime.Context) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ExecutableCommand(org.kie.api.command.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance)

Example 13 with CommandBasedStatefulKnowledgeSession

use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project jbpm by kiegroup.

the class DynamicProcessTest method testDynamicProcess.

@Test
public void testDynamicProcess() throws Exception {
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.HelloWorld");
    factory.name("HelloWorldProcess").version("1.0").packageName("org.jbpm").imports("org.jbpm.bpmn2.objects.Person").startNode(1).name("Start").done().humanTaskNode(2).name("Task1").actorId("krisv").taskName("MyTask").done().splitNode(3).name("Split").type(Split.TYPE_XOR).constraint(4, "c1", "code", Dialect.JAVA, "return new Person().getName() == null;", 1).constraint(5, "c2", "code", Dialect.JAVA, "return false;", 2).done().endNode(4).name("End1").done().endNode(5).name("End2").done().connection(1, 2).connection(2, 3).connection(3, 4).connection(3, 5);
    final RuleFlowProcess process = factory.validate().getProcess();
    Resource resource = ResourceFactory.newByteArrayResource(XmlRuleFlowProcessDumper.INSTANCE.dump(process).getBytes());
    // source path or target path must be set to be added into kbase
    resource.setSourcePath("/tmp/dynamicProcess.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(resource);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler testHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", testHandler);
    ksession.addEventListener(new ProcessEventListener() {

        public void beforeVariableChanged(ProcessVariableChangedEvent arg0) {
        }

        public void beforeProcessStarted(ProcessStartedEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent arg0) {
            logger.info("{}", arg0);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent arg0) {
        }

        public void afterProcessStarted(ProcessStartedEvent arg0) {
        }

        public void afterProcessCompleted(ProcessCompletedEvent arg0) {
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) {
        }

        public void afterNodeLeft(ProcessNodeLeftEvent arg0) {
        }
    });
    final ProcessInstanceImpl processInstance = (ProcessInstanceImpl) ksession.startProcess("org.jbpm.HelloWorld");
    HumanTaskNode node = new HumanTaskNode();
    node.setName("Task2");
    node.setId(6);
    List<DroolsAction> actions = new ArrayList<DroolsAction>();
    actions.add(new DroolsConsequenceAction("java", "System.out.println(\"OnEntry\");"));
    node.setActions("onEntry", actions);
    insertNodeInBetween(process, 2, 3, node);
    XmlBPMNProcessDumper.INSTANCE.dump(process);
    ((CommandBasedStatefulKnowledgeSession) ksession).getRunner().execute(new ExecutableCommand<Void>() {

        public Void execute(Context context) {
            StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
            ((ProcessInstanceImpl) ksession.getProcessInstance(processInstance.getId())).updateProcess(process);
            return null;
        }
    });
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) DroolsAction(org.jbpm.workflow.core.DroolsAction) RegistryContext(org.kie.internal.command.RegistryContext) Context(org.kie.api.runtime.Context) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) Resource(org.kie.api.io.Resource) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) ArrayList(java.util.ArrayList) RegistryContext(org.kie.internal.command.RegistryContext) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) RuleFlowProcessFactory(org.jbpm.ruleflow.core.RuleFlowProcessFactory) KieBase(org.kie.api.KieBase) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode) Test(org.junit.Test)

Example 14 with CommandBasedStatefulKnowledgeSession

use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project jbpm by kiegroup.

the class TimerCycleOnBinaryPackageTest method testStartTimerCycleFromDisc.

@Test(timeout = 20000)
public void testStartTimerCycleFromDisc() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("start", 2);
    KieBase kbase = createKnowledgeBaseFromDisc("BPMN2-StartTimerCycle.bpmn2");
    try {
        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
        ksession.addEventListener(countDownListener);
        assertEquals(0, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        long sessionId = ksession.getIdentifier();
        Environment env = ksession.getEnvironment();
        final List<Long> list = new ArrayList<Long>();
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                list.add(event.getProcessInstance().getId());
            }
        });
        ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
        countDownListener.waitTillCompleted();
        assertEquals(2, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        logger.info("dispose");
        ksession.dispose();
        countDownListener = new NodeLeftCountDownProcessEventListener("start", 2);
        ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
        ksession.addEventListener(countDownListener);
        AuditLoggerFactory.newInstance(Type.JPA, ksession, null);
        final List<Long> list2 = new ArrayList<Long>();
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                list2.add(event.getProcessInstance().getId());
            }
        });
        ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
        countDownListener.waitTillCompleted();
        assertEquals(4, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        abortProcessInstances(ksession);
        ksession.dispose();
    } finally {
        ksession = createKnowledgeSession(kbase);
        abortProcessInstances(ksession);
        ksession.dispose();
    }
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.process.NodeLeftCountDownProcessEventListener) KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) Environment(org.kie.api.runtime.Environment) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) TriggerRulesEventListener(org.jbpm.process.instance.event.listeners.TriggerRulesEventListener) Test(org.junit.Test)

Example 15 with CommandBasedStatefulKnowledgeSession

use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project jbpm by kiegroup.

the class TimerCycleOnBinaryPackageTest method testStartTimerCycleFromClasspathDRL.

@Test
@Ignore("beta4 phreak")
public void testStartTimerCycleFromClasspathDRL() throws Exception {
    KieBase kbase = createKnowledgeBaseWithoutDumper("rules-timer.drl");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    long sessionId = ksession.getIdentifier();
    Environment env = ksession.getEnvironment();
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
    ksession.fireAllRules();
    Thread.sleep(5000);
    assertEquals(2, list.size());
    logger.info("dispose");
    ksession.dispose();
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
    AuditLoggerFactory.newInstance(Type.JPA, ksession, null);
    final List<String> list2 = new ArrayList<String>();
    ksession.setGlobal("list", list2);
    ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
    ksession.fireAllRules();
    Thread.sleep(5000);
    assertEquals(3, list2.size());
}
Also used : KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) TriggerRulesEventListener(org.jbpm.process.instance.event.listeners.TriggerRulesEventListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)18 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)10 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)8 KieBase (org.kie.api.KieBase)7 Environment (org.kie.api.runtime.Environment)7 TriggerRulesEventListener (org.jbpm.process.instance.event.listeners.TriggerRulesEventListener)5 Context (org.kie.api.runtime.Context)4 RegistryContext (org.kie.internal.command.RegistryContext)4 InternalAgenda (org.drools.core.common.InternalAgenda)3 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)3 ExecutableCommand (org.kie.api.command.ExecutableCommand)3 HashMap (java.util.HashMap)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 ClassPathResource (org.drools.core.io.impl.ClassPathResource)2 PersistableRunner (org.drools.persistence.PersistableRunner)2 OptimisticLockRetryInterceptor (org.drools.persistence.jpa.OptimisticLockRetryInterceptor)2 DroolsPersistenceUtil.createEnvironment (org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment)2 RequirePersistence (org.jbpm.bpmn2.test.RequirePersistence)2 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.process.NodeLeftCountDownProcessEventListener)2