Search in sources :

Example 6 with NodeInstance

use of org.jbpm.workflow.instance.NodeInstance in project jbpm by kiegroup.

the class WorkflowProcessInstanceImpl method disconnect.

public void disconnect() {
    removeEventListeners();
    unregisterExternalEventNodeListeners();
    for (NodeInstance nodeInstance : nodeInstances) {
        if (nodeInstance instanceof EventBasedNodeInstanceInterface) {
            ((EventBasedNodeInstanceInterface) nodeInstance).removeEventListeners();
        }
    }
    super.disconnect();
}
Also used : EventBasedNodeInstanceInterface(org.jbpm.workflow.instance.node.EventBasedNodeInstanceInterface) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) EndNodeInstance(org.jbpm.workflow.instance.node.EndNodeInstance) NodeInstance(org.jbpm.workflow.instance.NodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance)

Example 7 with NodeInstance

use of org.jbpm.workflow.instance.NodeInstance 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 8 with NodeInstance

use of org.jbpm.workflow.instance.NodeInstance 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 9 with NodeInstance

use of org.jbpm.workflow.instance.NodeInstance in project jbpm by kiegroup.

the class CompositeNodeInstance method signalEvent.

@Override
public void signalEvent(String type, Object event) {
    List<NodeInstance> currentView = new ArrayList<NodeInstance>(this.nodeInstances);
    super.signalEvent(type, event);
    for (Node node : getCompositeNode().internalGetNodes()) {
        if (node instanceof EventNodeInterface) {
            if (((EventNodeInterface) node).acceptsEvent(type, event)) {
                if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
                    EventNodeInstanceInterface eventNodeInstance = (EventNodeInstanceInterface) getNodeInstance(node);
                    eventNodeInstance.signalEvent(type, event);
                } else if (node instanceof EventSubProcessNode) {
                    EventNodeInstanceInterface eventNodeInstance = (EventNodeInstanceInterface) getNodeInstance(node);
                    eventNodeInstance.signalEvent(type, event);
                } else {
                    List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
                    if (nodeInstances != null && !nodeInstances.isEmpty()) {
                        for (NodeInstance nodeInstance : nodeInstances) {
                            ((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event);
                        }
                    }
                }
            }
        }
    }
}
Also used : AsyncEventNode(org.jbpm.workflow.core.node.AsyncEventNode) EventNode(org.jbpm.workflow.core.node.EventNode) EventNodeInterface(org.jbpm.workflow.core.node.EventNodeInterface) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) StateBasedNode(org.jbpm.workflow.core.node.StateBasedNode) StartNode(org.jbpm.workflow.core.node.StartNode) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) AsyncEventNode(org.jbpm.workflow.core.node.AsyncEventNode) ActionNode(org.jbpm.workflow.core.node.ActionNode) EndNode(org.jbpm.workflow.core.node.EndNode) EventNode(org.jbpm.workflow.core.node.EventNode) Node(org.kie.api.definition.process.Node) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NodeInstance(org.jbpm.workflow.instance.NodeInstance)

Example 10 with NodeInstance

use of org.jbpm.workflow.instance.NodeInstance in project jbpm by kiegroup.

the class CompensationEventListener method signalEvent.

/**
 * When signaling compensation, you can do that in 1 of 2 ways:
 * 1. signalEvent("Compensation", <node-with-compensation-handler-id>)
 *    This is specific compensation, that only possibly triggers the compensation handler
 *    attached to the node referred to by the <node-with-compensation-handler-id>.
 * 2. signalEvent("Compensation", "implicit:" + <node-container-containing-compensation-scope-id> )
 *    This is implicit or general compensation, in which you trigger all visible compensation handlers
 *    (in the proper order, etc.) in the (sub-)process referred to by
 *    the <node-container-containing-compensation-scope-id>.
 */
