Search in sources :

Example 1 with InternalKogitoWorkItem

use of org.kie.kogito.process.workitems.InternalKogitoWorkItem in project kogito-runtimes by kiegroup.

the class WorkItemNodeInstance method cancel.

@Override
public void cancel() {
    InternalKogitoWorkItem item = getWorkItem();
    if (item != null && item.getState() != COMPLETED && item.getState() != ABORTED) {
        try {
            ((InternalKogitoWorkItemManager) getProcessInstance().getKnowledgeRuntime().getWorkItemManager()).internalAbortWorkItem(item.getStringId());
        } catch (WorkItemHandlerNotFoundException wihnfe) {
            getProcessInstance().setState(STATE_ABORTED);
            throw wihnfe;
        }
    }
    if (exceptionHandlingProcessInstanceId != null) {
        KogitoProcessRuntime kruntime = getKieRuntimeForSubprocess();
        ProcessInstance processInstance = (ProcessInstance) kruntime.getProcessInstance(exceptionHandlingProcessInstanceId);
        if (processInstance != null) {
            processInstance.setState(STATE_ABORTED);
        }
    }
    super.cancel();
}
Also used : InternalKogitoWorkItem(org.kie.kogito.process.workitems.InternalKogitoWorkItem) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) InternalKogitoWorkItemManager(org.kie.kogito.process.workitems.InternalKogitoWorkItemManager) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.jbpm.process.instance.ProcessInstance) AbstractProcessInstance(org.kie.kogito.process.impl.AbstractProcessInstance)

Example 2 with InternalKogitoWorkItem

use of org.kie.kogito.process.workitems.InternalKogitoWorkItem in project kogito-runtimes by kiegroup.

the class WorkItemNodeInstance method createWorkItem.

