Search in sources :

Example 51 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class XmlBPMNProcessDumper method visitVariables.

public static void visitVariables(List<Variable> variables, StringBuilder xmlDump) {
    if (!variables.isEmpty()) {
        xmlDump.append("    <!-- process variables -->" + EOL);
        for (Variable variable : variables) {
            if (variable.getMetaData("DataObject") == null) {
                xmlDump.append("    <property id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(variable.getName()) + "\" ");
                if (variable.getType() != null) {
                    xmlDump.append("itemSubjectRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute((String) variable.getMetaData("ItemSubjectRef")) + "\"");
                }
                // TODO: value?
                Map<String, Object> metaData = getMetaData(variable.getMetaData());
                if (metaData.isEmpty()) {
                    xmlDump.append("/>" + EOL);
                } else {
                    xmlDump.append(">" + EOL + "      <extensionElements>" + EOL);
                    writeMetaData(metaData, xmlDump);
                    xmlDump.append("      </extensionElements>" + EOL + "    </property>" + EOL);
                }
            }
        }
        for (Variable variable : variables) {
            if (variable.getMetaData("DataObject") != null) {
                xmlDump.append("    <dataObject id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(variable.getName()) + "\" ");
                if (variable.getType() != null) {
                    xmlDump.append("itemSubjectRef=\"_" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(variable.getName()) + "\"");
                }
                // TODO: value?
                Map<String, Object> metaData = getMetaData(variable.getMetaData());
                if (metaData.isEmpty()) {
                    xmlDump.append("/>" + EOL);
                } else {
                    xmlDump.append(">" + EOL + "      <extensionElements>" + EOL);
                    writeMetaData(metaData, xmlDump);
                    xmlDump.append("      </extensionElements>" + EOL + "    </property>" + EOL);
                }
            }
        }
        xmlDump.append(EOL);
    }
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable)

Example 52 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable 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 53 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class VariableScopeInstance method setContextInstanceContainer.

public void setContextInstanceContainer(ContextInstanceContainer contextInstanceContainer) {
    super.setContextInstanceContainer(contextInstanceContainer);
    if (getVariableScope() != null) {
        for (Variable variable : getVariableScope().getVariables()) {
            if (variable.getValue() != null) {
                setVariable(variable.getName(), variable.getValue());
            }
        }
    }
    if (contextInstanceContainer instanceof CompositeContextNodeInstance) {
        this.variableIdPrefix = ((Node) ((CompositeContextNodeInstance) contextInstanceContainer).getNode()).getUniqueId();
        this.variableInstanceIdPrefix = ((CompositeContextNodeInstance) contextInstanceContainer).getUniqueId();
    }
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) CompositeContextNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance)

Example 54 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class VariableScopeInstance method getVariable.

@SuppressWarnings("unchecked")
public Object getVariable(String name) {
    String defaultValue = null;
    if (name.contains(":")) {
        String[] items = name.split(":");
        name = items[0];
        defaultValue = items[1];
    }
    if (name.contains(VariableScope.VERSION_SEPARATOR) && !name.startsWith(VariableScope.VERSION_SEPARATOR)) {
        String[] items = name.split("\\" + VariableScope.VERSION_SEPARATOR);
        Variable var = getVariableScope().findVariable(items[0]);
        if (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 = (List<Object>) versions.getOrDefault(items[0], Collections.emptyList());
            if (items.length == 1) {
                return varVersions;
            }
            try {
                int version = Integer.parseInt(items[1]);
                if (version < 0) {
                    // negative position means last item
                    return varVersions.get(varVersions.size() - 1);
                }
                // otherwise return version with given number
                return varVersions.get(version);
            } catch (IndexOutOfBoundsException e) {
                // in case position/version points at not existing version return null
                return null;
            }
        }
    }
    Object value = internalGetVariable(name);
    if (value != null) {
        return value;
    }
    // support for processInstanceId and parentProcessInstanceId
    if ("processInstanceId".equals(name) && getProcessInstance() != null) {
        return getProcessInstance().getId();
    } else if ("parentProcessInstanceId".equals(name) && getProcessInstance() != null) {
        return getProcessInstance().getParentProcessInstanceId();
    } else if (("correlationKey".equals(name) || "businessKey".equals(name)) && getProcessInstance() != null) {
        return getProcessInstance().getCorrelationKey();
    } else if ("rootProcessInstanceId".equals(name) && getProcessInstance() != null) {
        return getProcessInstance().getRootProcessInstanceId();
    } else if ("rootProcessId".equals(name) && getProcessInstance() != null) {
        return getProcessInstance().getRootProcessId();
    } else if ("processId".equals(name) && getProcessInstance() != null) {
        return getProcessInstance().getProcessId();
    } else if ("processName".equals(name) && getProcessInstance() != null) {
        return getProcessInstance().getProcessName();
    } else if ("processInstanceName".equals(name) && getProcessInstance() != null) {
        return ((ProcessInstanceImpl) getProcessInstance()).getDescription();
    }
    return systemOrEnvironmentVariable(name, defaultValue);
}
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) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 55 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class CompositeContextNodeFactory method variable.

public CompositeContextNodeFactory variable(String name, DataType type, Object value, String metaDataName, Object metaDataValue) {
    Variable variable = new Variable();
    variable.setName(name);
    variable.setType(type);
    variable.setValue(value);
    if (metaDataName != null && metaDataValue != null) {
        variable.setMetaData(metaDataName, metaDataValue);
    }
    VariableScope variableScope = (VariableScope) getCompositeNode().getDefaultContext(VariableScope.VARIABLE_SCOPE);
    if (variableScope == null) {
        variableScope = new VariableScope();
        getCompositeNode().addContext(variableScope);
        getCompositeNode().setDefaultContext(variableScope);
    }
    variableScope.getVariables().add(variable);
    return this;
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) JsonVariableScope(io.automatiko.engine.workflow.base.core.context.variable.JsonVariableScope) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Aggregations

Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)57 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)27 Map (java.util.Map)22 HashMap (java.util.HashMap)18 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)16 ObjectDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)16 ArrayList (java.util.ArrayList)14 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 Matcher (java.util.regex.Matcher)12 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)11 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)10 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)9 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)9 ActionNode (io.automatiko.engine.workflow.process.core.node.ActionNode)9 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)9 List (java.util.List)9 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)8 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)8 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)8 ExecutableProcess (io.automatiko.engine.workflow.process.executable.core.ExecutableProcess)8