Search in sources :

Example 21 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jbpm by kiegroup.

the class MigrateProcessInstanceCommand method execute.

public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) ksession.getProcessInstance(processInstanceId);
    if (processInstance == null) {
        throw new IllegalArgumentException("Could not find process instance " + processInstanceId);
    }
    if (processId == null) {
        throw new IllegalArgumentException("Null process id");
    }
    WorkflowProcess process = (WorkflowProcess) ksession.getKieBase().getProcess(processId);
    if (process == null) {
        throw new IllegalArgumentException("Could not find process " + processId);
    }
    if (processInstance.getProcessId().equals(processId)) {
        return null;
    }
    synchronized (processInstance) {
        org.kie.api.definition.process.Process oldProcess = processInstance.getProcess();
        processInstance.disconnect();
        processInstance.setProcess(oldProcess);
        if (nodeMapping == null) {
            nodeMapping = new HashMap<String, Long>();
        }
        updateNodeInstances(processInstance, nodeMapping);
        processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) ksession);
        processInstance.setProcess(process);
        processInstance.reconnect();
    }
    return null;
}
Also used : WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess)

Example 22 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jbpm by kiegroup.

the class CompensationScopeInstance method handleException.

public void handleException(ExceptionHandler handler, String compensationActivityRef, Object dunno) {
    WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) getProcessInstance();
    NodeInstanceContainer nodeInstanceContainer = (NodeInstanceContainer) getContextInstanceContainer();
    if (handler instanceof CompensationHandler) {
        CompensationHandler compensationHandler = (CompensationHandler) handler;
        try {
            Node handlerNode = compensationHandler.getnode();
            if (handlerNode instanceof BoundaryEventNode) {
                NodeInstance compensationHandlerNodeInstance = nodeInstanceContainer.getNodeInstance(handlerNode);
                compensationInstances.add(compensationHandlerNodeInstance);
                // The BoundaryEventNodeInstance.signalEvent() contains the necessary logic
                // to check whether or not compensation may proceed (? : (not-active + completed))
                EventNodeInstance eventNodeInstance = (EventNodeInstance) compensationHandlerNodeInstance;
                eventNodeInstance.signalEvent("Compensation", compensationActivityRef);
            } else if (handlerNode instanceof EventSubProcessNode) {
                // Check that subprocess parent has completed.
                List<String> completedIds = processInstance.getCompletedNodeIds();
                if (completedIds.contains(((NodeImpl) handlerNode.getNodeContainer()).getMetaData("UniqueId"))) {
                    NodeInstance subProcessNodeInstance = ((NodeInstanceContainer) nodeInstanceContainer).getNodeInstance((Node) handlerNode.getNodeContainer());
                    compensationInstances.add(subProcessNodeInstance);
                    NodeInstance compensationHandlerNodeInstance = ((NodeInstanceContainer) subProcessNodeInstance).getNodeInstance(handlerNode);
                    compensationInstances.add(compensationHandlerNodeInstance);
                    EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) compensationHandlerNodeInstance;
                    eventNodeInstance.signalEvent("Compensation", compensationActivityRef);
                }
            }
            assert handlerNode instanceof BoundaryEventNode || handlerNode instanceof EventSubProcessNode : "Unexpected compensation handler node type : " + handlerNode.getClass().getSimpleName();
        } catch (Exception e) {
            throwWorkflowRuntimeException(nodeInstanceContainer, processInstance, "Unable to execute compensation.", e);
        }
    } else {
        Exception e = new IllegalArgumentException("Unsupported compensation handler: " + handler);
        throwWorkflowRuntimeException(nodeInstanceContainer, processInstance, e.getMessage(), e);
    }
}
Also used : NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) Node(org.kie.api.definition.process.Node) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) CompensationHandler(org.jbpm.process.core.context.exception.CompensationHandler) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) List(java.util.List) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) NodeInstance(org.jbpm.workflow.instance.NodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance)

Example 23 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jbpm by kiegroup.

the class CompensationScopeInstance method handleException.

