Search in sources :

Example 1 with EventNode

use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.

the class IntermediateCatchEventHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    Node node = (Node) parser.getCurrent();
    // determine type of event definition, so the correct type of node
    // can be generated
    org.w3c.dom.Node xmlNode = element.getFirstChild();
    while (xmlNode != null) {
        String nodeName = xmlNode.getNodeName();
        if ("signalEventDefinition".equals(nodeName)) {
            // reuse already created EventNode
            handleSignalNode(node, element, uri, localName, parser);
            node.setMetaData(EVENT_TYPE, "signal");
            node.setMetaData("functionFlowContinue", "true");
            break;
        } else if ("messageEventDefinition".equals(nodeName)) {
            // reuse already created EventNode
            handleMessageNode(node, element, uri, localName, parser);
            node.setMetaData(EVENT_TYPE, "message");
            node.setMetaData("functionFlowContinue", "true");
            break;
        } else if ("timerEventDefinition".equals(nodeName)) {
            // create new timerNode
            TimerNode timerNode = new TimerNode();
            timerNode.setId(node.getId());
            timerNode.setName(node.getName());
            timerNode.setMetaData("UniqueId", node.getMetaData().get("UniqueId"));
            node = timerNode;
            node.setMetaData(EVENT_TYPE, "timer");
            handleTimerNode(node, element, uri, localName, parser);
            node.setMetaData("functionFlowContinue", "true");
            break;
        } else if ("conditionalEventDefinition".equals(nodeName)) {
            // create new stateNode
            StateNode stateNode = new StateNode();
            stateNode.setId(node.getId());
            stateNode.setName(node.getName());
            stateNode.setMetaData("UniqueId", node.getMetaData().get("UniqueId"));
            node = stateNode;
            node.setMetaData(EVENT_TYPE, "conditional");
            handleStateNode(node, element, uri, localName, parser);
            node.setMetaData("functionFlowContinue", "true");
            break;
        } else if ("linkEventDefinition".equals(nodeName)) {
            CatchLinkNode linkNode = new CatchLinkNode();
            linkNode.setId(node.getId());
            node = linkNode;
            node.setMetaData(EVENT_TYPE, "link");
            handleLinkNode(element, node, xmlNode, parser);
            break;
        }
        xmlNode = xmlNode.getNextSibling();
    }
    node.setMetaData("DataOutputs", new LinkedHashMap<String, String>(dataOutputTypes));
    NodeContainer nodeContainer = (NodeContainer) parser.getParent();
    nodeContainer.addNode(node);
    ((ProcessBuildData) parser.getData()).addNode(node);
    return node;
}
Also used : CatchLinkNode(io.automatiko.engine.workflow.process.core.node.CatchLinkNode) ProcessBuildData(io.automatiko.engine.workflow.compiler.xml.ProcessBuildData) Element(org.w3c.dom.Element) TimerNode(io.automatiko.engine.workflow.process.core.node.TimerNode) CompositeNode(io.automatiko.engine.workflow.process.core.node.CompositeNode) CatchLinkNode(io.automatiko.engine.workflow.process.core.node.CatchLinkNode) Node(io.automatiko.engine.workflow.process.core.Node) StateNode(io.automatiko.engine.workflow.process.core.node.StateNode) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) StateNode(io.automatiko.engine.workflow.process.core.node.StateNode) NodeContainer(io.automatiko.engine.workflow.process.core.NodeContainer) TimerNode(io.automatiko.engine.workflow.process.core.node.TimerNode)

Example 2 with EventNode

use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.

the class EventNodeHandler method writeNode.

