forked from YaccConstructor/QuickGraph
-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathMsaglGraphPopulator.cs
110 lines (93 loc) · 3.96 KB
/
MsaglGraphPopulator.cs
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
using System.Diagnostics;
using JetBrains.Annotations;
using Microsoft.Msagl.Drawing;
using QuikGraph.Algorithms;
namespace QuikGraph.MSAGL
{
/// <summary>
/// Base class for MSAGL graph populator.
/// </summary>
/// <typeparam name="TVertex">Vertex type.</typeparam>
/// <typeparam name="TEdge">Edge type.</typeparam>
public abstract class MsaglGraphPopulator<TVertex, TEdge> : AlgorithmBase<IEdgeListGraph<TVertex, TEdge>>
where TEdge : IEdge<TVertex>
{
/// <summary>
/// Initializes a new instance of the <see cref="MsaglGraphPopulator{TVertex,TEdge}"/> class.
/// </summary>
/// <param name="visitedGraph">Graph to convert to MSAGL graph.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="visitedGraph"/> is <see langword="null"/>.</exception>
protected MsaglGraphPopulator([NotNull] IEdgeListGraph<TVertex, TEdge> visitedGraph)
: base(visitedGraph)
{
}
/// <summary>
/// MSAGL graph corresponding to <see cref="AlgorithmBase{TGraph}.VisitedGraph"/>.
/// </summary>
public Graph MsaglGraph { get; private set; }
#region Events
/// <summary>
/// Fired when a node is added to the graph.
/// </summary>
public event MsaglVertexNodeEventHandler<TVertex> NodeAdded;
/// <summary>
/// Called when a <see cref="T:Microsoft.Msagl.Drawing.Node"/> is added.
/// </summary>
/// <param name="args">Event arguments.</param>
protected virtual void OnNodeAdded([NotNull] MsaglVertexEventArgs<TVertex> args)
{
Debug.Assert(args != null);
NodeAdded?.Invoke(this, args);
}
/// <summary>
/// Fired when an edge is added to the graph.
/// </summary>
public event MsaglEdgeEventHandler<TVertex, TEdge> EdgeAdded;
/// <summary>
/// Called when an <see cref="T:Microsoft.Msagl.Drawing.Edge"/> is added.
/// </summary>
/// <param name="args">Event arguments.</param>
protected virtual void OnEdgeAdded([NotNull] MsaglEdgeEventArgs<TVertex, TEdge> args)
{
Debug.Assert(args != null);
EdgeAdded?.Invoke(this, args);
}
#endregion
#region AlgorithmBase<TGraph>
/// <inheritdoc />
protected override void InternalCompute()
{
MsaglGraph = new Graph(string.Empty)
{
Directed = VisitedGraph.IsDirected
};
foreach (TVertex vertex in VisitedGraph.Vertices)
{
Node node = AddNode(vertex);
node.UserData = vertex;
OnNodeAdded(new MsaglVertexEventArgs<TVertex>(vertex, node));
}
foreach (TEdge edge in VisitedGraph.Edges)
{
Edge msaglEdge = AddEdge(edge);
msaglEdge.UserData = edge;
OnEdgeAdded(new MsaglEdgeEventArgs<TVertex, TEdge>(edge, msaglEdge));
}
}
#endregion
/// <summary>
/// Called when a <paramref name="vertex"/> should be added to the graph.
/// </summary>
/// <param name="vertex">Vertex to add.</param>
/// <returns>Added <see cref="T:Microsoft.Msagl.Drawing.Node"/>.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="vertex"/> is <see langword="null"/>.</exception>
protected abstract Node AddNode([NotNull] TVertex vertex);
/// <summary>
/// Called when an <paramref name="edge"/> should be added to the graph.
/// </summary>
/// <param name="edge">Edge to add.</param>
/// <returns>Added <see cref="T:Microsoft.Msagl.Drawing.Edge"/>.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="edge"/> is <see langword="null"/>.</exception>
protected abstract Edge AddEdge([NotNull] TEdge edge);
}
}