Search in sources :

Example 11 with Connection

use of org.kie.api.definition.process.Connection in project jbpm by kiegroup.

the class NodeInstanceImpl method triggerCompleted.

protected void triggerCompleted(String type, boolean remove) {
    getExecutionErrorHandler().processed(this);
    Node node = getNode();
    if (node != null) {
        String uniqueId = (String) node.getMetaData().get("UniqueId");
        if (uniqueId == null) {
            uniqueId = ((NodeImpl) node).getUniqueId();
        }
        ((WorkflowProcessInstanceImpl) processInstance).addCompletedNodeId(uniqueId);
        ((WorkflowProcessInstanceImpl) processInstance).getIterationLevels().remove(uniqueId);
    }
    // if node instance was cancelled, or containing container instance was cancelled
    if ((getNodeInstanceContainer().getNodeInstance(getId()) == null) || (((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).getState() != ProcessInstance.STATE_ACTIVE)) {
        return;
    }
    if (remove) {
        ((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
    }
    List<Connection> connections = null;
    if (node != null) {
        if ("true".equals(System.getProperty("jbpm.enable.multi.con")) && ((NodeImpl) node).getConstraints().size() > 0) {
            int priority = Integer.MAX_VALUE;
            connections = ((NodeImpl) node).getDefaultOutgoingConnections();
            boolean found = false;
            List<NodeInstanceTrigger> nodeInstances = new ArrayList<NodeInstanceTrigger>();
            List<Connection> outgoingCopy = new ArrayList<Connection>(connections);
            while (!outgoingCopy.isEmpty()) {
                priority = Integer.MAX_VALUE;
                Connection selectedConnection = null;
                ConstraintEvaluator selectedConstraint = null;
                for (final Iterator<Connection> iterator = outgoingCopy.iterator(); iterator.hasNext(); ) {
                    final Connection connection = (Connection) iterator.next();
                    ConstraintEvaluator constraint = (ConstraintEvaluator) ((NodeImpl) node).getConstraint(connection);
                    if (constraint != null && constraint.getPriority() < priority && !constraint.isDefault()) {
                        priority = constraint.getPriority();
                        selectedConnection = connection;
                        selectedConstraint = constraint;
                    }
                }
                if (selectedConstraint == null) {
                    break;
                }
                if (selectedConstraint.evaluate(this, selectedConnection, selectedConstraint)) {
                    nodeInstances.add(new NodeInstanceTrigger(followConnection(selectedConnection), selectedConnection.getToType()));
                    found = true;
                }
                outgoingCopy.remove(selectedConnection);
            }
            for (NodeInstanceTrigger nodeInstance : nodeInstances) {
                // stop if this process instance has been aborted / completed
                if (((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).getState() != ProcessInstance.STATE_ACTIVE) {
                    return;
                }
                triggerNodeInstance(nodeInstance.getNodeInstance(), nodeInstance.getToType());
            }
            if (!found) {
                for (final Iterator<Connection> iterator = connections.iterator(); iterator.hasNext(); ) {
                    final Connection connection = (Connection) iterator.next();
                    ConstraintEvaluator constraint = (ConstraintEvaluator) ((NodeImpl) node).getConstraint(connection);
                    if (constraint.isDefault()) {
                        triggerConnection(connection);
                        found = true;
                        break;
                    }
                }
            }
            if (!found) {
                throw new IllegalArgumentException("Uncontrolled flow node could not find at least one valid outgoing connection " + getNode().getName());
            }
            return;
        } else {
            connections = node.getOutgoingConnections(type);
        }
    }
    if (connections == null || connections.isEmpty()) {
        boolean hidden = false;
        Node currentNode = getNode();
        if (currentNode != null && currentNode.getMetaData().get("hidden") != null) {
            hidden = true;
        }
        InternalKnowledgeRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
        if (!hidden) {
            ((InternalProcessRuntime) kruntime.getProcessRuntime()).getProcessEventSupport().fireBeforeNodeLeft(this, kruntime);
        }
        // notify container
        ((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, type);
        if (!hidden) {
            ((InternalProcessRuntime) kruntime.getProcessRuntime()).getProcessEventSupport().fireAfterNodeLeft(this, kruntime);
        }
    } else {
        Map<org.jbpm.workflow.instance.NodeInstance, String> nodeInstances = new HashMap<org.jbpm.workflow.instance.NodeInstance, String>();
        for (Connection connection : connections) {
            nodeInstances.put(followConnection(connection), connection.getToType());
        }
        for (Map.Entry<org.jbpm.workflow.instance.NodeInstance, String> nodeInstance : nodeInstances.entrySet()) {
            // stop if this process instance has been aborted / completed
            if (((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).getState() != ProcessInstance.STATE_ACTIVE) {
                return;
            }
            triggerNodeInstance(nodeInstance.getKey(), nodeInstance.getValue());
        }
    }
}
Also used : NodeInstanceContainer(org.kie.api.runtime.process.NodeInstanceContainer) HashMap(java.util.HashMap) Node(org.kie.api.definition.process.Node) Connection(org.kie.api.definition.process.Connection) ArrayList(java.util.ArrayList) ConstraintEvaluator(org.jbpm.process.instance.impl.ConstraintEvaluator) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) NodeInstance(org.kie.api.runtime.process.NodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) ActionNodeInstance(org.jbpm.workflow.instance.node.ActionNodeInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with Connection

use of org.kie.api.definition.process.Connection in project jbpm by kiegroup.

the class CompositeNode method linkOutgoingConnections.

public void linkOutgoingConnections(CompositeNode.NodeAndType outNode, String outType) {
    CompositeNode.NodeAndType oldNodeAndType = outConnectionMap.get(outType);
    if (oldNodeAndType != null) {
        if (oldNodeAndType.equals(outNode)) {
            return;
        } else {
            // remove old end nodes + connections
            List<Connection> oldOutConnections = oldNodeAndType.getNode().getOutgoingConnections(oldNodeAndType.getType());
            for (Connection connection : new ArrayList<Connection>(oldOutConnections)) {
                if (connection.getTo() instanceof CompositeNodeEnd) {
                    removeNode(connection.getTo());
                    ((ConnectionImpl) connection).terminate();
                }
            }
        }
    }
    outConnectionMap.put(outType, outNode);
    if (outNode != null) {
        List<Connection> connections = getOutgoingConnections(outType);
        for (Connection connection : connections) {
            CompositeNodeEnd end = new CompositeNodeEnd(this, connection.getTo(), outType);
            internalAddNode(end);
            if (outNode.getNode() != null) {
                new ConnectionImpl(outNode.getNode(), outNode.getType(), end, org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE);
            }
        }
    }
}
Also used : Connection(org.kie.api.definition.process.Connection) ArrayList(java.util.ArrayList) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl)

Example 13 with Connection

use of org.kie.api.definition.process.Connection in project jbpm by kiegroup.

the class CompositeNode method linkIncomingConnections.

public void linkIncomingConnections(String inType, CompositeNode.NodeAndType inNode) {
    CompositeNode.NodeAndType oldNodeAndType = inConnectionMap.get(inType);
    if (oldNodeAndType != null) {
        if (oldNodeAndType.equals(inNode)) {
            return;
        } else {
            // remove old start nodes + connections
            List<Connection> oldInConnections = oldNodeAndType.getNode().getIncomingConnections(oldNodeAndType.getType());
            if (oldInConnections != null) {
                for (Connection connection : new ArrayList<Connection>(oldInConnections)) {
                    if (connection.getFrom() instanceof CompositeNodeStart) {
                        removeNode(connection.getFrom());
                        ((ConnectionImpl) connection).terminate();
                    }
                }
            }
        }
    }
    inConnectionMap.put(inType, inNode);
    if (inNode != null) {
        List<Connection> connections = getIncomingConnections(inType);
        for (Connection connection : connections) {
            CompositeNodeStart start = new CompositeNodeStart(this, connection.getFrom(), inType);
            internalAddNode(start);
            if (inNode.getNode() != null) {
                new ConnectionImpl(start, org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE, inNode.getNode(), inNode.getType());
            }
        }
    }
}
Also used : Connection(org.kie.api.definition.process.Connection) ArrayList(java.util.ArrayList) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl)

Example 14 with Connection

use of org.kie.api.definition.process.Connection in project jbpm by kiegroup.

the class AbstractCompositeNodeHandler method visitConnectionsAndAssociations.

protected void visitConnectionsAndAssociations(Node node, StringBuilder xmlDump, int metaDataType) {
    // add associations
    List<Connection> connections = getSubConnections((CompositeNode) node);
    xmlDump.append("    <!-- connections -->" + EOL);
    for (Connection connection : connections) {
        XmlBPMNProcessDumper.INSTANCE.visitConnection(connection, xmlDump, metaDataType);
    }
    // add associations
    List<Association> associations = (List<Association>) node.getMetaData().get(ProcessHandler.ASSOCIATIONS);
    if (associations != null) {
        for (Association association : associations) {
            XmlBPMNProcessDumper.INSTANCE.visitAssociation(association, xmlDump);
        }
    }
}
Also used : Association(org.jbpm.bpmn2.core.Association) Connection(org.kie.api.definition.process.Connection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 15 with Connection

use of org.kie.api.definition.process.Connection in project jbpm by kiegroup.

the class MultiConditionalSequenceFlowNodeBuilder method build.

public void build(Process process, ProcessDescr processDescr, ProcessBuildContext context, Node node) {
    Map<ConnectionRef, Constraint> constraints = ((NodeImpl) node).getConstraints();
    // exclude split as it is handled with separate builder and nodes with non conditional sequence flows
    if (node instanceof Split || constraints.size() == 0) {
        return;
    }
    // we need to clone the map, so we can update the original while iterating.
    Map<ConnectionRef, Constraint> map = new HashMap<ConnectionRef, Constraint>(constraints);
    for (Iterator<Map.Entry<ConnectionRef, Constraint>> it = map.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<ConnectionRef, Constraint> entry = it.next();
        ConnectionRef connection = entry.getKey();
        ConstraintImpl constraint = (ConstraintImpl) entry.getValue();
        Connection outgoingConnection = null;
        for (Connection out : ((NodeImpl) node).getDefaultOutgoingConnections()) {
            if (out.getToType().equals(connection.getToType()) && out.getTo().getId() == connection.getNodeId()) {
                outgoingConnection = out;
            }
        }
        if (outgoingConnection == null) {
            throw new IllegalArgumentException("Could not find outgoing connection");
        }
        if ("rule".equals(constraint.getType())) {
            RuleConstraintEvaluator ruleConstraint = new RuleConstraintEvaluator();
            ruleConstraint.setDialect(constraint.getDialect());
            ruleConstraint.setName(constraint.getName());
            ruleConstraint.setPriority(constraint.getPriority());
            ruleConstraint.setDefault(constraint.isDefault());
            ((NodeImpl) node).setConstraint(outgoingConnection, ruleConstraint);
        } else if ("code".equals(constraint.getType())) {
            ReturnValueConstraintEvaluator returnValueConstraint = new ReturnValueConstraintEvaluator();
            returnValueConstraint.setDialect(constraint.getDialect());
            returnValueConstraint.setName(constraint.getName());
            returnValueConstraint.setPriority(constraint.getPriority());
            returnValueConstraint.setDefault(constraint.isDefault());
            ((NodeImpl) node).setConstraint(outgoingConnection, returnValueConstraint);
            ReturnValueDescr returnValueDescr = new ReturnValueDescr();
            returnValueDescr.setText(constraint.getConstraint());
            returnValueDescr.setResource(processDescr.getResource());
            ProcessDialect dialect = ProcessDialectRegistry.getDialect(constraint.getDialect());
            dialect.getReturnValueEvaluatorBuilder().build(context, returnValueConstraint, returnValueDescr, (NodeImpl) node);
        }
    }
}
Also used : NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) Constraint(org.jbpm.workflow.core.Constraint) HashMap(java.util.HashMap) ReturnValueDescr(org.drools.compiler.compiler.ReturnValueDescr) Connection(org.kie.api.definition.process.Connection) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) ConstraintImpl(org.jbpm.workflow.core.impl.ConstraintImpl) RuleConstraintEvaluator(org.jbpm.process.instance.impl.RuleConstraintEvaluator) Split(org.jbpm.workflow.core.node.Split) ConnectionRef(org.jbpm.workflow.core.impl.ConnectionRef) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Connection (org.kie.api.definition.process.Connection)24 ArrayList (java.util.ArrayList)10 Node (org.kie.api.definition.process.Node)10 Map (java.util.Map)6 Constraint (org.jbpm.workflow.core.Constraint)6 HashMap (java.util.HashMap)5 ConnectionImpl (org.jbpm.workflow.core.impl.ConnectionImpl)5 CompositeNode (org.jbpm.workflow.core.node.CompositeNode)5 EventNode (org.jbpm.workflow.core.node.EventNode)5 Split (org.jbpm.workflow.core.node.Split)5 StartNode (org.jbpm.workflow.core.node.StartNode)5 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)4 ActionNode (org.jbpm.workflow.core.node.ActionNode)4 EndNode (org.jbpm.workflow.core.node.EndNode)4 List (java.util.List)3 FaultNode (org.jbpm.workflow.core.node.FaultNode)3 ForEachNode (org.jbpm.workflow.core.node.ForEachNode)3 WorkItemNode (org.jbpm.workflow.core.node.WorkItemNode)3 NodeInstanceContainer (org.jbpm.workflow.instance.NodeInstanceContainer)3 NodeInstance (org.kie.api.runtime.process.NodeInstance)3