public IState Store()
        {
            string graphXml;

            using (var stream = new MemoryStream())
            {
                using (var streamReader = new StreamReader(stream))
                {
                    using (var xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings {
                        Indent = true
                    }))
                    {
                        var vertexId = new VertexIdentity <PluginId>(id => id.Id.ToString());
                        var edgeId   = new EdgeIdentity <PluginId, SEdge <PluginId> >(edge => edge.Target.ToString());
                        _graph.SerializeToGraphML(xmlWriter, vertexId, edgeId);
                        xmlWriter.Flush();
                        stream.Position = 0;
                        graphXml        = streamReader.ReadToEnd();
                    }
                }
            }
            return(new DependencyGraphState
            {
                CreatedBy = GetFinalCreatedBy().ToDictionary(kv => kv.Key.Id.ToString(), kv => kv.Value),
                Consumes = GetFinalConsumes().ToDictionary(kv => kv.Key.Id.ToString(), kv => kv.Value),
                Graph = graphXml,
                ExecutedPlugins = _executedPlugins,
            });
        }
Beispiel #2
0
 /// <summary>
 /// Serializes a graph instance to a generic XML stream, using an <see cref="XmlWriter"/>.
 /// </summary>
 /// <typeparam name="TVertex">Vertex type.</typeparam>
 /// <typeparam name="TEdge">Edge type.</typeparam>
 /// <typeparam name="TGraph">Graph type.</typeparam>
 /// <param name="graph">Graph to serialize.</param>
 /// <param name="writer">XML writer.</param>
 /// <param name="vertexIdentity">The vertex identity.</param>
 /// <param name="edgeIdentity">The edge identity.</param>
 /// <param name="graphElementName">Name of the graph element.</param>
 /// <param name="vertexElementName">Name of the vertex element.</param>
 /// <param name="edgeElementName">Name of the edge element.</param>
 /// <param name="namespaceUri">XML namespace.</param>
 public static void SerializeToXml <TVertex, TEdge, TGraph>(
     [NotNull] this TGraph graph,
     [NotNull] XmlWriter writer,
     [NotNull, InstantHandle] VertexIdentity <TVertex> vertexIdentity,
     [NotNull, InstantHandle] EdgeIdentity <TVertex, TEdge> edgeIdentity,
     [NotNull] string graphElementName,
     [NotNull] string vertexElementName,
     [NotNull] string edgeElementName,
     [NotNull] string namespaceUri)
     where TGraph : IVertexAndEdgeListGraph <TVertex, TEdge>
     where TEdge : IEdge <TVertex>
 {
     SerializeToXml(
         graph,
         writer,
         vertexIdentity,
         edgeIdentity,
         graphElementName,
         vertexElementName,
         edgeElementName,
         namespaceUri,
         null,
         null,
         null);
 }
