예제 #1
0
        /// Loads from a reader.
        /// \param reader A reader on the input file, e.g. a StreamReader.
        /// \param directedness Specifies the directedness of the graph to be loaded. Possible values:
        /// - \c Directedness.Directed: each created arc will be directed.
        /// - \c Directedness.Undirected: each created arc will be an edge (i.e. undirected).
        /// \return the loaded nodes, by index ascending
        public Node[] Load(TextReader reader, Directedness directedness)
        {
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();

            string[] tokens;
            var      whitespaces = new Regex(@"\s+");

            // first line: number of nodes and arcs
            tokens = whitespaces.Split(reader.ReadLine());
            int nodeCount = int.Parse(tokens[0], CultureInfo.InvariantCulture);
            int arcCount  = int.Parse(tokens[1], CultureInfo.InvariantCulture);

            Node[] nodes = new Node[nodeCount];
            for (int i = 0; i < nodeCount; i++)
            {
                nodes[i] = buildableGraph.AddNode();
            }

            Extensions.Clear();

            for (int i = 0; i < arcCount; i++)
            {
                tokens = whitespaces.Split(reader.ReadLine());
                int a = (int)(long.Parse(tokens[0], CultureInfo.InvariantCulture) - StartIndex);
                int b = (int)(long.Parse(tokens[1], CultureInfo.InvariantCulture) - StartIndex);

                Arc arc = buildableGraph.AddArc(nodes[a], nodes[b], directedness);

                int extensionCount = tokens.Length - 2;
                for (int j = 0; j < extensionCount - Extensions.Count; j++)
                {
                    Extensions.Add(new Dictionary <Arc, string>());
                }
                for (int j = 0; j < extensionCount; j++)
                {
                    Extensions[j][arc] = tokens[2 + j];
                }
            }

            return(nodes);
        }
예제 #2
0
        public Node[] Load(TextReader reader, Directedness directedness)
        {
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();
            Regex regex = new Regex("\\s+");

            string[] array = regex.Split(reader.ReadLine());
            int      num   = int.Parse(array[0], CultureInfo.InvariantCulture);
            int      num2  = int.Parse(array[1], CultureInfo.InvariantCulture);

            Node[] array2 = new Node[num];
            for (int i = 0; i < num; i++)
            {
                array2[i] = buildableGraph.AddNode();
            }
            Extensions.Clear();
            for (int j = 0; j < num2; j++)
            {
                array = regex.Split(reader.ReadLine());
                int num3 = (int)(long.Parse(array[0], CultureInfo.InvariantCulture) - StartIndex);
                int num4 = (int)(long.Parse(array[1], CultureInfo.InvariantCulture) - StartIndex);
                Arc key  = buildableGraph.AddArc(array2[num3], array2[num4], directedness);
                int num5 = array.Length - 2;
                for (int k = 0; k < num5 - Extensions.Count; k++)
                {
                    Extensions.Add(new Dictionary <Arc, string>());
                }
                for (int l = 0; l < num5; l++)
                {
                    Extensions[l][key] = array[2 + l];
                }
            }
            return(array2);
        }
예제 #3
0
        /// Loads from an XML document.
        public void Load(XDocument doc)
        {
            // Namespaces are ignored so we can load broken documents.
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();
            XElement xGraphML = doc.Root;

            // load properties
            Properties.Clear();
            Dictionary <string, GraphMLProperty> propertyById = new Dictionary <string, GraphMLProperty>();

            foreach (var xKey in Utils.ElementsLocal(xGraphML, "key"))
            {
                foreach (var handler in PropertyLoaders)
                {
                    try
                    {
                        GraphMLProperty p = handler(xKey);
                        Properties.Add(p);
                        propertyById[p.Id] = p;
                        break;
                    }
                    catch (ArgumentException) { }
                }
            }

            // load graph
            XElement     xGraph = Utils.ElementLocal(xGraphML, "graph");
            Directedness defaultDirectedness = (xGraph.Attribute("edgedefault").Value == "directed" ?
                                                Directedness.Directed : Directedness.Undirected);

            ReadProperties(propertyById, xGraph, Graph);
            // load nodes
            Dictionary <string, Node> nodeById = new Dictionary <string, Node>();

            foreach (var xNode in Utils.ElementsLocal(xGraph, "node"))
            {
                Node node = buildableGraph.AddNode();
                nodeById[xNode.Attribute("id").Value] = node;
                ReadProperties(propertyById, xNode, node);
            }
            // load arcs
            foreach (var xArc in Utils.ElementsLocal(xGraph, "edge"))
            {
                Node u = nodeById[xArc.Attribute("source").Value];
                Node v = nodeById[xArc.Attribute("target").Value];

                Directedness dir     = defaultDirectedness;
                XAttribute   dirAttr = xArc.Attribute("directed");
                if (dirAttr != null)
                {
                    dir = (dirAttr.Value == "true" ? Directedness.Directed : Directedness.Undirected);
                }

                Arc arc = buildableGraph.AddArc(u, v, dir);
                ReadProperties(propertyById, xArc, arc);
            }
        }
        public void Load(XDocument doc)
        {
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();
            XElement root = doc.Root;

            Properties.Clear();
            Dictionary <string, GraphMLProperty> dictionary = new Dictionary <string, GraphMLProperty>();

            foreach (XElement item in Utils.ElementsLocal(root, "key"))
            {
                using (List <Func <XElement, GraphMLProperty> > .Enumerator enumerator2 = PropertyLoaders.GetEnumerator())
                {
                    while (true)
                    {
                        if (enumerator2.MoveNext())
                        {
                            Func <XElement, GraphMLProperty> current2 = enumerator2.Current;
                            try
                            {
                                GraphMLProperty graphMLProperty = current2(item);
                                Properties.Add(graphMLProperty);
                                dictionary[graphMLProperty.Id] = graphMLProperty;
                            }
                            catch (ArgumentException)
                            {
                                continue;
                            }
                        }
                        break;
                    }
                }
            }
            XElement     xElement     = Utils.ElementLocal(root, "graph");
            Directedness directedness = (!(xElement.Attribute("edgedefault").Value == "directed")) ? Directedness.Undirected : Directedness.Directed;

            ReadProperties(dictionary, xElement, Graph);
            Dictionary <string, Node> dictionary2 = new Dictionary <string, Node>();

            foreach (XElement item2 in Utils.ElementsLocal(xElement, "node"))
            {
                Node node = buildableGraph.AddNode();
                dictionary2[item2.Attribute("id").Value] = node;
                ReadProperties(dictionary, item2, node);
            }
            foreach (XElement item3 in Utils.ElementsLocal(xElement, "edge"))
            {
                Node         u             = dictionary2[item3.Attribute("source").Value];
                Node         v             = dictionary2[item3.Attribute("target").Value];
                Directedness directedness2 = directedness;
                XAttribute   xAttribute    = item3.Attribute("directed");
                if (xAttribute != null)
                {
                    directedness2 = ((!(xAttribute.Value == "true")) ? Directedness.Undirected : Directedness.Directed);
                }
                Arc arc = buildableGraph.AddArc(u, v, directedness2);
                ReadProperties(dictionary, item3, arc);
            }
        }
