Search in sources :

Example 6 with InternalProcessRuntime

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

the class ProcessInstanceResolverStrategy method retrieveKnowledgeRuntime.

/**
 * Retrieve the {@link ProcessInstanceManager} object from the ObjectOutput- or
 * ObjectInputStream. The stream object will secretly also either be a
 * {@link MarshallerReaderContext} or a {@link MarshallerWriteContext}.
 * </p>
 * The knowledge runtime object is useful in order to reconnect the process
 * instance to the process and the knowledge runtime object.
 *
 * @param streamContext The marshaller stream/context.
 * @return A {@link InternalKnowledgeRuntime} object.
 */
public static InternalProcessRuntime retrieveKnowledgeRuntime(Object streamContext) {
    InternalProcessRuntime kruntime = null;
    if (streamContext instanceof MarshallerWriteContext) {
        MarshallerWriteContext context = (MarshallerWriteContext) streamContext;
        kruntime = context.getProcessRuntime();
    } else if (streamContext instanceof MarshallerReaderContext) {
        MarshallerReaderContext context = (MarshallerReaderContext) streamContext;
        kruntime = context.getProcessRuntime();
    } else {
        throw new UnsupportedOperationException("Unable to retrieve " + ProcessInstanceManager.class.getSimpleName() + " from " + streamContext.getClass().getName());
    }
    return kruntime;
}
Also used : ProcessInstanceManager(io.automatiko.engine.workflow.base.instance.ProcessInstanceManager) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime)

Example 7 with InternalProcessRuntime

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

the class ProtobufProcessMarshaller method readWorkItems.

public void readWorkItems(MarshallerReaderContext context) throws IOException {
    AutomatikoMessages.ProcessData _pdata = (AutomatikoMessages.ProcessData) context.parameterObject;
    InternalProcessRuntime wm = context.getProcessRuntime();
    for (AutomatikoMessages.WorkItem _workItem : _pdata.getExtension(AutomatikoMessages.workItem)) {
        WorkItem workItem = readWorkItem(context, _workItem);
        ((DefaultWorkItemManager) wm.getWorkItemManager()).internalAddWorkItem((WorkItem) workItem);
    }
}
Also used : DefaultWorkItemManager(io.automatiko.engine.workflow.base.instance.impl.workitem.DefaultWorkItemManager) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem)

Example 8 with InternalProcessRuntime

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

the class SplitInstance method executeStrategy.

