use of io.automatiko.engine.workflow.process.core.node.WorkItemNode in project automatiko-engine by automatiko-io.
the class ServerlessWorkflowParser method buildActionsForState.
protected void buildActionsForState(Workflow workflow, List<Action> actions, NodeContainer embeddedSubProcess, ServerlessWorkflowFactory factory, AtomicLong ids, BiConsumer<Node, Node> firstLastNodeConsumer, BiConsumer<Node, Node> actionConsumer, boolean isParallel) {
Node firstNode = null;
Node lastNode = null;
Node prevNode = null;
for (Action action : actions) {
if (action.getFunctionRef() != null) {
// handle function based action
Optional<FunctionDefinition> functionDefinition = workflow.getFunctions().getFunctionDefs().stream().filter(functionDef -> functionDef.getName().equals(action.getFunctionRef().getRefName())).distinct().findFirst();
if (functionDefinition.get().getType() == FunctionDefinition.Type.EXPRESSION) {
ActionNode actionNode = factory.expressionActionStateNode(ids.getAndIncrement(), action.getName(), embeddedSubProcess, functionDefinition.get().getOperation(), action);
if (firstNode == null) {
firstNode = actionNode;
}
lastNode = actionNode;
} else if (functionDefinition.get().getType() == null || functionDefinition.get().getType() == FunctionDefinition.Type.REST) {
WorkItemNode serviceNode = factory.serviceNode(ids.getAndIncrement(), action, functionDefinition.get(), embeddedSubProcess);
if (firstNode == null) {
firstNode = serviceNode;
}
lastNode = serviceNode;
if (workflow.getTimeouts() != null && workflow.getTimeouts().getActionExecTimeout() != null) {
serviceNode.setMetaData("timeout", String.valueOf(DateTimeUtils.parseDuration(workflow.getTimeouts().getActionExecTimeout())));
}
if (action.getRetryableErrors() != null && !action.getRetryableErrors().isEmpty()) {
List<ErrorDefinition> defs = new ArrayList<>();
for (String errorRef : action.getRetryableErrors()) {
ErrorDefinition errorDef = workflow.getErrors().getErrorDefs().stream().filter(error -> error.getName().equals(errorRef)).findFirst().orElseThrow(() -> new IllegalStateException("Missing error definition for " + errorRef));
defs.add(errorDef);
}
RetryDefinition retry = null;
BoundaryEventNode errorNode = factory.errorBoundaryEventNode(ids.getAndIncrement(), defs, retry, embeddedSubProcess, serviceNode, workflow);
EndNode onErrorEnd = factory.endNode(ids.getAndIncrement(), action.getName() + "onErrorEnd", false, embeddedSubProcess);
factory.connect(errorNode.getId(), onErrorEnd.getId(), "connect_" + errorNode.getId() + "_" + onErrorEnd.getId(), embeddedSubProcess, false);
}
} else {
throw new UnsupportedOperationException(functionDefinition.get().getType() + " is not yet supported");
}
} else if (action.getSubFlowRef() != null) {
// handler sub workflow action definition
String workflowId = Objects.requireNonNull(action.getSubFlowRef().getWorkflowId(), "Workflow id for subworkflow is mandatory");
boolean independent = false;
if (action.getSubFlowRef().getOnParentComplete() != null && action.getSubFlowRef().getOnParentComplete().equals(OnParentComplete.CONTINUE)) {
independent = true;
}
boolean waitForCompletion = true;
if (action.getSubFlowRef().getInvoke().equals(Invoke.ASYNC)) {
waitForCompletion = false;
}
SubProcessNode callactivity = factory.callActivity(ids.getAndIncrement(), action.getName(), workflowId, waitForCompletion, embeddedSubProcess);
callactivity.setIndependent(independent);
callactivity.setProcessVersion(action.getSubFlowRef().getVersion());
if (firstNode == null) {
firstNode = callactivity;
}
lastNode = callactivity;
}
if (action.getSleep() != null && action.getSleep().getBefore() != null) {
TimerNode sleep = factory.timerNode(ids.getAndIncrement(), "sleep-before-" + action.getName(), action.getSleep().getBefore(), embeddedSubProcess);
factory.connect(sleep.getId(), firstNode.getId(), "connection_" + sleep.getId() + "_" + firstNode.getId(), embeddedSubProcess, false);
firstNode = sleep;
}
if (action.getSleep() != null && action.getSleep().getAfter() != null) {
TimerNode sleep = factory.timerNode(ids.getAndIncrement(), "sleep-after-" + action.getName(), action.getSleep().getAfter(), embeddedSubProcess);
factory.connect(lastNode.getId(), sleep.getId(), "connection_" + lastNode.getId() + "_" + sleep.getId(), embeddedSubProcess, false);
lastNode = sleep;
}
actionConsumer.accept(firstNode, lastNode);
if (isParallel && actions.size() > 1) {
// reset first node as all of action nodes will be first nodes
firstNode = null;
} else {
if (prevNode != null) {
factory.connect(prevNode.getId(), lastNode.getId(), "connect_" + prevNode.getId() + "_" + lastNode.getId(), embeddedSubProcess, false);
}
}
prevNode = lastNode;
}
firstLastNodeConsumer.accept(firstNode, lastNode);
}
use of io.automatiko.engine.workflow.process.core.node.WorkItemNode in project automatiko-engine by automatiko-io.
the class ServerlessWorkflowFactory method serviceNode.
public WorkItemNode serviceNode(long id, Action action, FunctionDefinition function, NodeContainer nodeContainer) {
String actionName = action.getName();
String[] operationParts = function.getOperation().split("#");
String interfaceStr = operationParts[0];
String operationStr = operationParts[1];
WorkItemNode workItemNode = new WorkItemNode();
workItemNode.setId(id);
workItemNode.setName(actionName);
workItemNode.setMetaData(UNIQUE_ID_PARAM, Long.toString(id));
workItemNode.setMetaData("Type", SERVICE_TASK_TYPE);
workItemNode.setMetaData("Implementation", "##WebService");
Work work = new WorkImpl();
workItemNode.setWork(work);
work.setName(SERVICE_TASK_TYPE);
work.setParameter("Interface", interfaceStr);
work.setParameter("Operation", operationStr);
work.setParameter("interfaceImplementationRef", interfaceStr);
work.setParameter("implementation", "##WebService");
JsonNode params = action.getFunctionRef().getArguments();
String inputFilter = null;
String outputFilter = null;
String scopeFilter = null;
if (action.getActionDataFilter() != null) {
inputFilter = unwrapExpression(action.getActionDataFilter().getFromStateData());
outputFilter = unwrapExpression(action.getActionDataFilter().getResults());
scopeFilter = unwrapExpression(action.getActionDataFilter().getToStateData());
}
Set<String> paramNames = new LinkedHashSet<>();
if (params != null) {
Iterator<String> it = params.fieldNames();
while (it.hasNext()) {
String name = it.next();
String value = params.get(name).toString();
work.setParameter(name, unwrapExpression(value));
paramNames.add(name);
work.addParameterDefinition(new ParameterDefinitionImpl(name, new JsonNodeDataType()));
}
} else {
work.setParameter("ParameterType", JSON_NODE);
}
Assignment assignment = new Assignment("jq", null, null);
assignment.setMetaData("Action", new TaskInputJqAssignmentAction(inputFilter, paramNames));
workItemNode.addInAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(assignment), null));
Assignment outAssignment = new Assignment("jq", null, null);
outAssignment.setMetaData("Action", new TaskOutputJqAssignmentAction(outputFilter, scopeFilter));
workItemNode.addOutAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(outAssignment), null));
nodeContainer.addNode(workItemNode);
return workItemNode;
}
use of io.automatiko.engine.workflow.process.core.node.WorkItemNode in project automatiko-engine by automatiko-io.
the class WorkHandler method start.
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
parser.startElementBuilder(localName, attrs);
WorkItemNode workItemNode = (WorkItemNode) parser.getParent();
final String name = attrs.getValue("name");
emptyAttributeCheck(localName, "name", name, parser);
Work work = new WorkImpl();
work.setName(name);
workItemNode.setWork(work);
return work;
}
use of io.automatiko.engine.workflow.process.core.node.WorkItemNode in project automatiko-engine by automatiko-io.
the class WorkItemNodeHandler method writeNode.
public void writeNode(Node node, StringBuilder xmlDump, boolean includeMeta) {
WorkItemNode workItemNode = (WorkItemNode) node;
writeNode("workItem", workItemNode, xmlDump, includeMeta);
visitParameters(workItemNode, xmlDump);
xmlDump.append(">" + EOL);
if (includeMeta) {
writeMetaData(workItemNode, xmlDump);
}
Work work = workItemNode.getWork();
visitWork(work, xmlDump, includeMeta);
visitInMappings(workItemNode.getInMappings(), xmlDump);
visitOutMappings(workItemNode.getOutMappings(), xmlDump);
for (String eventType : workItemNode.getActionTypes()) {
writeActions(eventType, workItemNode.getActions(eventType), xmlDump);
}
writeTimers(workItemNode.getTimers(), xmlDump);
endNode("workItem", xmlDump);
}
use of io.automatiko.engine.workflow.process.core.node.WorkItemNode in project automatiko-engine by automatiko-io.
the class WorkItemNodeHandler method handleNode.
public void handleNode(final Node node, final Element element, final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
super.handleNode(node, element, uri, localName, parser);
WorkItemNode workItemNode = (WorkItemNode) node;
final String waitForCompletion = element.getAttribute("waitForCompletion");
workItemNode.setWaitForCompletion(!"false".equals(waitForCompletion));
for (String eventType : workItemNode.getActionTypes()) {
handleAction(workItemNode, element, eventType);
}
}
Aggregations