Search in sources :

Example 11 with NodeInstance

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

the class CompensationEventListener method createNodeInstanceContainers.

private Stack<NodeInstance> createNodeInstanceContainers(Node toCompensateNode, boolean generalCompensation) {
    Stack<NodeContainer> nestedNodes = new Stack<NodeContainer>();
    Stack<NodeInstance> generatedInstances = new Stack<NodeInstance>();
    NodeContainer parentContainer = toCompensateNode.getNodeContainer();
    while (!(parentContainer instanceof RuleFlowProcess)) {
        nestedNodes.add(parentContainer);
        parentContainer = ((Node) parentContainer).getNodeContainer();
    }
    NodeInstanceContainer parentInstance;
    if (nestedNodes.isEmpty()) {
        // nestedNodes is empty
        parentInstance = (NodeInstanceContainer) getProcessInstance();
    } else {
        parentInstance = (NodeInstanceContainer) ((WorkflowProcessInstanceImpl) getProcessInstance()).getNodeInstance((Node) nestedNodes.pop());
        generatedInstances.add((NodeInstance) parentInstance);
    }
    NodeInstanceContainer childInstance = parentInstance;
    while (!nestedNodes.isEmpty()) {
        // generate
        childInstance = (NodeInstanceContainer) parentInstance.getNodeInstance((Node) nestedNodes.pop());
        assert childInstance instanceof CompositeNodeInstance : "A node with child nodes should end up creating a CompositeNodeInstance type.";
        // track and modify
        generatedInstances.add((NodeInstance) childInstance);
        // loop
        parentInstance = (CompositeContextNodeInstance) childInstance;
    }
    if (generalCompensation) {
        childInstance = (NodeInstanceContainer) parentInstance.getNodeInstance(toCompensateNode);
        generatedInstances.add((NodeInstance) childInstance);
    }
    return generatedInstances;
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) NodeContainer(org.kie.api.definition.process.NodeContainer) NodeInstance(org.jbpm.workflow.instance.NodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance) Stack(java.util.Stack)

Example 12 with NodeInstance

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

the class WorkflowProcessInstanceImpl method reconnect.

public void reconnect() {
    validate();
    super.reconnect();
    for (NodeInstance nodeInstance : nodeInstances) {
        if (nodeInstance instanceof EventBasedNodeInstanceInterface) {
            ((EventBasedNodeInstanceInterface) nodeInstance).addEventListeners();
        }
    }
    registerExternalEventNodeListeners();
}
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 13 with NodeInstance

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

the class WorkflowProcessInstanceImpl method signalEvent.

@SuppressWarnings("unchecked")
public void signalEvent(String type, Object event) {
    logger.debug("Signal {} received with data {} in process instance {}", type, event, getId());
    synchronized (this) {
        if (getState() != ProcessInstance.STATE_ACTIVE) {
            return;
        }
        if ("timerTriggered".equals(type)) {
            TimerInstance timer = (TimerInstance) event;
            if (timer.getId() == slaTimerId) {
                handleSLAViolation();
                // no need to pass the event along as it was purely for SLA tracking
                return;
            }
        }
        if ("slaViolation".equals(type)) {
            handleSLAViolation();
            // no need to pass the event along as it was purely for SLA tracking
            return;
        }
        List<NodeInstance> currentView = new ArrayList<NodeInstance>(this.nodeInstances);
        try {
            this.activatingNodeIds = new ArrayList<String>();
            List<EventListener> listeners = eventListeners.get(type);
            if (listeners != null) {
                for (EventListener listener : listeners) {
                    listener.signalEvent(type, event);
                }
            }
            listeners = externalEventListeners.get(type);
            if (listeners != null) {
                for (EventListener listener : listeners) {
                    listener.signalEvent(type, event);
                }
            }
            for (Node node : getWorkflowProcess().getNodes()) {
                if (node instanceof EventNodeInterface) {
                    if (((EventNodeInterface) node).acceptsEvent(type, event, (e) -> resolveVariable(e))) {
                        if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
                            EventNodeInstance eventNodeInstance = (EventNodeInstance) getNodeInstance(node);
                            eventNodeInstance.signalEvent(type, event);
                        } else {
                            if (node instanceof EventSubProcessNode && ((resolveVariables(((EventSubProcessNode) node).getEvents()).contains(type)))) {
                                EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) getNodeInstance(node);
                                eventNodeInstance.signalEvent(type, event);
                            }
                            if (node instanceof DynamicNode && type.equals(((DynamicNode) node).getActivationEventName())) {
                                DynamicNodeInstance dynamicNodeInstance = (DynamicNodeInstance) getNodeInstance(node);
                                dynamicNodeInstance.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);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (((org.jbpm.workflow.core.WorkflowProcess) getWorkflowProcess()).isDynamic()) {
                for (Node node : getWorkflowProcess().getNodes()) {
                    if (type.equals(node.getName()) && node.getIncomingConnections().isEmpty()) {
                        NodeInstance nodeInstance = getNodeInstance(node);
                        if (event != null) {
                            Map<String, Object> dynamicParams = new HashMap<>();
                            if (event instanceof Map) {
                                dynamicParams.putAll((Map<String, Object>) event);
                            } else {
                                dynamicParams.put("Data", event);
                            }
                            ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).setDynamicParameters(dynamicParams);
                        }
                        ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).trigger(null, NodeImpl.CONNECTION_DEFAULT_TYPE);
                    }
                }
            }
        } finally {
            if (this.activatingNodeIds != null) {
                this.activatingNodeIds.clear();
                this.activatingNodeIds = null;
            }
        }
    }
}
Also used : DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) HashMap(java.util.HashMap) EventNodeInterface(org.jbpm.workflow.core.node.EventNodeInterface) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) DynamicNode(org.jbpm.workflow.core.node.DynamicNode) AsyncEventNode(org.jbpm.workflow.core.node.AsyncEventNode) StateBasedNode(org.jbpm.workflow.core.node.StateBasedNode) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) 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) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) AsyncEventNode(org.jbpm.workflow.core.node.AsyncEventNode) EventNode(org.jbpm.workflow.core.node.EventNode) EventNodeInstanceInterface(org.jbpm.workflow.instance.node.EventNodeInstanceInterface) DynamicNode(org.jbpm.workflow.core.node.DynamicNode) EventListener(org.kie.api.runtime.process.EventListener) 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) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with NodeInstance

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

