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); }
/// <summary> /// Creates a Graph from a DotGraph that contains "bb" graph property and "pos" properties for every node. /// </summary> /// <param name="dot"></param> /// <returns><see cref="Graph"/></returns> public static Graph ToGraph(DotGraph dot) { Graph result = new Graph(); Rect bb = new Rect(0, 0, 100, 100); if (dot.GraphAttributes.ContainsKey("bb")) { bb = parseDotBoundingBox(dot.GraphAttributes["bb"]); } Dictionary <string, Node> nodes = new Dictionary <string, Node>(); foreach (var dotNode in dot.Nodes) { Node n = result.AddNode(fromDotNode(dotNode.Key, dotNode.Value, bb)); nodes.Add(dotNode.Key, n); } foreach (var dotEdge in dot.Edges) { (string start, string end) = dotEdge.Key; result.AddEdge(new Edge(nodes[start].Id, nodes[end].Id)); } 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);