/// <summary> /// Creates a new instance for the BPMN <see cref="XDocument"/>. /// </summary> /// <param name="doc">The BPMN document to parse.</param> public BpmnDocument(XDocument doc) { Elements = new Dictionary <string, BpmnElement>(); Diagrams = new List <BpmnDiagram>(); TopLevelDiagrams = new List <BpmnDiagram>(); ElementToDiagram = new Dictionary <BpmnElement, BpmnDiagram>(); Messages = new List <string>(); // parse the XML file var callingElements = new List <BpmnElement>(); RecursiveElements(doc.Root, null, callingElements); // collect all elements that are linked to from a plane foreach (var diagram in Diagrams) { try { var planeElement = diagram.Plane.Element; ElementToDiagram[planeElement] = diagram; } catch (ArgumentException) { Messages.Add("Tried to add a diagram with the already existing id: " + diagram.Id); } } // collect all diagrams where the plane corresponds to a Top Level BpmnElement (Process/Choreography/Collaboration) foreach (var diagram in Diagrams) { BpmnDiagram parent = null; var element = diagram.Plane.Element; var elementName = element.Name; if (elementName == BpmnDiConstants.ProcessElement || elementName == BpmnDiConstants.ChoreographyElement || elementName == BpmnDiConstants.CollaborationElement) { TopLevelDiagrams.Add(diagram); foreach (var callingElement in callingElements) { if (callingElement.CalledElement == element.Id) { parent = callingElement.GetNearestAncestor(ElementToDiagram); if (parent != null) { parent.AddChild(diagram, callingElement); } } } foreach (var child in diagram.Plane.Element.Children) { CollectChildDiagrams(child, diagram); } } } }
/// <summary> /// Collect all <see cref="BpmnDiagram"/> where the plane corresponds to a <see cref="BpmnElement"/> in <paramref name="diagram"/>. /// </summary> /// <param name="bpmnElement">The element to check.</param> /// <param name="diagram">The diagram to collect the child diagrams for.</param> private void CollectChildDiagrams(BpmnElement bpmnElement, BpmnDiagram diagram) { var currentDiagram = diagram; if (ElementToDiagram.ContainsKey(bpmnElement)) { var childDiagram = ElementToDiagram[bpmnElement]; diagram.AddChild(childDiagram, bpmnElement); currentDiagram = childDiagram; } foreach (var child in bpmnElement.Children) { CollectChildDiagrams(child, currentDiagram); } if (bpmnElement.Process != null && Elements.ContainsKey(bpmnElement.Process)) { BpmnElement process = Elements[bpmnElement.Process]; CollectChildDiagrams(process, currentDiagram); } }