Beispiel #3
0
        /// <summary>
        /// Populates a DGML graph from a graph
        /// </summary>
        /// <typeparam name="TVertex"></typeparam>
        /// <typeparam name="TEdge"></typeparam>
        /// <param name="visitedGraph"></param>
        /// <param name="vertexIdentities"></param>
        /// <param name="edgeIdentities"></param>
        /// <param name="_formatNode"></param>
        /// <param name="_formatEdge"></param>
        /// <returns></returns>
        public static DirectedGraph ToDirectedGraphML <TVertex, TEdge>(
#if !NET20
            this
#endif
            IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph,
            VertexIdentity <TVertex> vertexIdentities,
            EdgeIdentity <TVertex, TEdge> edgeIdentities,
            Action <TVertex, DirectedGraphNode> _formatNode,
            Action <TEdge, DirectedGraphLink> _formatEdge)
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(visitedGraph != null);
            Contract.Requires(vertexIdentities != null);
            Contract.Requires(edgeIdentities != null);
            Contract.Ensures(Contract.Result <DirectedGraph>() != null);

            var algorithm = new DirectedGraphMLAlgorithm <TVertex, TEdge>(
                visitedGraph,
                vertexIdentities,
                edgeIdentities
                );

            if (_formatNode != null)
            {
                algorithm.FormatNode += _formatNode;
            }
            if (_formatEdge != null)
            {
                algorithm.FormatEdge += _formatEdge;
            }
            algorithm.Compute();

            return(algorithm.DirectedGraph);
        }
        /// <summary>
        /// Serializes a graph to a generic xml stream, using an <see cref="XmlWriter"/>.
        /// </summary>
        /// <typeparam name="TVertex">The type of the vertex.</typeparam>
        /// <typeparam name="TEdge">The type of the edge.</typeparam>
        /// <typeparam name="TGraph">The type of the graph.</typeparam>
        /// <param name="graph">The graph.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="vertexIdentity">The vertex identity.</param>
        /// <param name="edgeIdentity">The edge identity.</param>
        /// <param name="graphElementName">Name of the graph element.</param>
        /// <param name="vertexElementName">Name of the vertex element.</param>
        /// <param name="edgeElementName">Name of the edge element.</param>
        /// <param name="namespaceUri">The namespace URI.</param>
        public static void SerializeToXml <TVertex, TEdge, TGraph>(
#if !NET20
            this
#endif
            TGraph graph,
            XmlWriter writer,
            VertexIdentity <TVertex> vertexIdentity,
            EdgeIdentity <TVertex, TEdge> edgeIdentity,
            string graphElementName,
            string vertexElementName,
            string edgeElementName,
            string namespaceUri
            )
            where TGraph : IVertexAndEdgeListGraph <TVertex, TEdge>
            where TEdge : IEdge <TVertex>
        {
            SerializeToXml(
                graph,
                writer,
                vertexIdentity,
                edgeIdentity,
                graphElementName,
                vertexElementName,
                edgeElementName,
                namespaceUri,
                null,
                null,
                null);
        }
Beispiel #5
0
        public static DirectedGraph ToDirectedGraphML <TVertex, TEdge>(
            [NotNull] this IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph,
            [NotNull] VertexIdentity <TVertex> vertexIdentities,
            [NotNull] EdgeIdentity <TVertex, TEdge> edgeIdentities,
            [CanBeNull] Action <TVertex, DirectedGraphNode> formatNode,
            [CanBeNull] Action <TEdge, DirectedGraphLink> formatEdge)
            where TEdge : IEdge <TVertex>
        {
            var algorithm = new DirectedGraphMLAlgorithm <TVertex, TEdge>(
                visitedGraph,
                vertexIdentities,
                edgeIdentities);

            if (formatNode != null)
            {
                algorithm.FormatNode += formatNode;
            }
            if (formatEdge != null)
            {
                algorithm.FormatEdge += formatEdge;
            }
            algorithm.Compute();

            return(algorithm.DirectedGraph);
        }
Beispiel #6
0
        // The following use of XmlWriter.Create fails in Silverlight.

        public static void SerializeToGraphML <TVertex, TEdge, TGraph>(
#if !NET20
            this
#endif
            TGraph graph,
            string fileName,
            VertexIdentity <TVertex> vertexIdentities,
            EdgeIdentity <TVertex, TEdge> edgeIdentities)
            where TEdge : IEdge <TVertex>
            where TGraph : IEdgeListGraph <TVertex, TEdge>
        {
            Contract.Requires(fileName != null);
            Contract.Requires(fileName.Length > 0);

            var settings = new XmlWriterSettings()
            {
                Indent = true, IndentChars = "    "
            };
            var writer = XmlWriter.Create(fileName, settings);

            SerializeToGraphML <TVertex, TEdge, TGraph>(graph, writer, vertexIdentities, edgeIdentities);
            writer.Flush();
            writer.Close();
            writer.Dispose();
        }
