Search in sources :

Example 6 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class VariableUtil method resolveVariable.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String resolveVariable(String s, NodeInstance nodeInstance) {
    if (s == null) {
        return null;
    }
    Map<String, String> replacements = new HashMap<String, String>();
    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
    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(paramName) == null) {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((io.automatiko.engine.workflow.process.instance.NodeInstance) nodeInstance).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) nodeInstance.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    Object variableValue = evaluator.evaluate(paramName, new NodeInstanceResolverFactory((io.automatiko.engine.workflow.process.instance.NodeInstance) nodeInstance));
                    String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
                    replacements.put(replacementKey, variableValueString);
                } catch (Throwable t) {
                }
            }
        }
    }
    for (Map.Entry<String, String> replacement : replacements.entrySet()) {
        s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
    }
    return s;
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class InputJqAssignmentAction method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(WorkItem workItem, ProcessContext context) throws Exception {
    Object sdata = context.getProcessInstance().getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
    if (inputFilterExpression != null) {
        ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
        Map<String, Object> vars = new HashMap<>();
        vars.put("workflowdata", sdata);
        if (context.getVariable("$CONST") != null) {
            vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
        }
        sdata = evaluator.evaluate(inputFilterExpression, vars);
    }
    if (context.getNodeInstance() instanceof ContextableInstance) {
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ContextableInstance) context.getNodeInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
        variableScopeInstance.setVariable(JsonVariableScope.WORKFLOWDATA_KEY, sdata);
    }
}
Also used : ContextableInstance(io.automatiko.engine.workflow.base.instance.ContextableInstance) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 8 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class JavaScriptAction method execute.

public void execute(ProcessContext context) throws Exception {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    engine.put("kcontext", context);
    if (context.getProcessInstance() != null && context.getProcessInstance().getProcess() != null) {
        // insert process variables
        VariableScopeInstance variableScope = (VariableScopeInstance) ((WorkflowProcessInstance) context.getProcessInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
        Map<String, Object> variables = variableScope.getVariables();
        if (variables != null) {
            for (Entry<String, Object> variable : variables.entrySet()) {
                engine.put(variable.getKey(), variable.getValue());
            }
        }
    }
    engine.eval(expr);
}
Also used : VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 9 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class LightProcessRuntimeContext method setupParameters.

@Override
public void setupParameters(ProcessInstance processInstance, Map<String, Object> parameters, VariableInitializer variableInitializer) {
    Map<String, Object> variables = new HashMap<>();
    Process process = processInstance.getProcess();
    VariableScope variableScope = (VariableScope) ((ContextContainer) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
    // set input parameters
    if (parameters != null) {
        if (variableScope != null) {
            for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                variableScope.validateVariable(process.getName(), entry.getKey(), entry.getValue());
                variables.put(entry.getKey(), entry.getValue());
            }
        } else {
            throw new IllegalArgumentException("This process does not support parameters!");
        }
    }
    for (Variable var : variableScope.getVariables()) {
        if ((var.hasTag(Variable.AUTO_INITIALIZED_TAG) || var.getMetaData(Variable.DEFAULT_VALUE) != null) && variables.get(var.getName()) == null) {
            Object value = variableInitializer.initialize(process, var, variables);
            variableScope.validateVariable(process.getName(), var.getName(), value);
            variableScopeInstance.setVariable(var.getName(), value);
        }
        if (var.hasTag(Variable.INITIATOR_TAG) && variables.get(var.getName()) != null) {
            processInstance.setInitiator(variables.get(var.getName()).toString());
        }
    }
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) Process(io.automatiko.engine.api.definition.process.Process) HashMap(java.util.HashMap) Map(java.util.Map) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 10 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class AbstractProcessInstanceFactory method createProcessInstance.

public ProcessInstance createProcessInstance(Process process, CorrelationKey correlationKey, InternalProcessRuntime runtime, Map<String, Object> parameters, VariableInitializer variableInitializer) {
    ProcessInstance processInstance = createProcessInstance();
    processInstance.setProcessRuntime(runtime);
    processInstance.setProcess(process);
    if (correlationKey != null) {
        processInstance.getMetaData().put("CorrelationKey", correlationKey);
    }
    runtime.getProcessInstanceManager().addProcessInstance(processInstance, correlationKey);
    // set variable default values
    // TODO: should be part of processInstanceImpl?
    VariableScope variableScope = (VariableScope) ((ContextContainer) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
    // set input parameters
    if (parameters != null) {
        if (variableScope != null) {
            for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                variableScope.validateVariable(process.getName(), entry.getKey(), entry.getValue());
                variableScopeInstance.setVariable(entry.getKey(), entry.getValue());
            }
        } else {
            throw new IllegalArgumentException("This process does not support parameters!");
        }
    }
    for (Variable var : variableScope.getVariables()) {
        if ((var.hasTag(Variable.AUTO_INITIALIZED_TAG) || var.getMetaData(Variable.DEFAULT_VALUE) != null) && variableScopeInstance.getVariable(var.getName()) == null) {
            Object value = variableInitializer.initialize(process, var, variableScopeInstance.getVariables());
            variableScope.validateVariable(process.getName(), var.getName(), value);
            variableScopeInstance.setVariable(var.getName(), value);
        }
    }
    variableScopeInstance.enforceRequiredVariables();
    return processInstance;
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) Map(java.util.Map) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Aggregations

VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)44 HashMap (java.util.HashMap)26 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)20 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)18 Map (java.util.Map)16 Matcher (java.util.regex.Matcher)12 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)11 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)11 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)10 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)9 Transformation (io.automatiko.engine.workflow.process.core.node.Transformation)9 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)7 ArrayList (java.util.ArrayList)7 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)6 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)6 Serializable (java.io.Serializable)6 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)5 Process (io.automatiko.engine.api.definition.process.Process)4 DataType (io.automatiko.engine.api.workflow.datatype.DataType)4 Context (io.automatiko.engine.workflow.base.core.Context)4