public void writeNode(Node node, StringBuilder xmlDump, int metaDataType) {
    EventNode eventNode = (EventNode) node;
    String attachedTo = (String) eventNode.getMetaData("AttachedTo");
    if (attachedTo == null) {
        writeNode("intermediateCatchEvent", eventNode, xmlDump, metaDataType);
        xmlDump.append(">" + EOL);
        writeExtensionElements(eventNode, xmlDump);
        writeVariableName(eventNode, xmlDump);
        if (eventNode.getEventFilters().size() > 0) {
            String type = ((EventTypeFilter) eventNode.getEventFilters().get(0)).getType();
            if (type.startsWith("Message-")) {
                type = type.substring(8);
                xmlDump.append("      <messageEventDefinition messageRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\"/>" + EOL);
            } else {
                xmlDump.append("      <signalEventDefinition signalRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\"/>" + EOL);
            }
        }
        endNode("intermediateCatchEvent", xmlDump);
    } else {
        String type = ((EventTypeFilter) eventNode.getEventFilters().get(0)).getType();
        if (type.startsWith("Escalation-")) {
            type = type.substring(attachedTo.length() + 12);
            boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
            writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
            xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
            if (!cancelActivity) {
                xmlDump.append("cancelActivity=\"false\" ");
            }
            xmlDump.append(">" + EOL);
            xmlDump.append("      <escalationEventDefinition escalationRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\" />" + EOL);
            endNode("boundaryEvent", xmlDump);
        } else if (type.startsWith("Error-")) {
            type = type.substring(attachedTo.length() + 7);
            writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
            xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
            xmlDump.append(">" + EOL);
            String errorId = getErrorIdForErrorCode(type, eventNode);
            xmlDump.append("      <errorEventDefinition errorRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(errorId) + "\" />" + EOL);
            endNode("boundaryEvent", xmlDump);
        } else if (type.startsWith("Timer-")) {
            type = type.substring(attachedTo.length() + 7);
            boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
            writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
            xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
            if (!cancelActivity) {
                xmlDump.append("cancelActivity=\"false\" ");
            }
            xmlDump.append(">" + EOL);
            String duration = (String) eventNode.getMetaData("TimeDuration");
            String cycle = (String) eventNode.getMetaData("TimeCycle");
            if (duration != null && cycle != null) {
                xmlDump.append("      <timerEventDefinition>" + EOL + "        <timeDuration xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(duration) + "</timeDuration>" + EOL + "        <timeCycle xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(cycle) + "</timeCycle>" + EOL + "      </timerEventDefinition>" + EOL);
            } else if (duration != null) {
                xmlDump.append("      <timerEventDefinition>" + EOL + "        <timeDuration xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(duration) + "</timeDuration>" + EOL + "      </timerEventDefinition>" + EOL);
            } else {
                xmlDump.append("      <timerEventDefinition>" + EOL + "        <timeCycle xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(cycle) + "</timeCycle>" + EOL + "      </timerEventDefinition>" + EOL);
            }
            endNode("boundaryEvent", xmlDump);
        } else if (node.getMetaData().get("SignalName") != null) {
            boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
            writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
            xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
            if (!cancelActivity) {
                xmlDump.append("cancelActivity=\"false\" ");
            }
            xmlDump.append(">" + EOL);
            xmlDump.append("      <signalEventDefinition signalRef=\"" + type + "\"/>" + EOL);
            endNode("boundaryEvent", xmlDump);
        } else if (node.getMetaData().get("Condition") != null) {
            boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
            writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
            xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
            if (!cancelActivity) {
                xmlDump.append("cancelActivity=\"false\" ");
            }
            xmlDump.append(">" + EOL);
            xmlDump.append("      <conditionalEventDefinition>" + EOL);
            xmlDump.append("        <condition xsi:type=\"tFormalExpression\" language=\"https://automatiko.io/rule\">" + eventNode.getMetaData("Condition") + "</condition>" + EOL);
            xmlDump.append("      </conditionalEventDefinition>" + EOL);
            endNode("boundaryEvent", xmlDump);
        } else if (type.startsWith("Message-")) {
            type = type.substring(8);
            writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
            xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
            xmlDump.append(">" + EOL);
            xmlDump.append("      <messageEventDefinition messageRef=\"" + type + "\"/>" + EOL);
            endNode("boundaryEvent", xmlDump);
        } else {
            throw new IllegalArgumentException("Unknown boundary event type: \"" + type + "\"");
        }
    }
}
Also used : EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter)

Example 3 with EventNode

use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.

the class ServerlessWorkflowParser method parse.

public Process parse(Reader workflowFile) {
    AtomicLong ids = new AtomicLong(0);
    Workflow workflow = Workflow.fromSource(toString(workflowFile));
    ServerlessWorkflowFactory factory = new ServerlessWorkflowFactory();
    if (!"jq".equalsIgnoreCase(workflow.getExpressionLang())) {
        throw new IllegalArgumentException("Not supported expression language, only 'jq' is supported");
    }
    WorkflowProcess process = factory.createProcess(workflow);
    State start = WorkflowUtils.getStartingState(workflow);
    Node startNode;
    if (start.getType().equals(DefaultState.Type.EVENT)) {
        List<Node> nodes = new ArrayList<>();
        EventState eventState = (EventState) start;
        for (OnEvents onEvent : eventState.getOnEvents()) {
            if (eventState.isExclusive()) {
                // use event based gateway
                Join join = factory.joinNode(ids.getAndIncrement(), "join_" + eventState.getName(), Join.TYPE_XOR, process);
                for (String eventRef : onEvent.getEventRefs()) {
                    EventDefinition event = WorkflowUtils.getDefinedConsumedEvents(workflow).stream().filter(e -> e.getName().equals(eventRef)).findFirst().get();
                    StartNode startMessageNode = factory.messageStartNode(ids.getAndIncrement(), event, onEvent, process);
                    factory.connect(startMessageNode.getId(), join.getId(), "connection_" + startMessageNode.getId() + "_" + join.getId(), process, false);
                }
                buildActionsForState(workflow, onEvent.getActions(), process, factory, ids, (first, last) -> {
                    factory.connect(join.getId(), first.getId(), join.getId() + "_" + first.getId(), process, false);
                    nodes.add(last);
                }, (first, last) -> {
                }, false);
            } else {
                // use parallel gateway
                Join parallelJoin = factory.joinNode(ids.getAndIncrement(), "join_" + eventState.getName(), Join.TYPE_AND, process);
                for (String eventRef : onEvent.getEventRefs()) {
                    EventDefinition event = WorkflowUtils.getDefinedConsumedEvents(workflow).stream().filter(e -> e.getName().equals(eventRef)).findFirst().get();
                    StartNode startMessageNode = factory.messageStartNode(ids.getAndIncrement(), event, onEvent, process);
                    factory.connect(startMessageNode.getId(), parallelJoin.getId(), "connection_" + startMessageNode.getId() + "_" + parallelJoin.getId(), process, false);
                }
                buildActionsForState(workflow, onEvent.getActions(), process, factory, ids, (first, last) -> {
                    factory.connect(parallelJoin.getId(), first.getId(), parallelJoin.getId() + "_" + first.getId(), process, false);
                    nodes.add(last);
                }, (first, last) -> {
                }, false);
            }
        }
        // start node becomes the last node after message event handling so other parts are connected to it
        startNode = nodes.get(0);
        if (eventState.getStateDataFilter() != null && eventState.getStateDataFilter().getOutput() != null) {
            ActionNode stateDataFilterActionNode = factory.stateDataFilterActionNode(ids.getAndIncrement(), "", process, eventState.getStateDataFilter().getOutput());
            factory.connect(startNode.getId(), stateDataFilterActionNode.getId(), "connection_" + startNode.getId() + "_" + stateDataFilterActionNode.getId(), process, false);
            startNode = stateDataFilterActionNode;
        }
        if (eventState.getEnd() != null) {
            EndNode endNode = factory.endNode(ids.getAndIncrement(), eventState.getName() + "-end", eventState.getEnd().isTerminate(), process);
            if (eventState.getEnd().getProduceEvents() != null && !eventState.getEnd().getProduceEvents().isEmpty()) {
                produceEvents(eventState.getEnd().getProduceEvents(), factory, workflow, ids, process, startNode.getId(), endNode.getId());
            } else {
                factory.connect(startNode.getId(), endNode.getId(), "connection_" + startNode.getId() + "_" + endNode.getId(), process, false);
            }
        }
    } else {
        startNode = factory.startNode(ids.getAndIncrement(), start.getName() + "-start", process);
    }
    // map of state names to node ids for connecting purpose
    Map<String, Long> mappedNodes = new LinkedHashMap<>();
    Node currentNode = null;
    // process all states and create proper node representation for each state
    for (State state : workflow.getStates()) {
        if (state.getType().equals(Type.INJECT)) {
            ActionNode actionNode = factory.injectStateNode(ids.getAndIncrement(), state.getName(), process, ((InjectState) state).getData().toString());
            mappedNodes.put(state.getName(), actionNode.getId());
            if (state.getEnd() != null) {
                EndNode endNode = factory.endNode(ids.getAndIncrement(), state.getName() + "-end", state.getEnd().isTerminate(), process);
                if (state.getEnd().getProduceEvents() != null && !state.getEnd().getProduceEvents().isEmpty()) {
                    produceEvents(state.getEnd().getProduceEvents(), factory, workflow, ids, process, actionNode.getId(), endNode.getId());
                } else {
                    factory.connect(actionNode.getId(), endNode.getId(), "connection_" + actionNode.getId() + "_" + endNode.getId(), process, false);
                }
            }
            setUniqueId(actionNode, state);
            currentNode = actionNode;
        } else if (state.getType().equals(DefaultState.Type.OPERATION)) {
            OperationState operationState = (OperationState) state;
            CompositeContextNode embeddedSubProcess = factory.subProcessNode(ids.getAndIncrement(), state.getName(), process);
            currentNode = embeddedSubProcess;
            setUniqueId(embeddedSubProcess, state);
            // handle state data inputs
            Assignment inputAssignment = new Assignment("jq", "", "");
            inputAssignment.setMetaData("Action", new InputJqAssignmentAction(state.getStateDataFilter() == null ? null : factory.unwrapExpression(state.getStateDataFilter().getInput())));
            embeddedSubProcess.addInAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(inputAssignment), null));
            // handle state data outputs
            Assignment outputAssignment = new Assignment("jq", "", "");
            outputAssignment.setMetaData("Action", new OutputJqAssignmentAction(state.getStateDataFilter() == null ? null : factory.unwrapExpression(state.getStateDataFilter().getOutput())));
            embeddedSubProcess.addOutAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(outputAssignment), null));
            mappedNodes.put(state.getName(), embeddedSubProcess.getId());
            StartNode embeddedStartNode = factory.startNode(ids.getAndIncrement(), "EmbeddedStart", embeddedSubProcess);
            EndNode embeddedEndNode = factory.endNode(ids.getAndIncrement(), "EmbeddedEnd", false, embeddedSubProcess);
            if (operationState.getActions() == null || operationState.getActions().isEmpty()) {
                factory.connect(embeddedStartNode.getId(), embeddedEndNode.getId(), embeddedStartNode.getId() + "_" + embeddedEndNode.getId(), embeddedSubProcess, false);
                if (state.getEnd() != null) {
                    EndNode endNode = factory.endNode(ids.getAndIncrement(), state.getName() + "-end", state.getEnd().isTerminate(), process);
                    if (state.getEnd().getProduceEvents() != null && !state.getEnd().getProduceEvents().isEmpty()) {
                        produceEvents(state.getEnd().getProduceEvents(), factory, workflow, ids, process, embeddedSubProcess.getId(), endNode.getId());
                    } else {
                        factory.connect(embeddedSubProcess.getId(), endNode.getId(), "connection_" + embeddedSubProcess.getId() + "_" + endNode.getId(), process, false);
                    }
                }
                // ensure that start node is connected
                if (state.equals(start) && currentNode != null) {
                    factory.connect(startNode.getId(), currentNode.getId(), "connection_" + startNode.getId() + "_" + currentNode.getId(), process, false);
                }
                continue;
            }
            if (operationState.getActionMode() == null || operationState.getActionMode() == ActionMode.SEQUENTIAL) {
                buildActionsForState(workflow, operationState.getActions(), embeddedSubProcess, factory, ids, (first, last) -> {
                    factory.connect(embeddedStartNode.getId(), first.getId(), embeddedStartNode.getId() + "_" + first.getId(), embeddedSubProcess, false);
                    factory.connect(last.getId(), embeddedEndNode.getId(), last.getId() + "_" + embeddedEndNode.getId(), embeddedSubProcess, false);
                }, (first, last) -> {
                }, false);
            } else {
                Split split = factory.splitNode(ids.getAndIncrement(), "parallel-split-" + state.getName(), Split.TYPE_AND, embeddedSubProcess);
                Join join = factory.joinNode(ids.getAndIncrement(), "parallel-join-" + state.getName(), Join.TYPE_AND, embeddedSubProcess);
                factory.connect(embeddedStartNode.getId(), split.getId(), embeddedStartNode.getId() + "_" + split.getId(), embeddedSubProcess, false);
                factory.connect(join.getId(), embeddedEndNode.getId(), join.getId() + "_" + embeddedEndNode.getId(), embeddedSubProcess, false);
                buildActionsForState(workflow, operationState.getActions(), embeddedSubProcess, factory, ids, (first, last) -> {
                }, (first, last) -> {
                    factory.connect(split.getId(), first.getId(), split.getId() + "_" + first.getId(), embeddedSubProcess, false);
                    factory.connect(last.getId(), join.getId(), last.getId() + "_" + join.getId(), embeddedSubProcess, false);
                }, true);
            }
            if (state.getEnd() != null) {
                EndNode endNode = factory.endNode(ids.getAndIncrement(), state.getName() + "-end", state.getEnd().isTerminate(), process);
                if (state.getEnd().getProduceEvents() != null && !state.getEnd().getProduceEvents().isEmpty()) {
                    produceEvents(state.getEnd().getProduceEvents(), factory, workflow, ids, process, embeddedSubProcess.getId(), endNode.getId());
                } else {
                    factory.connect(embeddedSubProcess.getId(), endNode.getId(), "connection_" + embeddedSubProcess.getId() + "_" + endNode.getId(), process, false);
                }
            }
        } else if (state.getType().equals(DefaultState.Type.EVENT)) {
            EventState eventState = (EventState) state;
            if (eventState.equals(start)) {
                // event state that is start node is already handled
                continue;
            }
            for (OnEvents onEvent : eventState.getOnEvents()) {
                CompositeContextNode embeddedSubProcess = factory.subProcessNode(ids.getAndIncrement(), state.getName(), process);
                setUniqueId(embeddedSubProcess, state);
                currentNode = embeddedSubProcess;
                // handle state data inputs
                Assignment inputAssignment = new Assignment("jq", "", "");
                inputAssignment.setMetaData("Action", new InputJqAssignmentAction(state.getStateDataFilter() == null ? null : factory.unwrapExpression(state.getStateDataFilter().getInput())));
                embeddedSubProcess.addInAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(inputAssignment), null));
                // handle state data outputs
                Assignment outputAssignment = new Assignment("jq", "", "");
                outputAssignment.setMetaData("Action", new OutputJqAssignmentAction(state.getStateDataFilter() == null ? null : factory.unwrapExpression(state.getStateDataFilter().getOutput())));
                embeddedSubProcess.addOutAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(outputAssignment), null));
                mappedNodes.put(state.getName(), embeddedSubProcess.getId());
                StartNode embeddedStartNode = factory.startNode(ids.getAndIncrement(), "EmbeddedStart", embeddedSubProcess);
                EndNode embeddedEndNode = factory.endNode(ids.getAndIncrement(), "EmbeddedEnd", false, embeddedSubProcess);
                if (eventState.isExclusive()) {
                    // use event based gateway
                    Split eventSplit = factory.eventBasedSplit(ids.getAndIncrement(), "split_" + state.getName(), embeddedSubProcess);
                    Join join = factory.joinNode(ids.getAndIncrement(), "join_" + state.getName(), Join.TYPE_XOR, embeddedSubProcess);
                    factory.connect(embeddedStartNode.getId(), eventSplit.getId(), "connection_" + embeddedStartNode.getId() + "_" + eventSplit.getId(), embeddedSubProcess, false);
                    for (String eventRef : onEvent.getEventRefs()) {
                        EventDefinition event = WorkflowUtils.getDefinedConsumedEvents(workflow).stream().filter(e -> e.getName().equals(eventRef)).findFirst().get();
                        EventNode eventNode = factory.consumeEventNode(ids.getAndIncrement(), event, onEvent.getEventDataFilter(), embeddedSubProcess);
                        factory.connect(eventSplit.getId(), eventNode.getId(), "connection_" + eventSplit.getId() + "_" + eventNode.getId(), embeddedSubProcess, false);
                        factory.connect(eventNode.getId(), join.getId(), "connection_" + eventNode.getId() + "_" + join.getId(), embeddedSubProcess, false);
                    }
                    buildActionsForState(workflow, onEvent.getActions(), embeddedSubProcess, factory, ids, (first, last) -> {
                        factory.connect(join.getId(), first.getId(), join.getId() + "_" + first.getId(), embeddedSubProcess, false);
                        factory.connect(last.getId(), embeddedEndNode.getId(), last.getId() + "_" + embeddedEndNode.getId(), embeddedSubProcess, false);
                    }, (first, last) -> {
                    }, false);
                } else {
                    // use parallel gateway
                    Split parallelSplit = factory.splitNode(ids.getAndIncrement(), "split_" + state.getName(), Split.TYPE_AND, embeddedSubProcess);
                    Join parallelJoin = factory.joinNode(ids.getAndIncrement(), "join_" + state.getName(), Join.TYPE_AND, embeddedSubProcess);
                    factory.connect(embeddedStartNode.getId(), parallelSplit.getId(), "connection_" + embeddedStartNode.getId() + "_" + parallelSplit.getId(), embeddedSubProcess, false);
                    for (String eventRef : onEvent.getEventRefs()) {
                        EventDefinition event = WorkflowUtils.getDefinedConsumedEvents(workflow).stream().filter(e -> e.getName().equals(eventRef)).findFirst().get();
                        EventNode eventNode = factory.consumeEventNode(ids.getAndIncrement(), event, onEvent.getEventDataFilter(), embeddedSubProcess);
                        factory.connect(parallelSplit.getId(), eventNode.getId(), "connection_" + parallelSplit.getId() + "_" + eventNode.getId(), embeddedSubProcess, false);
                        factory.connect(eventNode.getId(), parallelJoin.getId(), "connection_" + eventNode.getId() + "_" + parallelJoin.getId(), embeddedSubProcess, false);
                    }
                    buildActionsForState(workflow, onEvent.getActions(), embeddedSubProcess, factory, ids, (first, last) -> {
                        factory.connect(parallelJoin.getId(), first.getId(), parallelJoin.getId() + "_" + first.getId(), embeddedSubProcess, false);
                        factory.connect(last.getId(), embeddedEndNode.getId(), last.getId() + "_" + embeddedEndNode.getId(), embeddedSubProcess, false);
                    }, (first, last) -> {
                    }, false);
                }
                if (state.getEnd() != null) {
                    EndNode endNode = factory.endNode(ids.getAndIncrement(), state.getName() + "-end", state.getEnd().isTerminate(), process);
                    if (state.getEnd().getProduceEvents() != null && !state.getEnd().getProduceEvents().isEmpty()) {
                        produceEvents(state.getEnd().getProduceEvents(), factory, workflow, ids, process, embeddedSubProcess.getId(), endNode.getId());
                    } else {
                        factory.connect(embeddedSubProcess.getId(), endNode.getId(), "connection_" + embeddedSubProcess.getId() + "_" + endNode.getId(), process, false);
                    }
                }
            }
        } else if (state.getType().equals(DefaultState.Type.CALLBACK)) {
            CallbackState callcackState = (CallbackState) state;
            CompositeContextNode embeddedSubProcess = factory.subProcessNode(ids.getAndIncrement(), state.getName(), process);
            currentNode = embeddedSubProcess;
            setUniqueId(embeddedSubProcess, state);
            // handle state data inputs
            Assignment inputAssignment = new Assignment("jq", "", "");
            inputAssignment.setMetaData("Action", new InputJqAssignmentAction(state.getStateDataFilter() == null ? null : factory.unwrapExpression(state.getStateDataFilter().getInput())));
            embeddedSubProcess.addInAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(inputAssignment), null));
            // handle state data outputs
            Assignment outputAssignment = new Assignment("jq", "", "");
            outputAssignment.setMetaData("Action", new OutputJqAssignmentAction(state.getStateDataFilter() == null ? null : factory.unwrapExpression(state.getStateDataFilter().getOutput())));
            embeddedSubProcess.addOutAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(outputAssignment), null));
            mappedNodes.put(state.getName(), embeddedSubProcess.getId());
            StartNode embeddedStartNode = factory.startNode(ids.getAndIncrement(), "EmbeddedStart", embeddedSubProcess);
            EndNode embeddedEndNode = factory.endNode(ids.getAndIncrement(), "EmbeddedEnd", false, embeddedSubProcess);
            EventDefinition event = WorkflowUtils.getDefinedConsumedEvents(workflow).stream().filter(e -> e.getName().equals(callcackState.getEventRef())).findFirst().get();
            EventNode eventNode = factory.consumeEventNode(ids.getAndIncrement(), event, callcackState.getEventDataFilter(), embeddedSubProcess);
            buildActionsForState(workflow, Collections.singletonList(callcackState.getAction()), embeddedSubProcess, factory, ids, (first, last) -> {
                factory.connect(embeddedStartNode.getId(), first.getId(), embeddedStartNode.getId() + "_" + first.getId(), embeddedSubProcess, false);
                factory.connect(last.getId(), eventNode.getId(), last.getId() + "_" + eventNode.getId(), embeddedSubProcess, false);
            }, (first, last) -> {
            }, false);
            factory.connect(eventNode.getId(), embeddedEndNode.getId(), "connection_" + eventNode.getId() + "_" + embeddedEndNode.getId(), embeddedSubProcess, false);
            if (state.getEnd() != null) {
                EndNode endNode = factory.endNode(ids.getAndIncrement(), state.getName() + "-end", state.getEnd().isTerminate(), process);
                if (state.getEnd().getProduceEvents() != null && !state.getEnd().getProduceEvents().isEmpty()) {
                    produceEvents(state.getEnd().getProduceEvents(), factory, workflow, ids, process, embeddedSubProcess.getId(), endNode.getId());
                } else {
                    factory.connect(embeddedSubProcess.getId(), endNode.getId(), "connection_" + embeddedSubProcess.getId() + "_" + endNode.getId(), process, false);
                }
            }
        } else if (state.getType().equals(Type.SLEEP)) {
            TimerNode sleep = factory.timerNode(ids.getAndIncrement(), "sleep-" + state.getName(), ((SleepState) state).getDuration(), process);
            mappedNodes.put(state.getName(), sleep.getId());
            setUniqueId(sleep, state);
            if (state.getEnd() != null) {
                EndNode endNode = factory.endNode(ids.getAndIncrement(), state.getName() + "-end", state.getEnd().isTerminate(), process);
                if (state.getEnd().getProduceEvents() != null && !state.getEnd().getProduceEvents().isEmpty()) {
                    produceEvents(state.getEnd().getProduceEvents(), factory, workflow, ids, process, sleep.getId(), endNode.getId());
                } else {
                    factory.connect(sleep.getId(), endNode.getId(), "connection_" + sleep.getId() + "_" + endNode.getId(), process, false);
                }
            }
            currentNode = sleep;
        } else if (state.getType().equals(DefaultState.Type.PARALLEL)) {
            ParallelState parallelState = (ParallelState) state;
            CompositeContextNode embeddedSubProcess = factory.subProcessNode(ids.getAndIncrement(), state.getName(), process);
            setUniqueId(embeddedSubProcess, state);
            currentNode = embeddedSubProcess;
            // handle state data inputs
            Assignment inputAssignment = new Assignment("jq", "", "");
            inputAssignment.setMetaData("Action", new InputJqAssignmentAction(state.getStateDataFilter() == null ? null : factory.unwrapExpression(state.getStateDataFilter().getInput())));
            embeddedSubProcess.addInAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(inputAssignment), null));
            // handle state data outputs
            Assignment outputAssignment = new Assignment("jq", "", "");
            outputAssignment.setMetaData("Action", new OutputJqAssignmentAction(state.getStateDataFilter() == null ? null : factory.unwrapExpression(state.getStateDataFilter().getOutput())));
            embeddedSubProcess.addOutAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(outputAssignment), null));
            mappedNodes.put(state.getName(), embeddedSubProcess.getId());
            StartNode embeddedStartNode = factory.startNode(ids.getAndIncrement(), "EmbeddedStart", embeddedSubProcess);
            EndNode embeddedEndNode = factory.endNode(ids.getAndIncrement(), "EmbeddedEnd", false, embeddedSubProcess);
            Split parallelSplit = factory.splitNode(ids.getAndIncrement(), "split_" + state.getName(), Split.TYPE_AND, embeddedSubProcess);
            Join parallelJoin;
            if (parallelState.getCompletionType().equals(CompletionType.AT_LEAST)) {
                parallelJoin = factory.joinNode(ids.getAndIncrement(), "join_" + state.getName(), Join.TYPE_N_OF_M, embeddedSubProcess);
                parallelJoin.setN(parallelState.getNumCompleted());
            } else {
                parallelJoin = factory.joinNode(ids.getAndIncrement(), "join_" + state.getName(), Join.TYPE_AND, embeddedSubProcess);
            }
            factory.connect(embeddedStartNode.getId(), parallelSplit.getId(), "connection_" + embeddedStartNode.getId() + "_" + parallelSplit.getId(), embeddedSubProcess, false);
            for (Branch branch : parallelState.getBranches()) {
                buildActionsForState(workflow, branch.getActions(), embeddedSubProcess, factory, ids, (first, last) -> {
                    factory.connect(parallelSplit.getId(), first.getId(), parallelSplit.getId() + "_" + first.getId(), embeddedSubProcess, false);
                    factory.connect(last.getId(), parallelJoin.getId(), last.getId() + "_" + parallelJoin.getId(), embeddedSubProcess, false);
                }, (first, last) -> {
                }, true);
            }
            factory.connect(parallelJoin.getId(), embeddedEndNode.getId(), "connection_" + parallelJoin.getId() + "_" + embeddedEndNode.getId(), embeddedSubProcess, false);
            if (state.getEnd() != null) {
                EndNode endNode = factory.endNode(ids.getAndIncrement(), state.getName() + "-end", state.getEnd().isTerminate(), process);
                if (state.getEnd().getProduceEvents() != null && !state.getEnd().getProduceEvents().isEmpty()) {
                    produceEvents(state.getEnd().getProduceEvents(), factory, workflow, ids, process, embeddedSubProcess.getId(), endNode.getId());
                } else {
                    factory.connect(embeddedSubProcess.getId(), endNode.getId(), "connection_" + embeddedSubProcess.getId() + "_" + endNode.getId(), process, false);
                }
            }
        } else if (state.getType().equals(DefaultState.Type.FOREACH)) {
        }
        // ensure that start node is connected
        if (state.equals(start) && currentNode != null) {
            factory.connect(startNode.getId(), currentNode.getId(), "connection_" + startNode.getId() + "_" + currentNode.getId(), process, false);
        }
    }
    for (State state : workflow.getStates()) {
        if (state.getType().equals(Type.SWITCH)) {
            // switch state must be processed at the end as it needs to reference other nodes by id
            SwitchState switchState = (SwitchState) state;
            if (switchState.getDataConditions() != null && !switchState.getDataConditions().isEmpty()) {
                Split splitNode = factory.splitNode(ids.getAndIncrement(), "split_" + state.getName(), Split.TYPE_XOR, process);
                currentNode = splitNode;
                setUniqueId(splitNode, state);
                mappedNodes.put(state.getName(), splitNode.getId());
                int priority = 1;
                for (DataCondition condition : switchState.getDataConditions()) {
                    boolean isDefaultConstraint = false;
                    if (switchState.getDefaultCondition() != null && switchState.getDefaultCondition().getTransition() != null && condition.getTransition() != null && condition.getTransition().getNextState().equals(switchState.getDefaultCondition().getTransition().getNextState())) {
                        isDefaultConstraint = true;
                    }
                    if (switchState.getDefaultCondition() != null && switchState.getDefaultCondition().getEnd() != null && condition.getEnd() != null) {
                        isDefaultConstraint = true;
                    }
                    Connection outgoingConnection = null;
                    long target = 0;
                    if (condition.getEnd() != null) {
                        EndNode endNode = factory.endNode(ids.getAndIncrement(), "end_" + switchState.getName(), false, process);
                        target = endNode.getId();
                        outgoingConnection = factory.connect(splitNode.getId(), endNode.getId(), "connection_" + splitNode.getId() + "_" + endNode.getId(), process, false);
                    } else if (condition.getTransition() != null && condition.getTransition().getNextState() != null) {
                        long source = splitNode.getId();
                        target = mappedNodes.get(condition.getTransition().getNextState());
                        outgoingConnection = factory.connect(source, target, "connection_" + source + "_" + target, process, false);
                    }
                    ReturnValueConstraintEvaluator returnValueConstraint = new ReturnValueConstraintEvaluator();
                    returnValueConstraint.setDialect("jq");
                    returnValueConstraint.setName(splitNode.getId() + "_" + target);
                    returnValueConstraint.setPriority(priority);
                    returnValueConstraint.setDefault(isDefaultConstraint);
                    returnValueConstraint.setType("DROOLS_DEFAULT");
                    returnValueConstraint.setConstraint(factory.unwrapExpression(condition.getCondition()));
                    returnValueConstraint.setEvaluator(new JqReturnValueEvaluator(factory.unwrapExpression(condition.getCondition())));
                    splitNode.setConstraint(outgoingConnection, returnValueConstraint);
                }
            } else if (switchState.getEventConditions() != null && !switchState.getEventConditions().isEmpty()) {
                Split splitNode = factory.eventBasedSplit(ids.getAndIncrement(), "split_" + state.getName(), process);
                currentNode = splitNode;
                setUniqueId(splitNode, state);
                mappedNodes.put(state.getName(), splitNode.getId());
                for (EventCondition eventCondition : switchState.getEventConditions()) {
                    EventDefinition event = WorkflowUtils.getDefinedConsumedEvents(workflow).stream().filter(e -> e.getName().equals(eventCondition.getEventRef())).findFirst().get();
                    EventNode eventNode = factory.consumeEventNode(ids.getAndIncrement(), event, eventCondition.getEventDataFilter(), process);
                    factory.connect(splitNode.getId(), eventNode.getId(), "connection_" + splitNode.getId() + "_" + eventNode.getId(), process, false);
                    long target = 0;
                    if (eventCondition.getEnd() != null) {
                        EndNode endNode = factory.endNode(ids.getAndIncrement(), "end_" + switchState.getName(), false, process);
                        target = endNode.getId();
                        factory.connect(splitNode.getId(), endNode.getId(), "connection_" + splitNode.getId() + "_" + endNode.getId(), process, false);
                    } else if (eventCondition.getTransition() != null && eventCondition.getTransition().getNextState() != null) {
                        target = mappedNodes.get(eventCondition.getTransition().getNextState());
                        factory.connect(eventNode.getId(), target, "connection_" + eventNode.getId() + "_" + target, process, false);
                    }
                }
                if (switchState.getTimeouts() != null && switchState.getTimeouts().getEventTimeout() != null) {
                    TimerNode timer = factory.timerNode(ids.getAndIncrement(), "event-switch-timeout", switchState.getTimeouts().getEventTimeout(), process);
                    factory.connect(splitNode.getId(), timer.getId(), "connection_" + splitNode.getId() + "_" + timer.getId(), process, false);
                    EndNode endNode = factory.endNode(ids.getAndIncrement(), "end_" + switchState.getName(), false, process);
                    factory.connect(timer.getId(), endNode.getId(), "connection_" + timer.getId() + "_" + endNode.getId(), process, false);
                }
            }
            // ensure that start node is connected
            if (state.equals(start) && currentNode != null) {
                factory.connect(startNode.getId(), currentNode.getId(), "connection_" + startNode.getId() + "_" + currentNode.getId(), process, false);
            }
        }
    }
    // next connect all nodes
    for (State state : workflow.getStates()) {
        if (!mappedNodes.containsKey(state.getName())) {
            continue;
        }
        long source = mappedNodes.get(state.getName());
        if (state.getTransition() != null && state.getTransition().getNextState() != null) {
            long target = mappedNodes.get(state.getTransition().getNextState());
            if (state.getTransition().getProduceEvents() != null && !state.getTransition().getProduceEvents().isEmpty()) {
                produceEvents(state.getTransition().getProduceEvents(), factory, workflow, ids, process, source, target);
            } else {
                factory.connect(source, target, "connection_" + source + "_" + target, process, false);
            }
        }
    }
    // attach error handling
    for (State state : workflow.getStates()) {
        if (!mappedNodes.containsKey(state.getName())) {
            continue;
        }
        long source = mappedNodes.get(state.getName());
        if (process.getNode(source) instanceof CompositeContextNode) {
            addErrorHandlingToState(workflow, state, factory, ids, process, (CompositeContextNode) process.getNode(source));
        }
    }
    factory.validate((ExecutableProcess) process);
    if (workflow.getTimeouts() != null && workflow.getTimeouts().getWorkflowExecTimeout() != null) {
        factory.addExecutionTimeout(ids.getAndIncrement(), workflow.getTimeouts().getWorkflowExecTimeout(), (ExecutableProcess) process);
    }
    process.setMetaData("SW-Workflow", workflow);
    return process;
}
Also used : Arrays(java.util.Arrays) TimerNode(io.automatiko.engine.workflow.process.core.node.TimerNode) Workflow(io.serverlessworkflow.api.Workflow) CompositeContextNode(io.automatiko.engine.workflow.process.core.node.CompositeContextNode) Map(java.util.Map) CompletionType(io.serverlessworkflow.api.states.ParallelState.CompletionType) EventState(io.serverlessworkflow.api.states.EventState) OnEvents(io.serverlessworkflow.api.events.OnEvents) Process(io.automatiko.engine.api.definition.process.Process) InputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.InputJqAssignmentAction) Branch(io.serverlessworkflow.api.branches.Branch) JqReturnValueEvaluator(io.automatiko.engine.workflow.base.instance.impl.jq.JqReturnValueEvaluator) EventCondition(io.serverlessworkflow.api.switchconditions.EventCondition) Invoke(io.serverlessworkflow.api.functions.SubFlowRef.Invoke) InjectState(io.serverlessworkflow.api.states.InjectState) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) ErrorDefinition(io.serverlessworkflow.api.error.ErrorDefinition) Reader(java.io.Reader) UUID(java.util.UUID) ReturnValueConstraintEvaluator(io.automatiko.engine.workflow.base.instance.impl.ReturnValueConstraintEvaluator) ParallelState(io.serverlessworkflow.api.states.ParallelState) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) State(io.serverlessworkflow.api.interfaces.State) WorkItemNode(io.automatiko.engine.workflow.process.core.node.WorkItemNode) Optional(java.util.Optional) Join(io.automatiko.engine.workflow.process.core.node.Join) WorkflowUtils(io.serverlessworkflow.utils.WorkflowUtils) DefaultState(io.serverlessworkflow.api.states.DefaultState) SubProcessNode(io.automatiko.engine.workflow.process.core.node.SubProcessNode) DataCondition(io.serverlessworkflow.api.switchconditions.DataCondition) Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) Action(io.serverlessworkflow.api.actions.Action) NodeContainer(io.automatiko.engine.workflow.process.core.NodeContainer) Split(io.automatiko.engine.workflow.process.core.node.Split) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ProduceEvent(io.serverlessworkflow.api.produce.ProduceEvent) OutputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.OutputJqAssignmentAction) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) FunctionDefinition(io.serverlessworkflow.api.functions.FunctionDefinition) OperationState(io.serverlessworkflow.api.states.OperationState) BoundaryEventNode(io.automatiko.engine.workflow.process.core.node.BoundaryEventNode) Type(io.serverlessworkflow.api.states.DefaultState.Type) BiConsumer(java.util.function.BiConsumer) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) Connection(io.automatiko.engine.api.definition.process.Connection) Node(io.automatiko.engine.workflow.process.core.Node) CallbackState(io.serverlessworkflow.api.states.CallbackState) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) IOException(java.io.IOException) DateTimeUtils(io.automatiko.engine.workflow.base.core.timer.DateTimeUtils) OnParentComplete(io.serverlessworkflow.api.functions.SubFlowRef.OnParentComplete) EventDefinition(io.serverlessworkflow.api.events.EventDefinition) SleepState(io.serverlessworkflow.api.states.SleepState) RetryDefinition(io.serverlessworkflow.api.retry.RetryDefinition) AtomicLong(java.util.concurrent.atomic.AtomicLong) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) ActionMode(io.serverlessworkflow.api.states.OperationState.ActionMode) SwitchState(io.serverlessworkflow.api.states.SwitchState) Collections(java.util.Collections) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) CompositeContextNode(io.automatiko.engine.workflow.process.core.node.CompositeContextNode) TimerNode(io.automatiko.engine.workflow.process.core.node.TimerNode) CompositeContextNode(io.automatiko.engine.workflow.process.core.node.CompositeContextNode) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) WorkItemNode(io.automatiko.engine.workflow.process.core.node.WorkItemNode) SubProcessNode(io.automatiko.engine.workflow.process.core.node.SubProcessNode) BoundaryEventNode(io.automatiko.engine.workflow.process.core.node.BoundaryEventNode) Node(io.automatiko.engine.workflow.process.core.Node) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) ArrayList(java.util.ArrayList) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) ReturnValueConstraintEvaluator(io.automatiko.engine.workflow.base.instance.impl.ReturnValueConstraintEvaluator) EventDefinition(io.serverlessworkflow.api.events.EventDefinition) LinkedHashMap(java.util.LinkedHashMap) Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) CallbackState(io.serverlessworkflow.api.states.CallbackState) BoundaryEventNode(io.automatiko.engine.workflow.process.core.node.BoundaryEventNode) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) InjectState(io.serverlessworkflow.api.states.InjectState) Branch(io.serverlessworkflow.api.branches.Branch) ParallelState(io.serverlessworkflow.api.states.ParallelState) TimerNode(io.automatiko.engine.workflow.process.core.node.TimerNode) DataCondition(io.serverlessworkflow.api.switchconditions.DataCondition) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) EventState(io.serverlessworkflow.api.states.EventState) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) JqReturnValueEvaluator(io.automatiko.engine.workflow.base.instance.impl.jq.JqReturnValueEvaluator) Connection(io.automatiko.engine.api.definition.process.Connection) Workflow(io.serverlessworkflow.api.Workflow) Join(io.automatiko.engine.workflow.process.core.node.Join) InputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.InputJqAssignmentAction) OutputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.OutputJqAssignmentAction) EventCondition(io.serverlessworkflow.api.switchconditions.EventCondition) OnEvents(io.serverlessworkflow.api.events.OnEvents) SwitchState(io.serverlessworkflow.api.states.SwitchState) AtomicLong(java.util.concurrent.atomic.AtomicLong) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) EventState(io.serverlessworkflow.api.states.EventState) InjectState(io.serverlessworkflow.api.states.InjectState) ParallelState(io.serverlessworkflow.api.states.ParallelState) State(io.serverlessworkflow.api.interfaces.State) DefaultState(io.serverlessworkflow.api.states.DefaultState) OperationState(io.serverlessworkflow.api.states.OperationState) CallbackState(io.serverlessworkflow.api.states.CallbackState) SleepState(io.serverlessworkflow.api.states.SleepState) SwitchState(io.serverlessworkflow.api.states.SwitchState) AtomicLong(java.util.concurrent.atomic.AtomicLong) Split(io.automatiko.engine.workflow.process.core.node.Split) OperationState(io.serverlessworkflow.api.states.OperationState)

