use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class SubProcessNodeInstance method handleOutMappings.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void handleOutMappings(ProcessInstance processInstance) {
VariableScopeInstance subProcessVariableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
SubProcessNode subProcessNode = getSubProcessNode();
if (subProcessNode != null) {
for (Iterator<io.automatiko.engine.workflow.process.core.node.DataAssociation> iterator = subProcessNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
io.automatiko.engine.workflow.process.core.node.DataAssociation mapping = iterator.next();
if (mapping.getTransformation() != null) {
Transformation transformation = mapping.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Map<String, Object> dataSet = new HashMap<String, Object>();
if (getNodeInstanceContainer() instanceof CompositeContextNodeInstance) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) getNodeInstanceContainer()).getContextInstance(VariableScope.VARIABLE_SCOPE);
if (variableScopeInstance != null) {
dataSet.putAll(variableScopeInstance.getVariables());
}
}
dataSet.putAll(subProcessVariableScopeInstance.getVariables());
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
if (variableScopeInstance != null && parameterValue != null) {
variableScopeInstance.setVariable(this, mapping.getTarget(), parameterValue);
} else {
logger.warn("Could not find variable scope for variable {}", mapping.getTarget());
logger.warn("Continuing without setting variable.");
}
}
} else {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
if (variableScopeInstance != null) {
Object value = subProcessVariableScopeInstance.getVariable(mapping.getSources().get(0));
if (value == null) {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
value = evaluator.evaluate(mapping.getSources().get(0), new VariableScopeResolverFactory(subProcessVariableScopeInstance));
} catch (Throwable t) {
// do nothing
}
}
variableScopeInstance.setVariable(this, mapping.getTarget(), value);
} else {
String output = mapping.getSources().get(0);
String target = mapping.getTarget();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
if (matcher.find()) {
String paramName = matcher.group(1);
String expression = paramName + " = " + output;
VariableScopeResolverFactory resolver = new VariableScopeResolverFactory(subProcessVariableScopeInstance);
resolver.addExtraParameters(((VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE)).getVariables());
Serializable compiled = MVEL.compileExpression(expression);
MVEL.executeExpression(compiled, resolver);
} else {
logger.error("Could not find variable scope for variable {}", mapping.getTarget());
logger.error("when trying to complete SubProcess node {}", getSubProcessNode().getName());
logger.error("Continuing without setting variable.");
}
}
}
}
} else {
// handle dynamic sub processes without data output mapping
mapDynamicOutputData(subProcessVariableScopeInstance.getVariables());
}
}
use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class ForEachNodeInstance method evaluateCollectionExpression.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Collection<?> evaluateCollectionExpression(String collectionExpression) {
Object collection;
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, collectionExpression);
if (variableScopeInstance != null) {
collection = variableScopeInstance.getVariable(collectionExpression);
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
collection = evaluator.evaluate(collectionExpression, new NodeInstanceResolverFactory(this));
} catch (Throwable t) {
throw new IllegalArgumentException("Could not find collection " + collectionExpression);
}
}
if (collection == null) {
return Collections.emptyList();
}
if (collection instanceof Collection<?>) {
return (Collection<?>) collection;
}
if (collection.getClass().isArray()) {
List<Object> list = new ArrayList<>();
Collections.addAll(list, (Object[]) collection);
return list;
}
throw new IllegalArgumentException("Unexpected collection type: " + collection.getClass());
}
use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class WorkflowProcessInstanceImpl method resolveVariable.
@SuppressWarnings({ "unchecked", "rawtypes" })
private String resolveVariable(String s, VariableResolverFactory factory) {
Map<String, String> replacements = new HashMap<>();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
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) {
Object variableValue = getVariable(paramName);
if (variableValue != null) {
replacements.put(replacementKey, variableValue.toString());
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((io.automatiko.engine.workflow.process.core.WorkflowProcess) getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
variableValue = evaluator.evaluate(paramName, factory);
String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
replacements.put(replacementKey, variableValueString);
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", paramName);
}
}
}
}
for (Map.Entry<String, String> replacement : replacements.entrySet()) {
s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
}
return s;
}
use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class DefaultVariableInitializer method defaultValue.
protected Object defaultValue(Process process, String valueExpression, Variable definition, Map<String, Object> data) {
try {
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(valueExpression);
if (matcher.find()) {
String paramName = matcher.group(1);
String defaultValue = null;
if (paramName.contains(":")) {
String[] items = paramName.split(":");
paramName = items[0];
defaultValue = items[1];
}
ExpressionEvaluator<?> evaluator = (ExpressionEvaluator<?>) ((WorkflowProcess) process).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
Object result = evaluator.evaluate(paramName, data);
return result == null ? defaultValue : result;
} else {
return definition.getType().readValue(valueExpression);
}
} catch (Throwable e) {
return null;
}
}
use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.
the class OutputJqAssignmentAction method execute.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(WorkItem workItem, ProcessContext context) throws Exception {
Object sdata = context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
if (outputFilterExpression != 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(outputFilterExpression, vars);
}
ObjectNode workflowData = (ObjectNode) context.getProcessInstance().getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
ObjectMapper mapper = new ObjectMapper();
if (!workflowData.equals(sdata)) {
Object updated = mapper.readerForUpdating(workflowData).readValue((JsonNode) sdata);
context.getProcessInstance().setVariable(JsonVariableScope.WORKFLOWDATA_KEY, updated);
}
}
Aggregations