protected InternalKogitoWorkItem createWorkItem(WorkItemNode workItemNode) {
    Work work = workItemNode.getWork();
    workItem = newWorkItem();
    workItem.setName(work.getName());
    workItem.setProcessInstanceId(getProcessInstance().getStringId());
    Map<String, Object> resolvedParameters = new HashMap<>();
    Collection<String> metaParameters = work.getMetaParameters();
    for (Entry<String, Object> e : work.getParameters().entrySet()) {
        if (!metaParameters.contains(e.getKey()) && e.getValue() != null) {
            resolvedParameters.put(e.getKey(), e.getValue());
            if (e.getValue() instanceof String) {
                // we try first is a variable
                Object value = this.getVariable((String) e.getValue());
                if (value != null) {
                    resolvedParameters.put(e.getKey(), value);
                } else {
                    resolvedParameters.put(e.getKey(), resolveValue(e.getValue()));
                }
            }
        }
    }
    workItem.setStartDate(new Date());
    Function<String, Object> varResolver = (varRef) -> {
        if (resolvedParameters.containsKey(varRef)) {
            return resolvedParameters.get(varRef);
        }
        return getVariable(varRef);
    };
    Map<String, Object> inputSet = NodeIoHelper.processInputs(this, varResolver);
    inputSet.putAll(resolvedParameters);
    if (dynamicParameters != null) {
        inputSet.putAll(dynamicParameters);
    }
    workItem.getParameters().putAll(inputSet);
    return workItem;
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) NamedDataType(org.kie.kogito.process.NamedDataType) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) GroupedNamedDataType(org.kie.kogito.process.GroupedNamedDataType) STATE_COMPLETED(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_COMPLETED) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Context(org.jbpm.process.core.Context) ContextInstance(org.jbpm.process.instance.ContextInstance) Map(java.util.Map) WorkItemExecutionException(org.kie.kogito.process.workitem.WorkItemExecutionException) KogitoWorkItemNodeInstance(org.kie.kogito.internal.process.runtime.KogitoWorkItemNodeInstance) ABORTED(org.kie.api.runtime.process.WorkItem.ABORTED) Collection(java.util.Collection) Set(java.util.Set) VARIABLE_SCOPE(org.jbpm.process.core.context.variable.VariableScope.VARIABLE_SCOPE) KogitoProcessContextImpl(org.jbpm.process.instance.KogitoProcessContextImpl) KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) VariableScope(org.jbpm.process.core.context.variable.VariableScope) List(java.util.List) ContextContainer(org.jbpm.process.core.ContextContainer) Node(org.jbpm.workflow.core.Node) KieRuntime(org.kie.api.runtime.KieRuntime) Model(org.kie.kogito.Model) ExceptionScopeInstance(org.jbpm.process.instance.context.exception.ExceptionScopeInstance) Entry(java.util.Map.Entry) Work(org.jbpm.process.core.Work) ParameterDefinition(org.jbpm.process.core.ParameterDefinition) InternalKogitoWorkItem(org.kie.kogito.process.workitems.InternalKogitoWorkItem) ExceptionScope(org.jbpm.process.core.context.exception.ExceptionScope) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) AbstractProcessInstance(org.kie.kogito.process.impl.AbstractProcessInstance) ProcessWorkItemHandlerException(org.kie.api.runtime.process.ProcessWorkItemHandlerException) HashMap(java.util.HashMap) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) Function(java.util.function.Function) ArrayList(java.util.ArrayList) EventListener(org.kie.api.runtime.process.EventListener) NodeIoHelper(org.jbpm.workflow.core.impl.NodeIoHelper) DataAssociation(org.jbpm.workflow.core.impl.DataAssociation) ContextInstanceFactoryRegistry(org.jbpm.process.instance.impl.ContextInstanceFactoryRegistry) ContextInstanceContainer(org.jbpm.process.instance.ContextInstanceContainer) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Logger(org.slf4j.Logger) STATE_ABORTED(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ABORTED) EnvironmentName(org.kie.api.runtime.EnvironmentName) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) IOEventDescription(org.kie.kogito.process.IOEventDescription) COMPLETED(org.kie.api.runtime.process.WorkItem.COMPLETED) KogitoWorkItemImpl(org.kie.kogito.process.workitems.impl.KogitoWorkItemImpl) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) ContextInstanceFactory(org.jbpm.process.instance.impl.ContextInstanceFactory) EventDescription(org.kie.kogito.process.EventDescription) InternalKogitoWorkItemManager(org.kie.kogito.process.workitems.InternalKogitoWorkItemManager) Processes(org.kie.kogito.process.Processes) Collections(java.util.Collections) HashMap(java.util.HashMap) Work(org.jbpm.process.core.Work) Date(java.util.Date)

Example 3 with InternalKogitoWorkItem

use of org.kie.kogito.process.workitems.InternalKogitoWorkItem in project kogito-runtimes by kiegroup.

the class KogitoDefaultWorkItemManager method completeWorkItem.

@Override
public void completeWorkItem(String id, Map<String, Object> results, Policy<?>... policies) {
    InternalKogitoWorkItem workItem = workItems.get(id);
    // work item may have been aborted
    if (workItem != null) {
        workItem.setResults(results);
        KogitoProcessInstance processInstance = kruntime.getProcessInstance(workItem.getProcessInstanceStringId());
        workItem.setState(COMPLETED);
        // process instance may have finished already
        if (processInstance != null) {
            processInstance.signalEvent("workItemCompleted", workItem);
        }
        workItems.remove(id);
    }
}
Also used : InternalKogitoWorkItem(org.kie.kogito.process.workitems.InternalKogitoWorkItem) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance)

Example 4 with InternalKogitoWorkItem

use of org.kie.kogito.process.workitems.InternalKogitoWorkItem in project kogito-runtimes by kiegroup.

the class PredictionAwareHumanTaskLifeCycleTest method configure.