public void handleException(String activityRef, Object dunno) {
    assert activityRef != null : "It should not be possible for the compensation activity reference to be null here.";
    CompensationScope compensationScope = (CompensationScope) getExceptionScope();
    // broadcast/general compensation in reverse order
    if (activityRef.startsWith(IMPLICIT_COMPENSATION_PREFIX)) {
        activityRef = activityRef.substring(IMPLICIT_COMPENSATION_PREFIX.length());
        assert activityRef.equals(compensationScope.getContextContainerId()) : "Compensation activity ref [" + activityRef + "] does not match" + " Compensation Scope container id [" + compensationScope.getContextContainerId() + "]";
        Map<String, ExceptionHandler> handlers = compensationScope.getExceptionHandlers();
        List<String> completedNodeIds = ((WorkflowProcessInstanceImpl) getProcessInstance()).getCompletedNodeIds();
        ListIterator<String> iter = completedNodeIds.listIterator(completedNodeIds.size());
        while (iter.hasPrevious()) {
            String completedId = iter.previous();
            ExceptionHandler handler = handlers.get(completedId);
            if (handler != null) {
                handleException(handler, completedId, null);
            }
        }
    } else {
        // Specific compensation
        ExceptionHandler handler = compensationScope.getExceptionHandler(activityRef);
        if (handler == null) {
            throw new IllegalArgumentException("Could not find CompensationHandler for " + activityRef);
        }
        handleException(handler, activityRef, null);
    }
    // Cancel all node instances created for compensation
    while (!compensationInstances.isEmpty()) {
        NodeInstance generatedInstance = compensationInstances.pop();
        ((NodeInstanceContainer) generatedInstance.getNodeInstanceContainer()).removeNodeInstance(generatedInstance);
    }
}
Also used : ExceptionHandler(org.jbpm.process.core.context.exception.ExceptionHandler) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) CompensationScope(org.jbpm.process.core.context.exception.CompensationScope) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) NodeInstance(org.jbpm.workflow.instance.NodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance)

Example 24 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jbpm by kiegroup.

the class JbpmJUnitTestCase method assertNumOfOutgoingConnections.

public void assertNumOfOutgoingConnections(ProcessInstance process, String nodeName, int num) {
    assertNodeExists(process, nodeName);
    WorkflowProcessInstanceImpl instance = (WorkflowProcessInstanceImpl) process;
    for (Node node : instance.getNodeContainer().getNodes()) {
        if (node.getName().equals(nodeName)) {
            if (node.getOutgoingConnections().size() != num) {
                fail("Expected outgoing connections: " + num + " - found " + node.getOutgoingConnections().size());
            } else {
                break;
            }
        }
    }
}
Also used : Node(org.kie.api.definition.process.Node) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)

Example 25 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jbpm by kiegroup.

the class JbpmJUnitTestCase method assertProcessVarExists.

public void assertProcessVarExists(ProcessInstance process, String... processVarNames) {
    WorkflowProcessInstanceImpl instance = (WorkflowProcessInstanceImpl) process;
    List<String> names = new ArrayList<String>();
    for (String nodeName : processVarNames) {
        names.add(nodeName);
    }
    for (String pvar : instance.getVariables().keySet()) {
        if (names.contains(pvar)) {
            names.remove(pvar);
        }
    }
    if (!names.isEmpty()) {
        String s = names.get(0);
        for (int i = 1; i < names.size(); i++) {
            s += ", " + names.get(i);
        }
        fail("Process Variable(s) do not exist: " + s);
    }
}
Also used : WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ArrayList(java.util.ArrayList)

Aggregations

WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)53 NodeInstance (org.kie.api.runtime.process.NodeInstance)17 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)11 WorkItemNodeInstance (org.jbpm.workflow.instance.node.WorkItemNodeInstance)11 Test (org.junit.Test)10 Node (org.kie.api.definition.process.Node)10 KieSession (org.kie.api.runtime.KieSession)9 Map (java.util.Map)7 RegistryContext (org.drools.core.command.impl.RegistryContext)6 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)6 Process (org.kie.api.definition.process.Process)6 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)5 VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)5 DynamicNodeInstance (org.jbpm.workflow.instance.node.DynamicNodeInstance)5 Person (com.salaboy.model.Person)4 KnowledgeBase (org.drools.KnowledgeBase)4 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)4 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)4