Exemple #1
0
        /// <summary>
        /// Serializes the filtered graph g to xml
        /// </summary>
        /// <param name="writer">xml writer</param>
        /// <param name="baseGraph">"base" graph of g</param>
        /// <param name="g">graph to serialize</param>
        /// <exception cref="ArgumentNullException">writer or g are null</exception>
        /// <exception cref="ArgumentException">g vertex or edge does not
        /// implement <see cref="QuickGraph.Concepts.Serialization.IGraphSerializable"/>.
        /// </exception>
        public void Serialize(
            XmlWriter writer,
            ISerializableVertexAndEdgeListGraph baseGraph,
            IVertexAndEdgeListGraph g
            )
        {
            GraphMltype graphml = new GraphMltype();

            KeyType graphTypeKey = new KeyType();

            graphTypeKey.ID = graphTypeKeyName;
            graphml.Key.AddKeyType(graphTypeKey);

            KeyType vertexProviderTypeKey = new KeyType();

            vertexProviderTypeKey.ID = vertexProviderTypeKeyName;
            graphml.Key.AddKeyType(vertexProviderTypeKey);

            KeyType edgeProviderTypeKey = new KeyType();

            edgeProviderTypeKey.ID = edgeProviderTypeKeyName;
            graphml.Key.AddKeyType(edgeProviderTypeKey);

            KeyType allowParralelEdgeKey = new KeyType();

            allowParralelEdgeKey.ID = allowParallelEdgesKeyName;
            graphml.Key.AddKeyType(allowParralelEdgeKey);

            KeyType nameKey = new KeyType();

            nameKey.ID = @"name";
            graphml.Key.AddKeyType(nameKey);

            GraphType graph = SerializeGraph(baseGraph, g);

            graphml.Items.AddGraph(graph);

            // add dtd
            // <!DOCTYPE graphml PUBLIC "-GraphML DTD" "http://graphml.graphdrawing.org/dtds/1.0rc/graphml.dtd">
            //writer.WriteDocType("graphml","-GraphML DTD",this.dtdPath,null);

            // serialize
            XmlSerializer ser = new XmlSerializer(typeof(GraphMltype));

            ser.Serialize(writer, graphml);
        }
Exemple #2
0
        public ISerializableVertexAndEdgeListGraph Deserialize(XmlReader reader)
        {
            this.createdEdges    = new Hashtable();
            this.createdVertices = new Hashtable();

            // rebuild graphml object
            XmlSerializer ser     = new XmlSerializer(typeof(GraphMltype));
            GraphMltype   graphml = (GraphMltype)ser.Deserialize(reader);

            // get graph
            GraphType gt = null;

            foreach (Object item in graphml.Items)
            {
                gt = item as GraphType;
                if (gt != null)
                {
                    break;
                }
            }


            if (gt == null)
            {
                throw new ArgumentException("no graph information found");
            }


            // retreive data for reflection
            if (this.typeFromXml)
            {
                foreach (Object item in gt.Items)
                {
                    DataType dt = item as DataType;
                    if (dt == null)
                    {
                        continue;
                    }
                    switch (dt.Key)
                    {
                    case graphTypeKeyName:
                        this.graphType = ToType(TextToString(dt.Text));
                        break;

                    case vertexProviderTypeKeyName:
                        this.vertexProviderType = ToType(TextToString(dt.Text));
                        break;

                    case edgeProviderTypeKeyName:
                        this.edgeProviderType = ToType(TextToString(dt.Text));
                        break;

                    case allowParallelEdgesKeyName:
                        this.allowParallelEdges = ToBool(TextToString(dt.Text));
                        break;
                    }
                }
            }

            if (this.GraphType == null)
            {
                throw new InvalidOperationException("GraphType is null");
            }
            if (this.VertexProviderType == null)
            {
                throw new InvalidOperationException("VertexProviderType is null");
            }
            if (this.EdgeProviderType == null)
            {
                throw new InvalidOperationException("EdgeProviderType is null");
            }

            // create graph
            ISerializableVertexAndEdgeListGraph g = CreateGraph(
                this.GraphType,
                this.VertexProviderType,
                this.EdgeProviderType,
                gt.EdgeDefault,
                this.allowParallelEdges
                );

            // populate graph vertices
            bool isVertexDeserialiable =
                typeof(IGraphDeSerializable).IsAssignableFrom(g.VertexProvider.VertexType);

            foreach (Object item in gt.Items)
            {
                NodeType node = item as NodeType;
                if (node == null)
                {
                    continue;
                }

                IVertex v = g.AddVertex();
                this.CreatedVertices[node.ID] = v;
                if (isVertexDeserialiable)
                {
                    IGraphSerializationInfo info = InfoFromNode(node);
                    ((IGraphDeSerializable)v).ReadGraphData(info);
                }
            }

            bool isEdgeDeserialiable =
                typeof(IGraphDeSerializable).IsAssignableFrom(g.EdgeProvider.EdgeType);

            foreach (Object item in gt.Items)
            {
                EdgeType edge = item as EdgeType;
                if (edge == null)
                {
                    continue;
                }

                IEdge e = g.AddEdge(
                    (IVertex)this.CreatedVertices[edge.Source],
                    (IVertex)this.CreatedVertices[edge.Target]
                    );
                this.CreatedEdges[edge.ID] = e.ID;

                if (isEdgeDeserialiable)
                {
                    IGraphSerializationInfo info = InfoFromEdge(edge);
                    ((IGraphDeSerializable)e).ReadGraphData(info);
                }
            }

            return(g);
        }