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();
}
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();
}
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());
}
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);
}
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;
}
Aggregations