Beispiel #7
0
        /// <summary>
        /// Serializes the given <paramref name="graph"/> instance into the given <paramref name="writer"/>.
        /// </summary>
        /// <param name="writer">The XML writer.</param>
        /// <param name="graph">Graph instance to serialize.</param>
        /// <param name="vertexIdentities">Vertex identity method.</param>
        /// <param name="edgeIdentities">Edge identity method.</param>
        public void Serialize(
            [NotNull] XmlWriter writer,
            [NotNull] TGraph graph,
            [NotNull] VertexIdentity <TVertex> vertexIdentities,
            [NotNull] EdgeIdentity <TVertex, TEdge> edgeIdentities)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (vertexIdentities is null)
            {
                throw new ArgumentNullException(nameof(vertexIdentities));
            }
            if (edgeIdentities is null)
            {
                throw new ArgumentNullException(nameof(edgeIdentities));
            }

            var worker = new WriterWorker(this, writer, graph, vertexIdentities, edgeIdentities);

            worker.Serialize();
        }
 /// <summary>
 /// Serializes the current graph to GraphML format
 /// </summary>
 public void serialize()
 {
     using (var xwriter = XmlWriter.Create("graphTest.bin"))
     {
         VertexIdentity <Fief> vIds = new VertexIdentity <Fief>(getIdFromFief);
         EdgeIdentity <Fief, TaggedEdge <Fief, string> > eIds = new EdgeIdentity <Fief, TaggedEdge <Fief, string> >(getStringFromEdge);
         this.myMap.SerializeToGraphML <Fief, TaggedEdge <Fief, string>, AdjacencyGraph <Fief, TaggedEdge <Fief, string> > >(xwriter, vIds, eIds);
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DirectedGraphMLAlgorithm{TVertex,TEdge}"/> class.
 /// </summary>
 /// <param name="visitedGraph">Graph to visit.</param>
 /// <param name="vertexIdentities">Vertex identity method.</param>
 /// <param name="edgeIdentities">Edge identity method.</param>
 public DirectedGraphMLAlgorithm(
     [NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph,
     [NotNull] VertexIdentity <TVertex> vertexIdentities,
     [NotNull] EdgeIdentity <TVertex, TEdge> edgeIdentities)
     : base(visitedGraph)
 {
     _vertexIdentities = vertexIdentities ?? throw new ArgumentNullException(nameof(vertexIdentities));
     _edgeIdentities   = edgeIdentities ?? throw new ArgumentNullException(nameof(edgeIdentities));
 }
        /// <summary>
        /// Serializes a graph to a generic xml stream, using an <see cref="XmlWriter"/>.
        /// </summary>
        /// <typeparam name="TVertex">The type of the vertex.</typeparam>
        /// <typeparam name="TEdge">The type of the edge.</typeparam>
        /// <typeparam name="TGraph">The type of the graph.</typeparam>
        /// <param name="writer">The writer.</param>
        /// <param name="graph">The graph.</param>
        /// <param name="vertexIdentity">The vertex identity.</param>
        /// <param name="edgeIdentity">The edge identity.</param>
        /// <param name="graphElementName">Name of the graph element.</param>
        /// <param name="vertexElementName">Name of the vertex element.</param>
        /// <param name="edgeElementName">Name of the edge element.</param>
        /// <param name="namespaceUri">The namespace URI (optional).</param>
        /// <param name="writeGraphAttributes">The write graph attributes (optional).</param>
        /// <param name="writeVertexAttributes">The write vertex attributes (optional).</param>
        /// <param name="writeEdgeAttributes">The write edge attributes (optional).</param>
        public static void SerializeToXml <TVertex, TEdge, TGraph>(
#if !NET20
            this
#endif
            TGraph graph,
            XmlWriter writer,
            VertexIdentity <TVertex> vertexIdentity,
            EdgeIdentity <TVertex, TEdge> edgeIdentity,
            string graphElementName,
            string vertexElementName,
            string edgeElementName,
            string namespaceUri,
            Action <XmlWriter, TGraph> writeGraphAttributes,
            Action <XmlWriter, TVertex> writeVertexAttributes,
            Action <XmlWriter, TEdge> writeEdgeAttributes
            )
            where TGraph : IVertexAndEdgeListGraph <TVertex, TEdge>
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(graph != null);
            Contract.Requires(writer != null);
            Contract.Requires(vertexIdentity != null);
            Contract.Requires(edgeIdentity != null);
            Contract.Requires(!String.IsNullOrEmpty(graphElementName));
            Contract.Requires(!String.IsNullOrEmpty(vertexElementName));
            Contract.Requires(!String.IsNullOrEmpty(edgeElementName));

            writer.WriteStartElement(graphElementName, namespaceUri);
            if (writeGraphAttributes != null)
            {
                writeGraphAttributes(writer, graph);
            }
            foreach (var vertex in graph.Vertices)
            {
                writer.WriteStartElement(vertexElementName, namespaceUri);
                writer.WriteAttributeString("id", namespaceUri, vertexIdentity(vertex));
                if (writeVertexAttributes != null)
                {
                    writeVertexAttributes(writer, vertex);
                }
                writer.WriteEndElement();
            }
            foreach (var edge in graph.Edges)
            {
                writer.WriteStartElement(edgeElementName, namespaceUri);
                writer.WriteAttributeString("id", namespaceUri, edgeIdentity(edge));
                writer.WriteAttributeString("source", namespaceUri, vertexIdentity(edge.Source));
                writer.WriteAttributeString("target", namespaceUri, vertexIdentity(edge.Target));
                if (writeEdgeAttributes != null)
                {
                    writeEdgeAttributes(writer, edge);
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
 public DirectedGraphMLAlgorithm(
     IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph,
     VertexIdentity <TVertex> vertexIdentities,
     EdgeIdentity <TVertex, TEdge> edgeIdentities)
     : base(visitedGraph)
 {
     Contract.Requires(vertexIdentities != null);
     this.vertexIdentities = vertexIdentities;
     this.edgeIdentities   = edgeIdentities;
 }
        /// <summary>
        /// Serializes the given <paramref name="graph"/> into GraphML in the given <paramref name="writer"/>.
        /// </summary>
        /// <typeparam name="TVertex">Vertex type.</typeparam>
        /// <typeparam name="TEdge">Edge type.</typeparam>
        /// <typeparam name="TGraph">Graph type.</typeparam>
        /// <param name="graph">Graph instance to serialize.</param>
        /// <param name="writer">The XML writer.</param>
        /// <param name="vertexIdentities">Vertex identity method.</param>
        /// <param name="edgeIdentities">Edge identity method.</param>
        public static void SerializeToGraphML <TVertex, TEdge, TGraph>(
            this TGraph graph,
            XmlWriter writer,
            VertexIdentity <TVertex> vertexIdentities,
            EdgeIdentity <TVertex, TEdge> edgeIdentities)
            where TEdge : IEdge <TVertex>
            where TGraph : IEdgeListGraph <TVertex, TEdge>
        {
            var serializer = new GraphMLSerializer <TVertex, TEdge, TGraph>();

            serializer.Serialize(writer, graph, vertexIdentities, edgeIdentities);
        }
Beispiel #13
0
        /// <summary>
        /// Serializes the given <paramref name="graph"/> into GraphML in the given <paramref name="writer"/>.
        /// </summary>
        /// <typeparam name="TVertex">Vertex type.</typeparam>
        /// <typeparam name="TEdge">Edge type.</typeparam>
        /// <typeparam name="TGraph">Graph type.</typeparam>
        /// <param name="graph">Graph instance to serialize.</param>
        /// <param name="writer">The XML writer.</param>
        /// <param name="vertexIdentity">Vertex identity method.</param>
        /// <param name="edgeIdentity">Edge identity method.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="graph"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="writer"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="vertexIdentity"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="edgeIdentity"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.InvalidOperationException">Failure while writing elements to GraphML.</exception>
        /// <exception cref="T:System.NotSupportedException">Serializing value on property without getter, or with unsupported property type.</exception>
        public static void SerializeToGraphML <TVertex, TEdge, TGraph>(
            [NotNull] this TGraph graph,
            [NotNull] XmlWriter writer,
            [NotNull] VertexIdentity <TVertex> vertexIdentity,
            [NotNull] EdgeIdentity <TVertex, TEdge> edgeIdentity)
            where TEdge : IEdge <TVertex>
            where TGraph : IEdgeListGraph <TVertex, TEdge>
        {
            var serializer = new GraphMLSerializer <TVertex, TEdge, TGraph>();

            serializer.Serialize(writer, graph, vertexIdentity, edgeIdentity);
        }
Beispiel #14
0
 public static DirectedGraph ToDirectedGraphML <TVertex, TEdge>(
     [NotNull] this IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph,
     [NotNull] VertexIdentity <TVertex> vertexIdentities,
     [NotNull] EdgeIdentity <TVertex, TEdge> edgeIdentities)
     where TEdge : IEdge <TVertex>
 {
     return(ToDirectedGraphML(
                visitedGraph,
                vertexIdentities,
                edgeIdentities,
                null,
                null));
 }
Beispiel #15
0
        /// <summary>
        /// Populates a DGML graph from a graph
        /// </summary>
        /// <typeparam name="TVertex"></typeparam>
        /// <typeparam name="TEdge"></typeparam>
        /// <param name="visitedGraph"></param>
        /// <param name="vertexIdentities"></param>
        /// <param name="edgeIdentities"></param>
        /// <returns></returns>
        public static DirectedGraph ToDirectedGraphML <TVertex, TEdge>(
            this IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph, VertexIdentity <TVertex> vertexIdentities,
            EdgeIdentity <TVertex, TEdge> edgeIdentities)
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(visitedGraph != null);
            Contract.Requires(vertexIdentities != null);
            Contract.Requires(edgeIdentities != null);
            Contract.Ensures(Contract.Result <DirectedGraph>() != null);

            return(ToDirectedGraphML <TVertex, TEdge>(
                       visitedGraph,
                       vertexIdentities,
                       edgeIdentities, null, null));
        }
Beispiel #16
0
        /// <summary>
        /// Serializes the given <paramref name="graph"/> into GraphML in the given <paramref name="writer"/>.
        /// </summary>
        /// <typeparam name="TVertex">Vertex type.</typeparam>
        /// <typeparam name="TEdge">Edge type.</typeparam>
        /// <typeparam name="TGraph">Graph type.</typeparam>
        /// <param name="graph">Graph instance to serialize.</param>
        /// <param name="writer">The XML writer.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="graph"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="writer"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.InvalidOperationException">Failure while writing elements to GraphML.</exception>
        /// <exception cref="T:System.NotSupportedException">Serializing value on property without getter, or with unsupported property type.</exception>
        public static void SerializeToGraphML <TVertex, TEdge, TGraph>(
            [NotNull] this TGraph graph,
            [NotNull] XmlWriter writer)
            where TEdge : IEdge <TVertex>
            where TGraph : IEdgeListGraph <TVertex, TEdge>
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }

            VertexIdentity <TVertex>      vertexIdentity = graph.GetVertexIdentity();
            EdgeIdentity <TVertex, TEdge> edgeIdentity   = graph.GetEdgeIdentity();

            SerializeToGraphML(graph, writer, vertexIdentity, edgeIdentity);
        }
Beispiel #17
0
        public void Serialize(
            XmlWriter writer,
            TGraph visitedGraph,
            VertexIdentity <TVertex> vertexIdentities,
            EdgeIdentity <TVertex, TEdge> edgeIdentities
            )
        {
            Contract.Requires(writer != null);
            Contract.Requires(visitedGraph != null);
            Contract.Requires(vertexIdentities != null);
            Contract.Requires(edgeIdentities != null);

            var worker = new WriterWorker(this, writer, visitedGraph, vertexIdentities, edgeIdentities);

            worker.Serialize();
        }
Beispiel #18
0
        public static void SerializeToGraphML <TVertex, TEdge, TGraph>(
#if !NET20
            this
#endif
            TGraph graph,
            XmlWriter writer,
            VertexIdentity <TVertex> vertexIdentities,
            EdgeIdentity <TVertex, TEdge> edgeIdentities)
            where TEdge : IEdge <TVertex>
            where TGraph : IEdgeListGraph <TVertex, TEdge>
        {
            Contract.Requires(graph != null);
            Contract.Requires(writer != null);

            var serializer = new GraphMLSerializer <TVertex, TEdge, TGraph>();

            serializer.Serialize(writer, graph, vertexIdentities, edgeIdentities);
        }
Beispiel #19
0
            public WriterWorker(
                [NotNull] GraphMLSerializer <TVertex, TEdge, TGraph> serializer,
                [NotNull] XmlWriter writer,
                [NotNull] TGraph graph,
                [NotNull] VertexIdentity <TVertex> vertexIdentities,
                [NotNull] EdgeIdentity <TVertex, TEdge> edgeIdentities)
            {
                Debug.Assert(serializer != null);
                Debug.Assert(writer != null);
                Debug.Assert(graph != null);
                Debug.Assert(vertexIdentities != null);
                Debug.Assert(edgeIdentities != null);

                _serializer       = serializer;
                _writer           = writer;
                _graph            = graph;
                _vertexIdentities = vertexIdentities;
                _edgeIdentities   = edgeIdentities;
            }
Beispiel #20
0
            public WriterWorker(
                GraphMLSerializer <TVertex, TEdge, TGraph> serializer,
                XmlWriter writer,
                TGraph visitedGraph,
                VertexIdentity <TVertex> vertexIdentities,
                EdgeIdentity <TVertex, TEdge> edgeIdentities)
            {
                Contract.Requires(serializer != null);
                Contract.Requires(writer != null);
                Contract.Requires(visitedGraph != null);
                Contract.Requires(vertexIdentities != null);
                Contract.Requires(edgeIdentities != null);

                this.serializer       = serializer;
                this.writer           = writer;
                this.visitedGraph     = visitedGraph;
                this.vertexIdentities = vertexIdentities;
                this.edgeIdentities   = edgeIdentities;
            }
        /// <summary>
        /// Serializes the given <paramref name="graph"/> into GraphML in the given <paramref name="writer"/>.
        /// </summary>
        /// <typeparam name="TVertex">Vertex type.</typeparam>
        /// <typeparam name="TEdge">Edge type.</typeparam>
        /// <typeparam name="TGraph">Graph type.</typeparam>
        /// <param name="graph">Graph instance to serialize.</param>
        /// <param name="writer">The XML writer.</param>
        /// <param name="vertexIdentities">Vertex identity method.</param>
        /// <param name="edgeIdentities">Edge identity method.</param>
        public static void SerializeToGraphML <TVertex, TEdge, TGraph>(
            [NotNull] this TGraph graph,
            [NotNull] XmlWriter writer,
            [NotNull] VertexIdentity <TVertex> vertexIdentities,
            [NotNull] EdgeIdentity <TVertex, TEdge> edgeIdentities)
            where TEdge : IEdge <TVertex>
            where TGraph : IEdgeListGraph <TVertex, TEdge>
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var serializer = new GraphMLSerializer <TVertex, TEdge, TGraph>();

            serializer.Serialize(writer, graph, vertexIdentities, edgeIdentities);
        }
Beispiel #22
0
        /// <summary>
        /// Serializes the given <paramref name="graph"/> into GraphML in a file at given <paramref name="filePath"/>.
        /// </summary>
        /// <typeparam name="TVertex">Vertex type.</typeparam>
        /// <typeparam name="TEdge">Edge type.</typeparam>
        /// <typeparam name="TGraph">Graph type.</typeparam>
        /// <param name="graph">Graph instance to serialize.</param>
        /// <param name="filePath">Path to the file where serializing the graph.</param>
        /// <param name="vertexIdentity">Vertex identity method.</param>
        /// <param name="edgeIdentity">Edge identity method.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="graph"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="vertexIdentity"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="edgeIdentity"/> is <see langword="null"/>.</exception>
        /// <exception cref="T:System.ArgumentException"><paramref name="filePath"/> is <see langword="null"/> or empty.</exception>
        /// <exception cref="T:System.InvalidOperationException">Failure while writing elements to GraphML.</exception>
        /// <exception cref="T:System.NotSupportedException">Serializing value on property without getter, or with unsupported property type.</exception>
        public static void SerializeToGraphML <TVertex, TEdge, TGraph>(
            [NotNull] this TGraph graph,
            [NotNull] string filePath,
            [NotNull] VertexIdentity <TVertex> vertexIdentity,
            [NotNull] EdgeIdentity <TVertex, TEdge> edgeIdentity)
            where TEdge : IEdge <TVertex>
            where TGraph : IEdgeListGraph <TVertex, TEdge>
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentException("Must provide a file path.", nameof(filePath));
            }

            var settings = new XmlWriterSettings {
                Indent = true, IndentChars = SerializationIndent
            };

            using (XmlWriter writer = XmlWriter.Create(filePath, settings))
            {
                SerializeToGraphML(graph, writer, vertexIdentity, edgeIdentity);
                writer.Flush();
            }
        }
