Search in sources :

Example 86 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class EndEventTest method testOnEntryExitNamespacedScript.

@Test
public void testOnEntryExitNamespacedScript() throws Exception {
    KieBase kbase = createKnowledgeBaseWithoutDumper("BPMN2-OnEntryExitNamespacedScriptProcess.bpmn2");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("MyTask", new SystemOutWorkItemHandler());
    List<String> myList = new ArrayList<String>();
    ksession.setGlobal("list", myList);
    ProcessInstance processInstance = ksession.startProcess("OnEntryExitScriptProcess");
    assertProcessInstanceCompleted(processInstance);
    assertEquals(4, myList.size());
}
Also used : KieBase(org.kie.api.KieBase) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) ArrayList(java.util.ArrayList) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test)

Example 87 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class IntermediateEventTest method testSignalBoundaryEventNoInteruptOnMultiInstanceSubprocess.

@Test
public void testSignalBoundaryEventNoInteruptOnMultiInstanceSubprocess() throws Exception {
    KieBase kbase = createKnowledgeBase("subprocess/BPMN2-MultiInstanceSubprocessWithBoundarySignalNoInterupting.bpmn2");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    Map<String, Object> params = new HashMap<String, Object>();
    List<String> approvers = new ArrayList<String>();
    approvers.add("john");
    approvers.add("john");
    params.put("approvers", approvers);
    ProcessInstance processInstance = ksession.startProcess("boundary-catch-error-event", params);
    assertProcessInstanceActive(processInstance);
    List<WorkItem> workItems = handler.getWorkItems();
    assertThat(workItems).isNotNull();
    assertThat(workItems.size()).isEqualTo(2);
    ksession.signalEvent("Outside", null, processInstance.getId());
    assertProcessInstanceActive(processInstance.getId(), ksession);
    for (WorkItem wi : workItems) {
        ksession.getWorkItemManager().completeWorkItem(wi.getId(), null);
    }
    assertProcessInstanceFinished(processInstance, ksession);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

Example 88 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class IntermediateEventTest method testIntermediateCatchEventTimerCycleWithErrorWithPersistence.

@Test(timeout = 10000)
@RequirePersistence
public void testIntermediateCatchEventTimerCycleWithErrorWithPersistence() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 2);
    KieBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventTimerCycleWithError.bpmn2");
    ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler());
    ksession.addEventListener(countDownListener);
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent");
    assertProcessInstanceActive(processInstance);
    final long piId = processInstance.getId();
    ksession.execute(new ExecutableCommand<Void>() {

        public Void execute(Context context) {
            StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
            WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(piId);
            processInstance.setVariable("x", 0);
            return null;
        }
    });
    // now wait for 1 second for timer to trigger
    countDownListener.waitTillCompleted();
    assertProcessInstanceActive(processInstance);
    Integer xValue = ksession.execute(new ExecutableCommand<Integer>() {

        public Integer execute(Context context) {
            StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
            WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(piId);
            return (Integer) processInstance.getVariable("x");
        }
    });
    assertThat(xValue).isEqualTo(2);
    ksession.abortProcessInstance(processInstance.getId());
    assertProcessInstanceFinished(processInstance, ksession);
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) Context(org.kie.api.runtime.Context) DoNothingWorkItemHandler(org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) RegistryContext(org.drools.core.command.impl.RegistryContext) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) KieBase(org.kie.api.KieBase) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test) RequirePersistence(org.jbpm.bpmn2.test.RequirePersistence)

Example 89 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class ActivityTest method testUserTaskWithDataStoreScenario.

@Test
public void testUserTaskWithDataStoreScenario() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-UserTaskWithDataStore.bpmn2");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler());
    ksession.startProcess("UserProcess");
// we can't test further as user tasks are asynchronous.
}
Also used : KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) DoNothingWorkItemHandler(org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler) Test(org.junit.Test)

Example 90 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession 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").startNode(1).name("Start").done().humanTaskNode(2).name("Task1").actorId("krisv").taskName("MyTask").done().endNode(3).name("End").done().connection(1, 2).connection(2, 3);
    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(4);
    insertNodeInBetween(process, 2, 3, node);
    ((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) RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) 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) RegistryContext(org.drools.core.command.impl.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)

Aggregations

StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)114 Test (org.junit.Test)79 KieBase (org.kie.api.KieBase)52 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)40 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)30 ArrayList (java.util.ArrayList)29 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)22 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)20 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)18 HashMap (java.util.HashMap)16 Environment (org.kie.api.runtime.Environment)12 KieSession (org.kie.api.runtime.KieSession)11 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)11 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)10 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 WorkItem (org.kie.api.runtime.process.WorkItem)9 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)8 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)8 Resource (org.kie.api.io.Resource)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7