Search in sources :

Example 16 with ProcessInstance

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));
    }
}
Also used : HashMap(java.util.HashMap) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) List(java.util.List) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) ProcessInstance(org.kie.kogito.process.ProcessInstance) Processes(org.kie.kogito.process.Processes) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 17 with ProcessInstance

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");
}
Also used : ProcessInstance(org.kie.kogito.process.ProcessInstance) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 18 with ProcessInstance

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();
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) Document(org.bson.Document) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) STATE_ACTIVE(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ACTIVE) MongoDBTransactionManager(org.kie.kogito.mongodb.transaction.MongoDBTransactionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ProcessInstances(org.kie.kogito.process.ProcessInstances) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) STATE_COMPLETED(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_COMPLETED) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) StreamSupport(java.util.stream.StreamSupport) DroolsAction(org.jbpm.workflow.core.DroolsAction) UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Container(org.testcontainers.junit.jupiter.Container) ActionNode(org.jbpm.workflow.core.node.ActionNode) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) Collection(java.util.Collection) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) MongoClients(com.mongodb.client.MongoClients) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) Assertions.entry(org.assertj.core.api.Assertions.entry) KogitoProcessInstancesFactory(org.kie.kogito.persistence.KogitoProcessInstancesFactory) DOCUMENT_ID(org.kie.kogito.mongodb.utils.DocumentConstants.DOCUMENT_ID) Test(org.junit.jupiter.api.Test) DocumentConstants(org.kie.kogito.mongodb.utils.DocumentConstants) List(java.util.List) ProcessInstance(org.kie.kogito.process.ProcessInstance) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Node(org.kie.api.definition.process.Node) ProcessInstanceReadMode(org.kie.kogito.process.ProcessInstanceReadMode) STATE_ERROR(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ERROR) Action(org.jbpm.process.instance.impl.Action) KogitoMongoDBContainer(org.kie.kogito.testcontainers.KogitoMongoDBContainer) Collections(java.util.Collections) WorkItem(org.kie.kogito.process.WorkItem) UnitOfWorkEndEvent(org.kie.kogito.uow.events.UnitOfWorkEndEvent) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) ActionNode(org.jbpm.workflow.core.node.ActionNode) Node(org.kie.api.definition.process.Node) ActionNode(org.jbpm.workflow.core.node.ActionNode) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) ClassPathResource(org.drools.core.io.impl.ClassPathResource) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables)

Example 19 with ProcessInstance

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);
}
Also used : Policy(org.kie.kogito.process.workitem.Policy) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) ProcessConfig(org.kie.kogito.process.ProcessConfig) HumanTaskHelper(org.jbpm.process.instance.impl.humantask.HumanTaskHelper) UnitOfWorkExecutor(org.kie.kogito.services.uow.UnitOfWorkExecutor) Supplier(java.util.function.Supplier) Consumer(java.util.function.Consumer) HumanTaskWorkItemImpl(org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemImpl) ProcessInstance(org.kie.kogito.process.ProcessInstance) Map(java.util.Map) Entry(java.util.Map.Entry) BiConsumer(java.util.function.BiConsumer) Process(org.kie.kogito.process.Process) HumanTaskWorkItem(org.kie.kogito.process.workitem.HumanTaskWorkItem) Processes(org.kie.kogito.process.Processes) ProcessInstanceNotFoundException(org.kie.kogito.process.ProcessInstanceNotFoundException) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) HumanTaskWorkItemImpl(org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemImpl)

Aggregations

ProcessInstance (org.kie.kogito.process.ProcessInstance)19 Test (org.junit.jupiter.api.Test)15 Application (org.kie.kogito.Application)10 Processes (org.kie.kogito.process.Processes)10 List (java.util.List)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 WorkItem (org.kie.kogito.process.WorkItem)8 HashMap (java.util.HashMap)7 ProcessInstances (org.kie.kogito.process.ProcessInstances)7 BpmnVariables (org.kie.kogito.process.bpmn2.BpmnVariables)7 Collections (java.util.Collections)6 ClassPathResource (org.drools.core.io.impl.ClassPathResource)6 ActionNode (org.jbpm.workflow.core.node.ActionNode)6 Model (org.kie.kogito.Model)6 SecurityPolicy (org.kie.kogito.auth.SecurityPolicy)6 KogitoProcessInstance (org.kie.kogito.internal.process.runtime.KogitoProcessInstance)6 Process (org.kie.kogito.process.Process)6 BpmnProcess (org.kie.kogito.process.bpmn2.BpmnProcess)6 StaticIdentityProvider (org.kie.kogito.services.identity.StaticIdentityProvider)6 ArrayList (java.util.ArrayList)5