use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.
the class AbstractAuditLogServiceTest method runTestLogger4WithCustomVariableIndexer.
public static void runTestLogger4WithCustomVariableIndexer(KieSession session, AuditLogService auditLogService) throws Exception {
final List<Long> workItemIds = new ArrayList<Long>();
session.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
workItemIds.add(workItem.getId());
}
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
}
});
// record the initial count to compare to later
List<ProcessInstanceLog> processInstances = auditLogService.findProcessInstances("com.sample.ruleflow");
int initialProcessInstanceSize = processInstances.size();
// start process instance
Map<String, Object> params = new HashMap<String, Object>();
List<String> list = new LinkedList<String>();
list.add("One");
list.add("Two");
list.add("Three");
params.put("list", list);
long processInstanceId = session.startProcess("com.sample.ruleflow3", params).getId();
// Test findVariableInstancesByName* methods: check for variables (only) in active processes
List<VariableInstanceLog> varLogs = auditLogService.findVariableInstancesByName("s", true);
Assertions.assertThat(varLogs).isNotEmpty();
Assertions.assertThat(varLogs.size()).isEqualTo(1);
for (Long workItemId : workItemIds) {
Map<String, Object> results = new HashMap<String, Object>();
results.put("Result", "ResultValue");
session.getWorkItemManager().completeWorkItem(workItemId, results);
}
logger.debug("Checking process instances for process 'com.sample.ruleflow3'");
processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
Assertions.assertThat(processInstances.size()).isEqualTo(initialProcessInstanceSize + 1);
ProcessInstanceLog processInstance = processInstances.get(initialProcessInstanceSize);
logger.debug("{} -> {} - {}", processInstance.toString(), processInstance.getStart(), processInstance.getEnd());
Assertions.assertThat(processInstance.getStart()).isNotNull();
// ProcessInstanceLog does not contain end date.
Assertions.assertThat(processInstance.getEnd()).isNotNull();
Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
List<VariableInstanceLog> variableInstances = auditLogService.findVariableInstances(processInstanceId);
Assertions.assertThat(variableInstances.size()).isEqualTo(13);
for (VariableInstanceLog variableInstance : variableInstances) {
logger.debug(variableInstance.toString());
Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
Assertions.assertThat(variableInstance.getDate()).isNotNull();
}
List<VariableInstanceLog> listVariables = new ArrayList<VariableInstanceLog>();
// collect only those that are related to list process variable
for (VariableInstanceLog v : variableInstances) {
if (v.getVariableInstanceId().equals("list")) {
listVariables.add(v);
}
}
Assertions.assertThat(listVariables.size()).isEqualTo(3);
List<String> variableValues = new ArrayList<String>();
List<String> variableIds = new ArrayList<String>();
for (VariableInstanceLog var : listVariables) {
variableValues.add(var.getValue());
variableIds.add(var.getVariableId());
// Various DBs return various empty values. (E.g. Oracle returns null.)
Assertions.assertThat(var.getOldValue()).isIn("", " ", null);
Assertions.assertThat(var.getProcessInstanceId()).isEqualTo(processInstance.getProcessInstanceId());
Assertions.assertThat(var.getProcessId()).isEqualTo(processInstance.getProcessId());
Assertions.assertThat(var.getVariableInstanceId()).isEqualTo("list");
}
Assertions.assertThat(variableValues).contains("One", "Two", "Three");
Assertions.assertThat(variableIds).contains("list[0]", "list[1]", "list[2]");
// Test findVariableInstancesByName* methods
List<VariableInstanceLog> emptyVarLogs = auditLogService.findVariableInstancesByName("s", true);
Assertions.assertThat(emptyVarLogs).isEmpty();
for (VariableInstanceLog origVarLog : variableInstances) {
varLogs = auditLogService.findVariableInstancesByName(origVarLog.getVariableId(), false);
for (VariableInstanceLog varLog : varLogs) {
Assertions.assertThat(varLog.getVariableId()).isEqualTo(origVarLog.getVariableId());
}
}
emptyVarLogs = auditLogService.findVariableInstancesByNameAndValue("s", "InitialValue", true);
Assertions.assertThat(emptyVarLogs).isEmpty();
String varId = "s";
String varValue = "ResultValue";
variableInstances = auditLogService.findVariableInstancesByNameAndValue(varId, varValue, false);
Assertions.assertThat(variableInstances.size()).isEqualTo(3);
VariableInstanceLog varLog = variableInstances.get(0);
Assertions.assertThat(varLog.getVariableId()).isEqualTo(varId);
Assertions.assertThat(varLog.getValue()).isEqualTo(varValue);
auditLogService.clear();
processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
Assertions.assertThat(processInstances).isEmpty();
}
use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.
the class AbstractAuditLogServiceTest method runTestLogger4LargeVariable.
public static void runTestLogger4LargeVariable(KieSession session, AuditLogService auditLogService) throws Exception {
session.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
Map<String, Object> results = new HashMap<String, Object>();
results.put("Result", "ResultValue");
manager.completeWorkItem(workItem.getId(), results);
}
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
}
});
// record the initial count to compare to later
List<ProcessInstanceLog> processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
int initialProcessInstanceSize = processInstances.size();
// start process instance
Map<String, Object> params = new HashMap<String, Object>();
List<String> list = new ArrayList<String>();
list.add("One");
list.add("Two");
String three = "";
for (int i = 0; i < 1024; i++) {
three += "*";
}
list.add(three);
params.put("list", list);
long processInstanceId = session.startProcess("com.sample.ruleflow3", params).getId();
logger.debug("Checking process instances for process 'com.sample.ruleflow3'");
processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
int expected = initialProcessInstanceSize + 1;
Assertions.assertThat(processInstances.size()).isEqualTo(expected).withFailMessage(String.format("Expected %d ProcessInstanceLog instances, not %d", expected, processInstances.size()));
ProcessInstanceLog processInstance = processInstances.get(initialProcessInstanceSize);
logger.debug("{} -> {} - {}", processInstance.toString(), processInstance.getStart(), processInstance.getEnd());
Assertions.assertThat(processInstance.getStart()).isNotNull();
Assertions.assertThat(processInstance.getEnd()).isNotNull().withFailMessage("ProcessInstanceLog does not contain end date.");
Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
List<VariableInstanceLog> variableInstances = auditLogService.findVariableInstances(processInstanceId);
Assertions.assertThat(variableInstances.size()).isEqualTo(8);
for (VariableInstanceLog variableInstance : variableInstances) {
logger.debug(variableInstance.toString());
Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
Assertions.assertThat(variableInstance.getDate()).isNotNull();
}
auditLogService.clear();
processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
Assertions.assertThat(processInstances).isNullOrEmpty();
}
use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.
the class AbstractAuditLogServiceTest method runTestLogger4.
public static void runTestLogger4(KieSession session, AuditLogService auditLogService) throws Exception {
final List<Long> workItemIds = new ArrayList<Long>();
session.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
workItemIds.add(workItem.getId());
}
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
}
});
// record the initial count to compare to later
List<ProcessInstanceLog> processInstances = auditLogService.findProcessInstances("com.sample.ruleflow");
int initialProcessInstanceSize = processInstances.size();
// start process instance
Map<String, Object> params = new HashMap<String, Object>();
List<String> list = new ArrayList<String>();
list.add("One");
list.add("Two");
list.add("Three");
params.put("list", list);
long processInstanceId = session.startProcess("com.sample.ruleflow3", params).getId();
// Test findVariableInstancesByName* methods: check for variables (only) in active processes
List<VariableInstanceLog> varLogs = auditLogService.findVariableInstancesByName("s", true);
Assertions.assertThat(varLogs).isNotEmpty();
Assertions.assertThat(varLogs.size()).isEqualTo(1);
for (Long workItemId : workItemIds) {
Map<String, Object> results = new HashMap<String, Object>();
results.put("Result", "ResultValue");
session.getWorkItemManager().completeWorkItem(workItemId, results);
}
logger.debug("Checking process instances for process 'com.sample.ruleflow3'");
processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
Assertions.assertThat(processInstances.size()).isEqualTo(initialProcessInstanceSize + 1);
ProcessInstanceLog processInstance = processInstances.get(initialProcessInstanceSize);
logger.debug("{} -> {} - {}", processInstance.toString(), processInstance.getStart(), processInstance.getEnd());
Assertions.assertThat(processInstance.getStart()).isNotNull();
Assertions.assertThat(processInstance.getEnd()).isNotNull().withFailMessage("ProcessInstanceLog does not contain end date.");
Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
List<VariableInstanceLog> variableInstances = auditLogService.findVariableInstances(processInstanceId);
Assertions.assertThat(variableInstances.size()).isEqualTo(11);
for (VariableInstanceLog variableInstance : variableInstances) {
logger.debug(variableInstance.toString());
Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
Assertions.assertThat(variableInstance.getDate()).isNotNull();
}
// Test findVariableInstancesByName* methods
List<VariableInstanceLog> emptyVarLogs = auditLogService.findVariableInstancesByName("s", true);
Assertions.assertThat(emptyVarLogs).isEmpty();
for (VariableInstanceLog origVarLog : variableInstances) {
varLogs = auditLogService.findVariableInstancesByName(origVarLog.getVariableId(), false);
for (VariableInstanceLog varLog : varLogs) {
Assertions.assertThat(varLog.getVariableId()).isEqualTo(origVarLog.getVariableId());
}
}
emptyVarLogs = auditLogService.findVariableInstancesByNameAndValue("s", "InitialValue", true);
Assertions.assertThat(emptyVarLogs).isEmpty();
String varId = "s";
String varValue = "ResultValue";
variableInstances = auditLogService.findVariableInstancesByNameAndValue(varId, varValue, false);
Assertions.assertThat(variableInstances.size()).isEqualTo(3);
VariableInstanceLog varLog = variableInstances.get(0);
Assertions.assertThat(varLog.getVariableId()).isEqualTo(varId);
Assertions.assertThat(varLog.getValue()).isEqualTo(varValue);
auditLogService.clear();
processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
Assertions.assertThat(processInstances).isEmpty();
}
use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.
the class BusinessRuleTaskHandler method handleDMN.
protected void handleDMN(WorkItem workItem, Map<String, Object> parameters, Map<String, Object> results) {
String namespace = (String) parameters.remove("Namespace");
String model = (String) parameters.remove("Model");
String decision = (String) parameters.remove("Decision");
DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
DMNModel dmnModel = runtime.getModel(namespace, model);
if (dmnModel == null) {
throw new IllegalArgumentException("DMN model '" + model + "' not found with namespace '" + namespace + "'");
}
DMNResult dmnResult = null;
DMNContext context = runtime.newContext();
for (Entry<String, Object> entry : parameters.entrySet()) {
context.set(entry.getKey(), entry.getValue());
}
if (decision != null && !decision.isEmpty()) {
dmnResult = runtime.evaluateDecisionByName(dmnModel, decision, context);
} else {
dmnResult = runtime.evaluateAll(dmnModel, context);
}
if (dmnResult.hasErrors()) {
String errors = dmnResult.getMessages(Severity.ERROR).stream().map(message -> message.toString()).collect(Collectors.joining(", "));
throw new RuntimeException("DMN result errors:: " + errors);
}
results.putAll(dmnResult.getContext().getAll());
}
use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.
the class TimerEventTest method testTimerAndGateway.
@Test
@BZ("1036761")
public void testTimerAndGateway() throws Exception {
KieSession ksession = createKSession(TIMER_AND_GATEWAY);
int sessionId = ksession.getId();
TestAsyncWorkItemHandler handler1 = new TestAsyncWorkItemHandler();
TestAsyncWorkItemHandler handler2 = new TestAsyncWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("task1", handler1);
ksession.getWorkItemManager().registerWorkItemHandler("task2", handler2);
ProcessInstance instance = ksession.createProcessInstance(TIMER_AND_GATEWAY_ID, new HashMap<String, Object>());
ksession.startProcessInstance(instance.getId());
WorkItem workItem1 = handler1.getWorkItem();
Assertions.assertThat(workItem1).isNotNull();
Assertions.assertThat(handler1.getWorkItem()).isNull();
// first safe state: task1 completed
ksession.getWorkItemManager().completeWorkItem(workItem1.getId(), null);
ksession = restoreKSession(TIMER_AND_GATEWAY);
ksession.getWorkItemManager().registerWorkItemHandler("task1", handler1);
ksession.getWorkItemManager().registerWorkItemHandler("task2", handler2);
// second safe state: timer completed, waiting on task2
for (int i = 0; i < 7; i++) {
Thread.sleep(1000);
}
WorkItem workItem2 = handler2.getWorkItem();
// Both sides of the join are completed. But on the process instance, there are two JoinInstance for the same
// Join, and since it is an AND join, it never reaches task2. It fails after the next assertion
Assertions.assertThat(workItem2).isNotNull();
Assertions.assertThat(handler1.getWorkItem()).isNull();
}
Aggregations