Search in sources :

Example 16 with DataAssociation

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

the class StartNodeInstance method signalEvent.

public void signalEvent(String type, Object event) {
    boolean hidden = false;
    if (getNode().getMetaData().get(HIDDEN) != null) {
        hidden = true;
    }
    InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
    if (!hidden) {
        runtime.getProcessEventSupport().fireBeforeNodeTriggered(this, runtime);
    }
    if (event != null) {
        String variableName = (String) getStartNode().getMetaData("TriggerMapping");
        if (!getStartNode().getOutAssociations().isEmpty()) {
            for (DataAssociation association : getStartNode().getOutAssociations()) {
                // } else
                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 {}", getStartNode().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) {
                    throw new IllegalArgumentException("Could not find variable for start node: " + variableName);
                }
                EventTransformer transformer = getStartNode().getEventTransformer();
                if (transformer != null) {
                    event = transformer.transformEvent(event);
                }
                variableScopeInstance.setVariable(this, variableName, event);
            }
        }
    }
    VariableScope variableScope = (VariableScope) ((ContextContainer) getProcessInstance().getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE);
    for (Variable var : variableScope.getVariables()) {
        if (var.getMetaData(Variable.DEFAULT_VALUE) != null && variableScopeInstance.getVariable(var.getName()) == null) {
            Object value = runtime.getVariableInitializer().initialize(getProcessInstance().getProcess(), var, variableScopeInstance.getVariables());
            variableScope.validateVariable(getProcessInstance().getProcess().getName(), var.getName(), value);
            variableScopeInstance.setVariable(var.getName(), value);
        }
    }
    triggerCompleted();
    if (!hidden) {
        runtime.getProcessEventSupport().fireAfterNodeTriggered(this, runtime);
    }
}
Also used : Serializable(java.io.Serializable) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) Matcher(java.util.regex.Matcher) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 17 with DataAssociation

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

the class RuleSetNodeInstance method evaluateParameters.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Map<String, Object> evaluateParameters(RuleSetNode ruleSetNode) {
    Map<String, Object> replacements = new HashMap<>();
    for (Iterator<DataAssociation> iterator = ruleSetNode.getInAssociations().iterator(); iterator.hasNext(); ) {
        DataAssociation association = iterator.next();
        if (association.getTransformation() != null) {
            Transformation transformation = association.getTransformation();
            DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
            if (transformer != null) {
                Object parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(association));
                replacements.put(association.getTarget(), parameterValue);
            }
        } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
            Object parameterValue = null;
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getSources().get(0));
            if (variableScopeInstance != null) {
                parameterValue = variableScopeInstance.getVariable(association.getSources().get(0));
            } else {
                try {
                    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    parameterValue = evaluator.evaluate(association.getSources().get(0), new NodeInstanceResolverFactory(this));
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", association.getSources().get(0));
                    logger.error("when trying to execute RuleSetNode {}", ruleSetNode.getName());
                    logger.error("Continuing without setting parameter.");
                }
            }
            replacements.put(association.getTarget(), parameterValue);
        }
    }
    for (Map.Entry<String, Object> entry : ruleSetNode.getParameters().entrySet()) {
        if (entry.getValue() instanceof String) {
            Object value = resolveVariable(entry.getValue());
            replacements.put(entry.getKey(), value);
        }
    }
    return replacements;
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) HashMap(java.util.HashMap) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with DataAssociation

use of io.automatiko.engine.workflow.process.core.node.DataAssociation 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 19 with DataAssociation

use of io.automatiko.engine.workflow.process.core.node.DataAssociation 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 20 with DataAssociation

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

the class ActionNodeInstance method setOutputVariable.

public void setOutputVariable(Object variable) {
    List<DataAssociation> outputs = getActionNode().getOutAssociations();
    if (outputs != null && !outputs.isEmpty()) {
        for (DataAssociation output : outputs) {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, getActionNode().getOutAssociations().get(0).getTarget());
            if (variableScopeInstance != null) {
                if (output.getTransformation() != null) {
                    Transformation transformation = output.getTransformation();
                    DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                    if (transformer != null) {
                        final Object currentValue = variable;
                        Map<String, Object> dataSet = new HashMap<String, Object>();
                        for (String source : output.getSources()) {
                            dataSet.put(source, currentValue);
                        }
                        variable = transformer.transform(transformation.getCompiledExpression(), dataSet);
                    }
                }
                Variable var = variableScopeInstance.getVariableScope().getVariables().stream().filter(v -> v.getId().equals(output.getTarget())).findFirst().orElse(null);
                if (var != null) {
                    variableScopeInstance.setVariable(var.getName(), variable);
                } else {
                    variableScopeInstance.setVariable(getActionNode().getOutAssociations().get(0).getTarget(), variable);
                }
            }
        }
    }
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) HashMap(java.util.HashMap)

Aggregations

DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)33 Assignment (io.automatiko.engine.workflow.process.core.node.Assignment)20 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)14 Transformation (io.automatiko.engine.workflow.process.core.node.Transformation)14 Map (java.util.Map)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)10 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)9 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)7 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)7 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)7 Matcher (java.util.regex.Matcher)7 LinkedList (java.util.LinkedList)6 List (java.util.List)6 Element (org.w3c.dom.Element)6 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 DataType (io.automatiko.engine.api.workflow.datatype.DataType)4 DataTransformerRegistry (io.automatiko.engine.workflow.base.core.impl.DataTransformerRegistry)4