Search in sources :

Example 1 with EventTransformer

use of io.automatiko.engine.workflow.base.core.event.EventTransformer 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 2 with EventTransformer

use of io.automatiko.engine.workflow.base.core.event.EventTransformer 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)

Aggregations

DataType (io.automatiko.engine.api.workflow.datatype.DataType)2 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)2 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)2 EventTransformer (io.automatiko.engine.workflow.base.core.event.EventTransformer)2 InternalProcessRuntime (io.automatiko.engine.workflow.base.instance.InternalProcessRuntime)2 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)2 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)2 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)2 Serializable (java.io.Serializable)2 Matcher (java.util.regex.Matcher)2 JobsService (io.automatiko.engine.api.jobs.JobsService)1 EventListener (io.automatiko.engine.api.runtime.process.EventListener)1 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)1 NodeInstanceState (io.automatiko.engine.api.runtime.process.NodeInstanceState)1 BaseEventDescription (io.automatiko.engine.api.workflow.BaseEventDescription)1 EventDescription (io.automatiko.engine.api.workflow.EventDescription)1 NamedDataType (io.automatiko.engine.api.workflow.NamedDataType)1 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)1 TimerInstance (io.automatiko.engine.services.time.TimerInstance)1 ProcessContext (io.automatiko.engine.workflow.base.core.context.ProcessContext)1