use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class SubProcessNode method getSourceParameters.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Map<String, Object> getSourceParameters(ProcessContext ctx, DataAssociation association) {
Map<String, Object> parameters = new HashMap<String, Object>();
for (String sourceParam : association.getSources()) {
Object parameterValue = null;
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, sourceParam);
if (variableScopeInstance != null) {
parameterValue = variableScopeInstance.getVariable(sourceParam);
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) ctx.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
parameterValue = evaluator.evaluate(sourceParam, new NodeInstanceResolverFactory(((NodeInstance) ctx.getNodeInstance())));
} catch (Throwable t) {
}
}
if (parameterValue != null) {
parameters.put(sourceParam, parameterValue);
}
}
return parameters;
}
use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class SubProcessNode method internalSubProcess.
public void internalSubProcess(Collection<io.automatiko.engine.api.workflow.Process> availableProcesses) {
io.automatiko.engine.api.workflow.Process process = (io.automatiko.engine.api.workflow.Process<Map<String, Object>>) availableProcesses.stream().filter(p -> p.id().equals(processId)).findFirst().orElse(null);
if (process == null) {
return;
}
this.subProcessFactory = new SubProcessFactory<Object>() {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Map<String, Object> bind(ProcessContext ctx) {
Map<String, Object> parameters = new HashMap<String, Object>();
for (Iterator<DataAssociation> iterator = getInAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation mapping = iterator.next();
Object parameterValue = null;
if (mapping.getTransformation() != null) {
Transformation transformation = mapping.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(ctx, mapping));
}
} else if (mapping.getAssignments() == null || mapping.getAssignments().isEmpty()) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getSources().get(0));
if (variableScopeInstance != null) {
parameterValue = variableScopeInstance.getVariable(mapping.getSources().get(0));
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) ctx.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
parameterValue = evaluator.evaluate(mapping.getSources().get(0), new NodeInstanceResolverFactory((NodeInstance) ctx.getNodeInstance()));
} catch (Throwable t) {
parameterValue = VariableUtil.resolveVariable(mapping.getSources().get(0), ctx.getNodeInstance());
if (parameterValue != null) {
parameters.put(mapping.getTarget(), parameterValue);
}
}
}
} else {
mapping.getAssignments().stream().forEach(a -> handleAssignment(ctx, parameters, a));
}
if (parameterValue != null) {
parameters.put(mapping.getTarget(), parameterValue);
}
}
return parameters;
}
@Override
public ProcessInstance createInstance(Object model) {
Model data = (Model) process.createModel();
data.fromMap((Map<String, Object>) model);
return process.createInstance(data);
}
@Override
public void unbind(ProcessContext ctx, Object model) {
Map<String, Object> result = ((Model) model).toMap();
for (Iterator<DataAssociation> iterator = getOutAssociations().iterator(); iterator.hasNext(); ) {
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 (ctx.getNodeInstance().getNodeInstanceContainer() instanceof CompositeContextNodeInstance) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) ctx.getNodeInstance().getNodeInstanceContainer()).getContextInstance(VariableScope.VARIABLE_SCOPE);
if (variableScopeInstance != null) {
dataSet.putAll(variableScopeInstance.getVariables());
}
}
dataSet.putAll(result);
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
if (variableScopeInstance != null && parameterValue != null) {
variableScopeInstance.setVariable(ctx.getNodeInstance(), mapping.getTarget(), parameterValue);
}
}
} else {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
if (variableScopeInstance != null) {
Object value = result.get(mapping.getSources().get(0));
if (value == null) {
try {
value = MVEL.eval(mapping.getSources().get(0), result);
} catch (Throwable t) {
// do nothing
}
}
variableScopeInstance.setVariable(ctx.getNodeInstance(), 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;
Serializable compiled = MVEL.compileExpression(expression);
MVEL.executeExpression(compiled, result);
}
}
}
}
}
@SuppressWarnings("unchecked")
@Override
public void abortInstance(String instanceId) {
process.instances().findById(instanceId).ifPresent(pi -> {
try {
((ProcessInstance<?>) pi).abort();
} catch (IllegalArgumentException e) {
// ignore it as this might be thrown in case of canceling already aborted instance
}
});
}
private void handleAssignment(ProcessContext ctx, Map<String, Object> output, Assignment assignment) {
AssignmentAction action = (AssignmentAction) assignment.getMetaData("Action");
try {
WorkItemImpl workItem = new WorkItemImpl();
action.execute(workItem, ctx);
output.putAll(workItem.getParameters());
} catch (WorkItemExecutionError e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("unable to execute Assignment", e);
}
}
};
}
use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method disconnect.
protected void disconnect(ProcessInstance<ProcessInstanceEntity> instance) {
((AbstractProcessInstance<?>) instance).internalRemoveProcessInstance(() -> {
try {
ProcessInstanceEntity entity = (ProcessInstanceEntity) JpaOperations.INSTANCE.findById(type, resolveId(instance.id(), instance));
byte[] reloaded = codec.decode(entity.content);
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(reloaded, process);
entity.toMap().forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
return wpi;
} catch (RuntimeException e) {
LOGGER.error("Unexpected exception thrown when reloading process instance {}", instance.id(), e);
return null;
}
});
}
use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class MongodbProcessInstances method disconnect.
protected void disconnect(ProcessInstance instance) {
((AbstractProcessInstance<?>) instance).internalRemoveProcessInstance(() -> {
try {
String resolvedId = instance.id();
Document returnedItem = collection().find(eq(INSTANCE_ID_FIELD, resolvedId)).projection(Projections.fields(Projections.include(INSTANCE_ID_FIELD, CONTENT_FIELD, VERSION_FIELD, VARIABLES_FIELD))).first();
if (returnedItem != null) {
byte[] reloaded = returnedItem.get(CONTENT_FIELD, Binary.class).getData();
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(reloaded), process);
String variablesJson = returnedItem.get(VARIABLES_FIELD, Document.class).toJson();
Model model = process.createModel();
Map<String, Object> loaded = marshallingStrategy.mapper().readValue(variablesJson, model.getClass()).toMap();
model.fromMap(loaded);
loaded.forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
return wpi;
} else {
return null;
}
} catch (IOException e) {
LOGGER.error("Unexpected exception thrown when reloading process instance {}", instance.id(), e);
return null;
}
});
}
use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class MongodbProcessInstances method unmarshallInstance.
protected ProcessInstance unmarshallInstance(ProcessInstanceReadMode mode, Document entity) {
try {
ProcessInstance pi;
if (mode == MUTABLE) {
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(entity.get(CONTENT_FIELD, Binary.class).getData()), process);
String variablesJson = entity.get(VARIABLES_FIELD, Document.class).toJson();
Model model = process.createModel();
Map<String, Object> loaded = marshallingStrategy.mapper().readValue(variablesJson, model.getClass()).toMap();
model.fromMap(loaded);
loaded.forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
pi = ((AbstractProcess) process).createInstance(wpi, model, entity.getLong(VERSION_FIELD));
} else {
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(entity.get(CONTENT_FIELD, Binary.class).getData()), process);
String variablesJson = entity.get(VARIABLES_FIELD, Document.class).toJson();
Model model = process.createModel();
Map<String, Object> loaded = marshallingStrategy.mapper().readValue(variablesJson, model.getClass()).toMap();
model.fromMap(loaded);
loaded.forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
pi = ((AbstractProcess) process).createReadOnlyInstance(wpi, model);
}
return pi;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Aggregations