Esempio n. 1
0
 /// <summary>
 /// Exports the Petri Net to a PNML document.
 /// </summary>
 /// <param name="petriNet">An instance of Petri Net to export.</param>
 /// <returns>An instance of PnmlDocument representing the persisted Petri Net.</returns>
 public PersistentDocument Export(PetriNet petriNet)
 {
     var xmlDocument = BuildPetriNetXmlDocument(petriNet);
     var result = new PnmlDocument();
     result.Load(xmlDocument);
     return result;
 }
Esempio n. 2
0
        /// <summary>
        /// Imports a PNML document and converts it to a PetriNet instance.
        /// </summary>
        /// <param name="document">An instance of PnmlDocument to import.</param>
        /// <returns>An instance of PetriNet that represents the Petri Net extracted from the document.</returns>
        public PetriNet Import(PersistentDocument document)
        {
            var petriNet = new PetriNet();

            // Loading PNML document content.
            var xDocument = GetXmlDocumentFromPnmlDocument(document);

            // Loading Places and Transitions.
            var nonArcElements = xDocument.Element("pnml").Element("net").Elements().Where(element => element.Name != "arc");
            foreach (var xPNElement in nonArcElements)
            {
                switch (xPNElement.Name.ToString())
                {
                    case "place":
                        var place = GetPlaceObjectFromPnmlElement(xPNElement);
                        if (place != null)
                        {
                            petriNet.Elements.Add(place);
                        }
                        break;

                    case "transition":
                        var transition = GetTransitionObjectFromPnmlElement(xPNElement);
                        if (transition != null)
                        {
                            petriNet.Elements.Add(transition);
                        }
                        break;
                }
            }

            // Loading Arcs.
            var arcElements = xDocument.Element("pnml").Element("net").Elements().Where(element => element.Name == "arc");
            foreach (var xArcElement in arcElements)
            {
                var arc = GetArcObjectFromPnmlElement(petriNet, xArcElement);
                if (arc != null)
                {
                    petriNet.Elements.Add(arc);
                }
            }

            petriNet.UpdateElementRelationships();
            return petriNet;
        }
Esempio n. 3
0
        private XDocument BuildPetriNetXmlDocument(PetriNet petriNet)
        {
            XDocument xmlDocument = new XDocument
            (
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement("pnml")
            );

            // Creating XML root elements.
            var netElement = new XElement("net");
            if (xmlDocument.Root != null)
            {
                xmlDocument.Root.Add(netElement);
            }

            // Adding Places.
            var places = petriNet.Elements.OfType<Place>();
            foreach (var place in places)
            {
                var placeElement = ConvertPlaceToPnmlElement(place);
                netElement.Add(placeElement);
            }

            // Adding Transitions.
            List<Type> transitionTypes = new List<Type> { typeof(Transition), typeof(AndSplit), typeof(AndJoin), typeof(OrSplit), typeof(OrJoin), typeof(Subprocess) };
            var transitions = petriNet.Elements.Where(element => transitionTypes.Contains(element.GetType()));
            foreach (var transition in transitions)
            {
                var transitionElement = ConvertTransitionToPnmlElement(transition);
                netElement.Add(transitionElement);
            }

            // Adding Arcs.
            var arcs = petriNet.Elements.OfType<Arc>();
            foreach (var arc in arcs)
            {
                var arcElement = ConvertArcToPnmlElement(arc);
                netElement.Add(arcElement);
            }

            // Adding Subprocesses.
            var subprocesses = petriNet.Elements.OfType<Subprocess>();
            foreach (var subprocess in subprocesses)
            {
                var subprocessElement = ConvertSubprocessToPnmlElement(subprocess);
                netElement.Add(subprocessElement);
            }

            return xmlDocument;
        }
Esempio n. 4
0
        private static Arc GetArcObjectFromPnmlElement(PetriNet petriNet, XElement pnmlElement)
        {
            Arc arc = new Arc();

            arc.ID = pnmlElement.Attribute("id").Value;
            string startElementID = pnmlElement.Attribute("source").Value;
            string endElementID = pnmlElement.Attribute("target").Value;
            arc.StartElement = petriNet.Elements.FirstOrDefault(element => element.ID == startElementID);
            arc.EndElement = petriNet.Elements.FirstOrDefault(element => element.ID == endElementID);

            if (pnmlElement.Element("graphics") != null)
            {
                if (pnmlElement.Element("graphics").Element("dimension") != null)
                {
                    arc.SetGraphicsMetadataItem("graphics.dimension.@x", int.Parse(pnmlElement.Element("graphics").Element("dimension").Attribute("x").Value));
                    arc.SetGraphicsMetadataItem("graphics.dimension.@y", int.Parse(pnmlElement.Element("graphics").Element("dimension").Attribute("y").Value));
                }
            }
            return arc;
        }
Esempio n. 5
0
        public void LoadPetriNet(PetriNet petriNet)
        {
            SelectedWidgets.Clear();
            Widgets.Clear();

            petriNet.UpdateElementRelationships();
            foreach (var element in petriNet.Elements)
            {
                var widget = CreateWidgetForElement(element);
                widget.Location = new Point
                (
                    (int)element.GetGraphicsMetadataItem("graphics.position.@x", 30),
                    (int)element.GetGraphicsMetadataItem("graphics.position.@y", 30)
                );
                Widgets.Add(widget);
            }

            HandleWidgetsChanged();
            HandleSelectedWidgetsChanged();
            UpdateControlAvailability();
            RedrawCanvas();
        }
Esempio n. 6
0
 public PetriNet ExportToPetriNet()
 {
     var result = new PetriNet();
     foreach (var widget in Widgets)
     {
         if (!(widget is ArcWidget))
         {
             widget.Element.SetGraphicsMetadataItem("graphics.position.@x", widget.Location.X);
             widget.Element.SetGraphicsMetadataItem("graphics.position.@y", widget.Location.Y);
             widget.Element.SetGraphicsMetadataItem("graphics.dimension.@x", widget.Size);
             widget.Element.SetGraphicsMetadataItem("graphics.dimension.@y", widget.Size);
         }
         result.Elements.Add(widget.Element);
     }
     result.UpdateElementRelationships();
     return result;
 }