Example 4 with EventNode

use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.

the class ServerlessWorkflowFactory method consumeEventNode.

public EventNode consumeEventNode(long id, EventDefinition eventDefinition, EventDataFilter eventDataFilter, NodeContainer nodeContainer) {
    EventNode eventNode = new EventNode();
    eventNode.setId(id);
    eventNode.setName(eventDefinition.getName());
    EventTypeFilter eventFilter = new EventTypeFilter();
    eventFilter.setType("Message-" + eventDefinition.getName());
    eventNode.addEventFilter(eventFilter);
    eventNode.setMetaData(UNIQUE_ID_PARAM, Long.toString(id));
    eventNode.setMetaData(Metadata.TRIGGER_TYPE, "ConsumeMessage");
    eventNode.setMetaData(Metadata.TRIGGER_REF, eventDefinition.getName());
    eventNode.setMetaData(Metadata.EVENT_TYPE, "message");
    eventNode.setMetaData(Metadata.MESSAGE_TYPE, JSON_NODE);
    eventNode.setMetaData(Metadata.TRIGGER_FILTER_EXPR, "hasAttributeWithValue(eventData, \"type\", \"" + eventDefinition.getType() + "\")");
    eventNode.setVariableName(DEFAULT_WORKFLOW_VAR);
    if (eventDefinition.getMetadata() != null) {
        eventDefinition.getMetadata().forEach((k, v) -> eventNode.setMetaData(k, v));
    }
    if (eventDefinition.getCorrelation() != null && !eventDefinition.getCorrelation().isEmpty()) {
        eventNode.setMetaData("TriggerCorrelationExpr", "extensionAttribute(eventData, \"" + eventDefinition.getCorrelation().get(0).getContextAttributeName() + "\")");
    }
    // if (ServerlessWorkflowUtils.correlationExpressionFromSource(eventDefinition.getSource()) != null) {
    // eventNode.setMetaData(Metadata.TRIGGER_CORRELATION_EXPR,
    // ServerlessWorkflowUtils.correlationExpressionFromSource(eventDefinition.getSource()));
    // }
    boolean useData = true;
    String outputFilter = null;
    String scopeFilter = null;
    if (eventDataFilter != null) {
        useData = eventDataFilter.isUseData();
        outputFilter = unwrapExpression(eventDataFilter.getData());
        scopeFilter = unwrapExpression(eventDataFilter.getToStateData());
    }
    if (useData) {
        Assignment outAssignment = new Assignment("jq", null, null);
        outAssignment.setMetaData("Action", new TaskOutputJqAssignmentAction(outputFilter, scopeFilter, true));
        eventNode.addOutAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(outAssignment), null));
    }
    nodeContainer.addNode(eventNode);
    return eventNode;
}
Also used : Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) BoundaryEventNode(io.automatiko.engine.workflow.process.core.node.BoundaryEventNode) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) TaskOutputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.TaskOutputJqAssignmentAction)

