use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.
the class BoundaryEventHandler method writeNode.
public void writeNode(Node node, StringBuilder xmlDump, int metaDataType) {
EventNode eventNode = (EventNode) node;
String attachedTo = (String) eventNode.getMetaData("AttachedTo");
if (attachedTo != null) {
String type = ((EventTypeFilter) eventNode.getEventFilters().get(0)).getType();
if (type.startsWith("Escalation-")) {
type = type.substring(attachedTo.length() + 12);
boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
if (!cancelActivity) {
xmlDump.append("cancelActivity=\"false\" ");
}
xmlDump.append(">" + EOL);
writeExtensionElements(node, xmlDump);
xmlDump.append(" <escalationEventDefinition escalationRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\" />" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (type.startsWith("Error-")) {
type = type.substring(attachedTo.length() + 7);
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
xmlDump.append(">" + EOL);
writeExtensionElements(node, xmlDump);
writeVariableName(eventNode, xmlDump);
String errorId = getErrorIdForErrorCode(type, eventNode);
xmlDump.append(" <errorEventDefinition errorRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(errorId) + "\" ");
xmlDump.append("/>" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (type.startsWith("Timer-")) {
type = type.substring(attachedTo.length() + 7);
boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
if (!cancelActivity) {
xmlDump.append("cancelActivity=\"false\" ");
}
xmlDump.append(">" + EOL);
writeExtensionElements(node, xmlDump);
String duration = (String) eventNode.getMetaData("TimeDuration");
String cycle = (String) eventNode.getMetaData("TimeCycle");
String date = (String) eventNode.getMetaData("TimeDate");
if (duration != null && cycle != null) {
String lang = (String) eventNode.getMetaData("Language");
String language = "";
if (lang != null && !lang.isEmpty()) {
language = "language=\"" + lang + "\" ";
}
xmlDump.append(" <timerEventDefinition>" + EOL + " <timeDuration xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(duration) + "</timeDuration>" + EOL + " <timeCycle xsi:type=\"tFormalExpression\" " + language + ">" + XmlDumper.replaceIllegalChars(cycle) + "</timeCycle>" + EOL + " </timerEventDefinition>" + EOL);
} else if (duration != null) {
xmlDump.append(" <timerEventDefinition>" + EOL + " <timeDuration xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(duration) + "</timeDuration>" + EOL + " </timerEventDefinition>" + EOL);
} else if (date != null) {
xmlDump.append(" <timerEventDefinition>" + EOL + " <timeDate xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(date) + "</timeDate>" + EOL + " </timerEventDefinition>" + EOL);
} else {
String lang = (String) eventNode.getMetaData("Language");
String language = "";
if (lang != null && !lang.isEmpty()) {
language = "language=\"" + lang + "\" ";
}
xmlDump.append(" <timerEventDefinition>" + EOL + " <timeCycle xsi:type=\"tFormalExpression\" " + language + ">" + XmlDumper.replaceIllegalChars(cycle) + "</timeCycle>" + EOL + " </timerEventDefinition>" + EOL);
}
endNode("boundaryEvent", xmlDump);
} else if (type.equals("Compensation")) {
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
xmlDump.append(">" + EOL);
writeExtensionElements(node, xmlDump);
xmlDump.append(" <compensateEventDefinition/>" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (node.getMetaData().get("SignalName") != null) {
boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
if (!cancelActivity) {
xmlDump.append("cancelActivity=\"false\" ");
}
xmlDump.append(">" + EOL);
writeExtensionElements(node, xmlDump);
xmlDump.append(" <signalEventDefinition signalRef=\"" + type + "\"/>" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (node.getMetaData().get("Condition") != null) {
boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
if (!cancelActivity) {
xmlDump.append("cancelActivity=\"false\" ");
}
xmlDump.append(">" + EOL);
writeExtensionElements(node, xmlDump);
xmlDump.append(" <conditionalEventDefinition>" + EOL);
xmlDump.append(" <condition xsi:type=\"tFormalExpression\" language=\"https://automatiko.io/rule\">" + eventNode.getMetaData("Condition") + "</condition>" + EOL);
xmlDump.append(" </conditionalEventDefinition>" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (type.startsWith("Message-")) {
type = type.substring(8);
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
xmlDump.append(">" + EOL);
writeExtensionElements(node, xmlDump);
xmlDump.append(" <messageEventDefinition messageRef=\"" + type + "\"/>" + EOL);
endNode("boundaryEvent", xmlDump);
}
}
}
use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.
the class BoundaryEventHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
Node node = (Node) parser.getCurrent();
String attachedTo = element.getAttribute("attachedToRef");
Attr cancelActivityAttr = element.getAttributeNode("cancelActivity");
boolean cancelActivity = true;
if (cancelActivityAttr != null) {
cancelActivity = Boolean.parseBoolean(cancelActivityAttr.getValue());
}
// determine type of event definition, so the correct type of node can be
// generated
org.w3c.dom.Node xmlNode = element.getFirstChild();
while (xmlNode != null) {
String nodeName = xmlNode.getNodeName();
if ("escalationEventDefinition".equals(nodeName)) {
// reuse already created EventNode
handleEscalationNode(node, element, uri, localName, parser, attachedTo, cancelActivity);
node.setMetaData(EVENT_TYPE, "escalation");
break;
} else if ("errorEventDefinition".equals(nodeName)) {
// reuse already created EventNode
handleErrorNode(node, element, uri, localName, parser, attachedTo, cancelActivity);
node.setMetaData(EVENT_TYPE, "error");
break;
} else if ("timerEventDefinition".equals(nodeName)) {
// reuse already created EventNode
handleTimerNode(node, element, uri, localName, parser, attachedTo, cancelActivity);
node.setMetaData(EVENT_TYPE, "timer");
break;
} else if ("compensateEventDefinition".equals(nodeName)) {
// reuse already created EventNode
handleCompensationNode(node, element, uri, localName, parser, attachedTo, cancelActivity);
node.setMetaData(EVENT_TYPE, "compensation");
break;
} else if ("signalEventDefinition".equals(nodeName)) {
// reuse already created EventNode
handleSignalNode(node, element, uri, localName, parser, attachedTo, cancelActivity);
node.setMetaData(EVENT_TYPE, "signal");
break;
} else if ("conditionalEventDefinition".equals(nodeName)) {
handleConditionNode(node, element, uri, localName, parser, attachedTo, cancelActivity);
node.setMetaData(EVENT_TYPE, "condition");
break;
} else if ("messageEventDefinition".equals(nodeName)) {
handleMessageNode(node, element, uri, localName, parser, attachedTo, cancelActivity);
node.setMetaData(EVENT_TYPE, "message");
break;
}
xmlNode = xmlNode.getNextSibling();
}
node.setMetaData("DataOutputs", new LinkedHashMap<String, String>(dataOutputTypes));
NodeContainer nodeContainer = (NodeContainer) parser.getParent();
nodeContainer.addNode(node);
((ProcessBuildData) parser.getData()).addNode(node);
return node;
}
use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.
the class WorkflowProcessInstanceImpl method getEventDescriptions.
@Override
public Set<EventDescription<?>> getEventDescriptions() {
if (getState() == ProcessInstance.STATE_COMPLETED || getState() == ProcessInstance.STATE_ABORTED) {
return Collections.emptySet();
}
VariableScope variableScope = (VariableScope) ((ContextContainer) getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
Set<EventDescription<?>> eventDesciptions = new LinkedHashSet<>();
List<EventListener> activeListeners = eventListeners.values().stream().flatMap(List::stream).collect(Collectors.toList());
activeListeners.addAll(externalEventListeners.values().stream().flatMap(List::stream).collect(Collectors.toList()));
activeListeners.forEach(el -> eventDesciptions.addAll(el.getEventDescriptions()));
((io.automatiko.engine.workflow.process.core.WorkflowProcess) getProcess()).getNodesRecursively().stream().filter(n -> n instanceof EventNodeInterface).forEach(n -> {
NamedDataType dataType = null;
if (((EventNodeInterface) n).getVariableName() != null) {
Map<String, Object> dataOutputs = (Map<String, Object>) n.getMetaData().get("DataOutputs");
if (dataOutputs != null) {
for (Entry<String, Object> dOut : dataOutputs.entrySet()) {
dataType = new NamedDataType(dOut.getKey(), dOut.getValue());
}
} else {
Variable eventVar = variableScope.findVariable(((EventNodeInterface) n).getVariableName());
if (eventVar != null) {
dataType = new NamedDataType(eventVar.getName(), eventVar.getType());
}
}
}
if (n instanceof BoundaryEventNode) {
BoundaryEventNode boundaryEventNode = (BoundaryEventNode) n;
StateBasedNodeInstance attachedToNodeInstance = (StateBasedNodeInstance) getNodeInstances(true).stream().filter(ni -> ni.getNode().getMetaData().get(UNIQUE_ID).equals(boundaryEventNode.getAttachedToNodeId())).findFirst().orElse(null);
if (attachedToNodeInstance != null) {
Map<String, String> properties = new HashMap<>();
properties.put("AttachedToID", attachedToNodeInstance.getNodeDefinitionId());
properties.put("AttachedToName", attachedToNodeInstance.getNodeName());
String eventType = EVENT_TYPE_SIGNAL;
String eventName = boundaryEventNode.getType();
Map<String, String> timerProperties = attachedToNodeInstance.extractTimerEventInformation();
if (timerProperties != null) {
properties.putAll(timerProperties);
eventType = "timer";
eventName = "timerTriggered";
}
eventDesciptions.add(new BaseEventDescription(eventName, (String) n.getMetaData().get(UNIQUE_ID), n.getName(), eventType, null, getId(), dataType, properties));
}
} else if (n instanceof EventSubProcessNode) {
EventSubProcessNode eventSubProcessNode = (EventSubProcessNode) n;
boolean isContainerActive = false;
if (eventSubProcessNode.getParentContainer() instanceof WorkflowProcess) {
isContainerActive = true;
} else if (eventSubProcessNode.getParentContainer() instanceof CompositeNode) {
isContainerActive = !getNodeInstances(((CompositeNode) eventSubProcessNode.getParentContainer()).getId()).isEmpty();
}
if (isContainerActive) {
Node startNode = eventSubProcessNode.findStartNode();
Map<Timer, ProcessAction> timers = eventSubProcessNode.getTimers();
if (timers != null && !timers.isEmpty()) {
getNodeInstances(eventSubProcessNode.getId()).forEach(ni -> {
Map<String, String> timerProperties = ((StateBasedNodeInstance) ni).extractTimerEventInformation();
if (timerProperties != null) {
eventDesciptions.add(new BaseEventDescription("timerTriggered", (String) startNode.getMetaData().get("UniqueId"), startNode.getName(), "timer", ni.getId(), getId(), null, timerProperties));
}
});
} else {
for (String eventName : eventSubProcessNode.getEvents()) {
if ("variableChanged".equals(eventName)) {
continue;
}
eventDesciptions.add(new BaseEventDescription(eventName, (String) startNode.getMetaData().get("UniqueId"), startNode.getName(), "signal", null, getId(), dataType));
}
}
}
} else if (n instanceof EventNode) {
NamedDataType finalDataType = dataType;
getNodeInstances(n.getId()).forEach(ni -> eventDesciptions.add(new BaseEventDescription(((EventNode) n).getType(), (String) n.getMetaData().get(UNIQUE_ID), n.getName(), (String) n.getMetaData().getOrDefault(EVENT_TYPE, EVENT_TYPE_SIGNAL), ni.getId(), getId(), finalDataType)));
} else if (n instanceof StateNode) {
getNodeInstances(n.getId()).forEach(ni -> eventDesciptions.add(new BaseEventDescription((String) n.getMetaData().get(CONDITION), (String) n.getMetaData().get(UNIQUE_ID), n.getName(), (String) n.getMetaData().getOrDefault(EVENT_TYPE, EVENT_TYPE_SIGNAL), ni.getId(), getId(), null)));
}
});
return eventDesciptions;
}
use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.
the class WorkflowProcessInstanceImpl method signalEvent.
@Override
@SuppressWarnings("unchecked")
public void signalEvent(String type, Object event) {
logger.debug("Signal {} received with data {} in process instance {}", type, event, getId());
synchronized (this) {
if (getState() != ProcessInstance.STATE_ACTIVE) {
return;
}
InternalProcessRuntime processRuntime = getProcessRuntime();
processRuntime.getProcessEventSupport().fireBeforeProcessSignaled(type, event, this, processRuntime);
if ("timerTriggered".equals(type)) {
TimerInstance timer = (TimerInstance) event;
if (timer.getId().equals(slaTimerId)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
}
if ("slaViolation".equals(type)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
List<NodeInstance> currentView = new ArrayList<>(this.nodeInstances);
try {
this.activatingNodeIds = new ArrayList<>();
List<EventListener> listeners = eventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
listeners = externalEventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
if (!type.startsWith("Compensation")) {
for (Node node : getWorkflowProcess().getNodes()) {
if (node instanceof EventNodeInterface && ((EventNodeInterface) node).acceptsEvent(type, event, getResolver(node, currentView))) {
if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
EventNodeInstance eventNodeInstance = (EventNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
if (node instanceof EventSubProcessNode && (resolveVariables(((EventSubProcessNode) node).getEvents()).contains(type))) {
EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
if (nodeInstances != null && !nodeInstances.isEmpty()) {
for (NodeInstance nodeInstance : nodeInstances) {
((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event);
}
}
}
}
} else if (node instanceof StartNode && ((StartNode) node).getTriggers() != null) {
boolean accepted = ((StartNode) node).getTriggers().stream().filter(EventTrigger.class::isInstance).anyMatch(t -> ((EventTrigger) t).getEventFilters().stream().anyMatch(e -> e.acceptsEvent(type, event)));
if (accepted && node.getMetaData().get("acceptStartSignal") != null) {
StartNodeInstance startNodeInstance = (StartNodeInstance) getNodeInstance(node);
startNodeInstance.signalEvent(type, event);
}
}
}
if (((io.automatiko.engine.workflow.process.core.WorkflowProcess) getWorkflowProcess()).isDynamic()) {
for (Node node : getWorkflowProcess().getNodes()) {
if (node.hasMatchingEventListner(type) && node.getIncomingConnections().isEmpty()) {
NodeInstance nodeInstance = getNodeInstance(node);
if (nodeInstance != null) {
if (event != null) {
Map<String, Object> dynamicParams = new HashMap<>(getVariables());
if (event instanceof Map) {
dynamicParams.putAll((Map<String, Object>) event);
} else if (event instanceof WorkflowProcessInstance) {
// ignore variables of process instance type
} else {
dynamicParams.put("Data", event);
}
nodeInstance.setDynamicParameters(dynamicParams);
}
nodeInstance.trigger(null, io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE);
}
} else if (this instanceof ExecutableProcessInstance && node instanceof CompositeNode) {
Optional<NodeInstance> instance = this.nodeInstances.stream().filter(ni -> ni.getNodeId() == node.getId()).findFirst();
instance.ifPresent(n -> ((CompositeNodeInstance) n).signalEvent(type, event));
}
}
}
}
} finally {
processRuntime.getProcessEventSupport().fireAfterProcessSignaled(type, event, this, processRuntime);
if (this.activatingNodeIds != null) {
this.activatingNodeIds.clear();
this.activatingNodeIds = null;
}
}
}
}
use of io.automatiko.engine.workflow.process.core.node.EventNode in project automatiko-engine by automatiko-io.
the class EventNodeHandler method writeNode.
public void writeNode(Node node, StringBuilder xmlDump, boolean includeMeta) {
EventNode eventNode = (EventNode) node;
writeNode("eventNode", eventNode, xmlDump, includeMeta);
String variableName = eventNode.getVariableName();
if (variableName != null && variableName.length() != 0) {
xmlDump.append("variableName=\"" + variableName + "\" ");
}
String scope = eventNode.getScope();
if (scope != null && scope.length() != 0) {
xmlDump.append("scope=\"" + scope + "\" ");
}
xmlDump.append(">" + EOL);
if (includeMeta) {
writeMetaData(eventNode, xmlDump);
}
xmlDump.append(" <eventFilters>" + EOL);
for (EventFilter filter : eventNode.getEventFilters()) {
if (filter instanceof EventTypeFilter) {
xmlDump.append(" <eventFilter " + "type=\"eventType\" " + "eventType=\"" + ((EventTypeFilter) filter).getType() + "\" />" + EOL);
} else {
throw new IllegalArgumentException("Unknown filter type: " + filter);
}
}
xmlDump.append(" </eventFilters>" + EOL);
endNode("eventNode", xmlDump);
}
Aggregations