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