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;
}
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);
}
}
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);
}
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());
}
}
}
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;
}
Aggregations