Пример #1
0
        /// <summary>
        /// Write a graph to a file
        /// If the graph is not initialized throws GraphInitializationException
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>Is everything OK</returns>
        public bool WriteFile(Graph.IGraphInterface graph)
        {
            if (!graph.GetIsInitialized())
            {
                throw new MyException.GraphException.GraphInitializationException();
            }

            if (ExistsFile())
            {
                DeleteFile();
            }

            CreateFile();

            using (StreamWriter streamWriter = new StreamWriter(GetPath()))
            {
                // Header
                streamWriter.WriteLine(READERWRITERHEADER + Graph.Graph.GraphRepresentationEnum.adjacencyList);
                streamWriter.WriteLine(READERWRITERBALLAST);
                streamWriter.WriteLine(READERWRITERNAME + graph.GetName());
                streamWriter.WriteLine(READERWRITERCOUNTVERTICES + graph.GetRealCountVertices());
                streamWriter.WriteLine(READERWRITERBALLAST);

                // Graph
                streamWriter.WriteLine(READERWRITERGRAPH);

                foreach (Graph.Vertex vertex in graph.AllVertices())
                {
                    streamWriter.WriteLine(vertex.GetUserName());

                    foreach (Graph.Vertex neighbour in graph.Neighbours(vertex))
                    {
                        streamWriter.WriteLine(LEFTSEPARATORADJACENCYLIST + neighbour.GetUserName() + RIGHTSEPARATORADJACENCYLIST);
                    }
                }

                // Colored graph
                streamWriter.WriteLine(READERWRITERBALLAST);
                streamWriter.Write(READERWRITERCOLOREDGRAPH);
            }

            return(true);
        }