Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/Utils/GraphVizGraph.cs @ 17401

Last change on this file since 17401 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 6.2 KB
Line 
1// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Diagnostics;
22using System.Globalization;
23using System.IO;
24using System.Text.RegularExpressions;
25
26namespace ICSharpCode.NRefactory.Utils
27{
28  /// <summary>
29  /// GraphViz graph.
30  /// </summary>
31  public sealed class GraphVizGraph
32  {
33    List<GraphVizNode> nodes = new List<GraphVizNode>();
34    List<GraphVizEdge> edges = new List<GraphVizEdge>();
35   
36    public string rankdir;
37    public string Title;
38   
39    public void AddEdge(GraphVizEdge edge)
40    {
41      edges.Add(edge);
42    }
43   
44    public void AddNode(GraphVizNode node)
45    {
46      nodes.Add(node);
47    }
48   
49    public void Save(string fileName)
50    {
51      using (StreamWriter writer = new StreamWriter(fileName))
52        Save(writer);
53    }
54   
55    public void Show()
56    {
57      Show(null);
58    }
59   
60    public void Show(string name)
61    {
62      if (name == null)
63        name = Title;
64      if (name != null)
65        foreach (char c in Path.GetInvalidFileNameChars())
66          name = name.Replace(c, '-');
67      string fileName = name != null ? Path.Combine(Path.GetTempPath(), name) : Path.GetTempFileName();
68      Save(fileName + ".gv");
69      Process.Start("dot", "\"" + fileName + ".gv\" -Tpng -o \"" + fileName + ".png\"").WaitForExit();
70      Process.Start(fileName + ".png");
71    }
72   
73    static string Escape(string text)
74    {
75      if (Regex.IsMatch(text, @"^[\w\d]+$")) {
76        return text;
77      } else {
78        return "\"" + text.Replace("\\", "\\\\").Replace("\r", "").Replace("\n", "\\n").Replace("\"", "\\\"") + "\"";
79      }
80    }
81   
82    static void WriteGraphAttribute(TextWriter writer, string name, string value)
83    {
84      if (value != null)
85        writer.WriteLine("{0}={1};", name, Escape(value));
86    }
87   
88    internal static void WriteAttribute(TextWriter writer, string name, double? value, ref bool isFirst)
89    {
90      if (value != null) {
91        WriteAttribute(writer, name, value.Value.ToString(CultureInfo.InvariantCulture), ref isFirst);
92      }
93    }
94   
95    internal static void WriteAttribute(TextWriter writer, string name, bool? value, ref bool isFirst)
96    {
97      if (value != null) {
98        WriteAttribute(writer, name, value.Value ? "true" : "false", ref isFirst);
99      }
100    }
101   
102    internal static void WriteAttribute(TextWriter writer, string name, string value, ref bool isFirst)
103    {
104      if (value != null) {
105        if (isFirst)
106          isFirst = false;
107        else
108          writer.Write(',');
109        writer.Write("{0}={1}", name, Escape(value));
110      }
111    }
112   
113    public void Save(TextWriter writer)
114    {
115      if (writer == null)
116        throw new ArgumentNullException("writer");
117      writer.WriteLine("digraph G {");
118      writer.WriteLine("node [fontsize = 16];");
119      WriteGraphAttribute(writer, "rankdir", rankdir);
120      foreach (GraphVizNode node in nodes) {
121        node.Save(writer);
122      }
123      foreach (GraphVizEdge edge in edges) {
124        edge.Save(writer);
125      }
126      writer.WriteLine("}");
127    }
128  }
129 
130  public sealed class GraphVizEdge
131  {
132    public readonly string Source, Target;
133   
134    /// <summary>edge stroke color</summary>
135    public string color;
136    /// <summary>use edge to affect node ranking</summary>
137    public bool? constraint;
138   
139    public string label;
140   
141    public string style;
142   
143    /// <summary>point size of label</summary>
144    public int? fontsize;
145   
146    public GraphVizEdge(string source, string target)
147    {
148      if (source == null)
149        throw new ArgumentNullException("source");
150      if (target == null)
151        throw new ArgumentNullException("target");
152      this.Source = source;
153      this.Target = target;
154    }
155   
156    public GraphVizEdge(int source, int target)
157    {
158      this.Source = source.ToString(CultureInfo.InvariantCulture);
159      this.Target = target.ToString(CultureInfo.InvariantCulture);
160    }
161   
162    public void Save(TextWriter writer)
163    {
164      writer.Write("{0} -> {1} [", Source, Target);
165      bool isFirst = true;
166      GraphVizGraph.WriteAttribute(writer, "label", label, ref isFirst);
167      GraphVizGraph.WriteAttribute(writer, "style", style, ref isFirst);
168      GraphVizGraph.WriteAttribute(writer, "fontsize", fontsize, ref isFirst);
169      GraphVizGraph.WriteAttribute(writer, "color", color, ref isFirst);
170      GraphVizGraph.WriteAttribute(writer, "constraint", constraint, ref isFirst);
171      writer.WriteLine("];");
172    }
173  }
174 
175  public sealed class GraphVizNode
176  {
177    public readonly string ID;
178    public string label;
179   
180    public string labelloc;
181   
182    /// <summary>point size of label</summary>
183    public int? fontsize;
184   
185    /// <summary>minimum height in inches</summary>
186    public double? height;
187   
188    /// <summary>space around label</summary>
189    public string margin;
190   
191    /// <summary>node shape</summary>
192    public string shape;
193   
194    public GraphVizNode(string id)
195    {
196      if (id == null)
197        throw new ArgumentNullException("id");
198      this.ID = id;
199    }
200   
201    public GraphVizNode(int id)
202    {
203      this.ID = id.ToString(CultureInfo.InvariantCulture);
204    }
205   
206    public void Save(TextWriter writer)
207    {
208      writer.Write(ID);
209      writer.Write(" [");
210      bool isFirst = true;
211      GraphVizGraph.WriteAttribute(writer, "label", label, ref isFirst);
212      GraphVizGraph.WriteAttribute(writer, "labelloc", labelloc, ref isFirst);
213      GraphVizGraph.WriteAttribute(writer, "fontsize", fontsize, ref isFirst);
214      GraphVizGraph.WriteAttribute(writer, "margin", margin, ref isFirst);
215      GraphVizGraph.WriteAttribute(writer, "shape", shape, ref isFirst);
216      writer.WriteLine("];");
217    }
218  }
219}
Note: See TracBrowser for help on using the repository browser.