Repository navigation
Expand file tree
/
Copy pathGraphvizAlgorithm.cs
More file actions
354 lines (304 loc) · 12 KB
/
Copy pathGraphvizAlgorithm.cs
File metadata and controls
354 lines (304 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
#if SUPPORTS_AGGRESSIVE_INLINING
using System.Runtime.CompilerServices;
#endif
using JetBrains.Annotations;
using QuikGraph.Graphviz.Dot;
namespace QuikGraph.Graphviz
{
/// <summary>
/// An algorithm that renders a graph to the Graphviz DOT format.
/// </summary>
/// <typeparam name="TVertex">Vertex type.</typeparam>
/// <typeparam name="TEdge">Edge type.</typeparam>
public class GraphvizAlgorithm<TVertex, TEdge>
where TEdge : IEdge<TVertex>
{
[NotNull]
private readonly Dictionary<TVertex, int> _verticesIds = new Dictionary<TVertex, int>();
/// <summary>
/// Initializes a new instance of the <see cref="GraphvizAlgorithm{TVertex,TEdge}"/> class.
/// </summary>
/// <param name="graph">Graph to convert to DOT.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="graph"/> is <see langword="null"/>.</exception>
public GraphvizAlgorithm([NotNull] IEdgeListGraph<TVertex, TEdge> graph)
: this(graph, GraphvizImageType.Png)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GraphvizAlgorithm{TVertex,TEdge}"/> class.
/// </summary>
/// <param name="graph">Graph to convert to DOT.</param>
/// <param name="imageType">Target output image type.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="graph"/> is <see langword="null"/>.</exception>
public GraphvizAlgorithm(
[NotNull] IEdgeListGraph<TVertex, TEdge> graph,
GraphvizImageType imageType)
{
ClusterCount = 0;
_visitedGraph = graph ?? throw new ArgumentNullException(nameof(graph));
ImageType = imageType;
GraphFormat = new GraphvizGraph();
CommonVertexFormat = new GraphvizVertex();
CommonEdgeFormat = new GraphvizEdge();
}
/// <summary>
/// Graph format.
/// </summary>
[NotNull]
public GraphvizGraph GraphFormat { get; }
/// <summary>
/// Common vertex format.
/// </summary>
[NotNull]
public GraphvizVertex CommonVertexFormat { get; }
/// <summary>
/// Common edge format.
/// </summary>
[NotNull]
public GraphvizEdge CommonEdgeFormat { get; }
[NotNull]
private IEdgeListGraph<TVertex, TEdge> _visitedGraph;
/// <summary>
/// Graph to convert.
/// </summary>
/// <exception cref="T:System.ArgumentNullException">Set value is <see langword="null"/>.</exception>
[NotNull]
public IEdgeListGraph<TVertex, TEdge> VisitedGraph
{
get => _visitedGraph;
set => _visitedGraph = value ?? throw new ArgumentNullException(nameof(value));
}
/// <summary>
/// Dot output stream.
/// </summary>
/// <remarks>Not <see langword="null"/> after a run of <see cref="Generate()"/> or <see cref="Generate(IDotEngine,string)"/>.</remarks>
public StringWriter Output { get; private set; }
/// <summary>
/// Current image output type.
/// </summary>
public GraphvizImageType ImageType { get; set; }
internal int ClusterCount { get; set; }
/// <summary>
/// Fired when formatting a clustered graph.
/// </summary>
public event FormatClusterEventHandler<TVertex, TEdge> FormatCluster;
private void OnFormatCluster([NotNull] IVertexAndEdgeListGraph<TVertex, TEdge> cluster)
{
Debug.Assert(cluster != null);
FormatClusterEventHandler<TVertex, TEdge> formatCluster = FormatCluster;
if (formatCluster is null)
return;
var args = new FormatClusterEventArgs<TVertex, TEdge>(cluster, new GraphvizGraph());
formatCluster(this, args);
string dot = args.GraphFormat.ToDot();
if (dot.Length != 0)
{
Output.WriteLine(dot);
}
}
/// <summary>
/// Fired when formatting a vertex.
/// </summary>
public event FormatVertexEventHandler<TVertex> FormatVertex;
private void OnFormatVertex([NotNull] TVertex vertex)
{
Debug.Assert(vertex != null);
Output.Write($"{_verticesIds[vertex]}");
FormatVertexEventHandler<TVertex> formatVertex = FormatVertex;
if (formatVertex != null)
{
var vertexFormat = new GraphvizVertex();
formatVertex(this, new FormatVertexEventArgs<TVertex>(vertex, vertexFormat));
string dot = vertexFormat.InternalToDot(CommonVertexFormat);
if (dot.Length != 0)
{
Output.Write($" [{dot}]");
}
}
Output.WriteLine(";");
}
/// <summary>
/// Fired when formatting an edge.
/// </summary>
public event FormatEdgeAction<TVertex, TEdge> FormatEdge;
private void OnFormatEdge([NotNull] TEdge edge)
{
Debug.Assert(edge != null);
FormatEdgeAction<TVertex, TEdge> formatEdge = FormatEdge;
if (formatEdge != null)
{
var edgeFormat = new GraphvizEdge();
formatEdge(this, new FormatEdgeEventArgs<TVertex, TEdge>(edge, edgeFormat));
string dot = edgeFormat.ToDot();
if (dot.Length != 0)
{
Output.Write($" [{dot}]");
}
}
Output.WriteLine(";");
}
/// <summary>
/// Generates the DOT corresponding to <see cref="VisitedGraph"/>.
/// </summary>
/// <returns>DOT serialization of <see cref="VisitedGraph"/>.</returns>
[Pure]
[NotNull]
public string Generate()
{
ClusterCount = 0;
_verticesIds.Clear();
Output = new StringWriter();
// Build vertex id map
int i = 0;
var vertices = new HashSet<TVertex>(VisitedGraph.Vertices);
foreach (TVertex vertex in VisitedGraph.Vertices)
{
_verticesIds.Add(vertex, i++);
}
var edges = new HashSet<TEdge>(VisitedGraph.Edges);
Output.Write(VisitedGraph.IsDirected ? "digraph " : "graph ");
Output.Write(GraphFormat.Name);
Output.WriteLine(" {");
string graphFormat = GraphFormat.ToDot();
if (graphFormat.Length > 0)
{
Output.WriteLine(graphFormat);
}
string vertexFormat = CommonVertexFormat.ToDot();
if (vertexFormat.Length > 0)
{
Output.WriteLine($"node [{vertexFormat}];");
}
string edgeFormat = CommonEdgeFormat.ToDot();
if (edgeFormat.Length > 0)
{
Output.WriteLine($"edge [{edgeFormat}];");
}
if (VisitedGraph is IClusteredGraph clusteredGraph)
{
WriteClusters(vertices, edges, clusteredGraph);
}
WriteVertices(vertices);
WriteEdges(edges);
Output.Write("}");
return Output.ToString();
}
/// <summary>
/// Generates the DOT corresponding to <see cref="VisitedGraph"/> using <paramref name="dot"/> engine
/// and puts result in <paramref name="outputFilePath"/>.
/// </summary>
/// <returns>File path containing DOT serialization of <see cref="VisitedGraph"/>.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="dot"/> is <see langword="null"/>.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="outputFilePath"/> is <see langword="null"/> or empty.</exception>
[NotNull]
public string Generate([NotNull] IDotEngine dot, [NotNull] string outputFilePath)
{
if (dot is null)
throw new ArgumentNullException(nameof(dot));
if (string.IsNullOrEmpty(outputFilePath))
throw new ArgumentException("Output file path cannot be null or empty.", nameof(outputFilePath));
return dot.Run(ImageType, Generate(), outputFilePath);
}
private void WriteClusters(
[NotNull, ItemNotNull] ICollection<TVertex> remainingVertices,
[NotNull, ItemNotNull] ICollection<TEdge> remainingEdges,
[NotNull] IClusteredGraph parent)
{
Debug.Assert(remainingVertices != null);
Debug.Assert(remainingEdges != null);
Debug.Assert(parent != null);
++ClusterCount;
foreach (IVertexAndEdgeListGraph<TVertex, TEdge> subGraph in parent.Clusters)
{
Output.Write($"subgraph cluster{ClusterCount}");
Output.WriteLine(" {");
OnFormatCluster(subGraph);
if (subGraph is IClusteredGraph clusteredGraph)
{
WriteClusters(remainingVertices, remainingEdges, clusteredGraph);
}
if (parent.Collapsed)
{
foreach (TVertex vertex in subGraph.Vertices)
{
remainingVertices.Remove(vertex);
}
foreach (TEdge edge in subGraph.Edges)
{
remainingEdges.Remove(edge);
}
}
else
{
WriteVertices(remainingVertices, subGraph.Vertices);
WriteEdges(remainingEdges, subGraph.Edges);
}
Output.WriteLine("}");
}
}
#if SUPPORTS_AGGRESSIVE_INLINING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private void WriteVertex([NotNull] TVertex vertex)
{
Debug.Assert(vertex != null);
OnFormatVertex(vertex);
}
private void WriteVertices([NotNull, ItemNotNull] IEnumerable<TVertex> vertices)
{
Debug.Assert(vertices != null);
foreach (TVertex vertex in vertices)
{
WriteVertex(vertex);
}
}
private void WriteVertices(
[NotNull, ItemNotNull] ICollection<TVertex> remainingVertices,
[NotNull, ItemNotNull] IEnumerable<TVertex> vertices)
{
Debug.Assert(remainingVertices != null);
Debug.Assert(vertices != null);
foreach (TVertex vertex in vertices.Where(remainingVertices.Contains))
{
WriteVertex(vertex);
remainingVertices.Remove(vertex);
}
}
#if SUPPORTS_AGGRESSIVE_INLINING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private void WriteEdge([NotNull] TEdge edge)
{
Debug.Assert(edge != null);
Output.Write(VisitedGraph.IsDirected
? $"{_verticesIds[edge.Source]} -> {_verticesIds[edge.Target]}"
: $"{_verticesIds[edge.Source]} -- {_verticesIds[edge.Target]}");
OnFormatEdge(edge);
}
private void WriteEdges([NotNull, ItemNotNull] IEnumerable<TEdge> edges)
{
Debug.Assert(edges != null);
foreach (TEdge edge in edges)
{
WriteEdge(edge);
}
}
private void WriteEdges(
[NotNull, ItemNotNull] ICollection<TEdge> remainingEdges,
[NotNull, ItemNotNull] IEnumerable<TEdge> edges)
{
Debug.Assert(remainingEdges != null);
Debug.Assert(edges != null);
foreach (TEdge edge in edges.Where(remainingEdges.Contains))
{
WriteEdge(edge);
remainingEdges.Remove(edge);
}
}
}
}