/// <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); } }
/// <summary> /// Creates a <see cref="BpmnDiagram"/>. /// </summary> /// <param name="xNode">The XML node to start with</param> /// <returns>The parsed <see cref="BpmnDiagram"/></returns> private BpmnDiagram BuildDiagram(XElement xNode) { var diagram = new BpmnDiagram(xNode); var bpmnPlane = BuildPlane(BpmnNM.GetElement(xNode, BpmnNM.BpmnDi, BpmnDiConstants.BpmnPlaneElement)); if (bpmnPlane != null) { diagram.AddPlane(bpmnPlane); } foreach (var xChild in BpmnNM.GetElements(xNode, BpmnNM.BpmnDi, BpmnDiConstants.BpmnLabelStyleElement)) { var style = new BpmnLabelStyle(xChild); diagram.AddStyle(style); } // Setting a default LabelStyle for all labels that do not have their own style. diagram.DefaultStyle = BpmnLabelStyle.NewDefaultInstance(); return(diagram); }
/// <summary> /// Adds a child diagramm to this diagram /// </summary> /// <param name="diagram">The child diagram</param> /// <param name="localRoot">The local root element</param> public void AddChild(BpmnDiagram diagram, BpmnElement localRoot) { Children.Add(diagram, localRoot); }