Search in sources :

Example 51 with ProcessInstance

use of io.automatiko.engine.api.runtime.process.ProcessInstance 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)

Example 52 with ProcessInstance

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

the class DefaultWorkItemManager method abortWorkItem.

public void abortWorkItem(String id, Policy<?>... policies) {
    WorkItemImpl workItem = (WorkItemImpl) workItems.get(id);
    // work item may have been aborted
    if (workItem != null) {
        ProcessInstance processInstance = runtime.getProcessInstance(workItem.getProcessInstanceId());
        workItem.setState(ABORTED);
        // process instance may have finished already
        if (processInstance != null) {
            processInstance.signalEvent("workItemAborted", workItem);
        }
        workItems.remove(id);
    }
}
Also used : ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance)

Example 53 with ProcessInstance

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

the class ProcessRuntimeImpl method abortProcessInstance.

public void abortProcessInstance(String processInstanceId) {
    ProcessInstance processInstance = getProcessInstance(processInstanceId);
    if (processInstance == null) {
        throw new IllegalArgumentException("Could not find process instance for id " + processInstanceId);
    }
    ((io.automatiko.engine.workflow.base.instance.ProcessInstance) processInstance).setState(ProcessInstance.STATE_ABORTED);
}
Also used : ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance)

Example 54 with ProcessInstance

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

the class ProcessRuntimeImpl method startProcessInstance.

public ProcessInstance startProcessInstance(String processInstanceId, String trigger, Object triggerData) {
    ProcessInstance processInstance = getProcessInstance(processInstanceId);
    ((io.automatiko.engine.workflow.base.instance.ProcessInstance) processInstance).configureSLA();
    getProcessEventSupport().fireBeforeProcessStarted(processInstance, this);
    ((io.automatiko.engine.workflow.base.instance.ProcessInstance) processInstance).setReferenceFromRoot(null);
    ((io.automatiko.engine.workflow.base.instance.ProcessInstance) processInstance).start(trigger, triggerData);
    getProcessEventSupport().fireAfterProcessStarted(processInstance, this);
    return processInstance;
}
Also used : ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance)

Example 55 with ProcessInstance

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

the class LightWorkItemManager method failWorkItem.

@Override
public void failWorkItem(String id, Throwable error) {
    WorkItemImpl workItem = (WorkItemImpl) workItems.get(id);
    // work item may have been aborted
    if (workItem != null) {
        ProcessInstance processInstance = workItem.getProcessInstance();
        workItem.setState(FAILED);
        // process instance may have finished already
        if (processInstance != null) {
            workItem.setResult("Error", error);
            processInstance.signalEvent("workItemFailed", workItem);
        }
        workItems.remove(id);
    }
}
Also used : WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance)

Aggregations

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