use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.
the class GetProcessInstancesTest method noProcessInstancesLeftAfterRollback.
@Test
public void noProcessInstancesLeftAfterRollback() throws Exception {
long[] notProcess = new long[2];
UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
ut.begin();
StatefulKnowledgeSession ksession = reloadKnowledgeSession();
notProcess[0] = ksession.createProcessInstance("org.jbpm.processinstance.helloworld", null).getId();
notProcess[1] = ksession.createProcessInstance("org.jbpm.processinstance.helloworld", null).getId();
assertEquals(2, ksession.getProcessInstances().size());
ut.rollback();
// Validate that proc inst mgr cache is also flushed on rollback
assertEquals(0, ksession.getProcessInstances().size());
ksession = reloadKnowledgeSession(ksession);
assertEquals(0, ksession.getProcessInstances().size());
ksession.dispose();
assertProcessInstancesNotExist(notProcess);
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.
the class PersistentStatefulSessionTest method testLocalTransactionPerStatement.
@Test
public void testLocalTransactionPerStatement() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(ruleString.getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
List<?> list = new ArrayList<Object>();
ksession.setGlobal("list", list);
ksession.insert(1);
ksession.insert(2);
ksession.insert(3);
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.
the class PersistentStatefulSessionTest method testPersistenceVariables.
@Test
public void testPersistenceVariables() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("VariablesProcess.rf"), ResourceType.DRF);
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
logger.debug(error.toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
long id = ksession.getIdentifier();
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("name", "John Doe");
ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess", parameters);
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
assertEquals("John Doe", workItem.getParameter("name"));
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNotNull(workItem);
assertEquals("John Doe", workItem.getParameter("text"));
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNull(workItem);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNull(processInstance);
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.
the class PersistentStatefulSessionTest method testPersistenceState.
@Test
public void testPersistenceState() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("StateProcess.rf"), ResourceType.DRF);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
long id = ksession.getIdentifier();
ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
logger.debug("Started process instance {}", processInstance.getId());
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession.insert(new ArrayList<Object>());
ksession.fireAllRules();
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNull(processInstance);
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.
the class PersistentStatefulSessionTest method testProcessListener.
@Test
public void testProcessListener() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
final List<ProcessEvent> events = new ArrayList<ProcessEvent>();
ProcessEventListener listener = new ProcessEventListener() {
public void afterNodeLeft(ProcessNodeLeftEvent event) {
logger.debug("After node left: {}", event.getNodeInstance().getNodeName());
events.add(event);
}
public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
logger.debug("After node triggered: {}", event.getNodeInstance().getNodeName());
events.add(event);
}
public void afterProcessCompleted(ProcessCompletedEvent event) {
logger.debug("After process completed");
events.add(event);
}
public void afterProcessStarted(ProcessStartedEvent event) {
logger.debug("After process started");
events.add(event);
}
public void beforeNodeLeft(ProcessNodeLeftEvent event) {
logger.debug("Before node left: {}", event.getNodeInstance().getNodeName());
events.add(event);
}
public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
logger.debug("Before node triggered: {}", event.getNodeInstance().getNodeName());
events.add(event);
}
public void beforeProcessCompleted(ProcessCompletedEvent event) {
logger.debug("Before process completed");
events.add(event);
}
public void beforeProcessStarted(ProcessStartedEvent event) {
logger.debug("Before process started");
events.add(event);
}
public void afterVariableChanged(ProcessVariableChangedEvent event) {
logger.debug("After Variable Changed");
events.add(event);
}
public void beforeVariableChanged(ProcessVariableChangedEvent event) {
logger.debug("Before Variable Changed");
events.add(event);
}
};
ksession.addEventListener(listener);
ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
logger.debug("Started process instance {}", processInstance.getId());
assertEquals(12, events.size());
assertTrue(events.get(0) instanceof ProcessStartedEvent);
assertTrue(events.get(1) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(2) instanceof ProcessNodeLeftEvent);
assertTrue(events.get(3) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(4) instanceof ProcessNodeLeftEvent);
assertTrue(events.get(5) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(6) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(7) instanceof ProcessNodeLeftEvent);
assertTrue(events.get(8) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(9) instanceof ProcessNodeLeftEvent);
assertTrue(events.get(10) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(11) instanceof ProcessStartedEvent);
ksession.removeEventListener(listener);
events.clear();
processInstance = ksession.startProcess("org.drools.test.TestProcess");
logger.debug("Started process instance {}", processInstance.getId());
assertTrue(events.isEmpty());
}
Aggregations