use of io.automatiko.engine.workflow.process.core.node.Trigger in project automatiko-engine by automatiko-io.
the class ProcessHandler method postProcessNodes.
private void postProcessNodes(ExecutableProcess process, NodeContainer container) {
List<String> eventSubProcessHandlers = new ArrayList<String>();
for (Node node : container.getNodes()) {
if (node instanceof StartNode) {
List<DataAssociation> associations = ((StartNode) node).getOutAssociations();
if (associations != null) {
for (DataAssociation da : associations) {
VariableScope scope = (VariableScope) process.getDefaultContext(VariableScope.VARIABLE_SCOPE);
Variable variable = scope.findVariable(da.getTarget());
if (variable != null) {
da.setTarget(variable.getName());
}
}
}
} else if (node instanceof StateNode) {
StateNode stateNode = (StateNode) node;
String condition = (String) stateNode.getMetaData("Condition");
stateNode.setCondition(context -> {
return (boolean) MVEL.executeExpression(condition, context.getProcessInstance().getVariables());
});
} else if (node instanceof NodeContainer) {
// prepare event sub process
if (node instanceof EventSubProcessNode) {
EventSubProcessNode eventSubProcessNode = (EventSubProcessNode) node;
Node[] nodes = eventSubProcessNode.getNodes();
for (Node subNode : nodes) {
// avoids cyclomatic complexity
if (subNode == null || !(subNode instanceof StartNode)) {
continue;
}
List<Trigger> triggers = ((StartNode) subNode).getTriggers();
if (triggers == null) {
continue;
}
for (Trigger trigger : triggers) {
if (trigger instanceof EventTrigger) {
final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
for (EventFilter filter : filters) {
if (filter instanceof EventTypeFilter) {
eventSubProcessNode.addEvent((EventTypeFilter) filter);
String type = ((EventTypeFilter) filter).getType();
if (type.startsWith("Error-") || type.startsWith("Escalation")) {
String faultCode = (String) subNode.getMetaData().get("FaultCode");
String replaceRegExp = "Error-|Escalation-";
final String signalType = type;
ExceptionScope exceptionScope = (ExceptionScope) ((ContextContainer) eventSubProcessNode.getParentContainer()).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
if (exceptionScope == null) {
exceptionScope = new ExceptionScope();
((ContextContainer) eventSubProcessNode.getParentContainer()).addContext(exceptionScope);
((ContextContainer) eventSubProcessNode.getParentContainer()).setDefaultContext(exceptionScope);
}
String faultVariable = null;
if (trigger.getInAssociations() != null && !trigger.getInAssociations().isEmpty()) {
faultVariable = findVariable(trigger.getInAssociations().get(0).getSources().get(0), process.getVariableScope());
}
ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
ConsequenceAction action = new ConsequenceAction("java", "");
action.setMetaData("Action", new SignalProcessInstanceAction(signalType, faultVariable, SignalProcessInstanceAction.PROCESS_INSTANCE_SCOPE));
exceptionHandler.setAction(action);
exceptionHandler.setFaultVariable(faultVariable);
exceptionHandler.setRetryAfter((Integer) subNode.getMetaData().get("ErrorRetry"));
exceptionHandler.setRetryIncrement((Integer) subNode.getMetaData().get("ErrorRetryIncrement"));
if (subNode.getMetaData().get("ErrorRetryIncrementMultiplier") != null) {
exceptionHandler.setRetryIncrementMultiplier(((Number) subNode.getMetaData().get("ErrorRetryIncrementMultiplier")).floatValue());
}
exceptionHandler.setRetryLimit((Integer) subNode.getMetaData().get("ErrorRetryLimit"));
if (faultCode != null) {
String trimmedType = type.replaceFirst(replaceRegExp, "");
for (String error : trimmedType.split(",")) {
exceptionScope.setExceptionHandler(error, exceptionHandler);
eventSubProcessHandlers.add(error);
}
} else {
exceptionScope.setExceptionHandler(faultCode, exceptionHandler);
}
} else if (type.equals("Compensation")) {
// 1. Find the parent sub-process to this event sub-process
NodeContainer parentSubProcess;
NodeContainer subProcess = eventSubProcessNode.getParentContainer();
Object isForCompensationObj = eventSubProcessNode.getMetaData("isForCompensation");
if (isForCompensationObj == null) {
eventSubProcessNode.setMetaData("isForCompensation", true);
logger.warn("Overriding empty or false value of \"isForCompensation\" attribute on Event Sub-Process [" + eventSubProcessNode.getMetaData("UniqueId") + "] and setting it to true.");
}
if (subProcess instanceof ExecutableProcess) {
// (instance)?!?
throw new IllegalArgumentException("Compensation Event Sub-Processes at the process level are not supported.");
}
parentSubProcess = ((Node) subProcess).getParentContainer();
// 2. The event filter (never fires, purely for dumping purposes) has
// already been added
// 3. Add compensation scope
String compensationHandlerId = (String) ((CompositeNode) subProcess).getMetaData("UniqueId");
addCompensationScope(process, eventSubProcessNode, parentSubProcess, compensationHandlerId);
}
}
}
} else if (trigger instanceof ConstraintTrigger) {
ConstraintTrigger constraintTrigger = (ConstraintTrigger) trigger;
if (constraintTrigger.getConstraint() != null) {
String processId = ((ExecutableProcess) container).getId();
String type = "RuleFlowStateEventSubProcess-Event-" + processId + "-" + eventSubProcessNode.getUniqueId();
EventTypeFilter eventTypeFilter = new EventTypeFilter();
eventTypeFilter.setType(type);
eventSubProcessNode.addEvent(eventTypeFilter);
eventSubProcessNode.addEvent("variableChanged");
((StartNode) subNode).setCondition(context -> {
return (boolean) MVEL.executeExpression(constraintTrigger.getConstraint(), context.getProcessInstance().getVariables());
});
}
}
}
}
// for( Node subNode : nodes)
}
postProcessNodes(process, (NodeContainer) node);
} else if (node instanceof EndNode) {
handleIntermediateOrEndThrowCompensationEvent((EndNode) node);
} else if (node instanceof ActionNode) {
handleIntermediateOrEndThrowCompensationEvent((ActionNode) node);
} else if (node instanceof EventNode) {
final EventNode eventNode = (EventNode) node;
if (!(eventNode instanceof BoundaryEventNode) && eventNode.getDefaultIncomingConnections().size() == 0) {
throw new IllegalArgumentException("Event node '" + node.getName() + "' [" + node.getId() + "] has no incoming connection");
}
}
}
// handler
for (Node node : container.getNodes()) {
if (node instanceof FaultNode) {
FaultNode faultNode = (FaultNode) node;
if (eventSubProcessHandlers.contains(faultNode.getFaultName())) {
faultNode.setTerminateParent(false);
}
}
}
}
use of io.automatiko.engine.workflow.process.core.node.Trigger in project automatiko-engine by automatiko-io.
the class StartEventHandler method writeNode.
@Override
public void writeNode(Node node, StringBuilder xmlDump, int metaDataType) {
StartNode startNode = (StartNode) node;
writeNode("startEvent", startNode, xmlDump, metaDataType);
xmlDump.append(" isInterrupting=\"");
if (startNode.isInterrupting()) {
xmlDump.append("true");
} else {
xmlDump.append("false");
}
xmlDump.append("\">" + EOL);
writeExtensionElements(startNode, xmlDump);
List<Trigger> triggers = startNode.getTriggers();
if (triggers != null) {
if (triggers.size() > 1) {
throw new IllegalArgumentException("Multiple start triggers not supported");
}
Trigger trigger = triggers.get(0);
if (trigger instanceof ConstraintTrigger) {
ConstraintTrigger constraintTrigger = (ConstraintTrigger) trigger;
if (constraintTrigger.getHeader() == null) {
xmlDump.append(" <conditionalEventDefinition>" + EOL);
xmlDump.append(" <condition xsi:type=\"tFormalExpression\" language=\"" + XmlBPMNProcessDumper.RULE_LANGUAGE + "\">" + constraintTrigger.getConstraint() + "</condition>" + EOL);
xmlDump.append(" </conditionalEventDefinition>" + EOL);
}
} else if (trigger instanceof EventTrigger) {
EventTrigger eventTrigger = (EventTrigger) trigger;
String mapping = null;
String nameMapping = "event";
if (!trigger.getInMappings().isEmpty()) {
mapping = eventTrigger.getInMappings().keySet().iterator().next();
nameMapping = eventTrigger.getInMappings().values().iterator().next();
} else {
mapping = (String) startNode.getMetaData("TriggerMapping");
}
if (mapping != null) {
xmlDump.append(" <dataOutput id=\"_" + startNode.getId() + "_Output\" name=\"" + nameMapping + "\" />" + EOL + " <dataOutputAssociation>" + EOL + " <sourceRef>_" + startNode.getId() + "_Output</sourceRef>" + EOL + " <targetRef>" + mapping + "</targetRef>" + EOL + " </dataOutputAssociation>" + EOL);
}
String type = ((EventTypeFilter) eventTrigger.getEventFilters().get(0)).getType();
if (type.startsWith("Message-")) {
type = type.substring(8);
xmlDump.append(" <messageEventDefinition messageRef=\"" + type + "\"/>" + EOL);
} else if (type.startsWith("Error-")) {
type = type.substring(6);
String errorId = getErrorIdForErrorCode(type, startNode);
xmlDump.append(" <errorEventDefinition errorRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(errorId) + "\"/>" + EOL);
} else if (type.startsWith("Escalation-")) {
type = type.substring(11);
xmlDump.append(" <escalationEventDefinition escalationRef=\"" + type + "\"/>" + EOL);
} else if (type.equals("Compensation")) {
xmlDump.append(" <compensateEventDefinition/>" + EOL);
} else {
xmlDump.append(" <signalEventDefinition signalRef=\"" + type + "\" />" + EOL);
}
} else {
throw new IllegalArgumentException("Unsupported trigger type " + trigger);
}
if (startNode.getTimer() != null) {
Timer timer = startNode.getTimer();
xmlDump.append(" <timerEventDefinition>" + EOL);
if (timer != null && (timer.getDelay() != null || timer.getDate() != null)) {
if (timer.getTimeType() == Timer.TIME_DATE) {
xmlDump.append(" <timeDate xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(timer.getDate()) + "</timeDate>" + EOL);
} else if (timer.getTimeType() == Timer.TIME_DURATION) {
xmlDump.append(" <timeDuration xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(timer.getDelay()) + "</timeDuration>" + EOL);
} else if (timer.getTimeType() == Timer.TIME_CYCLE) {
if (timer.getPeriod() != null) {
xmlDump.append(" <timeCycle xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(timer.getDelay()) + "###" + XmlDumper.replaceIllegalChars(timer.getPeriod()) + "</timeCycle>" + EOL);
} else {
xmlDump.append(" <timeCycle xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(timer.getDelay()) + "</timeCycle>" + EOL);
}
}
}
xmlDump.append(" </timerEventDefinition>" + EOL);
}
} else if (startNode.getTimer() != null) {
Timer timer = startNode.getTimer();
xmlDump.append(" <timerEventDefinition>" + EOL);
if (timer != null && (timer.getDelay() != null || timer.getDate() != null)) {
if (timer.getTimeType() == Timer.TIME_DATE) {
xmlDump.append(" <timeDate xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(timer.getDate()) + "</timeDate>" + EOL);
} else if (timer.getTimeType() == Timer.TIME_DURATION) {
xmlDump.append(" <timeDuration xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(timer.getDelay()) + "</timeDuration>" + EOL);
} else if (timer.getTimeType() == Timer.TIME_CYCLE) {
if (timer.getPeriod() != null) {
xmlDump.append(" <timeCycle xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(timer.getDelay()) + "###" + XmlDumper.replaceIllegalChars(timer.getPeriod()) + "</timeCycle>" + EOL);
} else {
xmlDump.append(" <timeCycle xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(timer.getDelay()) + "</timeCycle>" + EOL);
}
}
}
xmlDump.append(" </timerEventDefinition>" + EOL);
}
endNode("startEvent", xmlDump);
}
use of io.automatiko.engine.workflow.process.core.node.Trigger in project automatiko-engine by automatiko-io.
the class StartEventHandler method handleCompensationNode.
protected void handleCompensationNode(final StartNode startNode, final org.w3c.dom.Node xmlNode) {
if (startNode.isInterrupting()) {
logger.warn("Compensation Event Sub-Processes [" + startNode.getMetaData("UniqueId") + "] may not be specified as interrupting:" + " overriding attribute and setting to not-interrupting.");
}
startNode.setInterrupting(false);
/**
* From the BPMN2 spec, P.264: "For a Start Event: This Event "catches" the
* compensation for an Event Sub-Process. No further information is required.
* The Event Sub-Process will provide the id necessary to match the Compensation
* Event with the Event that threw the compensation"
*
* In other words, the id of the Sub-Process containing this Event Sub-Process
* is what should be used as the activityRef value in any Intermediate (throw)
* or End compensation event that targets this particular Event Sub-Process.
*
* This is similar to the logic used for a Compensation Boundary Event: it's
* signaled using the id of the activity to which the CBE is attached to.
*/
String activityRef = ((Element) xmlNode).getAttribute("activityRef");
if (activityRef != null && activityRef.length() > 0) {
logger.warn("activityRef value [" + activityRef + "] on Start Event '" + startNode.getMetaData("UniqueId") + "' ignored per the BPMN2 specification.");
}
// so that this node will get processed in ProcessHandler.postProcessNodes(...)
EventTrigger startTrigger = new EventTrigger();
EventFilter eventFilter = new NonAcceptingEventTypeFilter();
((NonAcceptingEventTypeFilter) eventFilter).setType("Compensation");
startTrigger.addEventFilter(eventFilter);
List<Trigger> startTriggers = new ArrayList<>();
startTriggers.add(startTrigger);
startNode.setTriggers(startTriggers);
String mapping = (String) startNode.getMetaData("TriggerMapping");
if (mapping != null) {
startTrigger.addInMapping(mapping, startNode.getOutMapping(mapping));
}
}
use of io.automatiko.engine.workflow.process.core.node.Trigger in project automatiko-engine by automatiko-io.
the class XmlBPMNProcessDumper method visitInterfaces.
protected void visitInterfaces(Node[] nodes, StringBuilder xmlDump) {
for (Node node : nodes) {
if (node instanceof WorkItemNode) {
Work work = ((WorkItemNode) node).getWork();
if (work != null) {
if ("Service Task".equals(work.getName())) {
String interfaceName = (String) work.getParameter("Interface");
if (interfaceName == null) {
interfaceName = "";
}
String interfaceRef = (String) work.getParameter("interfaceImplementationRef");
if (interfaceRef == null) {
interfaceRef = "";
}
String operationName = (String) work.getParameter("Operation");
if (operationName == null) {
operationName = "";
}
String operationRef = (String) work.getParameter("operationImplementationRef");
if (operationRef == null) {
operationRef = "";
}
String parameterType = (String) work.getParameter("ParameterType");
if (parameterType == null) {
parameterType = "";
}
xmlDump.append(" <itemDefinition id=\"" + getUniqueNodeId(node) + "_InMessageType\" " + ("".equals(parameterType) || "java.lang.Object".equals(parameterType) ? "" : "structureRef=\"" + parameterType + "\" ") + "/>" + EOL + " <message id=\"" + getUniqueNodeId(node) + "_InMessage\" itemRef=\"" + getUniqueNodeId(node) + "_InMessageType\" />" + EOL + " <interface id=\"" + getUniqueNodeId(node) + "_ServiceInterface\" name=\"" + interfaceName + "\" implementationRef=\"" + interfaceRef + "\" >" + EOL + " <operation id=\"" + getUniqueNodeId(node) + "_ServiceOperation\" name=\"" + operationName + "\" implementationRef=\"" + operationRef + "\" >" + EOL + " <inMessageRef>" + getUniqueNodeId(node) + "_InMessage</inMessageRef>" + EOL + " </operation>" + EOL + " </interface>" + EOL + EOL);
} else if ("Send Task".equals(work.getName())) {
String messageType = (String) work.getParameter("MessageType");
if (messageType == null) {
messageType = "";
}
xmlDump.append(" <itemDefinition id=\"" + getUniqueNodeId(node) + "_MessageType\" " + ("".equals(messageType) || "java.lang.Object".equals(messageType) ? "" : "structureRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageType) + "\" ") + "/>" + EOL + " <message id=\"" + getUniqueNodeId(node) + "_Message\" itemRef=\"" + getUniqueNodeId(node) + "_MessageType\" />" + EOL + EOL);
} else if ("Receive Task".equals(work.getName())) {
String messageId = (String) work.getParameter("MessageId");
String messageType = (String) work.getParameter("MessageType");
if (messageType == null) {
messageType = "";
}
xmlDump.append(" <itemDefinition id=\"" + getUniqueNodeId(node) + "_MessageType\" " + ("".equals(messageType) || "java.lang.Object".equals(messageType) ? "" : "structureRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageType) + "\" ") + "/>" + EOL + " <message id=\"" + messageId + "\" itemRef=\"" + getUniqueNodeId(node) + "_MessageType\" />" + EOL + EOL);
}
}
} else if (node instanceof EndNode) {
String messageType = (String) node.getMetaData().get("MessageType");
if (messageType != null) {
xmlDump.append(" <itemDefinition id=\"" + getUniqueNodeId(node) + "_MessageType\" " + ("".equals(messageType) || "java.lang.Object".equals(messageType) ? "" : "structureRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageType) + "\" ") + "/>" + EOL + " <message id=\"" + getUniqueNodeId(node) + "_Message\" itemRef=\"" + getUniqueNodeId(node) + "_MessageType\" />" + EOL + EOL);
}
} else if (node instanceof ActionNode) {
String messageType = (String) node.getMetaData().get("MessageType");
if (messageType != null) {
xmlDump.append(" <itemDefinition id=\"" + getUniqueNodeId(node) + "_MessageType\" " + ("".equals(messageType) || "java.lang.Object".equals(messageType) ? "" : "structureRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageType) + "\" ") + "/>" + EOL + " <message id=\"" + getUniqueNodeId(node) + "_Message\" itemRef=\"" + getUniqueNodeId(node) + "_MessageType\" />" + EOL + EOL);
}
} else if (node instanceof EventNode) {
List<EventFilter> filters = ((EventNode) node).getEventFilters();
if (filters.size() > 0) {
String messageRef = ((EventTypeFilter) filters.get(0)).getType();
if (messageRef.startsWith("Message-")) {
messageRef = messageRef.substring(8);
String messageType = (String) node.getMetaData().get("MessageType");
xmlDump.append(" <itemDefinition id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageRef) + "Type\" " + ("".equals(messageType) || "java.lang.Object".equals(messageType) ? "" : "structureRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageType) + "\" ") + "/>" + EOL + " <message id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageRef) + "\" itemRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageRef) + "Type\" />" + EOL + EOL);
}
}
} else if (node instanceof StartNode) {
StartNode startNode = (StartNode) node;
if (startNode.getTriggers() != null && !startNode.getTriggers().isEmpty()) {
Trigger trigger = startNode.getTriggers().get(0);
if (trigger instanceof EventTrigger) {
String eventType = ((EventTypeFilter) ((EventTrigger) trigger).getEventFilters().get(0)).getType();
if (eventType.startsWith("Message-")) {
eventType = eventType.substring(8);
String messageType = (String) node.getMetaData().get("MessageType");
xmlDump.append(" <itemDefinition id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(eventType) + "Type\" " + ("".equals(messageType) || "java.lang.Object".equals(messageType) ? "" : "structureRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(messageType) + "\" ") + "/>" + EOL + " <message id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(eventType) + "\" itemRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(eventType) + "Type\" />" + EOL + EOL);
}
}
}
} else if (node instanceof ForEachNode) {
ForEachNode forEachNode = (ForEachNode) node;
String type = null;
if (forEachNode.getVariableType() instanceof ObjectDataType) {
type = ((ObjectDataType) forEachNode.getVariableType()).getClassName();
}
xmlDump.append(" <itemDefinition id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(forEachNode) + "_multiInstanceItemType\" " + (type == null || "java.lang.Object".equals(type) ? "" : "structureRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\" ") + "/>" + EOL + EOL);
}
if (node instanceof CompositeNode) {
visitInterfaces(((CompositeNode) node).getNodes(), xmlDump);
}
}
}
use of io.automatiko.engine.workflow.process.core.node.Trigger in project automatiko-engine by automatiko-io.
the class ProcessHandler method handleIntermediateOrEndThrowCompensationEvent.
protected void handleIntermediateOrEndThrowCompensationEvent(ExtendedNodeImpl throwEventNode) {
if (throwEventNode.getMetaData("compensation-activityRef") != null) {
String activityRef = (String) throwEventNode.getMetaData().remove("compensation-activityRef");
NodeContainer nodeParent = (NodeContainer) throwEventNode.getParentContainer();
if (nodeParent instanceof EventSubProcessNode) {
boolean compensationEventSubProcess = false;
List<Trigger> startTriggers = ((EventSubProcessNode) nodeParent).findStartNode().getTriggers();
CESP_CHECK: for (Trigger trigger : startTriggers) {
if (trigger instanceof EventTrigger) {
for (EventFilter filter : ((EventTrigger) trigger).getEventFilters()) {
if (((EventTypeFilter) filter).getType().equals("Compensation")) {
compensationEventSubProcess = true;
break CESP_CHECK;
}
}
}
}
if (compensationEventSubProcess) {
// BPMN2 spec, p. 252, p. 248: intermediate and end compensation event
// visibility scope
nodeParent = (NodeContainer) ((NodeImpl) nodeParent).getParentContainer();
}
}
String parentId;
if (nodeParent instanceof ExecutableProcess) {
parentId = ((ExecutableProcess) nodeParent).getId();
} else {
parentId = (String) ((NodeImpl) nodeParent).getMetaData("UniqueId");
}
String compensationEvent;
if (activityRef.length() == 0) {
// general/implicit compensation
compensationEvent = CompensationScope.IMPLICIT_COMPENSATION_PREFIX + parentId;
} else {
// specific compensation
compensationEvent = activityRef;
}
throwEventNode.setMetaData("CompensationEvent", compensationEvent);
ConsequenceAction compensationAction = new ConsequenceAction("java", "");
compensationAction.setMetaData("Action", new ProcessInstanceCompensationAction(compensationEvent));
if (throwEventNode instanceof ActionNode) {
((ActionNode) throwEventNode).setAction(compensationAction);
} else if (throwEventNode instanceof EndNode) {
List<ProcessAction> actions = new ArrayList<ProcessAction>();
actions.add(compensationAction);
((EndNode) throwEventNode).setActions(EndNode.EVENT_NODE_ENTER, actions);
}
throwEventNode.setMetaData("TriggerType", "Compensation");
}
}
Aggregations