use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.
the class BusinessRuleTaskIT method testDecision.
@Test
public void testDecision() throws Exception {
Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("decision/models/dmnprocess.bpmn2"));
resourcesTypeMap.put(TYPE.DECISION, Collections.singletonList("decision/models/vacationDaysAlt/vacationDaysAlt.dmn"));
Application app = generateCode(resourcesTypeMap);
assertThat(app).isNotNull();
Process<? extends Model> p = app.get(Processes.class).processById("DmnProcess");
// first run 16, 1 and expected days is 27
{
Model m = p.createModel();
HashMap<String, Object> vars = new HashMap<>();
vars.put("age", 16);
vars.put("yearsOfService", 1);
m.fromMap(vars);
ProcessInstance<? extends Model> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = processInstance.variables();
assertThat(result.toMap().get("vacationDays")).isNotNull().isEqualTo(BigDecimal.valueOf(27));
}
// second run 44, 20 and expected days is 24
{
Model m = p.createModel();
HashMap<String, Object> vars = new HashMap<>();
vars.put("age", 44);
vars.put("yearsOfService", 20);
m.fromMap(vars);
ProcessInstance<? extends Model> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = processInstance.variables();
assertThat(result.toMap().get("vacationDays")).isNotNull().isEqualTo(BigDecimal.valueOf(24));
}
// second run 50, 30 and expected days is 30
{
Model m = p.createModel();
HashMap<String, Object> vars = new HashMap<>();
vars.put("age", 50);
vars.put("yearsOfService", 30);
m.fromMap(vars);
ProcessInstance<? extends Model> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = processInstance.variables();
assertThat(result.toMap().get("vacationDays")).isNotNull().isEqualTo(BigDecimal.valueOf(30));
}
}
use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.
the class DocumentProcessInstanceMarshallerTest method testUnmarshalProcessInstance.
@Test
void testUnmarshalProcessInstance() {
ProcessInstance<BpmnVariables> processInstance = (ProcessInstance<BpmnVariables>) marshaller.unmarshallProcessInstance(doc.toJson().getBytes(), process);
assertNotNull(processInstance, "Unmarshalled value should not be null");
assertThat(processInstance.id()).isEqualTo(doc.get("id"));
assertThat(processInstance.description()).isEqualTo(doc.get("description"));
assertThat(processInstance.description()).isEqualTo("User Task");
Collection<? extends ProcessInstance<BpmnVariables>> values = process.instances().values();
assertThat(values).isNotEmpty();
BpmnVariables variables = processInstance.variables();
String testVar = (String) variables.get("test");
assertThat(testVar).isEqualTo("testValue");
}
use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.
the class MongoDBProcessInstancesIT method testFindByIdReadMode.
void testFindByIdReadMode(MongoDBTransactionManager transactionManager) {
BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask-Script.bpmn2")).get(0);
// workaround as BpmnProcess does not compile the scripts but just reads the xml
for (Node node : ((WorkflowProcess) process.get()).getNodes()) {
if (node instanceof ActionNode) {
DroolsAction a = ((ActionNode) node).getAction();
a.setMetaData("Action", (Action) kcontext -> {
System.out.println("The variable value is " + kcontext.getVariable("s") + " about to call toString on it");
kcontext.getVariable("s").toString();
});
}
}
process.setProcessInstancesFactory(new MongoDBProcessInstancesFactory(mongoClient, transactionManager));
process.configure();
ProcessInstance<BpmnVariables> mutablePi = process.createInstance(BpmnVariables.create(Collections.singletonMap("var", "value")));
mutablePi.start();
assertThat(mutablePi.status()).isEqualTo(STATE_ERROR);
assertThat(mutablePi.error()).hasValueSatisfying(error -> {
assertThat(error.errorMessage()).contains("java.lang.NullPointerException");
assertThat(error.failedNodeId()).isEqualTo("ScriptTask_1");
});
assertThat(mutablePi.variables().toMap()).containsExactly(entry("var", "value"));
ProcessInstances<BpmnVariables> instances = process.instances();
assertThat(instances.size()).isOne();
ProcessInstance<BpmnVariables> pi = instances.findById(mutablePi.id(), ProcessInstanceReadMode.READ_ONLY).get();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> pi.abort());
ProcessInstance<BpmnVariables> readOnlyPi = instances.findById(mutablePi.id(), ProcessInstanceReadMode.READ_ONLY).get();
assertThat(readOnlyPi.status()).isEqualTo(STATE_ERROR);
assertThat(readOnlyPi.error()).hasValueSatisfying(error -> {
assertThat(error.errorMessage()).contains("java.lang.NullPointerException");
assertThat(error.failedNodeId()).isEqualTo("ScriptTask_1");
});
assertThat(readOnlyPi.variables().toMap()).containsExactly(entry("var", "value"));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> readOnlyPi.abort());
instances.findById(mutablePi.id()).get().abort();
assertThat(instances.size()).isZero();
}
use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.
the class TaskManagementService method updateTask.
@Override
public TaskInfo updateTask(String processId, String processInstanceId, String taskId, TaskInfo taskInfo, boolean shouldReplace, Policy<?>... policies) {
ProcessInstance<?> pi = getProcessInstance(processId, processInstanceId, taskId);
KogitoWorkItem workItem = UnitOfWorkExecutor.executeInUnitOfWork(processConfig.unitOfWorkManager(), () -> pi.updateWorkItem(taskId, wi -> {
HumanTaskWorkItemImpl humanTask = HumanTaskHelper.asHumanTask(wi);
setField(humanTask::setAdminGroups, taskInfo::getAdminGroups, shouldReplace);
setField(humanTask::setAdminUsers, taskInfo::getAdminUsers, shouldReplace);
setField(humanTask::setExcludedUsers, taskInfo::getExcludedUsers, shouldReplace);
setField(humanTask::setPotentialUsers, taskInfo::getPotentialUsers, shouldReplace);
setField(humanTask::setPotentialGroups, taskInfo::getPotentialGroups, shouldReplace);
setField(humanTask::setTaskPriority, taskInfo::getPriority, shouldReplace);
setField(humanTask::setTaskDescription, taskInfo::getDescription, shouldReplace);
setMap(humanTask::setParameters, humanTask::setParameter, taskInfo.getInputParams(), shouldReplace);
return wi;
}, policies));
return convert((HumanTaskWorkItem) workItem);
}
Aggregations