the class WorkflowProcessInstanceImpl method setState.

public void setState(final int state, String outcome) {
    super.setState(state, outcome);
    // TODO move most of this to ProcessInstanceImpl
    if (state == ProcessInstance.STATE_COMPLETED || state == ProcessInstance.STATE_ABORTED) {
        if (this.slaCompliance == SLA_PENDING) {
            if (System.currentTimeMillis() > slaDueDate.getTime()) {
                // completion of the process instance is after expected SLA due date, mark it accordingly
                this.slaCompliance = SLA_VIOLATED;
            } else {
                this.slaCompliance = state == ProcessInstance.STATE_COMPLETED ? SLA_MET : SLA_ABORTED;
            }
        }
        InternalKnowledgeRuntime kruntime = getKnowledgeRuntime();
        InternalProcessRuntime processRuntime = (InternalProcessRuntime) kruntime.getProcessRuntime();
        processRuntime.getProcessEventSupport().fireBeforeProcessCompleted(this, kruntime);
        // deactivate all node instances of this process instance
        while (!nodeInstances.isEmpty()) {
            NodeInstance nodeInstance = nodeInstances.get(0);
            ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).cancel();
        }
        if (this.slaTimerId > -1) {
            processRuntime.getTimerManager().cancelTimer(this.slaTimerId);
            logger.debug("SLA Timer {} has been canceled", this.slaTimerId);
        }
        removeEventListeners();
        processRuntime.getProcessInstanceManager().removeProcessInstance(this);
        processRuntime.getProcessEventSupport().fireAfterProcessCompleted(this, kruntime);
        if (isSignalCompletion()) {
            RuntimeManager manager = (RuntimeManager) kruntime.getEnvironment().get(EnvironmentName.RUNTIME_MANAGER);
            if (getParentProcessInstanceId() > 0 && manager != null) {
                try {
                    org.kie.api.runtime.manager.Context<?> context = ProcessInstanceIdContext.get(getParentProcessInstanceId());
                    String caseId = (String) kruntime.getEnvironment().get(EnvironmentName.CASE_ID);
                    if (caseId != null) {
                        context = CaseContext.get(caseId);
                    }
                    RuntimeEngine runtime = manager.getRuntimeEngine(context);
                    KieRuntime managedkruntime = (KieRuntime) runtime.getKieSession();
                    managedkruntime.signalEvent("processInstanceCompleted:" + getId(), this);
                } catch (SessionNotFoundException e) {
                // in case no session is found for parent process let's skip signal for process instance completion
                }
            } else {
                processRuntime.getSignalManager().signalEvent("processInstanceCompleted:" + getId(), this);
            }
        }
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) KieRuntime(org.kie.api.runtime.KieRuntime) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) 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) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException)

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