示例#1
0
        private void CreateVertices(Graph p_graph, GraphInformation p_graphInfo)
        {
            List <VertexItem> verticesInfo = null;
            int        i  = 0;
            VertexItem vi = null;

            verticesInfo = p_graphInfo.VertexCollection;
            for (i = 0; i < verticesInfo.Count; i++)
            {
                vi = verticesInfo[i];
                try
                {
                    p_graph.AddVertex(vi.Name);
                }
                catch (ArgumentException)
                {
                    throw new VertexInfoEmptyNameException(
                              string.Format(CultureInfo.InvariantCulture,
                                            "Vertex [#{0}] without name found",
                                            i + 1));
                }
                catch (InvalidOperationException)
                {
                    throw new VertexInfoDuplicationException(
                              string.Format(CultureInfo.InvariantCulture,
                                            "Vertices with the same name found; {0}", vi.Name));
                }
            }
        }
示例#2
0
        private void RouteEdges(Graph p_graph, GraphInformation p_graphInfo)
        {
            List <VertexItem> verticesInfo = null;
            int             i          = 0;
            VertexItem      vi         = null;
            Vertex          srcV       = null;
            Vertex          targetV    = null;
            List <EdgeItem> vEdgesInfo = null;
            int             j          = 0;
            double          weight     = 0;

            verticesInfo = p_graphInfo.VertexCollection;
            for (i = 0; i < verticesInfo.Count; i++)
            {
                vi   = verticesInfo[i];
                srcV = p_graph.Vertices[vi.Name] as Vertex;

                vEdgesInfo = vi.EdgeCollection;
                for (j = 0; j < vEdgesInfo.Count; j++)
                {
                    string targetName = vEdgesInfo[j].Target;
                    try
                    {
                        targetV = string.IsNullOrEmpty(targetName) ? null : p_graph.Vertices[targetName] as Vertex;
                        weight  = Convert.ToDouble(vEdgesInfo[j].Weight);
                        p_graph.AddEdge(srcV, targetV, weight);
                    }
                    catch (KeyNotFoundException)
                    {
                        throw new EdgeInfoEndNotExistsException(
                                  string.Format(CultureInfo.InvariantCulture,
                                                "Route edge from {0} failed, end vertex {1} does not exist.",
                                                vi.Name, targetV));
                    }
                    catch (InvalidOperationException)
                    {
                        throw new EdgeInfoEndDuplicationException(
                                  string.Format(CultureInfo.InvariantCulture,
                                                "Route edge from {0} failed, duplicate route to end vertex {1}.",
                                                vi.Name, targetName));
                    }
                    catch (FormatException)
                    {
                        throw new FormatException(
                                  string.Format(CultureInfo.InvariantCulture,
                                                "Weight [{0}] data for edge {1} ==> {2} is not a number in a valid format.",
                                                vEdgesInfo[j].Weight, vi.Name, targetName));
                    }
                }
            }
        }