Search in sources :

Example 16 with ProcessRuntime

use of io.automatiko.engine.api.runtime.process.ProcessRuntime in project automatiko-engine by automatiko-io.

the class ProcessEventSupport method fireBeforeSLAViolated.

public void fireBeforeSLAViolated(final ProcessInstance instance, ProcessRuntime runtime) {
    final Iterator<ProcessEventListener> iter = getEventListenersIterator();
    final List<ProcessEventListener> delayedListeners = new ArrayList<ProcessEventListener>();
    final SLAViolatedEvent event = new SLAViolatedEventImpl(instance, runtime);
    if (iter.hasNext()) {
        do {
            ProcessEventListener listener = iter.next();
            if (listener instanceof DelayedExecution) {
                delayedListeners.add(listener);
            } else {
                listener.beforeSLAViolated(event);
            }
        } while (iter.hasNext());
    }
    unitOfWorkManager.currentUnitOfWork().intercept(WorkUnit.create(event, e -> {
        delayedListeners.forEach(l -> l.beforeSLAViolated(e));
    }));
}
Also used : ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) UnitOfWorkManager(io.automatiko.engine.api.uow.UnitOfWorkManager) DelayedExecution(io.automatiko.engine.api.event.process.DelayedExecution) ProcessNodeTriggeredEvent(io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) Iterator(java.util.Iterator) ProcessNodeLeftEvent(io.automatiko.engine.api.event.process.ProcessNodeLeftEvent) ProcessSignaledEvent(io.automatiko.engine.api.event.process.ProcessSignaledEvent) ProcessStartedEvent(io.automatiko.engine.api.event.process.ProcessStartedEvent) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) ProcessRuntime(io.automatiko.engine.api.runtime.process.ProcessRuntime) ArrayList(java.util.ArrayList) WorkUnit(io.automatiko.engine.api.uow.WorkUnit) List(java.util.List) ProcessNodeInstanceFailedEvent(io.automatiko.engine.api.event.process.ProcessNodeInstanceFailedEvent) ProcessCompletedEvent(io.automatiko.engine.api.event.process.ProcessCompletedEvent) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) Transition(io.automatiko.engine.api.workflow.workitem.Transition) SLAViolatedEvent(io.automatiko.engine.api.event.process.SLAViolatedEvent) ProcessWorkItemTransitionEvent(io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent) DelayedExecution(io.automatiko.engine.api.event.process.DelayedExecution) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ArrayList(java.util.ArrayList) SLAViolatedEvent(io.automatiko.engine.api.event.process.SLAViolatedEvent)

Example 17 with ProcessRuntime

use of io.automatiko.engine.api.runtime.process.ProcessRuntime in project automatiko-engine by automatiko-io.

the class ProcessEventSupport method fireBeforeSLAViolated.

public void fireBeforeSLAViolated(final ProcessInstance instance, NodeInstance nodeInstance, ProcessRuntime runtime) {
    final Iterator<ProcessEventListener> iter = getEventListenersIterator();
    final List<ProcessEventListener> delayedListeners = new ArrayList<ProcessEventListener>();
    final SLAViolatedEvent event = new SLAViolatedEventImpl(instance, nodeInstance, runtime);
    if (iter.hasNext()) {
        do {
            ProcessEventListener listener = iter.next();
            if (listener instanceof DelayedExecution) {
                delayedListeners.add(listener);
            } else {
                listener.beforeSLAViolated(event);
            }
        } while (iter.hasNext());
    }
    unitOfWorkManager.currentUnitOfWork().intercept(WorkUnit.create(event, e -> {
        delayedListeners.forEach(l -> l.beforeSLAViolated(e));
    }));
}
Also used : ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) UnitOfWorkManager(io.automatiko.engine.api.uow.UnitOfWorkManager) DelayedExecution(io.automatiko.engine.api.event.process.DelayedExecution) ProcessNodeTriggeredEvent(io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) Iterator(java.util.Iterator) ProcessNodeLeftEvent(io.automatiko.engine.api.event.process.ProcessNodeLeftEvent) ProcessSignaledEvent(io.automatiko.engine.api.event.process.ProcessSignaledEvent) ProcessStartedEvent(io.automatiko.engine.api.event.process.ProcessStartedEvent) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) ProcessRuntime(io.automatiko.engine.api.runtime.process.ProcessRuntime) ArrayList(java.util.ArrayList) WorkUnit(io.automatiko.engine.api.uow.WorkUnit) List(java.util.List) ProcessNodeInstanceFailedEvent(io.automatiko.engine.api.event.process.ProcessNodeInstanceFailedEvent) ProcessCompletedEvent(io.automatiko.engine.api.event.process.ProcessCompletedEvent) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) Transition(io.automatiko.engine.api.workflow.workitem.Transition) SLAViolatedEvent(io.automatiko.engine.api.event.process.SLAViolatedEvent) ProcessWorkItemTransitionEvent(io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent) DelayedExecution(io.automatiko.engine.api.event.process.DelayedExecution) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ArrayList(java.util.ArrayList) SLAViolatedEvent(io.automatiko.engine.api.event.process.SLAViolatedEvent)

