use of io.automatiko.engine.workflow.process.core.node.DataAssociation in project automatiko-engine by automatiko-io.
the class MappableNodeFactory method inMappingWithJqAssignment.
default MappableNodeFactory inMappingWithJqAssignment(String inputExpression, String... params) {
Assignment assignment = new Assignment("jq", null, null);
assignment.setMetaData("Action", new TaskInputJqAssignmentAction(inputExpression, params));
getMappableNode().addInAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(assignment), null));
return this;
}
use of io.automatiko.engine.workflow.process.core.node.DataAssociation in project automatiko-engine by automatiko-io.
the class ProcessHandler method postProcessNodes.
private void postProcessNodes(ExecutableProcess process, NodeContainer container) {
List<String> eventSubProcessHandlers = new ArrayList<String>();
for (Node node : container.getNodes()) {
if (node instanceof StartNode) {
List<DataAssociation> associations = ((StartNode) node).getOutAssociations();
if (associations != null) {
for (DataAssociation da : associations) {
VariableScope scope = (VariableScope) process.getDefaultContext(VariableScope.VARIABLE_SCOPE);
Variable variable = scope.findVariable(da.getTarget());
if (variable != null) {
da.setTarget(variable.getName());
}
}
}
} else if (node instanceof StateNode) {
StateNode stateNode = (StateNode) node;
String condition = (String) stateNode.getMetaData("Condition");
stateNode.setCondition(context -> {
return (boolean) MVEL.executeExpression(condition, context.getProcessInstance().getVariables());
});
} else if (node instanceof NodeContainer) {
// prepare event sub process
if (node instanceof EventSubProcessNode) {
EventSubProcessNode eventSubProcessNode = (EventSubProcessNode) node;
Node[] nodes = eventSubProcessNode.getNodes();
for (Node subNode : nodes) {
// avoids cyclomatic complexity
if (subNode == null || !(subNode instanceof StartNode)) {
continue;
}
List<Trigger> triggers = ((StartNode) subNode).getTriggers();
if (triggers == null) {
continue;
}
for (Trigger trigger : triggers) {
if (trigger instanceof EventTrigger) {
final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
for (EventFilter filter : filters) {
if (filter instanceof EventTypeFilter) {
eventSubProcessNode.addEvent((EventTypeFilter) filter);
String type = ((EventTypeFilter) filter).getType();
if (type.startsWith("Error-") || type.startsWith("Escalation")) {
String faultCode = (String) subNode.getMetaData().get("FaultCode");
String replaceRegExp = "Error-|Escalation-";
final String signalType = type;
ExceptionScope exceptionScope = (ExceptionScope) ((ContextContainer) eventSubProcessNode.getParentContainer()).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
if (exceptionScope == null) {
exceptionScope = new ExceptionScope();
((ContextContainer) eventSubProcessNode.getParentContainer()).addContext(exceptionScope);
((ContextContainer) eventSubProcessNode.getParentContainer()).setDefaultContext(exceptionScope);
}
String faultVariable = null;
if (trigger.getInAssociations() != null && !trigger.getInAssociations().isEmpty()) {
faultVariable = findVariable(trigger.getInAssociations().get(0).getSources().get(0), process.getVariableScope());
}
ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
ConsequenceAction action = new ConsequenceAction("java", "");
action.setMetaData("Action", new SignalProcessInstanceAction(signalType, faultVariable, SignalProcessInstanceAction.PROCESS_INSTANCE_SCOPE));
exceptionHandler.setAction(action);
exceptionHandler.setFaultVariable(faultVariable);
exceptionHandler.setRetryAfter((Integer) subNode.getMetaData().get("ErrorRetry"));
exceptionHandler.setRetryIncrement((Integer) subNode.getMetaData().get("ErrorRetryIncrement"));
if (subNode.getMetaData().get("ErrorRetryIncrementMultiplier") != null) {
exceptionHandler.setRetryIncrementMultiplier(((Number) subNode.getMetaData().get("ErrorRetryIncrementMultiplier")).floatValue());
}
exceptionHandler.setRetryLimit((Integer) subNode.getMetaData().get("ErrorRetryLimit"));
if (faultCode != null) {
String trimmedType = type.replaceFirst(replaceRegExp, "");
for (String error : trimmedType.split(",")) {
exceptionScope.setExceptionHandler(error, exceptionHandler);
eventSubProcessHandlers.add(error);
}
} else {
exceptionScope.setExceptionHandler(faultCode, exceptionHandler);
}
} else if (type.equals("Compensation")) {
// 1. Find the parent sub-process to this event sub-process
NodeContainer parentSubProcess;
NodeContainer subProcess = eventSubProcessNode.getParentContainer();
Object isForCompensationObj = eventSubProcessNode.getMetaData("isForCompensation");
if (isForCompensationObj == null) {
eventSubProcessNode.setMetaData("isForCompensation", true);
logger.warn("Overriding empty or false value of \"isForCompensation\" attribute on Event Sub-Process [" + eventSubProcessNode.getMetaData("UniqueId") + "] and setting it to true.");
}
if (subProcess instanceof ExecutableProcess) {
// (instance)?!?
throw new IllegalArgumentException("Compensation Event Sub-Processes at the process level are not supported.");
}
parentSubProcess = ((Node) subProcess).getParentContainer();
// 2. The event filter (never fires, purely for dumping purposes) has
// already been added
// 3. Add compensation scope
String compensationHandlerId = (String) ((CompositeNode) subProcess).getMetaData("UniqueId");
addCompensationScope(process, eventSubProcessNode, parentSubProcess, compensationHandlerId);
}
}
}
} else if (trigger instanceof ConstraintTrigger) {
ConstraintTrigger constraintTrigger = (ConstraintTrigger) trigger;
if (constraintTrigger.getConstraint() != null) {
String processId = ((ExecutableProcess) container).getId();
String type = "RuleFlowStateEventSubProcess-Event-" + processId + "-" + eventSubProcessNode.getUniqueId();
EventTypeFilter eventTypeFilter = new EventTypeFilter();
eventTypeFilter.setType(type);
eventSubProcessNode.addEvent(eventTypeFilter);
eventSubProcessNode.addEvent("variableChanged");
((StartNode) subNode).setCondition(context -> {
return (boolean) MVEL.executeExpression(constraintTrigger.getConstraint(), context.getProcessInstance().getVariables());
});
}
}
}
}
// for( Node subNode : nodes)
}
postProcessNodes(process, (NodeContainer) node);
} else if (node instanceof EndNode) {
handleIntermediateOrEndThrowCompensationEvent((EndNode) node);
} else if (node instanceof ActionNode) {
handleIntermediateOrEndThrowCompensationEvent((ActionNode) node);
} else if (node instanceof EventNode) {
final EventNode eventNode = (EventNode) node;
if (!(eventNode instanceof BoundaryEventNode) && eventNode.getDefaultIncomingConnections().size() == 0) {
throw new IllegalArgumentException("Event node '" + node.getName() + "' [" + node.getId() + "] has no incoming connection");
}
}
}
// handler
for (Node node : container.getNodes()) {
if (node instanceof FaultNode) {
FaultNode faultNode = (FaultNode) node;
if (eventSubProcessHandlers.contains(faultNode.getFaultName())) {
faultNode.setTerminateParent(false);
}
}
}
}
use of io.automatiko.engine.workflow.process.core.node.DataAssociation in project automatiko-engine by automatiko-io.
the class ScriptTaskHandler method readDataOutputAssociation.
protected void readDataOutputAssociation(org.w3c.dom.Node xmlNode, ActionNode actionNode, Map<String, String> dataOutputs) {
// sourceRef
org.w3c.dom.Node subNode = xmlNode.getFirstChild();
List<String> sources = new ArrayList<>();
sources.add(subNode.getTextContent());
subNode = subNode.getNextSibling();
while ("sourceRef".equals(subNode.getNodeName())) {
sources.add(subNode.getTextContent());
subNode = subNode.getNextSibling();
}
// targetRef
String target = subNode.getTextContent();
// transformation
Transformation transformation = null;
subNode = subNode.getNextSibling();
if (subNode != null && "transformation".equals(subNode.getNodeName())) {
String lang = subNode.getAttributes().getNamedItem("language").getNodeValue();
String expression = subNode.getTextContent();
DataTransformer transformer = transformerRegistry.find(lang);
if (transformer == null) {
throw new IllegalArgumentException("No transformer registered for language " + lang);
}
transformation = new Transformation(lang, expression);
subNode = subNode.getNextSibling();
}
// assignments
List<Assignment> assignments = new LinkedList<Assignment>();
while (subNode != null) {
String expressionLang = ((Element) subNode).getAttribute("expressionLanguage");
if (expressionLang == null || expressionLang.trim().isEmpty()) {
expressionLang = "XPath";
}
org.w3c.dom.Node ssubNode = subNode.getFirstChild();
String from = ssubNode.getTextContent();
String to = ssubNode.getNextSibling().getTextContent();
assignments.add(new Assignment(expressionLang, from, to));
subNode = subNode.getNextSibling();
}
actionNode.addOutAssociation(new DataAssociation(sources.stream().map(source -> dataOutputs.get(source)).collect(Collectors.toList()), target, assignments, transformation));
}
use of io.automatiko.engine.workflow.process.core.node.DataAssociation 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.process.core.node.DataAssociation in project automatiko-engine by automatiko-io.
the class WorkItemNodeInstance method triggerCompleted.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void triggerCompleted(WorkItem workItem) {
this.workItem = workItem;
WorkItemNode workItemNode = getWorkItemNode();
if (workItemNode != null && workItem.getState() == WorkItem.ABORTED) {
cancel();
continueToNextNode(io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE, workItemNode);
return;
} else if (workItemNode != null && workItem.getState() == WorkItem.COMPLETED) {
validateWorkItemResultVariable(getProcessInstance().getProcessName(), workItemNode.getOutAssociations(), workItem);
for (Iterator<DataAssociation> iterator = getWorkItemNode().getOutAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation association = iterator.next();
if (association.getTransformation() != null) {
Transformation transformation = association.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(workItem.getParameters());
dataSet.putAll(workItem.getResults());
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null && parameterValue != null) {
variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), parameterValue);
variableScopeInstance.setVariable(this, association.getTarget(), parameterValue);
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("when trying to complete Work Item {}", workItem.getName());
logger.warn("Continuing without setting variable.");
}
if (parameterValue != null) {
((WorkItemImpl) workItem).setParameter(association.getTarget(), parameterValue);
}
}
} else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null) {
Object value = workItem.getResult(association.getSources().get(0));
if (value == null) {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
value = evaluator.evaluate(association.getSources().get(0), new WorkItemResolverFactory(workItem));
} catch (Throwable t) {
// do nothing
}
}
Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
DataType dataType = varDef.getType();
// exclude java.lang.Object as it is considered unknown type
if (!dataType.getStringType().endsWith("java.lang.Object") && !dataType.getStringType().endsWith("Object") && value instanceof String) {
value = dataType.readValue((String) value);
} else {
variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), value);
}
variableScopeInstance.setVariable(this, association.getTarget(), value);
} else {
String output = association.getSources().get(0);
String target = association.getTarget();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
if (matcher.find()) {
String paramName = matcher.group(1);
String expression = VariableUtil.transformDotNotation(paramName, output);
NodeInstanceResolverFactory resolver = new NodeInstanceResolverFactory(this);
resolver.addExtraParameters(workItem.getResults());
Serializable compiled = MVEL.compileExpression(expression);
MVEL.executeExpression(compiled, resolver);
String varName = VariableUtil.nameFromDotNotation(paramName);
variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, varName);
variableScopeInstance.setVariable(this, varName, variableScopeInstance.getVariable(varName));
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("when trying to complete Work Item {}", workItem.getName());
logger.warn("Continuing without setting variable.");
}
}
} else {
try {
association.getAssignments().forEach(this::handleAssignment);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
// handle dynamic nodes
if (getNode() == null) {
setMetaData("NodeType", workItem.getName());
mapDynamicOutputData(workItem.getResults());
}
triggerCompleted();
}
Aggregations