protected void executeStrategy(Split split, String type) {
    // TODO make different strategies for each type
    String uniqueId = (String) getNode().getMetaData().get(UNIQUE_ID);
    if (uniqueId == null) {
        uniqueId = ((NodeImpl) getNode()).getUniqueId();
    }
    switch(split.getType()) {
        case Split.TYPE_AND:
            triggerCompleted(io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE, true);
            break;
        case Split.TYPE_XOR:
            List<Connection> outgoing = split.getDefaultOutgoingConnections();
            int priority = Integer.MAX_VALUE;
            Connection selected = null;
            for (final Iterator<Connection> iterator = outgoing.iterator(); iterator.hasNext(); ) {
                final Connection connection = (Connection) iterator.next();
                ConstraintEvaluator constraint = (ConstraintEvaluator) split.getConstraint(connection);
                if (constraint != null && constraint.getPriority() < priority && !constraint.isDefault()) {
                    try {
                        if (constraint.evaluate(this, connection, constraint)) {
                            selected = connection;
                            priority = constraint.getPriority();
                        }
                    } catch (RuntimeException e) {
                        throw new RuntimeException("Exception when trying to evaluate constraint " + constraint.getName() + " in split " + split.getName(), e);
                    }
                }
            }
            ((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
            if (selected == null) {
                for (final Iterator<Connection> iterator = outgoing.iterator(); iterator.hasNext(); ) {
                    final Connection connection = (Connection) iterator.next();
                    if (split.isDefault(connection)) {
                        selected = connection;
                        break;
                    }
                }
            }
            if (selected == null) {
                throw new IllegalArgumentException("XOR split could not find at least one valid outgoing connection for split " + getSplit().getName());
            }
            if (!hasLoop(selected.getTo(), split)) {
                setLevel(1);
                ((NodeInstanceContainer) getNodeInstanceContainer()).setCurrentLevel(1);
            }
            triggerConnection(selected);
            ((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
            break;
        case Split.TYPE_OR:
            ((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
            outgoing = split.getDefaultOutgoingConnections();
            boolean found = false;
            List<NodeInstanceTrigger> nodeInstances = new ArrayList<NodeInstanceTrigger>();
            List<Connection> outgoingCopy = new ArrayList<Connection>(outgoing);
            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) split.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 (getProcessInstance().getState() == STATE_COMPLETED || getProcessInstance().getState() == STATE_ABORTED) {
                    return;
                }
                triggerNodeInstance(nodeInstance.getNodeInstance(), nodeInstance.getToType());
            }
            if (!found) {
                for (final Iterator<Connection> iterator = outgoing.iterator(); iterator.hasNext(); ) {
                    final Connection connection = (Connection) iterator.next();
                    ConstraintEvaluator constraint = (ConstraintEvaluator) split.getConstraint(connection);
                    if (constraint != null && constraint.isDefault() || split.isDefault(connection)) {
                        triggerConnection(connection);
                        found = true;
                        break;
                    }
                }
            }
            if (!found) {
                throw new IllegalArgumentException("OR split could not find at least one valid outgoing connection for split " + getSplit().getName());
            }
            ((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
            break;
        case Split.TYPE_XAND:
            ((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
            Node node = getNode();
            List<Connection> connections = null;
            if (node != null) {
                connections = node.getOutgoingConnections(type);
            }
            if (connections == null || connections.isEmpty()) {
                ((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, type);
            } else {
                ExclusiveGroupInstance groupInstance = new ExclusiveGroupInstance();
                io.automatiko.engine.api.runtime.process.NodeInstanceContainer parent = getNodeInstanceContainer();
                if (parent instanceof ContextInstanceContainer) {
                    ((ContextInstanceContainer) parent).addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, groupInstance);
                } else {
                    throw new IllegalArgumentException("An Exclusive AND is only possible if the parent is a context instance container");
                }
                Map<io.automatiko.engine.workflow.process.instance.NodeInstance, String> nodeInstancesMap = new HashMap<io.automatiko.engine.workflow.process.instance.NodeInstance, String>();
                for (Connection connection : connections) {
                    nodeInstancesMap.put(followConnection(connection), connection.getToType());
                }
                for (NodeInstance nodeInstance : nodeInstancesMap.keySet()) {
                    groupInstance.addNodeInstance(nodeInstance);
                }
                for (Map.Entry<io.automatiko.engine.workflow.process.instance.NodeInstance, String> entry : nodeInstancesMap.entrySet()) {
                    // stop if this process instance has been aborted / completed
                    if (getProcessInstance().getState() != ProcessInstance.STATE_ACTIVE) {
                        return;
                    }
                    boolean hidden = false;
                    if (getNode().getMetaData().get("hidden") != null) {
                        hidden = true;
                    }
                    InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
                    if (!hidden) {
                        runtime.getProcessEventSupport().fireBeforeNodeLeft(this, runtime);
                    }
                    ((io.automatiko.engine.workflow.process.instance.NodeInstance) entry.getKey()).trigger(this, entry.getValue());
                    if (!hidden) {
                        runtime.getProcessEventSupport().fireAfterNodeLeft(this, runtime);
                    }
                }
            }
            ((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
            break;
        default:
            throw new IllegalArgumentException("Illegal split type " + split.getType());
    }
}
Also used : NodeInstanceContainer(io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) HashMap(java.util.HashMap) Node(io.automatiko.engine.api.definition.process.Node) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ArrayList(java.util.ArrayList) ConstraintEvaluator(io.automatiko.engine.workflow.base.instance.impl.ConstraintEvaluator) WorkflowRuntimeException(io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException) ContextInstanceContainer(io.automatiko.engine.workflow.base.instance.ContextInstanceContainer) ExclusiveGroupInstance(io.automatiko.engine.workflow.base.instance.context.exclusive.ExclusiveGroupInstance) Connection(io.automatiko.engine.api.definition.process.Connection) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with InternalProcessRuntime

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

the class StartNodeInstance method signalEvent.

public void signalEvent(String type, Object event) {
    boolean hidden = false;
    if (getNode().getMetaData().get(HIDDEN) != null) {
        hidden = true;
    }
    InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
    if (!hidden) {
        runtime.getProcessEventSupport().fireBeforeNodeTriggered(this, runtime);
    }
    if (event != null) {
        String variableName = (String) getStartNode().getMetaData("TriggerMapping");
        if (!getStartNode().getOutAssociations().isEmpty()) {
            for (DataAssociation association : getStartNode().getOutAssociations()) {
                // } else
                if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null) {
                        Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
                        DataType dataType = varDef.getType();
                        // exclude java.lang.Object as it is considered unknown type
                        if (!dataType.getStringType().endsWith("java.lang.Object") && !dataType.getStringType().endsWith("Object") && event instanceof String) {
                            event = dataType.readValue((String) event);
                        } else {
                            variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), event);
                        }
                        variableScopeInstance.setVariable(this, association.getTarget(), event);
                    } else {
                        String output = association.getSources().get(0);
                        String target = association.getTarget();
                        Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
                        if (matcher.find()) {
                            String paramName = matcher.group(1);
                            String expression = VariableUtil.transformDotNotation(paramName, output);
                            NodeInstanceResolverFactory resolver = new NodeInstanceResolverFactory(this);
                            resolver.addExtraParameters(Collections.singletonMap(association.getSources().get(0), event));
                            Serializable compiled = MVEL.compileExpression(expression);
                            MVEL.executeExpression(compiled, resolver);
                            String varName = VariableUtil.nameFromDotNotation(paramName);
                            variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, varName);
                            variableScopeInstance.setVariable(this, varName, variableScopeInstance.getVariable(varName));
                        } else {
                            logger.warn("Could not find variable scope for variable {}", association.getTarget());
                            logger.warn("when trying to complete start node {}", getStartNode().getName());
                            logger.warn("Continuing without setting variable.");
                        }
                    }
                } else {
                    Object data = event;
                    association.getAssignments().stream().forEach(assignment -> handleAssignment(assignment, data));
                }
            }
        } else {
            if (variableName != null) {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, variableName);
                if (variableScopeInstance == null) {
                    throw new IllegalArgumentException("Could not find variable for start node: " + variableName);
                }
                EventTransformer transformer = getStartNode().getEventTransformer();
                if (transformer != null) {
                    event = transformer.transformEvent(event);
                }
                variableScopeInstance.setVariable(this, variableName, event);
            }
        }
    }
    VariableScope variableScope = (VariableScope) ((ContextContainer) getProcessInstance().getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE);
    for (Variable var : variableScope.getVariables()) {
        if (var.getMetaData(Variable.DEFAULT_VALUE) != null && variableScopeInstance.getVariable(var.getName()) == null) {
            Object value = runtime.getVariableInitializer().initialize(getProcessInstance().getProcess(), var, variableScopeInstance.getVariables());
            variableScope.validateVariable(getProcessInstance().getProcess().getName(), var.getName(), value);
            variableScopeInstance.setVariable(var.getName(), value);
        }
    }
    triggerCompleted();
    if (!hidden) {
        runtime.getProcessEventSupport().fireAfterNodeTriggered(this, runtime);
    }
}
Also used : Serializable(java.io.Serializable) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) Matcher(java.util.regex.Matcher) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 10 with InternalProcessRuntime

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

the class StateBasedNodeInstance method handleSLAViolation.

protected void handleSLAViolation() {
    if (slaCompliance == io.automatiko.engine.api.runtime.process.ProcessInstance.SLA_PENDING) {
        InternalProcessRuntime processRuntime = getProcessInstance().getProcessRuntime();
        processRuntime.getProcessEventSupport().fireBeforeSLAViolated(getProcessInstance(), this, processRuntime);
        logger.debug("SLA violated on node instance {}", getId());
        this.slaCompliance = io.automatiko.engine.api.runtime.process.ProcessInstance.SLA_VIOLATED;
        this.slaTimerId = null;
        processRuntime.getProcessEventSupport().fireAfterSLAViolated(getProcessInstance(), this, processRuntime);
    }
}
Also used : InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime)

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