Example 18 with ProcessRuntime

use of io.automatiko.engine.api.runtime.process.ProcessRuntime in project automatiko-engine by automatiko-io.

the class ProcessEventSupport method fireBeforeProcessSignaled.

public void fireBeforeProcessSignaled(String signal, Object data, final ProcessInstance instance, ProcessRuntime runtime) {
    final Iterator<ProcessEventListener> iter = getEventListenersIterator();
    final List<ProcessEventListener> delayedListeners = new ArrayList<ProcessEventListener>();
    final ProcessSignaledEvent event = new ProcessSignaledEventImpl(signal, data, instance, runtime);
    if (iter.hasNext()) {
        do {
            ProcessEventListener listener = iter.next();
            if (listener instanceof DelayedExecution) {
                delayedListeners.add(listener);
            } else {
                listener.beforeProcessSignaled(event);
            }
        } while (iter.hasNext());
    }
    unitOfWorkManager.currentUnitOfWork().intercept(WorkUnit.create(event, (e) -> {
        delayedListeners.forEach(l -> l.beforeProcessSignaled(e));
    }));
}
Also used : ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) UnitOfWorkManager(io.automatiko.engine.api.uow.UnitOfWorkManager) DelayedExecution(io.automatiko.engine.api.event.process.DelayedExecution) ProcessNodeTriggeredEvent(io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) Iterator(java.util.Iterator) ProcessNodeLeftEvent(io.automatiko.engine.api.event.process.ProcessNodeLeftEvent) ProcessSignaledEvent(io.automatiko.engine.api.event.process.ProcessSignaledEvent) ProcessStartedEvent(io.automatiko.engine.api.event.process.ProcessStartedEvent) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) ProcessRuntime(io.automatiko.engine.api.runtime.process.ProcessRuntime) ArrayList(java.util.ArrayList) WorkUnit(io.automatiko.engine.api.uow.WorkUnit) List(java.util.List) ProcessNodeInstanceFailedEvent(io.automatiko.engine.api.event.process.ProcessNodeInstanceFailedEvent) ProcessCompletedEvent(io.automatiko.engine.api.event.process.ProcessCompletedEvent) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) Transition(io.automatiko.engine.api.workflow.workitem.Transition) SLAViolatedEvent(io.automatiko.engine.api.event.process.SLAViolatedEvent) ProcessWorkItemTransitionEvent(io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent) DelayedExecution(io.automatiko.engine.api.event.process.DelayedExecution) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ArrayList(java.util.ArrayList) ProcessSignaledEvent(io.automatiko.engine.api.event.process.ProcessSignaledEvent)

Example 19 with ProcessRuntime

use of io.automatiko.engine.api.runtime.process.ProcessRuntime in project automatiko-engine by automatiko-io.

the class ProcessEventSupport method fireBeforeNodeLeft.

public void fireBeforeNodeLeft(final NodeInstance nodeInstance, ProcessRuntime runtime) {
    final Iterator<ProcessEventListener> iter = getEventListenersIterator();
    final List<ProcessEventListener> delayedListeners = new ArrayList<ProcessEventListener>();
    final ProcessNodeLeftEvent event = new ProcessNodeLeftEventImpl(nodeInstance, runtime);
    if (iter.hasNext()) {
        do {
            ProcessEventListener listener = iter.next();
            if (listener instanceof DelayedExecution) {
                delayedListeners.add(listener);
            } else {
                listener.beforeNodeLeft(event);
            }
        } while (iter.hasNext());
    }
    unitOfWorkManager.currentUnitOfWork().intercept(WorkUnit.create(event, e -> {
        delayedListeners.forEach(l -> l.beforeNodeLeft(e));
    }));
}
Also used : ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) UnitOfWorkManager(io.automatiko.engine.api.uow.UnitOfWorkManager) DelayedExecution(io.automatiko.engine.api.event.process.DelayedExecution) ProcessNodeTriggeredEvent(io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) Iterator(java.util.Iterator) ProcessNodeLeftEvent(io.automatiko.engine.api.event.process.ProcessNodeLeftEvent) ProcessSignaledEvent(io.automatiko.engine.api.event.process.ProcessSignaledEvent) ProcessStartedEvent(io.automatiko.engine.api.event.process.ProcessStartedEvent) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) ProcessRuntime(io.automatiko.engine.api.runtime.process.ProcessRuntime) ArrayList(java.util.ArrayList) WorkUnit(io.automatiko.engine.api.uow.WorkUnit) List(java.util.List) ProcessNodeInstanceFailedEvent(io.automatiko.engine.api.event.process.ProcessNodeInstanceFailedEvent) ProcessCompletedEvent(io.automatiko.engine.api.event.process.ProcessCompletedEvent) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) Transition(io.automatiko.engine.api.workflow.workitem.Transition) SLAViolatedEvent(io.automatiko.engine.api.event.process.SLAViolatedEvent) ProcessWorkItemTransitionEvent(io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent) DelayedExecution(io.automatiko.engine.api.event.process.DelayedExecution) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ArrayList(java.util.ArrayList) ProcessNodeLeftEvent(io.automatiko.engine.api.event.process.ProcessNodeLeftEvent)