Beispiel #23
0
        /// <summary>
        /// Serializes a graph instance to a generic XML stream, using an <see cref="XmlWriter"/>.
        /// </summary>
        /// <typeparam name="TVertex">Vertex type.</typeparam>
        /// <typeparam name="TEdge">Edge type.</typeparam>
        /// <typeparam name="TGraph">Graph type.</typeparam>
        /// <param name="graph">Graph to serialize.</param>
        /// <param name="writer">XML writer.</param>
        /// <param name="vertexIdentity">The vertex identity.</param>
        /// <param name="edgeIdentity">The edge identity.</param>
        /// <param name="graphElementName">Name of the graph element.</param>
        /// <param name="vertexElementName">Name of the vertex element.</param>
        /// <param name="edgeElementName">Name of the edge element.</param>
        /// <param name="namespaceUri">XMl namespace.</param>
        /// <param name="writeGraphAttributes">Delegate to write graph attributes (optional).</param>
        /// <param name="writeVertexAttributes">Delegate to write vertex attributes (optional).</param>
        /// <param name="writeEdgeAttributes">Delegate to write edge attributes (optional).</param>
        public static void SerializeToXml <TVertex, TEdge, TGraph>(
            [NotNull] this TGraph graph,
            [NotNull] XmlWriter writer,
            [NotNull, InstantHandle] VertexIdentity <TVertex> vertexIdentity,
            [NotNull, InstantHandle] EdgeIdentity <TVertex, TEdge> edgeIdentity,
            [NotNull] string graphElementName,
            [NotNull] string vertexElementName,
            [NotNull] string edgeElementName,
            [NotNull] string namespaceUri,
            [CanBeNull, InstantHandle] Action <XmlWriter, TGraph> writeGraphAttributes,
            [CanBeNull, InstantHandle] Action <XmlWriter, TVertex> writeVertexAttributes,
            [CanBeNull, InstantHandle] Action <XmlWriter, TEdge> writeEdgeAttributes)
            where TGraph : IVertexAndEdgeListGraph <TVertex, TEdge>
            where TEdge : IEdge <TVertex>
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (vertexIdentity is null)
            {
                throw new ArgumentNullException(nameof(vertexIdentity));
            }
            if (edgeIdentity is null)
            {
                throw new ArgumentNullException(nameof(edgeIdentity));
            }
            if (string.IsNullOrEmpty(graphElementName))
            {
                throw new ArgumentException($"{nameof(graphElementName)} cannot be null or empty.", nameof(graphElementName));
            }
            if (string.IsNullOrEmpty(vertexElementName))
            {
                throw new ArgumentException($"{nameof(vertexElementName)} cannot be null or empty.", nameof(vertexElementName));
            }
            if (string.IsNullOrEmpty(edgeElementName))
            {
                throw new ArgumentException($"{nameof(edgeElementName)} cannot be null or empty.", nameof(edgeElementName));
            }
            if (namespaceUri is null)
            {
                throw new ArgumentNullException(nameof(namespaceUri));
            }

            writer.WriteStartElement(graphElementName, namespaceUri);

            writeGraphAttributes?.Invoke(writer, graph);

            foreach (TVertex vertex in graph.Vertices)
            {
                writer.WriteStartElement(vertexElementName, namespaceUri);
                writer.WriteAttributeString("id", namespaceUri, vertexIdentity(vertex));
                writeVertexAttributes?.Invoke(writer, vertex);
                writer.WriteEndElement();
            }

            foreach (TEdge edge in graph.Edges)
            {
                writer.WriteStartElement(edgeElementName, namespaceUri);
                writer.WriteAttributeString("id", namespaceUri, edgeIdentity(edge));
                writer.WriteAttributeString("source", namespaceUri, vertexIdentity(edge.Source));
                writer.WriteAttributeString("target", namespaceUri, vertexIdentity(edge.Target));
                writeEdgeAttributes?.Invoke(writer, edge);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }