Search in sources :

Example 11 with Assignment

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

the class EventNodeInstance method signalEvent.

public void signalEvent(String type, Object event) {
    if ("timerTriggered".equals(type)) {
        TimerInstance timerInstance = (TimerInstance) event;
        if (timerInstance.getId().equals(slaTimerId)) {
            handleSLAViolation();
        }
    } else if (("slaViolation:" + getId()).equals(type)) {
        handleSLAViolation();
    } else {
        String variableName = getEventNode().getVariableName();
        if (!getEventNode().getOutAssociations().isEmpty()) {
            for (DataAssociation association : getEventNode().getOutAssociations()) {
                if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null) {
                        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") && event instanceof String) {
                            event = dataType.readValue((String) event);
                        } else {
                            variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), event);
                        }
                        variableScopeInstance.setVariable(this, association.getTarget(), event);
                    } 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(Collections.singletonMap(association.getSources().get(0), event));
                            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 start node {}", getEventNode().getName());
                            logger.warn("Continuing without setting variable.");
                        }
                    }
                } else {
                    Object data = event;
                    association.getAssignments().stream().forEach(assignment -> handleAssignment(assignment, data));
                }
            }
        } else if (variableName != null) {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, variableName);
            if (variableScopeInstance != null) {
                EventTransformer transformer = getEventNode().getEventTransformer();
                if (transformer != null) {
                    event = transformer.transformEvent(event);
                }
                variableScopeInstance.setVariable(this, variableName, event);
            } else {
                String output = "event";
                Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(variableName);
                if (matcher.find()) {
                    String paramName = matcher.group(1);
                    String expression = VariableUtil.transformDotNotation(paramName, output);
                    NodeInstanceResolverFactory resolver = new NodeInstanceResolverFactory(this);
                    resolver.addExtraParameters(Collections.singletonMap("event", event));
                    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 {}", variableName);
                    logger.warn("when trying to complete start node {}", getEventNode().getName());
                    logger.warn("Continuing without setting variable.");
                }
            }
        }
        triggerCompleted();
    }
}
Also used : VariableUtil(io.automatiko.engine.workflow.base.instance.impl.util.VariableUtil) Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) Date(java.util.Date) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) DataType(io.automatiko.engine.api.workflow.datatype.DataType) HashMap(java.util.HashMap) ProcessContext(io.automatiko.engine.workflow.base.core.context.ProcessContext) NamedDataType(io.automatiko.engine.api.workflow.NamedDataType) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) Matcher(java.util.regex.Matcher) JobsService(io.automatiko.engine.api.jobs.JobsService) BaseEventDescription(io.automatiko.engine.api.workflow.BaseEventDescription) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) Map(java.util.Map) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) EventListener(io.automatiko.engine.api.runtime.process.EventListener) ExtendedNodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.ExtendedNodeInstanceImpl) VARIABLE_SCOPE(io.automatiko.engine.workflow.base.core.context.variable.VariableScope.VARIABLE_SCOPE) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) NodeInstanceState(io.automatiko.engine.api.runtime.process.NodeInstanceState) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) Set(java.util.Set) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) EMPTY_EVENT_LISTENER(io.automatiko.engine.workflow.process.instance.impl.DummyEventListener.EMPTY_EVENT_LISTENER) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) Serializable(java.io.Serializable) TimerInstance(io.automatiko.engine.services.time.TimerInstance) ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) PatternConstants(io.automatiko.engine.workflow.util.PatternConstants) Entry(java.util.Map.Entry) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) EventDescription(io.automatiko.engine.api.workflow.EventDescription) AssignmentAction(io.automatiko.engine.workflow.base.instance.impl.AssignmentAction) WorkflowProcessInstance(io.automatiko.engine.workflow.process.instance.WorkflowProcessInstance) Collections(java.util.Collections) MVEL(org.mvel2.MVEL) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) Serializable(java.io.Serializable) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) TimerInstance(io.automatiko.engine.services.time.TimerInstance) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) Matcher(java.util.regex.Matcher) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) NamedDataType(io.automatiko.engine.api.workflow.NamedDataType)

Example 12 with Assignment

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

the class ServerlessWorkflowFactory method messageStartNode.

public StartNode messageStartNode(long id, EventDefinition eventDefinition, OnEvents onEvents, NodeContainer nodeContainer) {
    StartNode startNode = new StartNode();
    startNode.setId(id);
    startNode.setName(eventDefinition.getName());
    startNode.setMetaData(Metadata.TRIGGER_MAPPING, DEFAULT_WORKFLOW_VAR);
    startNode.setMetaData(Metadata.TRIGGER_TYPE, "ConsumeMessage");
    // if (ServerlessWorkflowUtils.correlationExpressionFromSource(eventDefinition.getSource()) != null) {
    // startNode.setMetaData(Metadata.TRIGGER_CORRELATION_EXPR,
    // ServerlessWorkflowUtils.correlationExpressionFromSource(eventDefinition.getSource()));
    // }
    startNode.setMetaData(Metadata.TRIGGER_REF, eventDefinition.getName());
    startNode.setMetaData(Metadata.MESSAGE_TYPE, JSON_NODE);
    if (eventDefinition.getCorrelation() != null && !eventDefinition.getCorrelation().isEmpty()) {
        startNode.setMetaData("TriggerCorrelationExpr", "extensionAttribute(eventData, \"" + eventDefinition.getCorrelation().get(0).getContextAttributeName() + "\")");
        startNode.setMetaData("acceptStartSignal", "true");
    }
    startNode.setMetaData(Metadata.TRIGGER_FILTER_EXPR, "hasAttributeWithValue(eventData, \"type\", \"" + eventDefinition.getType() + "\")");
    EventTrigger trigger = new EventTrigger();
    EventTypeFilter eventFilter = new EventTypeFilter();
    eventFilter.setType("Message-" + eventDefinition.getName());
    trigger.addEventFilter(eventFilter);
    String mapping = (String) startNode.getMetaData(Metadata.TRIGGER_MAPPING);
    if (mapping != null) {
        trigger.addInMapping(mapping, startNode.getOutMapping(mapping));
    }
    startNode.addTrigger(trigger);
    startNode.setMetaData(UNIQUE_ID_PARAM, Long.toString(id));
    if (eventDefinition.getMetadata() != null) {
        eventDefinition.getMetadata().forEach((k, v) -> startNode.setMetaData(k, v));
    }
    // 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 (onEvents.getEventDataFilter() != null) {
        useData = onEvents.getEventDataFilter().isUseData();
        outputFilter = unwrapExpression(onEvents.getEventDataFilter().getData());
        scopeFilter = unwrapExpression(onEvents.getEventDataFilter().getToStateData());
    }
    if (useData) {
        Assignment outAssignment = new Assignment("jq", null, null);
        outAssignment.setMetaData("Action", new TaskOutputJqAssignmentAction(outputFilter, scopeFilter, true));
        startNode.addOutAssociation(new DataAssociation(Collections.emptyList(), "", Arrays.asList(outAssignment), null));
    }
    nodeContainer.addNode(startNode);
    return startNode;
}
Also used : Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) EventTrigger(io.automatiko.engine.workflow.process.core.node.EventTrigger) TaskOutputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.TaskOutputJqAssignmentAction)

Example 13 with Assignment

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

the class StartNodeVisitor method visitNode.

@Override
public void visitNode(WorkflowProcess process, String factoryField, StartNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
    body.addStatement(getAssignedFactoryMethod(factoryField, StartNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "Start")).addStatement(getFactoryMethod(getNodeId(node), METHOD_INTERRUPTING, new BooleanLiteralExpr(node.isInterrupting())));
    visitMetaData(node.getMetaData(), body, getNodeId(node));
    boolean serverless = ProcessToExecModelGenerator.isServerlessWorkflow(process);
    if (serverless) {
        for (DataAssociation association : node.getOutAssociations()) {
            if (association.getAssignments() != null && !association.getAssignments().isEmpty()) {
                TaskOutputJqAssignmentAction action = (TaskOutputJqAssignmentAction) association.getAssignments().get(0).getMetaData("Action");
                String outputFilter = action.getOutputFilterExpression();
                String scopeFilter = action.getScopeFilter();
                body.addStatement(getFactoryMethod(getNodeId(node), METHOD_OUT_JQ_MAPPING, (outputFilter != null ? new StringLiteralExpr().setString(outputFilter) : new NullLiteralExpr()), (scopeFilter != null ? new StringLiteralExpr().setString(scopeFilter) : new NullLiteralExpr()), new BooleanLiteralExpr(action.isIgnoreScopeFilter())));
            }
        }
    } else {
        for (DataAssociation entry : node.getOutAssociations()) {
            if (entry.getAssignments() != null && !entry.getAssignments().isEmpty()) {
                Assignment assignment = entry.getAssignments().get(0);
                body.addStatement(getFactoryMethod(getNodeId(node), "outMapping", new StringLiteralExpr(entry.getSources().get(0)), new NullLiteralExpr(), new StringLiteralExpr(assignment.getDialect()), new StringLiteralExpr(assignment.getFrom()), new StringLiteralExpr(assignment.getTo())));
            } else {
                body.addStatement(getFactoryMethod(getNodeId(node), "outMapping", new StringLiteralExpr(entry.getSources().get(0)), new StringLiteralExpr(entry.getTarget()), new NullLiteralExpr(), new NullLiteralExpr(), new NullLiteralExpr()));
            }
        }
    }
    body.addStatement(getDoneMethod(getNodeId(node)));
    if (node.getTimer() != null) {
        Timer timer = node.getTimer();
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_TIMER, getOrNullExpr(timer.getDelay()), getOrNullExpr(timer.getPeriod()), getOrNullExpr(timer.getDate()), new IntegerLiteralExpr(node.getTimer().getTimeType())));
    } else if (node.getTriggers() != null && !node.getTriggers().isEmpty()) {
        Map<String, Object> nodeMetaData = node.getMetaData();
        TriggerMetaData trigger = new TriggerMetaData((String) nodeMetaData.get(TRIGGER_REF), (String) nodeMetaData.get(TRIGGER_TYPE), (String) nodeMetaData.get(MESSAGE_TYPE), (String) nodeMetaData.get(TRIGGER_MAPPING), String.valueOf(node.getId()), node.getName(), (String) nodeMetaData.get(TRIGGER_CORRELATION), (String) nodeMetaData.get(TRIGGER_CORRELATION_EXPR)).validate();
        trigger.addContext(node.getMetaData());
        trigger.addContext(Collections.singletonMap("_node_", node));
        // mark the trigger as capable of starting new instance only if this is top level start node
        if (node.getParentContainer() instanceof WorkflowProcess) {
            trigger.setStart(true);
        }
        metadata.addTrigger(trigger);
        handleTrigger(node, nodeMetaData, body, variableScope, metadata);
    } else {
        // since there is start node without trigger then make sure it is startable
        metadata.setStartable(true);
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) TaskOutputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.TaskOutputJqAssignmentAction) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) StartNodeFactory(io.automatiko.engine.workflow.process.executable.core.factory.StartNodeFactory) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) Map(java.util.Map) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess)

Example 14 with Assignment

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

the class ScriptTaskHandler method readDataInputAssociation.

protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, ActionNode actionNode, Map<String, String> dataInputs) {
    // sourceRef
    org.w3c.dom.Node subNode = xmlNode.getFirstChild();
    if ("sourceRef".equals(subNode.getNodeName())) {
        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.addInAssociation(new DataAssociation(sources, dataInputs.get(target), assignments, transformation));
    }
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer)

Example 15 with Assignment

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

the class TaskHandler method readDataInputAssociation.

protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, WorkItemNode workItemNode, Map<String, String> dataInputs) {
    // sourceRef
    org.w3c.dom.Node subNode = xmlNode.getFirstChild();
    if ("sourceRef".equals(subNode.getNodeName())) {
        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);
            // transformation.setCompiledExpression(transformer.compile(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();
        }
        workItemNode.addInAssociation(new DataAssociation(sources, dataInputs.get(target), assignments, transformation));
    } else {
        // targetRef
        String to = subNode.getTextContent();
        // assignment
        subNode = subNode.getNextSibling();
        if (subNode != null) {
            org.w3c.dom.Node subSubNode = subNode.getFirstChild();
            NodeList nl = subSubNode.getChildNodes();
            if (nl.getLength() > 1) {
                // not supported ?
                workItemNode.getWork().setParameter(dataInputs.get(to), subSubNode.getTextContent());
                return;
            } else if (nl.getLength() == 0) {
                return;
            }
            Object result = null;
            Object from = nl.item(0);
            if (from instanceof Text) {
                String text = ((Text) from).getTextContent();
                if (text.startsWith("\"") && text.endsWith("\"")) {
                    result = text.substring(1, text.length() - 1);
                } else {
                    result = text;
                }
            } else {
                result = nl.item(0);
            }
            workItemNode.getWork().setParameter(dataInputs.get(to), result);
        }
    }
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) Text(org.w3c.dom.Text) LinkedList(java.util.LinkedList) Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer)

Aggregations

Assignment (io.automatiko.engine.workflow.process.core.node.Assignment)20 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)20 ArrayList (java.util.ArrayList)9 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)8 Transformation (io.automatiko.engine.workflow.process.core.node.Transformation)8 Map (java.util.Map)8 LinkedList (java.util.LinkedList)6 Element (org.w3c.dom.Element)6 DataTransformerRegistry (io.automatiko.engine.workflow.base.core.impl.DataTransformerRegistry)5 TaskOutputJqAssignmentAction (io.automatiko.engine.workflow.base.instance.impl.jq.TaskOutputJqAssignmentAction)5 List (java.util.List)5 Node (io.automatiko.engine.workflow.process.core.Node)4 NodeContainer (io.automatiko.engine.workflow.process.core.NodeContainer)4 HashMap (java.util.HashMap)4 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)3 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)3 ProcessContext (io.automatiko.engine.workflow.base.core.context.ProcessContext)3 VARIABLE_SCOPE (io.automatiko.engine.workflow.base.core.context.variable.VariableScope.VARIABLE_SCOPE)3 ProcessInstance (io.automatiko.engine.workflow.base.instance.ProcessInstance)3 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)3