예제 #5
0
        /// Loads from a reader.
        /// \param reader A reader on the input file, e.g. a StreamReader.
        /// \param directedness Specifies the directedness of the graph to be loaded. Possible values:
        /// - \c Directedness.Directed: each created arc will be directed.
        /// - \c Directedness.Undirected: each created arc will be undirected.
        /// - \c null (default): arcs defined in \c \@arcs sections will be directed,
        ///   while those defined in \c \@edges sections will be undirected.
        public void Load(TextReader reader, Directedness?directedness)
        {
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();

            NodeMaps.Clear();
            var nodeFromLabel = new Dictionary <string, Node>();

            ArcMaps.Clear();
            Attributes.Clear();

            Regex         splitRegex       = new Regex(@"\s*((""(\""|.)*"")|(\S+))\s*", RegexOptions.Compiled);
            string        section          = "";
            Directedness  currDir          = Directedness.Directed;   // are currently read arcs directed?
            bool          prevHeader       = false;
            List <string> columnNames      = null;
            int           labelColumnIndex = -1;

            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                line = line.Trim();
                if (line == "" || line[0] == '#')
                {
                    continue;
                }
                List <string> tokens = splitRegex.Matches(line).Cast <Match>()
                                       .Select(m =>
                {
                    string s = m.Groups[1].Value;
                    if (s == "")
                    {
                        return(s);
                    }
                    if (s[0] == '"' && s[s.Length - 1] == '"')
                    {
                        s = Unescape(s.Substring(1, s.Length - 2));
                    }
                    return(s);
                }).ToList();
                string first = tokens.First();

                // header?
                if (line[0] == '@')
                {
                    section = first.Substring(1);
                    currDir = directedness ?? (section == "arcs" ? Directedness.Directed : Directedness.Undirected);

                    prevHeader = true;
                    continue;
                }

                switch (section)
                {
                case "nodes":
                case "red_nodes":
                case "blue_nodes":
                {
                    if (prevHeader)
                    {
                        columnNames = tokens;
                        for (int i = 0; i < columnNames.Count; i++)
                        {
                            string column = columnNames[i];
                            if (column == "label")
                            {
                                labelColumnIndex = i;
                            }
                            if (!NodeMaps.ContainsKey(column))
                            {
                                NodeMaps[column] = new Dictionary <Node, string>();
                            }
                        }
                    }
                    else
                    {
                        Node node = buildableGraph.AddNode();
                        for (int i = 0; i < tokens.Count; i++)
                        {
                            NodeMaps[columnNames[i]][node] = tokens[i];
                            if (i == labelColumnIndex)
                            {
                                nodeFromLabel[tokens[i]] = node;
                            }
                        }
                    }
                } break;

                case "arcs":
                case "edges":
                {
                    if (prevHeader)
                    {
                        columnNames = tokens;
                        foreach (var column in columnNames)
                        {
                            if (!ArcMaps.ContainsKey(column))
                            {
                                ArcMaps[column] = new Dictionary <Arc, string>();
                            }
                        }
                    }
                    else
                    {
                        Node u   = nodeFromLabel[tokens[0]];
                        Node v   = nodeFromLabel[tokens[1]];
                        Arc  arc = buildableGraph.AddArc(u, v, currDir);
                        for (int i = 2; i < tokens.Count; i++)
                        {
                            ArcMaps[columnNames[i - 2]][arc] = tokens[i];
                        }
                    }
                } break;

                case "attributes":
                {
                    Attributes[tokens[0]] = tokens[1];
                } break;
                }
                prevHeader = false;
            }             // while can read from file
        }