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);
}
}
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;
}
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();
}
}
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;
}
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);
}
}
}
}
}
Aggregations