Example 20 with ProcessRuntime

use of io.automatiko.engine.api.runtime.process.ProcessRuntime in project automatiko-engine by automatiko-io.

the class SubProcessNodeInstance method internalTrigger.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void internalTrigger(final NodeInstance from, String type) {
    super.internalTrigger(from, type);
    // if node instance was cancelled, abort
    if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
        return;
    }
    if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("A SubProcess node only accepts default incoming connections!");
    }
    Map<String, Object> parameters = new HashMap<String, Object>();
    for (Iterator<DataAssociation> iterator = getSubProcessNode().getInAssociations().iterator(); iterator.hasNext(); ) {
        DataAssociation mapping = iterator.next();
        Object parameterValue = null;
        if (mapping.getTransformation() != null) {
            Transformation transformation = mapping.getTransformation();
            DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
            if (transformer != null) {
                parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(mapping));
            }
        } else {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getSources().get(0));
            if (variableScopeInstance != null) {
                parameterValue = variableScopeInstance.getVariable(mapping.getSources().get(0));
            } else {
                try {
                    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    parameterValue = evaluator.evaluate(mapping.getSources().get(0), new NodeInstanceResolverFactory(this));
                } catch (Throwable t) {
                    parameterValue = VariableUtil.resolveVariable(mapping.getSources().get(0), this);
                    if (parameterValue != null) {
                        parameters.put(mapping.getTarget(), parameterValue);
                    } else {
                        logger.error("Could not find variable scope for variable {}", mapping.getSources().get(0));
                        logger.error("when trying to execute SubProcess node {}", getSubProcessNode().getName());
                        logger.error("Continuing without setting parameter.");
                    }
                }
            }
        }
        if (parameterValue != null) {
            parameters.put(mapping.getTarget(), parameterValue);
        }
    }
    String processId = getSubProcessNode().getProcessId();
    if (processId == null) {
        // if process id is not given try with process name
        processId = getSubProcessNode().getProcessName();
    }
    // resolve processId if necessary
    Map<String, String> replacements = new HashMap<String, String>();
    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(processId);
    while (matcher.find()) {
        String paramName = matcher.group(1);
        String replacementKey = paramName;
        String defaultValue = "";
        if (paramName.contains(":")) {
            String[] items = paramName.split(":");
            paramName = items[0];
            defaultValue = items[1];
        }
        if (replacements.get(replacementKey) == null) {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
            if (variableScopeInstance != null) {
                Object variableValue = variableScopeInstance.getVariable(paramName);
                String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
                replacements.put(replacementKey, variableValueString);
            } else {
                try {
                    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    Object variableValue = evaluator.evaluate(paramName, new NodeInstanceResolverFactory(this));
                    String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
                    replacements.put(replacementKey, variableValueString);
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", paramName);
                    logger.error("when trying to replace variable in processId for sub process {}", getNodeName());
                    logger.error("Continuing without setting process id.");
                }
            }
        }
    }
    for (Map.Entry<String, String> replacement : replacements.entrySet()) {
        processId = processId.replace("#{" + replacement.getKey() + "}", replacement.getValue());
    }
    // start process instance
    Process process = getProcessInstance().getProcessRuntime().getProcess(processId);
    if (process == null) {
        logger.error("Could not find process {}", processId);
        logger.error("Aborting process");
        ((ProcessInstance) getProcessInstance()).setState(ProcessInstance.STATE_ABORTED);
        throw new RuntimeException("Could not find process " + processId);
    } else {
        ProcessRuntime kruntime = ((ProcessInstance) getProcessInstance()).getProcessRuntime();
        if (getSubProcessNode().getMetaData("MICollectionInput") != null) {
            // remove foreach input variable to avoid problems when running in variable
            // strict mode
            parameters.remove(getSubProcessNode().getMetaData("MICollectionInput"));
        }
        ProcessInstance processInstance = null;
        if (((WorkflowProcessInstanceImpl) getProcessInstance()).getCorrelationKey() != null) {
            // in case there is correlation key on parent instance pass it along to child so
            // it can be easily correlated
            // since correlation key must be unique for active instances it appends
            // processId and timestamp
            List<String> businessKeys = new ArrayList<String>();
            businessKeys.add(((WorkflowProcessInstanceImpl) getProcessInstance()).getCorrelationKey());
            businessKeys.add(processId);
            businessKeys.add(String.valueOf(System.currentTimeMillis()));
            CorrelationKey subProcessCorrelationKey = new StringCorrelationKey(businessKeys.stream().collect(Collectors.joining(":")));
            processInstance = (ProcessInstance) ((CorrelationAwareProcessRuntime) kruntime).createProcessInstance(processId, subProcessCorrelationKey, parameters);
        } else {
            processInstance = (ProcessInstance) kruntime.createProcessInstance(processId, parameters);
        }
        this.processInstanceId = processInstance.getId();
        ((ProcessInstanceImpl) processInstance).setMetaData("ParentProcessInstanceId", getProcessInstance().getId());
        ((ProcessInstanceImpl) processInstance).setMetaData("ParentNodeInstanceId", getUniqueId());
        ((ProcessInstanceImpl) processInstance).setMetaData("ParentNodeId", getSubProcessNode().getUniqueId());
        ((ProcessInstanceImpl) processInstance).setParentProcessInstanceId(getProcessInstance().getId());
        ((ProcessInstanceImpl) processInstance).setRootProcessInstanceId(StringUtils.isEmpty(getProcessInstance().getRootProcessInstanceId()) ? getProcessInstance().getId() : getProcessInstance().getRootProcessInstanceId());
        ((ProcessInstanceImpl) processInstance).setRootProcessId(StringUtils.isEmpty(getProcessInstance().getRootProcessId()) ? getProcessInstance().getProcessId() : getProcessInstance().getRootProcessId());
        ((ProcessInstanceImpl) processInstance).setSignalCompletion(getSubProcessNode().isWaitForCompletion());
        ((ProcessInstanceImpl) processInstance).addChild(processInstance.getProcessId(), processInstance.getId());
        kruntime.startProcessInstance(processInstance.getId());
        if (!getSubProcessNode().isWaitForCompletion()) {
            triggerCompleted();
        } else if (processInstance.getState() == ProcessInstance.STATE_COMPLETED || processInstance.getState() == ProcessInstance.STATE_ABORTED) {
            processInstanceCompleted(processInstance);
        } else {
            addProcessListener();
        }
    }
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) StringCorrelationKey(io.automatiko.engine.services.correlation.StringCorrelationKey) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) Process(io.automatiko.engine.api.definition.process.Process) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) CorrelationKey(io.automatiko.engine.services.correlation.CorrelationKey) StringCorrelationKey(io.automatiko.engine.services.correlation.StringCorrelationKey) ProcessRuntime(io.automatiko.engine.api.runtime.process.ProcessRuntime) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) CorrelationAwareProcessRuntime(io.automatiko.engine.services.correlation.CorrelationAwareProcessRuntime) CorrelationAwareProcessRuntime(io.automatiko.engine.services.correlation.CorrelationAwareProcessRuntime) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ProcessInstanceImpl(io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ProcessRuntime (io.automatiko.engine.api.runtime.process.ProcessRuntime)22 ProcessInstance (io.automatiko.engine.api.runtime.process.ProcessInstance)21 ArrayList (java.util.ArrayList)21 List (java.util.List)20 DelayedExecution (io.automatiko.engine.api.event.process.DelayedExecution)19 ProcessCompletedEvent (io.automatiko.engine.api.event.process.ProcessCompletedEvent)19 ProcessEventListener (io.automatiko.engine.api.event.process.ProcessEventListener)19 ProcessNodeInstanceFailedEvent (io.automatiko.engine.api.event.process.ProcessNodeInstanceFailedEvent)19 ProcessNodeLeftEvent (io.automatiko.engine.api.event.process.ProcessNodeLeftEvent)19 ProcessNodeTriggeredEvent (io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent)19 ProcessSignaledEvent (io.automatiko.engine.api.event.process.ProcessSignaledEvent)19 ProcessStartedEvent (io.automatiko.engine.api.event.process.ProcessStartedEvent)19 ProcessVariableChangedEvent (io.automatiko.engine.api.event.process.ProcessVariableChangedEvent)19 ProcessWorkItemTransitionEvent (io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent)19 SLAViolatedEvent (io.automatiko.engine.api.event.process.SLAViolatedEvent)19 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)19 WorkItem (io.automatiko.engine.api.runtime.process.WorkItem)19 UnitOfWorkManager (io.automatiko.engine.api.uow.UnitOfWorkManager)19 WorkUnit (io.automatiko.engine.api.uow.WorkUnit)19 Transition (io.automatiko.engine.api.workflow.workitem.Transition)19