public static DotGraph ToDot(Graph gr) { DotGraph result = new DotGraph(); foreach (var node in gr.Nodes) { DotNode dotNode = new DotNode(); dotNode.Attributes.Add("label", node.NodeName); var id = ensureUniqueId(result.Nodes, node.Id); result.Nodes.Add(id, dotNode); } foreach (var edge in gr.Edges) { var key = (edge.StartId.ToString(), edge.EndId.ToString()); if (!result.Edges.ContainsKey(key)) { result.Edges.Add(key, new List <DotEdge>()); } DotEdge dotEdge = new DotEdge(); result.Edges[key].Add(dotEdge); } return(result); }
public DotGraph Load() { DotGraph gr = new DotGraph(); dotString = dotString.Trim(); if (dotString.Length < 7) { return(gr); } List <string> tokens = Tokenizer.Tokenize(dotString); tokenIter = tokens.GetEnumerator(); string?first = nextToken(); if (string.IsNullOrWhiteSpace(first) || (!first.Equals("graph", casei) && !first.Equals("digraph", casei) && !first.Equals("strict", casei))) { return(gr); } if (first.Equals("strict", casei)) { gr.IsStrict = true; tokenIter.MoveNext(); } gr.IsDirected = tokenIter.Current.Equals("digraph", casei); tokenIter.MoveNext(); if (tokenIter.Current != "{") { tokenIter.MoveNext(); } if (tokenIter.Current == "{") { string?lastToken = null; IDictionary <string, string> attributeContext = gr.GraphAttributes; for (string?token = nextToken(), nextTok = nextToken(); token != "}" && token != null; token = nextTok, nextTok = nextToken()) { if (token == "--" || token == "->") { if (nextTok == null || nextTok == "}") { break; } gr.Nodes.TryAdd(nextTok, new DotNode()); if (lastToken != null) { if (!gr.Edges.ContainsKey((lastToken, nextTok))) { gr.Edges.Add((lastToken, nextTok), new List <DotEdge>()); } var edge = new DotEdge(); gr.Edges[(lastToken, nextTok)].Add(edge);