Search in sources :

Example 11 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class SingleSessionCommandServiceTest method testPersistenceWorkItems.

@Test
public void testPersistenceWorkItems() throws Exception {
    setUp();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KiePackage> kpkgs = getProcessWorkItems();
    kbase.addPackages(kpkgs);
    Properties properties = new Properties();
    properties.setProperty("drools.commandService", PersistableRunner.class.getName());
    properties.setProperty("drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty("drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty("drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    SessionConfiguration config = SessionConfiguration.newInstance(properties);
    PersistableRunner service = new PersistableRunner(kbase, config, env);
    Long sessionId = service.getSessionId();
    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    ProcessInstance processInstance = service.execute(startProcessCommand);
    logger.info("Started process instance {}", processInstance.getId());
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    workItem = handler.getWorkItem();
    assertNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) JPASignalManagerFactory(org.jbpm.persistence.processinstance.JPASignalManagerFactory) CompleteWorkItemCommand(org.drools.core.command.runtime.process.CompleteWorkItemCommand) Properties(java.util.Properties) JPAWorkItemManagerFactory(org.drools.persistence.jpa.processinstance.JPAWorkItemManagerFactory) StartProcessCommand(org.drools.core.command.runtime.process.StartProcessCommand) WorkItem(org.kie.api.runtime.process.WorkItem) PersistableRunner(org.drools.persistence.PersistableRunner) GetProcessInstanceCommand(org.drools.core.command.runtime.process.GetProcessInstanceCommand) KiePackage(org.kie.api.definition.KiePackage) JPAProcessInstanceManagerFactory(org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) SessionConfiguration(org.drools.core.SessionConfiguration) JpaJDKTimerService(org.drools.persistence.jpa.JpaJDKTimerService) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 12 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class SingleSessionCommandServiceTest method testPersistenceSubProcess.

@Test
public void testPersistenceSubProcess() {
    setUp();
    Properties properties = new Properties();
    properties.setProperty("drools.commandService", PersistableRunner.class.getName());
    properties.setProperty("drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty("drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty("drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    SessionConfiguration config = SessionConfiguration.newInstance(properties);
    InternalKnowledgeBase ruleBase = KnowledgeBaseFactory.newKnowledgeBase();
    KiePackage pkg = getProcessSubProcess();
    ruleBase.addPackages((Collection) Arrays.asList(pkg));
    PersistableRunner service = new PersistableRunner(ruleBase, config, env);
    Long sessionId = service.getSessionId();
    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    RuleFlowProcessInstance processInstance = (RuleFlowProcessInstance) service.execute(startProcessCommand);
    logger.info("Started process instance {}", processInstance.getId());
    long processInstanceId = processInstance.getId();
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, ruleBase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    SubProcessNodeInstance subProcessNodeInstance = (SubProcessNodeInstance) nodeInstances.iterator().next();
    long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    RuleFlowProcessInstance subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(subProcessInstance);
    service.dispose();
    service = new PersistableRunner(sessionId, ruleBase, config, env);
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    service.dispose();
    service = new PersistableRunner(sessionId, ruleBase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(subProcessInstance);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) JPASignalManagerFactory(org.jbpm.persistence.processinstance.JPASignalManagerFactory) CompleteWorkItemCommand(org.drools.core.command.runtime.process.CompleteWorkItemCommand) Properties(java.util.Properties) JPAWorkItemManagerFactory(org.drools.persistence.jpa.processinstance.JPAWorkItemManagerFactory) StartProcessCommand(org.drools.core.command.runtime.process.StartProcessCommand) WorkItem(org.kie.api.runtime.process.WorkItem) PersistableRunner(org.drools.persistence.PersistableRunner) GetProcessInstanceCommand(org.drools.core.command.runtime.process.GetProcessInstanceCommand) SubProcessNodeInstance(org.jbpm.workflow.instance.node.SubProcessNodeInstance) KiePackage(org.kie.api.definition.KiePackage) JPAProcessInstanceManagerFactory(org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory) SubProcessNodeInstance(org.jbpm.workflow.instance.node.SubProcessNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) SessionConfiguration(org.drools.core.SessionConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) JpaJDKTimerService(org.drools.persistence.jpa.JpaJDKTimerService) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 13 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class VariablePersistenceStrategyTest method testAbortWorkItemWithVariablePersistence.

@Test
public void testAbortWorkItemWithVariablePersistence() throws Exception {
    MyEntity myEntity = new MyEntity("This is a test Entity");
    MyVariableSerializable myVariableSerializable = new MyVariableSerializable("This is a test SerializableObject");
    EntityManager em = emf.createEntityManager();
    UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    utx.begin();
    em.joinTransaction();
    em.persist(myEntity);
    utx.commit();
    em.close();
    Environment env = createEnvironment();
    KieBase kbase = createKnowledgeBase("VPSProcessWithWorkItems.rf");
    KieSession ksession = createSession(kbase, env);
    logger.debug("### Starting process ###");
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("x", "SomeString");
    parameters.put("y", myEntity);
    parameters.put("z", myVariableSerializable);
    long processInstanceId = ksession.startProcess("com.sample.ruleflow", parameters).getId();
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    logger.debug("### Retrieving process instance ###");
    ksession = reloadSession(ksession, kbase, env);
    WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    assertEquals("SomeString", processInstance.getVariable("x"));
    assertEquals("This is a test Entity", ((MyEntity) processInstance.getVariable("y")).getTest());
    assertEquals("This is a test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("z")).getText());
    assertNull(processInstance.getVariable("a"));
    assertNull(processInstance.getVariable("b"));
    assertNull(processInstance.getVariable("c"));
    logger.debug("### Completing first work item ###");
    Map<String, Object> results = new HashMap<String, Object>();
    results.put("zeta", processInstance.getVariable("z"));
    results.put("equis", processInstance.getVariable("x") + "->modifiedResult");
    // we simulate a failure here, aborting the work item
    ksession.getWorkItemManager().abortWorkItem(workItem.getId());
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    logger.debug("### Retrieving process instance ###");
    ksession = reloadSession(ksession, kbase, env);
    processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    logger.debug("######## Getting the already Persisted Variables #########");
    // we expect the variables to be unmodifed
    assertEquals("SomeString", processInstance.getVariable("x"));
    assertEquals("This is a test Entity", ((MyEntity) processInstance.getVariable("y")).getTest());
    assertEquals("This is a test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("z")).getText());
    assertEquals("Some new String", processInstance.getVariable("a"));
    assertEquals("This is a new test Entity", ((MyEntity) processInstance.getVariable("b")).getTest());
    assertEquals("This is a new test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("c")).getText());
}
Also used : MyEntity(org.jbpm.persistence.session.objects.MyEntity) UserTransaction(javax.transaction.UserTransaction) TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) HashMap(java.util.HashMap) WorkItem(org.kie.api.runtime.process.WorkItem) InitialContext(javax.naming.InitialContext) EntityManager(javax.persistence.EntityManager) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) MyVariableSerializable(org.jbpm.persistence.session.objects.MyVariableSerializable) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 14 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class VariablePersistenceStrategyTest method testPersistenceVariablesSubProcess.

@Test
public void testPersistenceVariablesSubProcess() throws NamingException, NotSupportedException, SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    MyEntity myEntity = new MyEntity("This is a test Entity with annotation in fields");
    MyEntityMethods myEntityMethods = new MyEntityMethods("This is a test Entity with annotations in methods");
    MyEntityOnlyFields myEntityOnlyFields = new MyEntityOnlyFields("This is a test Entity with annotations in fields and without accesors methods");
    MyVariableSerializable myVariableSerializable = new MyVariableSerializable("This is a test SerializableObject");
    EntityManager em = emf.createEntityManager();
    UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    utx.begin();
    em.joinTransaction();
    em.persist(myEntity);
    em.persist(myEntityMethods);
    em.persist(myEntityOnlyFields);
    utx.commit();
    em.close();
    Environment env = createEnvironment();
    KieBase kbase = createKnowledgeBase("VariablePersistenceStrategySubProcess.rf");
    KieSession ksession = createSession(kbase, env);
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("x", "SomeString");
    parameters.put("y", myEntity);
    parameters.put("m", myEntityMethods);
    parameters.put("f", myEntityOnlyFields);
    parameters.put("z", myVariableSerializable);
    long processInstanceId = ksession.startProcess("com.sample.ruleflow", parameters).getId();
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = ksession.getProcessInstance(processInstanceId);
    assertNull(processInstance);
}
Also used : MyEntity(org.jbpm.persistence.session.objects.MyEntity) UserTransaction(javax.transaction.UserTransaction) TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) HashMap(java.util.HashMap) WorkItem(org.kie.api.runtime.process.WorkItem) InitialContext(javax.naming.InitialContext) MyEntityOnlyFields(org.jbpm.persistence.session.objects.MyEntityOnlyFields) EntityManager(javax.persistence.EntityManager) MyEntityMethods(org.jbpm.persistence.session.objects.MyEntityMethods) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) MyVariableSerializable(org.jbpm.persistence.session.objects.MyVariableSerializable) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 15 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class TestWorkItemHandler method getWorkItem.

public WorkItem getWorkItem() {
    WorkItem result = workItem;
    workItem = null;
    return result;
}
Also used : WorkItem(org.kie.api.runtime.process.WorkItem)

Aggregations

WorkItem (org.kie.api.runtime.process.WorkItem)174 Test (org.junit.Test)139 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)127 KieBase (org.kie.api.KieBase)110 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)91 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)87 HashMap (java.util.HashMap)72 KieSession (org.kie.api.runtime.KieSession)48 WorkItemManager (org.kie.api.runtime.process.WorkItemManager)33 ArrayList (java.util.ArrayList)29 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)27 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)26 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)22 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)20 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)14 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)13 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)13 Map (java.util.Map)11 StringReader (java.io.StringReader)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)10