use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl in project automatiko-engine by automatiko-io.
the class DynamicUtils method internalAddDynamicWorkItem.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void internalAddDynamicWorkItem(final WorkflowProcessInstance processInstance, final DynamicNodeInstance dynamicContext, InternalProcessRuntime runtime, String workItemName, Map<String, Object> parameters) {
final WorkItemImpl workItem = new WorkItemImpl();
workItem.setState(WorkItem.ACTIVE);
workItem.setProcessInstanceId(processInstance.getId());
workItem.setParentProcessInstanceId(processInstance.getParentProcessInstanceId());
workItem.setName(workItemName);
workItem.setParameters(parameters);
for (Map.Entry<String, Object> entry : workItem.getParameters().entrySet()) {
if (entry.getValue() instanceof String) {
String s = (String) entry.getValue();
Object variableValue = null;
String defaultValue = null;
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
while (matcher.find()) {
String paramName = matcher.group(1);
if (paramName.contains(":")) {
String[] items = paramName.split(":");
paramName = items[0];
defaultValue = items[1];
}
variableValue = processInstance.getVariable(paramName);
if (variableValue == null) {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) processInstance.getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
variableValue = evaluator.evaluate(paramName, new ProcessInstanceResolverFactory(processInstance));
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", paramName);
logger.error("when trying to replace variable in string for Dynamic Work Item {}", workItemName);
logger.error("Continuing without setting parameter.");
}
}
}
if (variableValue != null) {
workItem.setParameter(entry.getKey(), variableValue == null ? defaultValue : variableValue);
}
}
}
final WorkItemNodeInstance workItemNodeInstance = new WorkItemNodeInstance();
workItemNodeInstance.setProcessInstance(processInstance);
workItemNodeInstance.internalSetWorkItem(workItem);
workItemNodeInstance.setMetaData("NodeType", workItemName);
workItem.setNodeInstanceId(workItemNodeInstance.getId());
workItemNodeInstance.setNodeInstanceContainer(dynamicContext == null ? processInstance : dynamicContext);
workItemNodeInstance.addEventListeners();
executeWorkItem(runtime, workItem, workItemNodeInstance);
}
use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl 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.impl.workitem.WorkItemImpl in project automatiko-engine by automatiko-io.
the class ProtobufProcessMarshaller method readWorkItem.
@Override
public WorkItemImpl readWorkItem(MarshallerReaderContext context) {
try {
ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
Header _header = PersisterHelper.readFromStreamWithHeaderPreloaded(context, registry);
AutomatikoMessages.WorkItem _workItem = AutomatikoMessages.WorkItem.parseFrom(_header.getPayload(), registry);
return (WorkItemImpl) readWorkItem(context, _workItem, persistWorkItemVars);
} catch (IOException e) {
throw new IllegalArgumentException("IOException while fetching work item instance : " + e.getMessage(), e);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("ClassNotFoundException while fetching work item instance : " + e.getMessage(), e);
}
}
use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl in project automatiko-engine by automatiko-io.
the class WorkItemNodeInstance method createWorkItem.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected WorkItem createWorkItem(WorkItemNode workItemNode) {
Work work = workItemNode.getWork();
if (workItem == null) {
workItem = newWorkItem();
((WorkItemImpl) workItem).setName(work.getName());
((WorkItemImpl) workItem).setProcessInstanceId(getProcessInstance().getId());
((WorkItemImpl) workItem).setParentProcessInstanceId(getProcessInstance().getParentProcessInstanceId());
((WorkItemImpl) workItem).setParameters(new HashMap<>(work.getParameters()));
workItem.setStartDate(new Date());
}
// if there are any dynamic parameters add them
if (dynamicParameters != null) {
workItem.getParameters().putAll(dynamicParameters);
}
for (DataAssociation association : workItemNode.getInAssociations()) {
if (association.getTransformation() != null) {
Transformation transformation = association.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(association));
if (parameterValue != null) {
((WorkItemImpl) workItem).setParameter(association.getTarget(), parameterValue);
}
}
} else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
Object parameterValue = null;
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getSources().get(0));
if (variableScopeInstance != null) {
parameterValue = variableScopeInstance.getVariable(association.getSources().get(0));
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
parameterValue = evaluator.evaluate(association.getSources().get(0), new NodeInstanceResolverFactory(this));
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", association.getSources().get(0));
logger.error("when trying to execute Work Item {}", work.getName());
logger.error("Continuing without setting parameter.");
}
}
if (parameterValue != null) {
((WorkItemImpl) workItem).setParameter(association.getTarget(), parameterValue);
}
} else {
association.getAssignments().stream().forEach(this::handleAssignment);
}
}
for (Map.Entry<String, Object> entry : workItem.getParameters().entrySet()) {
if (entry.getValue() instanceof String) {
String s = (String) entry.getValue();
Map<String, Object> replacements = new HashMap<>();
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(replacementKey) == null) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, paramName);
if (variableScopeInstance != null) {
Object variableValue = variableScopeInstance.getVariable(paramName);
Object variableValueString = variableValue == null ? defaultValue : variableValue;
replacements.put(replacementKey, variableValueString);
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
Object variableValue = evaluator.evaluate(paramName, new NodeInstanceResolverFactory(this));
Object variableValueString = variableValue == null ? defaultValue : variableValue;
replacements.put(replacementKey, variableValueString);
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", paramName);
logger.error("when trying to replace variable in string for Work Item {}", work.getName());
logger.error("Continuing without setting parameter.");
}
}
}
}
if (replacements.size() > 1) {
for (Map.Entry<String, Object> replacement : replacements.entrySet()) {
s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue().toString());
}
((WorkItemImpl) workItem).setParameter(entry.getKey(), s);
} else if (replacements.size() == 1) {
((WorkItemImpl) workItem).setParameter(entry.getKey(), replacements.values().iterator().next());
}
}
}
return workItem;
}
use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl in project automatiko-engine by automatiko-io.
the class EventNodeInstance method handleAssignment.
private void handleAssignment(Assignment assignment, Object result) {
AssignmentAction action = (AssignmentAction) assignment.getMetaData("Action");
if (action == null) {
return;
}
try {
ProcessContext context = new ProcessContext(getProcessInstance().getProcessRuntime());
context.setNodeInstance(this);
WorkItemImpl workItem = new WorkItemImpl();
workItem.setResult("workflowdata", result);
workItem.setResult("event", result);
action.execute(workItem, context);
} catch (WorkItemExecutionError e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("unable to execute Assignment", e);
}
}
Aggregations