Search in sources :

Example 36 with InternalProcessRuntime

use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.

the class NodeInstanceImpl method retry.

public void retry() {
    boolean hidden = false;
    if (getNode().getMetaData().get(HIDDEN) != null) {
        hidden = true;
    }
    InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
    try {
        internalChangeState(NodeInstanceState.Active);
        internalTrigger(this, io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE);
        Collection<Connection> outgoing = getNode().getOutgoingConnections(io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE);
        for (Connection conn : outgoing) {
            if (conn.getTo().getId() == getNodeId()) {
                this.metaData.put(OUTGOING_CONNECTION, conn.getMetaData().get(UNIQUE_ID));
                break;
            }
        }
        if (!hidden) {
            runtime.getProcessEventSupport().fireAfterNodeLeft(this, runtime);
        }
    } catch (Exception e) {
        String errorId = captureError(e);
        internalChangeState(NodeInstanceState.Failed);
        runtime.getProcessEventSupport().fireAfterNodeInstanceFailed(getProcessInstance(), this, errorId, getRootException(e).getMessage(), e, runtime);
        // stop after capturing error
        return;
    }
}
Also used : Connection(io.automatiko.engine.api.definition.process.Connection) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) WorkflowRuntimeException(io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException)

Example 37 with InternalProcessRuntime

use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.

the class NodeInstanceImpl method continueToNextNode.

protected void continueToNextNode(String type, Node node) {
    List<Connection> connections = null;
    if (node != null) {
        if ("true".equals(System.getProperty("jbpm.enable.multi.con")) && ((NodeImpl) node).getConstraints().size() > 0) {
            int priority;
            connections = ((NodeImpl) node).getDefaultOutgoingConnections();
            boolean found = false;
            List<NodeInstanceTrigger> nodeInstances = new ArrayList<>();
            List<Connection> outgoingCopy = new ArrayList<>(connections);
            while (!outgoingCopy.isEmpty()) {
                priority = Integer.MAX_VALUE;
                Connection selectedConnection = null;
                ConstraintEvaluator selectedConstraint = null;
                for (final Connection connection : outgoingCopy) {
                    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 (((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) getNodeInstanceContainer()).getState() != STATE_ACTIVE) {
                    return;
                }
                triggerNodeInstance(nodeInstance.getNodeInstance(), nodeInstance.getToType());
            }
            if (!found) {
                for (final Connection connection : connections) {
                    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;
        }
        InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
        if (!hidden) {
            runtime.getProcessEventSupport().fireBeforeNodeLeft(this, runtime);
        }
        // notify container
        ((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, type);
        if (!hidden) {
            runtime.getProcessEventSupport().fireAfterNodeLeft(this, runtime);
        }
    } else {
        Map<io.automatiko.engine.workflow.process.instance.NodeInstance, String> nodeInstances = new HashMap<>();
        for (Connection connection : connections) {
            nodeInstances.put(followConnection(connection), connection.getToType());
        }
        for (Map.Entry<io.automatiko.engine.workflow.process.instance.NodeInstance, String> nodeInstance : nodeInstances.entrySet()) {
            // stop if this process instance has been aborted / completed
            int state = ((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) getNodeInstanceContainer()).getState();
            if (state == STATE_COMPLETED || state == STATE_ABORTED) {
                return;
            }
            try {
                triggerNodeInstance(nodeInstance.getKey(), nodeInstance.getValue());
            } catch (Exception e) {
                logger.warn("Error caught at executing node instance " + nodeInstance.getKey(), e);
            }
        }
    }
}
Also used : NodeInstanceContainer(io.automatiko.engine.api.runtime.process.NodeInstanceContainer) HashMap(java.util.HashMap) Node(io.automatiko.engine.api.definition.process.Node) ArrayList(java.util.ArrayList) ConstraintEvaluator(io.automatiko.engine.workflow.base.instance.impl.ConstraintEvaluator) Connection(io.automatiko.engine.api.definition.process.Connection) WorkflowRuntimeException(io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) ActionNodeInstance(io.automatiko.engine.workflow.process.instance.node.ActionNodeInstance) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) CompositeNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeNodeInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 38 with InternalProcessRuntime

use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.

the class DmnDecisionInProcessTest method createProcessRuntime.

protected InternalProcessRuntime createProcessRuntime(Process... process) {
    Map<String, Process> mappedProcesses = Stream.of(process).collect(Collectors.toMap(Process::getId, p -> p));
    InternalProcessRuntime processRuntime = new ProcessRuntimeImpl(mappedProcesses);
    return processRuntime;
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RuleSetNode(io.automatiko.engine.workflow.process.core.node.RuleSetNode) HashMap(java.util.HashMap) ProcessRuntime(io.automatiko.engine.api.runtime.process.ProcessRuntime) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) ArrayList(java.util.ArrayList) BooleanDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.BooleanDataType) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) Map(java.util.Map) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Process(io.automatiko.engine.api.definition.process.Process) Node(io.automatiko.engine.workflow.process.core.Node) DmnRuntimeProvider(io.automatiko.engine.decision.dmn.DmnRuntimeProvider) ConnectionImpl(io.automatiko.engine.workflow.process.core.impl.ConnectionImpl) ProcessRuntimeImpl(io.automatiko.engine.workflow.base.instance.ProcessRuntimeImpl) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) DmnDecisionModel(io.automatiko.engine.decision.dmn.DmnDecisionModel) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) ProcessRuntimeImpl(io.automatiko.engine.workflow.base.instance.ProcessRuntimeImpl) Process(io.automatiko.engine.api.definition.process.Process) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime)

Example 39 with InternalProcessRuntime

use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.

the class VariableScopeInstance method setVariable.

@SuppressWarnings("unchecked")
public void setVariable(NodeInstance nodeInstance, String name, Object value) {
    if (name == null) {
        throw new IllegalArgumentException("The name of a variable may not be null!");
    }
    Variable var = getVariableScope().findVariable(name);
    if (var != null) {
        name = var.getName();
    }
    Object oldValue = getVariable(name);
    if (oldValue == null) {
        if (value == null) {
            return;
        }
    }
    // check if variable that is being set is readonly and has already been set
    if (oldValue != null && getVariableScope().isReadOnly(name)) {
        throw new VariableViolationException(getProcessInstance().getId(), name, "Variable '" + name + "' is already set and is marked as read only");
    }
    // in case variable is marked as notnull (via tag) then null values should be ignored
    if (value == null && getVariableScope().isNullable(name)) {
        return;
    }
    // in case variable is versioned store the old value into versioned variable by variable name
    if (var != null && var.hasTag(Variable.VERSIONED_TAG)) {
        Map<String, List<Object>> versions = (Map<String, List<Object>>) variables.computeIfAbsent(VariableScope.VERSIONED_VARIABLES, key -> new ConcurrentHashMap<>());
        List<Object> varVersions = versions.computeIfAbsent(name, k -> new ArrayList<>());
        int versionLimit = Integer.parseInt(var.getMetaData().getOrDefault(Variable.VAR_VERSIONS_LIMIT, "10").toString());
        if (oldValue != null) {
            varVersions.add(oldValue);
            // and remove the oldest if exceeding
            if (varVersions.size() > versionLimit) {
                varVersions.remove(0);
            }
        }
    }
    if (getProcessInstance().getProcessRuntime().getVariableInitializer() != null) {
        for (VariableAugmentor augmentor : getProcessInstance().getProcessRuntime().getVariableInitializer().augmentors()) {
            if (augmentor.accept(var, value)) {
                // run any of the available augmentors on the value
                if (oldValue != null) {
                    value = augmentor.augmentOnUpdate(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, value);
                } else if (value == null) {
                    augmentor.augmentOnDelete(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, oldValue);
                } else {
                    value = augmentor.augmentOnCreate(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, value);
                }
            }
        }
    }
    ProcessInstance processInstance = getProcessInstance();
    if (nodeInstance != null) {
        processInstance = nodeInstance.getProcessInstance();
    }
    ProcessEventSupport processEventSupport = ((InternalProcessRuntime) getProcessInstance().getProcessRuntime()).getProcessEventSupport();
    processEventSupport.fireBeforeVariableChanged((variableIdPrefix == null ? "" : variableIdPrefix + ":") + name, (variableInstanceIdPrefix == null ? "" : variableInstanceIdPrefix + ":") + name, oldValue, value, getVariableScope().tags(name), processInstance, nodeInstance, getProcessInstance().getProcessRuntime());
    internalSetVariable(name, value);
    processEventSupport.fireAfterVariableChanged((variableIdPrefix == null ? "" : variableIdPrefix + ":") + name, (variableInstanceIdPrefix == null ? "" : variableInstanceIdPrefix + ":") + name, oldValue, value, getVariableScope().tags(name), processInstance, nodeInstance, getProcessInstance().getProcessRuntime());
    processInstance.signalEvent("variableChanged", value);
}
Also used : AbstractContextInstance(io.automatiko.engine.workflow.base.instance.context.AbstractContextInstance) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) ArrayList(java.util.ArrayList) ProcessInstanceImpl(io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl) ContextInstanceContainer(io.automatiko.engine.workflow.base.instance.ContextInstanceContainer) List(java.util.List) VariableAugmentor(io.automatiko.engine.api.workflow.VariableAugmentor) VariableViolationException(io.automatiko.engine.api.workflow.VariableViolationException) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) ProcessEventSupport(io.automatiko.engine.workflow.base.core.event.ProcessEventSupport) Map(java.util.Map) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) CompositeContextNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) Collections(java.util.Collections) Node(io.automatiko.engine.workflow.process.core.Node) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableViolationException(io.automatiko.engine.api.workflow.VariableViolationException) ProcessEventSupport(io.automatiko.engine.workflow.base.core.event.ProcessEventSupport) VariableAugmentor(io.automatiko.engine.api.workflow.VariableAugmentor) ArrayList(java.util.ArrayList) List(java.util.List) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

InternalProcessRuntime (io.automatiko.engine.workflow.base.instance.InternalProcessRuntime)39 Test (org.junit.jupiter.api.Test)18 AbstractBaseTest (io.automatiko.engine.workflow.test.util.AbstractBaseTest)17 ArrayList (java.util.ArrayList)17 ExecutableProcess (io.automatiko.engine.workflow.process.executable.core.ExecutableProcess)16 HashMap (java.util.HashMap)12 ProcessInstance (io.automatiko.engine.api.runtime.process.ProcessInstance)11 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)10 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)10 List (java.util.List)10 Map (java.util.Map)10 ConnectionImpl (io.automatiko.engine.workflow.process.core.impl.ConnectionImpl)9 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)8 ProcessRuntimeImpl (io.automatiko.engine.workflow.base.instance.ProcessRuntimeImpl)7 Date (java.util.Date)7 Process (io.automatiko.engine.api.definition.process.Process)6 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)6 ProcessAction (io.automatiko.engine.workflow.process.core.ProcessAction)6 ActionNode (io.automatiko.engine.workflow.process.core.node.ActionNode)6 WorkflowProcessInstance (io.automatiko.engine.workflow.process.instance.WorkflowProcessInstance)6