/// <summary> /// Create the graph element and stores graph level data. /// </summary> /// <param name="writer">xml writer</param> /// <param name="baseGraph">"base" graph of g</param> /// <param name="g">graph to serialize</param> protected override void WriteGraphElem( XmlWriter writer, ISerializableVertexAndEdgeListGraph baseGraph, IVertexAndEdgeListGraph g ) { writer.WriteStartElement("gxl"); // adding xsd ref writer.WriteAttributeString("xmlns", "http://www.gupro.de/GXL/xmlschema/gxl-1.0.xsd"); // adding graph node writer.WriteStartElement("graph"); writer.WriteAttributeString("edgeids", "true"); if (g.IsDirected) { writer.WriteAttributeString("edgemode", "directed"); } else { writer.WriteAttributeString("edgemode", "undirected"); } // adding type information IGraphSerializationInfo info = new GraphSerializationInfo(true); info.Add("graph-type", GetTypeQualifiedName(baseGraph)); info.Add("vertex-provider-type", GetTypeQualifiedName(baseGraph.VertexProvider)); info.Add("edge-provider-type", GetTypeQualifiedName(baseGraph.EdgeProvider)); info.Add("allow-parallel-edges", g.AllowParallelEdges); WriteInfo(writer, info); }
public void WriteReadGraphMlAdjacencyGraph() { StringWriter sw = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(sw); writer.Formatting = Formatting.Indented; GraphMLGraphSerializer ser = new GraphMLGraphSerializer(); ser.Serialize(writer, Graph); Console.WriteLine("------------- serialized graph -----------------"); Console.WriteLine(sw.ToString()); XmlAssert.XmlValid(sw.ToString()); StringReader sr = new StringReader(sw.ToString()); XmlTextReader reader = new XmlTextReader(sr); ser.TypeFromXml = true; ISerializableVertexAndEdgeListGraph g = ser.Deserialize(reader); ser = new GraphMLGraphSerializer(); StringWriter swResult = new StringWriter(); writer = new XmlTextWriter(swResult); writer.Formatting = Formatting.Indented; ser.Serialize(writer, g); Console.WriteLine("------------- deserialized graph -----------------"); Console.WriteLine(swResult.ToString()); XmlAssert.XmlEquals(sw.ToString(), swResult.ToString()); CheckGraphEqual(Graph, g); }
public static void CloneOutVertexTree( IVertexListGraph g, ISerializableVertexAndEdgeListGraph sub, IVertex v, int maxDepth ) { if (g == null) { throw new ArgumentNullException("g"); } if (sub == null) { throw new ArgumentNullException("sub"); } if (v == null) { throw new ArgumentNullException("v"); } DepthFirstSearchAlgorithm dfs = new DepthFirstSearchAlgorithm(g); PopulatorVisitor pop = new PopulatorVisitor(sub); dfs.StartVertex += new VertexEventHandler(pop.StartVertex); dfs.TreeEdge += new EdgeEventHandler(pop.TreeEdge); dfs.MaxDepth = maxDepth; dfs.Initialize(); dfs.Visit(v, 0); }
/// <summary> /// Serializes g to xml /// </summary> /// <param name="writer">xml writer</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="IGraphSerializable"/>. /// </exception> public void Serialize( XmlWriter writer, ISerializableVertexAndEdgeListGraph g ) { Serialize(writer, g, g); }
/// <summary> /// Create the graph element and stores graph level data. /// </summary> /// <param name="writer">xml writer</param> /// <param name="baseGraph">"base" graph of g</param> /// <param name="g">graph to serialize</param> protected override void WriteGraphElem( XmlWriter writer, ISerializableVertexAndEdgeListGraph baseGraph, IVertexAndEdgeListGraph g ) { writer.WriteStartElement("gxl"); // adding xsd ref writer.WriteAttributeString("xmlns","http://www.gupro.de/GXL/xmlschema/gxl-1.0.xsd"); // adding graph node writer.WriteStartElement("graph"); writer.WriteAttributeString("edgeids","true"); if (g.IsDirected) writer.WriteAttributeString("edgemode","directed"); else writer.WriteAttributeString("edgemode","undirected"); // adding type information IGraphSerializationInfo info = new GraphSerializationInfo(true); info.Add("graph-type",GetTypeQualifiedName(baseGraph)); info.Add("vertex-provider-type",GetTypeQualifiedName(baseGraph.VertexProvider)); info.Add("edge-provider-type",GetTypeQualifiedName(baseGraph.EdgeProvider)); info.Add("allow-parallel-edges",g.AllowParallelEdges); WriteInfo(writer,info); }
/// <summary> /// Deserializes data from Xml stream. /// </summary> /// <param name="reader">xml stream</param> /// <returns>deserialized data</returns> public ISerializableVertexAndEdgeListGraph Deserialize(XmlReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } this.createdVertices = new Hashtable(); this.createdEdges = new Hashtable(); ISerializableVertexAndEdgeListGraph g = ReadGraphElem(reader); do { if (reader.NodeType == XmlNodeType.Element) { if (!ReadVertexOrEdge(reader, g)) { break; } } else if (reader.NodeType == XmlNodeType.EndElement) { break; } if (!reader.Read()) { break; } }while(reader.Read()); ReadEndGraphElem(reader); return(g); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="g"></param> protected void ReadEdge( XmlReader reader, ISerializableVertexAndEdgeListGraph g ) { MoveToAttribute(reader, "id", true); string id = this.ParseEdgeID(reader.Value); MoveToAttribute(reader, "from", true); string sourceid = ParseVertexID(reader.Value); MoveToAttribute(reader, "to", true); string targetid = ParseVertexID(reader.Value); // add edge. IEdge e = g.AddEdge( (IVertex)CreatedVertices[sourceid], (IVertex)CreatedVertices[targetid] ); // add to table CreatedEdges[id] = e; GraphSerializationInfo info = ReadInfo(reader); if (info != null) { ((IGraphDeSerializable)e).ReadGraphData(info); } this.MovePastEndElement(reader, "edge"); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="g"></param> protected void ReadEdge( XmlReader reader, ISerializableVertexAndEdgeListGraph g ) { MoveToAttribute(reader,"id",true); string id = this.ParseEdgeID(reader.Value); MoveToAttribute(reader,"from",true); string sourceid = ParseVertexID(reader.Value); MoveToAttribute(reader,"to",true); string targetid = ParseVertexID(reader.Value); // add edge. IEdge e=g.AddEdge( (IVertex)CreatedVertices[sourceid], (IVertex)CreatedVertices[targetid] ); // add to table CreatedEdges[id]=e; GraphSerializationInfo info = ReadInfo(reader); if (info != null) ((IGraphDeSerializable)e).ReadGraphData(info); this.MovePastEndElement(reader,"edge"); }
public PopulatorVisitor( ISerializableVertexAndEdgeListGraph graph ) { if (graph==null) throw new ArgumentNullException("graph"); this.graph = graph; }
/// <summary> /// Constructs a serializer around g /// </summary> /// <param name="g">graph to serialize</param> /// <exception cref="ArgumentNullException">g is null</exception> public XmlGraphSerializer(ISerializableVertexAndEdgeListGraph g) { if (g == null) { throw new ArgumentNullException("graph"); } m_Graph = g; }
/// <summary> /// Output graph to xml /// </summary> /// <param name="g"></param> public void WriteToXml(ISerializableVertexAndEdgeListGraph g) { // output to xml XmlTextWriter writer = new XmlTextWriter(Console.Out); writer.Formatting = Formatting.Indented; XmlGraphSerializer ser = new XmlGraphSerializer(g); ser.Serialize(writer); }
public PopulatorVisitor( ISerializableVertexAndEdgeListGraph graph ) { if (graph == null) { throw new ArgumentNullException("graph"); } this.graph = graph; }
/// <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); }
private GraphType SerializeGraph( ISerializableVertexAndEdgeListGraph baseGraph, IVertexAndEdgeListGraph g) { // create graph node GraphType graph = new GraphType(); // fill up graph if (g.IsDirected) { graph.EdgeDefault = GraphEdgeDefaultType.Directed; } else { graph.EdgeDefault = GraphEdgeDefaultType.Undirected; } // adding type information IGraphSerializationInfo info = new GraphSerializationInfo(true); info.Add(graphTypeKeyName, GetTypeQualifiedName(baseGraph)); info.Add(vertexProviderTypeKeyName, GetTypeQualifiedName(baseGraph.VertexProvider)); info.Add(edgeProviderTypeKeyName, GetTypeQualifiedName(baseGraph.EdgeProvider)); info.Add(allowParallelEdgesKeyName, g.AllowParallelEdges); // add data... foreach (DataType dt in ToDatas(info)) { graph.Items.AddData(dt); } // add vertices foreach (IVertex v in g.Vertices) { graph.Items.AddNode(SerializeVertex(v)); } // add edges foreach (IEdge e in g.Edges) { graph.Items.AddEdge(SerializeEdge(e)); } return(graph); }
/// <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 ) { if (writer == null) { throw new ArgumentNullException("writer"); } if (baseGraph == null) { throw new ArgumentNullException("baseGraph"); } if (g == null) { throw new ArgumentNullException("g"); } // Add graph node WriteGraphElem(writer, baseGraph, g); // add vertices foreach (IVertex v in g.Vertices) { // get vertex data GraphSerializationInfo info = new GraphSerializationInfo(true); ((IGraphSerializable)v).WriteGraphData(info); // write it to xml WriteVertexElem(writer, v, info); } // add edges foreach (IEdge e in g.Edges) { // get edge data GraphSerializationInfo info = new GraphSerializationInfo(true); ((IGraphSerializable)e).WriteGraphData(info); // write to xml WriteEdgeElem(writer, e, info); } // finish graph node WriteEndGraphElem(writer); }
/// <summary> /// Reads graph data and creates new graph instance /// </summary> /// <param name="reader">xml reader opened on graph data</param> /// <returns>created graph instance</returns> protected override ISerializableVertexAndEdgeListGraph ReadGraphElem(XmlReader reader) { MoveToElement(reader, "gxl"); MoveToElement(reader, "graph"); // get directed state bool directed = true; if (MoveToAttribute(reader, "edgemode", false)) { if (reader.Value != "directed") { directed = false; } } IGraphSerializationInfo info = ReadInfo(reader); // getting types... if (this.GraphType == null) { this.GraphType = Type.GetType(info["graph-type"].ToString(), true); } if (this.VertexProviderType == null) { this.VertexProviderType = Type.GetType(info["vertex-provider-type"].ToString(), true); } if (this.EdgeProviderType == null) { this.EdgeProviderType = Type.GetType(info["edge-provider-type"].ToString(), true); } bool allowParallelEdges = bool.Parse(info["allow-parallel-edges"].ToString()); ISerializableVertexAndEdgeListGraph g = CreateGraph( this.GraphType, this.VertexProviderType, this.EdgeProviderType, directed, allowParallelEdges ); return(g); }
public void ReadWriteGxlAdjacencyGraph() { StringWriter sw = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(sw); writer.Formatting = Formatting.Indented; GxlGraphSerializer ser = new GxlGraphSerializer(); ser.Serialize(writer, Graph); XmlAssert.XmlValid(sw.ToString()); StringReader sr = new StringReader(sw.ToString()); XmlTextReader reader = new XmlTextReader(sr); ISerializableVertexAndEdgeListGraph g = ser.Deserialize(reader); CheckGraphEqual(Graph, g); }
/// <summary> /// Reads vertex or edge data /// </summary> /// <param name="reader"></param> /// <param name="g"></param> protected override bool ReadVertexOrEdge( XmlReader reader, ISerializableVertexAndEdgeListGraph g ) { if (reader.Name == "node") { ReadVertex(reader, g); return(true); } else if (reader.Name == "edge") { ReadEdge(reader, g); return(true); } else { return(false); } }
public static void CloneOutVertexTree(IVertexListGraph g, ISerializableVertexAndEdgeListGraph sub, IVertex v, int maxDepth) { if (g == null) { throw new ArgumentNullException("g"); } if (sub == null) { throw new ArgumentNullException("sub"); } if (v == null) { throw new ArgumentNullException("v"); } DepthFirstSearchAlgorithm algorithm = new DepthFirstSearchAlgorithm(g); PopulatorVisitor visitor = new PopulatorVisitor(sub); algorithm.StartVertex += new VertexEventHandler(visitor, (IntPtr) this.StartVertex); algorithm.TreeEdge += new EdgeEventHandler(visitor, (IntPtr) this.TreeEdge); algorithm.MaxDepth = maxDepth; algorithm.Initialize(); algorithm.Visit(v, 0); }
/// <summary> /// /// </summary> /// <param name="reader"></param> protected void ReadGraphXml(XmlReader reader) { if (!reader.Read() || reader.Name != "graph") { throw new Exception("could not find graph node"); } // getting types MoveToAttribute(reader, "type"); Type graphType = Type.GetType(reader.Value, true); MoveToAttribute(reader, "vertex-provider-type"); Type vertexProviderType = Type.GetType(reader.Value, true); MoveToAttribute(reader, "edge-provider-type"); Type edgeProviderType = Type.GetType(reader.Value, true); MoveToAttribute(reader, "directed"); bool directed = bool.Parse(reader.Value); MoveToAttribute(reader, "allow-parallel-edges"); bool allowParallelEdges = bool.Parse(reader.Value); // create providers IVertexProvider vp = (IVertexProvider)vertexProviderType.GetConstructor(Type.EmptyTypes).Invoke(null); IEdgeProvider ep = (IEdgeProvider)edgeProviderType.GetConstructor(Type.EmptyTypes).Invoke(null); // create graph Type[] gts = new Type[3]; gts[0] = typeof(IVertexProvider); gts[1] = typeof(IEdgeProvider); gts[2] = typeof(bool); Object[] gps = new Object[3]; gps[0] = vp; gps[1] = ep; gps[2] = allowParallelEdges; Graph = (ISerializableVertexAndEdgeListGraph)graphType.GetConstructor(gts).Invoke(gps); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="g"></param> protected void ReadVertex( XmlReader reader, ISerializableVertexAndEdgeListGraph g ) { MoveToAttribute(reader, "id", true); string id = this.ParseVertexID(reader.Value); // add vertex. IVertex v = g.AddVertex(); // add to table CreatedVertices[id] = v; GraphSerializationInfo info = ReadInfo(reader); if (info != null) { ((IGraphDeSerializable)v).ReadGraphData(info); } this.MovePastEndElement(reader, "node"); }
public void ReadWriteGraphMlAdjacencyGraph() { StringWriter sw = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(sw); writer.Formatting = Formatting.Indented; GraphMLGraphSerializer ser = new GraphMLGraphSerializer(@"../../graphml.dtd"); ser.Serialize(writer, Graph); Console.WriteLine(sw.ToString()); Validate(sw.ToString()); Console.WriteLine("Validated"); StringReader sr = new StringReader(sw.ToString()); XmlTextReader reader = new XmlTextReader(sr); ISerializableVertexAndEdgeListGraph g = ser.Deserialize(reader); writer = new XmlTextWriter(Console.Out); writer.Formatting = Formatting.Indented; Console.WriteLine("Outputting reloaded"); ser.Serialize(writer, g); }
public static void CloneOutVertexTree( IVertexListGraph g, ISerializableVertexAndEdgeListGraph sub, IVertex v, int maxDepth ) { if (g==null) throw new ArgumentNullException("g"); if (sub==null) throw new ArgumentNullException("sub"); if (v==null) throw new ArgumentNullException("v"); DepthFirstSearchAlgorithm dfs = new DepthFirstSearchAlgorithm(g); PopulatorVisitor pop = new PopulatorVisitor(sub); dfs.StartVertex += new VertexEventHandler(pop.StartVertex); dfs.TreeEdge += new EdgeEventHandler(pop.TreeEdge); dfs.MaxDepth = maxDepth; dfs.Initialize(); dfs.Visit(v,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 ) { if (writer==null) throw new ArgumentNullException("writer"); if (baseGraph==null) throw new ArgumentNullException("baseGraph"); if (g==null) throw new ArgumentNullException("g"); // Add graph node WriteGraphElem(writer,baseGraph,g); // add vertices foreach(IVertex v in g.Vertices) { // get vertex data GraphSerializationInfo info = new GraphSerializationInfo(true); ((IGraphSerializable)v).WriteGraphData(info); // write it to xml WriteVertexElem(writer,v,info); } // add edges foreach(IEdge e in g.Edges) { // get edge data GraphSerializationInfo info = new GraphSerializationInfo(true); ((IGraphSerializable)e).WriteGraphData(info); // write to xml WriteEdgeElem(writer,e,info); } // finish graph node WriteEndGraphElem(writer); }
/// <summary> /// Output graph to xml /// </summary> /// <param name="g"></param> public void WriteToXml(ISerializableVertexAndEdgeListGraph g) { // output to xml XmlTextWriter writer = new XmlTextWriter(Console.Out); writer.Formatting = Formatting.Indented; XmlGraphSerializer ser = new XmlGraphSerializer(g); ser.Serialize(writer); }
/// <summary> /// Empty constructor /// </summary> public XmlGraphSerializer() { m_Graph = null; }
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); }
/// <summary> /// Constructs a serializer around g /// </summary> /// <param name="g">graph to serialize</param> /// <exception cref="ArgumentNullException">g is null</exception> public XmlGraphSerializer(ISerializableVertexAndEdgeListGraph g) { if (g==null) throw new ArgumentNullException("graph"); m_Graph = g; }
/// <summary> /// Reads vertex or edge data /// </summary> /// <param name="reader"></param> /// <param name="g"></param> protected override bool ReadVertexOrEdge( XmlReader reader, ISerializableVertexAndEdgeListGraph g ) { if (reader.Name == "node") { ReadVertex(reader,g); return true; } else if (reader.Name == "edge") { ReadEdge(reader,g); return true; } else return false; }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="g"></param> protected void ReadVertex( XmlReader reader, ISerializableVertexAndEdgeListGraph g ) { MoveToAttribute(reader,"id",true); string id = this.ParseVertexID(reader.Value); // add vertex. IVertex v=g.AddVertex(); // add to table CreatedVertices[id]=v; GraphSerializationInfo info = ReadInfo(reader); if (info != null) ((IGraphDeSerializable)v).ReadGraphData(info); this.MovePastEndElement(reader,"node"); }
/// <summary> /// Reads vertex or edge data /// </summary> /// <param name="reader"></param> /// <param name="g"></param> protected abstract bool ReadVertexOrEdge( XmlReader reader, ISerializableVertexAndEdgeListGraph g );
/// <summary> /// Create the graph element and stores graph level data. /// </summary> /// <param name="writer">xml writer</param> /// <param name="baseGraph">"base" graph of g</param> /// <param name="g">graph to serialize</param> protected abstract void WriteGraphElem( XmlWriter writer, ISerializableVertexAndEdgeListGraph baseGraph, IVertexAndEdgeListGraph g );
/// <summary> /// Create the graph element and stores graph level data. /// </summary> /// <param name="writer">xml writer</param> /// <param name="baseGraph">"base" graph of g</param> /// <param name="g">graph to serialize</param> protected abstract void WriteGraphElem( XmlWriter writer, ISerializableVertexAndEdgeListGraph baseGraph, IVertexAndEdgeListGraph g );
/// <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); }
/// <summary> /// Reads vertex or edge data /// </summary> /// <param name="reader"></param> /// <param name="g"></param> protected abstract bool ReadVertexOrEdge( XmlReader reader, ISerializableVertexAndEdgeListGraph g );
/// <summary> /// Empty constructor /// </summary> public XmlGraphSerializer() { m_Graph = null; }
private GraphType SerializeGraph( ISerializableVertexAndEdgeListGraph baseGraph, IVertexAndEdgeListGraph g) { // create graph node GraphType graph = new GraphType(); // fill up graph if (g.IsDirected) graph.EdgeDefault = GraphEdgeDefaultType.Directed; else graph.EdgeDefault= GraphEdgeDefaultType.Undirected; // adding type information IGraphSerializationInfo info = new GraphSerializationInfo(true); info.Add(graphTypeKeyName,GetTypeQualifiedName(baseGraph)); info.Add(vertexProviderTypeKeyName,GetTypeQualifiedName(baseGraph.VertexProvider)); info.Add(edgeProviderTypeKeyName,GetTypeQualifiedName(baseGraph.EdgeProvider)); info.Add(allowParallelEdgesKeyName,g.AllowParallelEdges); // add data... foreach(DataType dt in ToDatas(info)) { graph.Items.AddData(dt); } // add vertices foreach(IVertex v in g.Vertices) graph.Items.AddNode( SerializeVertex(v) ); // add edges foreach(IEdge e in g.Edges) graph.Items.AddEdge( SerializeEdge(e) ); return graph; }
/// <summary> /// /// </summary> /// <param name="reader"></param> protected void ReadGraphXml(XmlReader reader) { if (!reader.Read() || reader.Name != "graph") throw new Exception("could not find graph node"); // getting types MoveToAttribute(reader,"type"); Type graphType = Type.GetType(reader.Value,true); MoveToAttribute(reader,"vertex-provider-type"); Type vertexProviderType = Type.GetType(reader.Value,true); MoveToAttribute(reader,"edge-provider-type"); Type edgeProviderType = Type.GetType(reader.Value,true); MoveToAttribute(reader,"directed"); bool directed = bool.Parse(reader.Value); MoveToAttribute(reader,"allow-parallel-edges"); bool allowParallelEdges = bool.Parse(reader.Value); // create providers IVertexProvider vp = (IVertexProvider)vertexProviderType.GetConstructor(Type.EmptyTypes).Invoke(null); IEdgeProvider ep = (IEdgeProvider)edgeProviderType.GetConstructor(Type.EmptyTypes).Invoke(null); // create graph Type[] gts = new Type[3]; gts[0]=typeof(IVertexProvider); gts[1]=typeof(IEdgeProvider); gts[2]=typeof(bool); Object[] gps = new Object[3]; gps[0]=vp; gps[1]=ep; gps[2]=allowParallelEdges; Graph = (ISerializableVertexAndEdgeListGraph)graphType.GetConstructor(gts).Invoke(gps); }
/// <summary> /// Serializes g to xml /// </summary> /// <param name="writer">xml writer</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="IGraphSerializable"/>. /// </exception> public void Serialize( XmlWriter writer, ISerializableVertexAndEdgeListGraph g ) { Serialize(writer,g,g); }