use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class RuleSetNodeInstance method resolveVariable.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object resolveVariable(Object s) {
if (s instanceof String) {
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher((String) s);
while (matcher.find()) {
String paramName = matcher.group(1);
String defaultValue = null;
if (paramName.contains(":")) {
String[] items = paramName.split(":");
paramName = items[0];
defaultValue = items[1];
}
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
if (variableScopeInstance != null) {
Object variableValue = variableScopeInstance.getVariable(paramName);
if (variableValue != null) {
return variableValue;
} else {
return defaultValue;
}
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
Object variableValue = evaluator.evaluate(paramName, new NodeInstanceResolverFactory(this));
if (variableValue != null) {
return variableValue;
} else {
return defaultValue;
}
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", paramName);
}
}
}
}
return s;
}
use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class ServerlessFunctions method expression.
public static Object expression(ProcessContext context, String expression, String inputFilter, String outputFilter, String scope) {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
Map<String, Object> vars = new HashMap<>();
Map<String, Object> variables = new HashMap<>();
vars.put("workflowdata", context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY));
if (context.getVariable("$CONST") != null) {
variables.put("CONST", context.getVariable("$CONST"));
vars.put("workflow_variables", variables);
}
if (inputFilter != null) {
Object filteredInput = evaluator.evaluate(inputFilter, vars);
vars.put("workflowdata", filteredInput);
}
Object content = evaluator.evaluate(expression, vars);
if (outputFilter != null) {
vars.put("workflowdata", content);
content = evaluator.evaluate(outputFilter, vars);
}
JsonNode outcome;
if (scope != null) {
vars.put("workflowdata", context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY));
variables.put("v", content);
vars.put("workflow_variables", variables);
outcome = (JsonNode) evaluator.evaluate(scope + "=$v", vars);
} else {
outcome = (JsonNode) context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
}
try {
Object updated = mapper.readerForUpdating(context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY)).readValue(outcome);
context.setVariable(JsonVariableScope.WORKFLOWDATA_KEY, updated);
return content;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class ServerlessFunctions method expressionAsString.
public static String expressionAsString(ProcessContext context, String expression) {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
Map<String, Object> vars = new HashMap<>();
vars.put("workflowdata", context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY));
if (context.getVariable("$CONST") != null) {
vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
}
Object content = evaluator.evaluate(expression, vars);
if (content == null) {
return null;
}
if (content instanceof TextNode) {
return ((TextNode) content).asText();
}
return content.toString();
}
use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class ServerlessFunctions method variableExpression.
public static Object variableExpression(ProcessContext context, String expression) {
if (expression.startsWith("${") || expression.startsWith("\"${")) {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
Map<String, Object> vars = new HashMap<>();
vars.put("workflowdata", context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY));
if (context.getVariable("$CONST") != null) {
vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
}
Object content = evaluator.evaluate(unwrapExpression(expression), vars);
return content;
} else {
return fromJson(expression);
}
}
use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class MvelNodeInstanceVariableExpression method evaluate.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <T> T evaluate(String expression, ProcessContext context, Class<T> clazz) {
Map<String, Object> replacements = new HashMap<>();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(expression);
while (matcher.find()) {
String paramName = matcher.group(1);
String replacementKey = paramName;
String defaultValue = null;
if (paramName.contains(":")) {
String[] items = paramName.split(":");
paramName = items[0];
defaultValue = items[1];
}
if (replacements.get(paramName) == null) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) context.getNodeInstance()).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) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
Object variableValue = evaluator.evaluate(paramName, resolverFactory((NodeInstance) context.getNodeInstance()));
replacements.put(replacementKey, variableValue == null ? defaultValue : variableValue);
} catch (Throwable t) {
replacements.put(replacementKey, defaultValue);
}
}
}
}
for (Map.Entry<String, Object> replacement : replacements.entrySet()) {
expression = expression.replace("#{" + replacement.getKey() + "}", replacement.getValue().toString());
}
if (expression == null) {
return null;
}
if (clazz.isInstance(expression)) {
return clazz.cast(expression);
}
if (clazz == String.class) {
return (T) expression.toString();
}
throw new IllegalStateException("Variable expression evaluated to non compatible type with " + clazz);
}
Aggregations