@BeforeEach
public void configure() {
    predictNow = new AtomicBoolean(false);
    trainedTasks = new ArrayList<>();
    predictionService = new PredictionService() {

        @Override
        public void train(org.kie.api.runtime.process.WorkItem task, Map<String, Object> inputData, Map<String, Object> outputData) {
            trainedTasks.add(((InternalKogitoWorkItem) task).getStringId());
        }

        @Override
        public PredictionOutcome predict(org.kie.api.runtime.process.WorkItem task, Map<String, Object> inputData) {
            if (predictNow.get()) {
                return new PredictionOutcome(95, 75, Collections.singletonMap("output", "predicted value"));
            }
            return new PredictionOutcome();
        }

        @Override
        public String getIdentifier() {
            return "test";
        }
    };
    CachedWorkItemHandlerConfig wiConfig = new CachedWorkItemHandlerConfig();
    wiConfig.register("Human Task", new HumanTaskWorkItemHandler(new PredictionAwareHumanTaskLifeCycle(predictionService)));
    config = new StaticProcessConfig(wiConfig, new DefaultProcessEventListenerConfig(), new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()), null);
}
Also used : InternalKogitoWorkItem(org.kie.kogito.process.workitems.InternalKogitoWorkItem) HumanTaskWorkItemHandler(org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemHandler) StaticProcessConfig(org.kie.kogito.process.impl.StaticProcessConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CachedWorkItemHandlerConfig(org.kie.kogito.process.impl.CachedWorkItemHandlerConfig) CollectingUnitOfWorkFactory(org.kie.kogito.services.uow.CollectingUnitOfWorkFactory) DefaultProcessEventListenerConfig(org.kie.kogito.process.impl.DefaultProcessEventListenerConfig) DefaultUnitOfWorkManager(org.kie.kogito.services.uow.DefaultUnitOfWorkManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with InternalKogitoWorkItem

use of org.kie.kogito.process.workitems.InternalKogitoWorkItem in project kogito-runtimes by kiegroup.

the class ActivityTest method testUserTaskVerifyParameters.

@Test
public void testUserTaskVerifyParameters() throws Exception {
    kruntime = createKogitoProcessRuntime("BPMN2-UserTask.bpmn2");
    kruntime.getKieSession().getEnvironment().set("deploymentId", "test-deployment-id");
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    KogitoProcessInstance processInstance = kruntime.startProcess("UserTask");
    assertEquals(KogitoProcessInstance.STATE_ACTIVE, processInstance.getState());
    org.kie.kogito.internal.process.runtime.KogitoWorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("john", workItem.getParameter("ActorId"));
    final String pId = processInstance.getStringId();
    kruntime.getKieSession().execute((ExecutableCommand<Void>) context -> {
        KogitoProcessInstance processInstance1 = kruntime.getProcessInstance(pId);
        assertNotNull(processInstance1);
        NodeInstance nodeInstance = ((KogitoNodeInstanceContainer) processInstance1).getNodeInstance(((InternalKogitoWorkItem) workItem).getNodeInstanceStringId());
        assertNotNull(nodeInstance);
        assertTrue(nodeInstance instanceof WorkItemNodeInstance);
        String deploymentId = ((WorkItemNodeInstance) nodeInstance).getWorkItem().getDeploymentId();
        String nodeInstanceId = ((WorkItemNodeInstance) nodeInstance).getWorkItem().getNodeInstanceStringId();
        long nodeId = ((WorkItemNodeInstance) nodeInstance).getWorkItem().getNodeId();
        assertEquals(((InternalKogitoWorkItem) workItem).getDeploymentId(), deploymentId);
        assertEquals(((InternalKogitoWorkItem) workItem).getNodeId(), nodeId);
        assertEquals(((InternalKogitoWorkItem) workItem).getNodeInstanceStringId(), nodeInstanceId);
        return null;
    });
    kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null);
    assertProcessInstanceFinished(processInstance, kruntime);
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) Address(org.jbpm.bpmn2.objects.Address) Account(org.jbpm.bpmn2.objects.Account) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HelloService(org.jbpm.bpmn2.objects.HelloService) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) Disabled(org.junit.jupiter.api.Disabled) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) Assignment(org.jbpm.workflow.core.node.Assignment) KogitoNodeInstanceContainer(org.kie.kogito.internal.process.runtime.KogitoNodeInstanceContainer) BigDecimal(java.math.BigDecimal) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) Map(java.util.Map) ProcessBuildContext(org.jbpm.process.builder.ProcessBuildContext) ProcessDialectRegistry(org.jbpm.process.builder.dialect.ProcessDialectRegistry) ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) ActionBuilder(org.jbpm.process.builder.ActionBuilder) ProcessClassBuilder(org.jbpm.process.builder.ProcessClassBuilder) RuleAwareProcessEventListener(org.jbpm.process.instance.event.listeners.RuleAwareProcessEventListener) DoNothingWorkItemHandler(org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler) Process(org.kie.api.definition.process.Process) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) SendTaskHandler(org.jbpm.bpmn2.handler.SendTaskHandler) VariableScope(org.jbpm.process.core.context.variable.VariableScope) Test(org.junit.jupiter.api.Test) List(java.util.List) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) Assertions.fail(org.assertj.core.api.Assertions.fail) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Person(org.jbpm.bpmn2.objects.Person) KogitoWorkItemManager(org.kie.kogito.internal.process.runtime.KogitoWorkItemManager) InternalKogitoWorkItem(org.kie.kogito.process.workitems.InternalKogitoWorkItem) NodeContainer(org.kie.api.definition.process.NodeContainer) DynamicUtils(org.jbpm.workflow.instance.node.DynamicUtils) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) DataDefinition(org.jbpm.workflow.core.impl.DataDefinition) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AssignmentBuilder(org.jbpm.process.builder.AssignmentBuilder) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) NodeInstance(org.kie.api.runtime.process.NodeInstance) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) ExecutableCommand(org.kie.api.command.ExecutableCommand) TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) ArrayList(java.util.ArrayList) TriggerRulesEventListener(org.jbpm.process.instance.event.listeners.TriggerRulesEventListener) DataAssociation(org.jbpm.workflow.core.impl.DataAssociation) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KogitoWorkflowProcessInstance(org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance) ServiceTaskHandler(org.jbpm.bpmn2.handler.ServiceTaskHandler) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) DataTransformer(org.kie.api.runtime.process.DataTransformer) ActionNode(org.jbpm.workflow.core.node.ActionNode) ReturnValueEvaluatorBuilder(org.jbpm.process.builder.ReturnValueEvaluatorBuilder) RequirePersistence(org.jbpm.bpmn2.test.RequirePersistence) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) DataTransformerRegistry(org.jbpm.process.core.impl.DataTransformerRegistry) Node(org.kie.api.definition.process.Node) NodeLeftCountDownProcessEventListener(org.jbpm.test.util.NodeLeftCountDownProcessEventListener) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) ReceiveTaskHandler(org.jbpm.bpmn2.handler.ReceiveTaskHandler) InternalKogitoWorkItem(org.kie.kogito.process.workitems.InternalKogitoWorkItem) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) Test(org.junit.jupiter.api.Test)

Aggregations

InternalKogitoWorkItem (org.kie.kogito.process.workitems.InternalKogitoWorkItem)14 HashMap (java.util.HashMap)5 KogitoProcessInstance (org.kie.kogito.internal.process.runtime.KogitoProcessInstance)5 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)4 Test (org.junit.jupiter.api.Test)4 KogitoProcessRuntime (org.kie.kogito.internal.process.runtime.KogitoProcessRuntime)3 KogitoWorkItem (org.kie.kogito.internal.process.runtime.KogitoWorkItem)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 WorkItemHandlerNotFoundException (org.drools.core.WorkItemHandlerNotFoundException)2 VariableScope (org.jbpm.process.core.context.variable.VariableScope)2 ProcessInstance (org.jbpm.process.instance.ProcessInstance)2 DataAssociation (org.jbpm.workflow.core.impl.DataAssociation)2 WorkItemNode (org.jbpm.workflow.core.node.WorkItemNode)2 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)2 WorkflowRuntimeException (org.jbpm.workflow.instance.WorkflowRuntimeException)2 AbstractProcessInstance (org.kie.kogito.process.impl.AbstractProcessInstance)2 InternalKogitoWorkItemManager (org.kie.kogito.process.workitems.InternalKogitoWorkItemManager)2