Search in sources :

Example 6 with NodeInstanceContainer

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

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

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

the class StateNodeInstance method activationCreated.

public void activationCreated(MatchCreatedEvent event) {
    Connection selected = null;
    for (Connection connection : getNode().getOutgoingConnections(NodeImpl.CONNECTION_DEFAULT_TYPE)) {
        Constraint constraint = getStateNode().getConstraint(connection);
        if (constraint != null) {
            String constraintName = getActivationEventType() + "-" + connection.getTo().getId() + "-" + connection.getToType();
            if (constraintName.equals(event.getMatch().getRule().getName()) && checkProcessInstance((Activation) event.getMatch())) {
                selected = connection;
            }
        }
    }
    if (selected != null) {
        removeEventListeners();
        ((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
        triggerConnection(selected);
    }
}
Also used : NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) Constraint(org.jbpm.workflow.core.Constraint) Connection(org.kie.api.definition.process.Connection) Activation(org.drools.core.spi.Activation)

Example 9 with NodeInstanceContainer

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

the class StateNodeInstance method internalTrigger.

public void internalTrigger(NodeInstance from, String type) {
    super.internalTrigger(from, type);
    // if node instance was cancelled, abort
    if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
        return;
    }
    // TODO: composite states trigger
    StateNode stateNode = getStateNode();
    Connection selected = null;
    int priority = Integer.MAX_VALUE;
    for (Connection connection : stateNode.getOutgoingConnections(NodeImpl.CONNECTION_DEFAULT_TYPE)) {
        Constraint constraint = stateNode.getConstraint(connection);
        if (constraint != null && constraint.getPriority() < priority) {
            String rule = "RuleFlowStateNode-" + getProcessInstance().getProcessId() + "-" + getStateNode().getUniqueId() + "-" + connection.getTo().getId() + "-" + connection.getToType();
            boolean isActive = ((InternalAgenda) getProcessInstance().getKnowledgeRuntime().getAgenda()).isRuleActiveInRuleFlowGroup("DROOLS_SYSTEM", rule, getProcessInstance().getId());
            if (isActive) {
                selected = connection;
                priority = constraint.getPriority();
            }
        }
    }
    if (selected != null) {
        ((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
        triggerConnection(selected);
    } else {
        addTriggerListener();
        addActivationListener();
    }
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) Constraint(org.jbpm.workflow.core.Constraint) StateNode(org.jbpm.workflow.core.node.StateNode) Connection(org.kie.api.definition.process.Connection) Constraint(org.jbpm.workflow.core.Constraint)

Example 10 with NodeInstanceContainer

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

the class EndNodeInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    super.internalTrigger(from, type);
    if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("An EndNode only accepts default incoming connections!");
    }
    boolean hidden = false;
    if (getNode().getMetaData().get("hidden") != null) {
        hidden = true;
    }
    InternalKnowledgeRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
    if (!hidden) {
        ((InternalProcessRuntime) kruntime.getProcessRuntime()).getProcessEventSupport().fireBeforeNodeLeft(this, kruntime);
    }
    ((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
    if (getEndNode().isTerminate()) {
        if (getNodeInstanceContainer() instanceof CompositeNodeInstance) {
            if (getEndNode().getScope() == EndNode.PROCESS_SCOPE) {
                getProcessInstance().setState(ProcessInstance.STATE_COMPLETED);
            } else {
                while (!getNodeInstanceContainer().getNodeInstances().isEmpty()) {
                    ((org.jbpm.workflow.instance.NodeInstance) getNodeInstanceContainer().getNodeInstances().iterator().next()).cancel();
                }
                ((NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, null);
            }
        } else {
            ((NodeInstanceContainer) getNodeInstanceContainer()).setState(ProcessInstance.STATE_COMPLETED);
        }
    } else {
        ((NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, null);
    }
    if (!hidden) {
        ((InternalProcessRuntime) kruntime.getProcessRuntime()).getProcessEventSupport().fireAfterNodeLeft(this, kruntime);
    }
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) NodeInstance(org.kie.api.runtime.process.NodeInstance)

Aggregations

NodeInstanceContainer (org.jbpm.workflow.instance.NodeInstanceContainer)14 NodeInstance (org.kie.api.runtime.process.NodeInstance)7 NodeImpl (org.jbpm.workflow.core.impl.NodeImpl)3 NodeInstance (org.jbpm.workflow.instance.NodeInstance)3 Connection (org.kie.api.definition.process.Connection)3 Node (org.kie.api.definition.process.Node)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)2 ContextInstanceContainer (org.jbpm.process.instance.ContextInstanceContainer)2 ProcessInstance (org.jbpm.process.instance.ProcessInstance)2 Constraint (org.jbpm.workflow.core.Constraint)2 BoundaryEventNode (org.jbpm.workflow.core.node.BoundaryEventNode)2 WorkflowRuntimeException (org.jbpm.workflow.instance.WorkflowRuntimeException)2 NodeInstanceImpl (org.jbpm.workflow.instance.impl.NodeInstanceImpl)2 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)2 EventNodeInstance (org.jbpm.workflow.instance.node.EventNodeInstance)2 EventSubProcessNodeInstance (org.jbpm.workflow.instance.node.EventSubProcessNodeInstance)2 StateBasedNodeInstance (org.jbpm.workflow.instance.node.StateBasedNodeInstance)2 TimerNodeInstance (org.jbpm.workflow.instance.node.TimerNodeInstance)2