Skip to content

Saving and loading graphs

Alexander Smirnov edited this page Jul 13, 2016 · 1 revision

< Back to contents >

Last checked against version: 2.1.8

Graph data and visuals save/load

There are no default serialization engine in GraphX, instead there is LogicCore::FileServiceProvider property present that allows to assign custom file services provider based on IFileServiceProvider interface.

Example save and load procedures in ShowcaseApp.WPF project are impemented using YAXLib (https://yaxlib.codeplex.com/) with all of its features and limitations.

To be able to save and load your Graph using GraphArea::SerializeToFile() and GraphArea::DeserializeFromFile() methods (which uses IFileServiceProvider methods internally), your code must meet specific conditions:

  • Vertex and Edge data classes must have filled unique positive ID property. Basicaly it can be any unique number assigned by some counter. Automatic ID resolving is enabled by default, making absent IDs to be filled with unique values automatically (this can be controlled by Area.GenerateGraph() method parameter).
  • Edge and Vertex data classes along with any other used class must implement default parameterless constructor. As the reflection is actively being used in serialization process this is a must.
  • Your data graph class must implement interface IMutableVertexAndEdgeSet<TVertex, TEdge>. BidirectionalGraph already implements this by default.


Limitations:

  • There are some possible problems serializing complex type such as BitmapImage. In that cases custom serializers must be coded for each individual type. Look in YAXLib docs for more info.
  • Only properties can be serialized and no fields.
  • All serialized classes must implement parameterless constructors. Workaround in case of generic classes usage can be custom converter or

 

Special usage FAQ

For complete usage info look in:


Useful attributes

It is vital to manually exclude some properties from serialization (for ex. get-only props). This can be done by specifying YAXDontSerialize attribute for property.

Custom serializers example

Custom serializers can be specified for any property. This approach made possible end-user customization of default serialization process which is vital in many special cases.

Currently, an example provides optional custom serializers for:

  • Point (surpasses some weird default deserialization issues)
  • Point[]

Point serializer example which stores point values in a string:

public class YAXPointSerializer: ICustomSerializer<Point>
    {

        private Point Deserialize(string str)
        {
            var res = str.Split(new char[] { '|' });
            if (res.Length == 2) return new Point(Convert.ToDouble(res[0]), Convert.ToDouble(res[1]));
            else return new Point();
        }

        public Point DeserializeFromAttribute(System.Xml.Linq.XAttribute attrib)
        {
            return Deserialize(attrib.Value);
        }

        public Point DeserializeFromElement(System.Xml.Linq.XElement element)
        {
            return Deserialize(element.Value);
        }

        public Point DeserializeFromValue(string value)
        {
            return Deserialize(value);
        }

        public void SerializeToAttribute(Point objectToSerialize, System.Xml.Linq.XAttribute attrToFill)
        {
            attrToFill.Value = String.Format("{0}|{1}", objectToSerialize.X, objectToSerialize.Y);
        }

        public void SerializeToElement(Point objectToSerialize, System.Xml.Linq.XElement elemToFill)
        {
            elemToFill.Value = String.Format("{0}|{1}", objectToSerialize.X, objectToSerialize.Y);
        }

        public string SerializeToValue(Point objectToSerialize)
        {
            return String.Format("{0}|{1}", objectToSerialize.X, objectToSerialize.Y);
        }
    }