Example 5 with EventNode

use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.

the class CompositeNodeInstance method signalEvent.

@Override
public void signalEvent(String type, Object event) {
    List<NodeInstance> currentView = new ArrayList<>(this.nodeInstances);
    super.signalEvent(type, event);
    for (Node node : getCompositeNode().internalGetNodes()) {
        if (node instanceof EventNodeInterface && ((EventNodeInterface) node).acceptsEvent(type, event)) {
            if (node instanceof EventNode && ((EventNode) node).getFrom() == null || node instanceof EventSubProcessNode) {
                EventNodeInstanceInterface eventNodeInstance = (EventNodeInstanceInterface) getNodeInstance(node);
                eventNodeInstance.signalEvent(type, event);
            } else {
                List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
                if (nodeInstances != null && !nodeInstances.isEmpty()) {
                    for (NodeInstance nodeInstance : nodeInstances) {
                        ((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event);
                    }
                }
            }
        }
        if (type.equals(node.getName()) && node.getIncomingConnections().isEmpty()) {
            NodeInstance nodeInstance = getNodeInstance(node);
            if (nodeInstance != null) {
                if (event != null) {
                    Map<String, Object> dynamicParams = new HashMap<>(getProcessInstance().getVariables());
                    if (event instanceof Map) {
                        dynamicParams.putAll((Map<String, Object>) event);
                    } else if (event instanceof WorkflowProcessInstance) {
                    // ignore variables of process instance type
                    } else {
                        dynamicParams.put("Data", event);
                    }
                    nodeInstance.setDynamicParameters(dynamicParams);
                }
                nodeInstance.trigger(null, null);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) EventNodeInterface(io.automatiko.engine.workflow.process.core.node.EventNodeInterface) EventSubProcessNode(io.automatiko.engine.workflow.process.core.node.EventSubProcessNode) StateBasedNode(io.automatiko.engine.workflow.process.core.node.StateBasedNode) CompositeNode(io.automatiko.engine.workflow.process.core.node.CompositeNode) Node(io.automatiko.engine.api.definition.process.Node) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) EventSubProcessNode(io.automatiko.engine.workflow.process.core.node.EventSubProcessNode) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) ArrayList(java.util.ArrayList) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) NodeInstance(io.automatiko.engine.workflow.process.instance.NodeInstance) HashMap(java.util.HashMap) Map(java.util.Map) WorkflowProcessInstance(io.automatiko.engine.workflow.process.instance.WorkflowProcessInstance)

Aggregations

EventNode (io.automatiko.engine.workflow.process.core.node.EventNode)37 BoundaryEventNode (io.automatiko.engine.workflow.process.core.node.BoundaryEventNode)19 EventTypeFilter (io.automatiko.engine.workflow.base.core.event.EventTypeFilter)18 CompositeNode (io.automatiko.engine.workflow.process.core.node.CompositeNode)18 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)18 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)18 ActionNode (io.automatiko.engine.workflow.process.core.node.ActionNode)17 Node (io.automatiko.engine.api.definition.process.Node)16 ProcessAction (io.automatiko.engine.workflow.process.core.ProcessAction)13 ConsequenceAction (io.automatiko.engine.workflow.process.core.impl.ConsequenceAction)13 EventSubProcessNode (io.automatiko.engine.workflow.process.core.node.EventSubProcessNode)13 FaultNode (io.automatiko.engine.workflow.process.core.node.FaultNode)13 StateNode (io.automatiko.engine.workflow.process.core.node.StateNode)13 WorkItemNode (io.automatiko.engine.workflow.process.core.node.WorkItemNode)13 ArrayList (java.util.ArrayList)13 EventFilter (io.automatiko.engine.workflow.base.core.event.EventFilter)11 StateBasedNode (io.automatiko.engine.workflow.process.core.node.StateBasedNode)11 CancelNodeInstanceAction (io.automatiko.engine.workflow.base.instance.impl.actions.CancelNodeInstanceAction)10 HumanTaskNode (io.automatiko.engine.workflow.process.core.node.HumanTaskNode)10 SubProcessNode (io.automatiko.engine.workflow.process.core.node.SubProcessNode)10