public void signalEvent(String compensationType, Object activityRefStr) {
    if (activityRefStr == null || !(activityRefStr instanceof String)) {
        throw new WorkflowRuntimeException(null, getProcessInstance(), "Compensation can only be triggered with String events, not an event of type " + activityRefStr == null ? "null" : activityRefStr.getClass().getSimpleName());
    }
    // 1. parse the activity ref (is it general or specific compensation?)
    String activityRef = (String) activityRefStr;
    String toCompensateNodeId = activityRef;
    boolean generalCompensation = false;
    if (activityRef.startsWith(IMPLICIT_COMPENSATION_PREFIX)) {
        toCompensateNodeId = activityRef.substring(IMPLICIT_COMPENSATION_PREFIX.length());
        generalCompensation = true;
    }
    org.jbpm.process.core.Process process = (org.jbpm.process.core.Process) instance.getProcess();
    // 2. for specific compensation: find the node that will be compensated
    // for general compensation: find the compensation scope container that contains all the visible compensation handlers
    Node toCompensateNode = null;
    ContextContainer compensationScopeContainer = null;
    if (generalCompensation) {
        if (toCompensateNodeId.equals(instance.getProcessId())) {
            compensationScopeContainer = process;
        } else {
            compensationScopeContainer = (ContextContainer) findNode(toCompensateNodeId);
        }
    } else {
        toCompensateNode = findNode(toCompensateNodeId);
    }
    // c. handle the exception (which also cleans up the generated node instances)
    if (toCompensateNode != null || compensationScopeContainer != null) {
        CompensationScope compensationScope = null;
        if (compensationScopeContainer != null) {
            compensationScope = (CompensationScope) compensationScopeContainer.getDefaultContext(COMPENSATION_SCOPE);
        } else {
            compensationScope = (CompensationScope) ((NodeImpl) toCompensateNode).resolveContext(COMPENSATION_SCOPE, toCompensateNodeId);
        }
        assert compensationScope != null : "Compensation scope for node [" + toCompensateNodeId + "] could not be found!";
        CompensationScopeInstance scopeInstance;
        if (compensationScope.getContextContainerId().equals(process.getId())) {
            // process level compensation
            scopeInstance = (CompensationScopeInstance) instance.getContextInstance(compensationScope);
        } else {
            // nested compensation
            Stack<NodeInstance> generatedInstances;
            if (toCompensateNode == null) {
                // logic is the same if it's specific or general
                generatedInstances = createNodeInstanceContainers((Node) compensationScopeContainer, true);
            } else {
                generatedInstances = createNodeInstanceContainers(toCompensateNode, false);
            }
            NodeInstance nodeInstanceContainer = generatedInstances.peek();
            scopeInstance = ((CompensationScopeInstance) ((ContextInstanceContainer) nodeInstanceContainer).getContextInstance(compensationScope));
            scopeInstance.addCompensationInstances(generatedInstances);
        }
        scopeInstance.handleException(activityRef, null);
    }
}
Also used : NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) CompensationScopeInstance(org.jbpm.process.instance.context.exception.CompensationScopeInstance) Node(org.kie.api.definition.process.Node) Process(org.kie.api.definition.process.Process) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ContextContainer(org.jbpm.process.core.ContextContainer) CompensationScope(org.jbpm.process.core.context.exception.CompensationScope) NodeInstance(org.jbpm.workflow.instance.NodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance)

Aggregations

NodeInstance (org.jbpm.workflow.instance.NodeInstance)14 CompositeNodeInstance (org.jbpm.workflow.instance.node.CompositeNodeInstance)6 EventNodeInstance (org.jbpm.workflow.instance.node.EventNodeInstance)6 EventSubProcessNodeInstance (org.jbpm.workflow.instance.node.EventSubProcessNodeInstance)6 Node (org.kie.api.definition.process.Node)6 EventSubProcessNode (org.jbpm.workflow.core.node.EventSubProcessNode)4 NodeInstanceContainer (org.jbpm.workflow.instance.NodeInstanceContainer)4 DynamicNodeInstance (org.jbpm.workflow.instance.node.DynamicNodeInstance)4 EndNodeInstance (org.jbpm.workflow.instance.node.EndNodeInstance)4 ActionNode (org.jbpm.workflow.core.node.ActionNode)3 AsyncEventNode (org.jbpm.workflow.core.node.AsyncEventNode)3 EndNode (org.jbpm.workflow.core.node.EndNode)3 EventNode (org.jbpm.workflow.core.node.EventNode)3 StartNode (org.jbpm.workflow.core.node.StartNode)3 StateBasedNode (org.jbpm.workflow.core.node.StateBasedNode)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompensationScope (org.jbpm.process.core.context.exception.CompensationScope)2 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)2 NodeImpl (org.jbpm.workflow.core.impl.NodeImpl)2