1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Collections.Specialized;
|
---|
6 | using System.Text;
|
---|
7 | using Netron.Diagramming.Core.Analysis;
|
---|
8 | namespace Netron.Diagramming.Core
|
---|
9 | {
|
---|
10 | public partial class Model : IGraph, ITree
|
---|
11 | {
|
---|
12 | #region Fields
|
---|
13 | private bool mIsDirected;
|
---|
14 | private ITree mSpanningTree;
|
---|
15 | #endregion
|
---|
16 |
|
---|
17 | /// <summary>
|
---|
18 | /// Get the in-degree of a node, the number of edges for which the node
|
---|
19 | /// is the target.
|
---|
20 | /// </summary>
|
---|
21 | /// <param name="node"></param>
|
---|
22 | /// <returns></returns>
|
---|
23 | int IGraph.InDegree(INode node)
|
---|
24 | {
|
---|
25 | return (node as INode).InDegree;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /// <summary>
|
---|
29 | /// Get the out-degree of a node, the number of edges for which the node
|
---|
30 | /// is the source.
|
---|
31 | /// </summary>
|
---|
32 | /// <param name="node"></param>
|
---|
33 | /// <returns></returns>
|
---|
34 | int IGraph.OutDegree(INode node)
|
---|
35 | {
|
---|
36 | return (node as INode).OutDegree;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Get the degree of a node, the number of edges for which a node
|
---|
41 | /// is either the source or the target.
|
---|
42 | /// </summary>
|
---|
43 | /// <param name="node"></param>
|
---|
44 | /// <returns></returns>
|
---|
45 | int IGraph.Degree(INode node)
|
---|
46 | {
|
---|
47 | return (node as INode).Degree;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Indicates if the edges of this graph are directed or undirected.
|
---|
52 | /// </summary>
|
---|
53 | /// <value></value>
|
---|
54 | bool IGraph.IsDirected
|
---|
55 | {
|
---|
56 | get { return mIsDirected; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /// <summary>
|
---|
60 | /// Returns the nodes for the current page. This is the same as the <see cref="Model.Shapes"/> property except that the return type is
|
---|
61 | /// here <see cref="INode"/> rather than <see cref="IShape"/> type.
|
---|
62 | /// </summary>
|
---|
63 | /// <value></value>
|
---|
64 | CollectionBase<INode> IGraph.Nodes
|
---|
65 | {
|
---|
66 | get {
|
---|
67 | CollectionBase<INode> nodes = new CollectionBase<INode>();
|
---|
68 | foreach (IShape shape in this.CurrentPage.Shapes)
|
---|
69 | {
|
---|
70 | nodes.Add(shape as INode);
|
---|
71 | }
|
---|
72 | return nodes;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /// <summary>
|
---|
77 | /// Gets the edges.
|
---|
78 | /// </summary>
|
---|
79 | /// <value>The edges.</value>
|
---|
80 | CollectionBase<IEdge> IGraph.Edges
|
---|
81 | {
|
---|
82 | get
|
---|
83 | {
|
---|
84 | CollectionBase<IEdge> edges = new CollectionBase<IEdge>();
|
---|
85 | foreach (IConnection cn in this.CurrentPage.Connections)
|
---|
86 | {
|
---|
87 | edges.Add(cn as IEdge);
|
---|
88 | }
|
---|
89 | return edges;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | /// <summary>
|
---|
93 | /// Get the source Node for the given Edge instance.
|
---|
94 | /// </summary>
|
---|
95 | /// <param name="edge"></param>
|
---|
96 | /// <returns></returns>
|
---|
97 | INode IGraph.FromNode(IEdge edge)
|
---|
98 | {
|
---|
99 | return edge.SourceNode;
|
---|
100 | }
|
---|
101 |
|
---|
102 | INode IGraph.ToNode(IEdge edge)
|
---|
103 | {
|
---|
104 | return edge.TargetNode;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /// <summary>
|
---|
108 | /// Given an Edge and an incident Node, return the other Node
|
---|
109 | /// connected to the edge.
|
---|
110 | /// </summary>
|
---|
111 | /// <param name="edge"></param>
|
---|
112 | /// <param name="node"></param>
|
---|
113 | /// <returns></returns>
|
---|
114 | INode IGraph.AdjacentNode(IEdge edge, INode node)
|
---|
115 | {
|
---|
116 | if (edge.SourceNode == node)
|
---|
117 | return edge.TargetNode;
|
---|
118 | else if (edge.TargetNode == node)
|
---|
119 | return edge.SourceNode;
|
---|
120 | else
|
---|
121 | throw new InconsistencyException("The node is not a target or source node of the given edge.");
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | /// <summary>
|
---|
126 | /// Gets the collection of all adjacent nodes connected to the given node by an
|
---|
127 | /// incoming edge (i.e., all nodes that "point" at this one).
|
---|
128 | /// </summary>
|
---|
129 | /// <param name="node"></param>
|
---|
130 | /// <returns></returns>
|
---|
131 | CollectionBase<INode> IGraph.InNeighbors(INode node)
|
---|
132 | {
|
---|
133 | return node.InNeighbors;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /// <summary>
|
---|
137 | /// Gets the collection of adjacent nodes connected to the given node by an
|
---|
138 | /// outgoing edge (i.e., all nodes "pointed" to by this one).
|
---|
139 | /// </summary>
|
---|
140 | /// <param name="node"></param>
|
---|
141 | /// <returns></returns>
|
---|
142 | CollectionBase<INode> IGraph.OutNeighbors(INode node)
|
---|
143 | {
|
---|
144 | return node.OutNeighbors;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /// <summary>
|
---|
148 | /// Get an iterator over all nodes connected to the given node.
|
---|
149 | /// </summary>
|
---|
150 | /// <param name="node"></param>
|
---|
151 | /// <returns></returns>
|
---|
152 | CollectionBase<INode> IGraph.Neighbors(INode node)
|
---|
153 | {
|
---|
154 | return node.Neighbors;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /// <summary>
|
---|
158 | /// Edgeses the specified node.
|
---|
159 | /// </summary>
|
---|
160 | /// <param name="node">The node.</param>
|
---|
161 | /// <returns></returns>
|
---|
162 | CollectionBase<IEdge> IGraph.EdgesOf(INode node)
|
---|
163 | {
|
---|
164 | return node.Edges;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /// <summary>
|
---|
168 | /// Ins the edges.
|
---|
169 | /// </summary>
|
---|
170 | /// <param name="node">The node.</param>
|
---|
171 | /// <returns></returns>
|
---|
172 | CollectionBase<IEdge> IGraph.InEdges(INode node)
|
---|
173 | {
|
---|
174 | return node.InEdges;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /// <summary>
|
---|
178 | /// Outs the edges.
|
---|
179 | /// </summary>
|
---|
180 | /// <param name="node">The node.</param>
|
---|
181 | /// <returns></returns>
|
---|
182 | CollectionBase<IEdge> IGraph.OutEdges(INode node)
|
---|
183 | {
|
---|
184 | return node.OutEdges;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /// <summary>
|
---|
189 | /// Gets the spanning tree.
|
---|
190 | /// </summary>
|
---|
191 | /// <value>The spanning tree.</value>
|
---|
192 | ITree IGraph.SpanningTree
|
---|
193 | {
|
---|
194 | get { return mSpanningTree; }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /// <summary>
|
---|
198 | /// Makes the spanning tree.
|
---|
199 | /// </summary>
|
---|
200 | /// <param name="node">The node.</param>
|
---|
201 | /// <returns></returns>
|
---|
202 | void IGraph.MakeSpanningTree(INode node)
|
---|
203 | {
|
---|
204 | // build unweighted spanning tree by BFS
|
---|
205 | LinkedList<INode> q = new LinkedList<INode>();
|
---|
206 | BitArray visit = new BitArray(this.CurrentPage.Shapes.Count);
|
---|
207 | q.AddFirst(node);
|
---|
208 | visit[this.CurrentPage.Shapes.IndexOf(node as IShape)] = true ;
|
---|
209 | CollectionBase<IEdge> edges = (this as IGraph).Edges;
|
---|
210 | INode n;
|
---|
211 | while (q.Count>0)
|
---|
212 | {
|
---|
213 | INode p = q.First.Value;
|
---|
214 | q.RemoveFirst();
|
---|
215 | foreach (IEdge edge in p.Edges)
|
---|
216 | {
|
---|
217 | n = edge.AdjacentNode(p);
|
---|
218 | if (n == null) continue;
|
---|
219 | try
|
---|
220 | {
|
---|
221 | if (!visit[this.CurrentPage.Shapes.IndexOf(n as IShape)])
|
---|
222 | {
|
---|
223 | q.AddLast(n);
|
---|
224 | visit[this.CurrentPage.Shapes.IndexOf(n as IShape)] = true;
|
---|
225 | n.ParentNode = p;
|
---|
226 | n.ParentEdge = edge;
|
---|
227 | if (p.Children == null)
|
---|
228 | p.Children = new CollectionBase<INode>();
|
---|
229 | p.Children.Add(n);
|
---|
230 | }
|
---|
231 | }
|
---|
232 | catch (ArgumentOutOfRangeException)
|
---|
233 | { continue; }
|
---|
234 | }
|
---|
235 |
|
---|
236 | }
|
---|
237 | mSpanningTree = this as ITree;
|
---|
238 |
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | void ITree.ForEach<T>(Action<T> action, INode startNode)
|
---|
243 | {
|
---|
244 | (action as Action<INode>).Invoke(startNode);
|
---|
245 | if (startNode.Children == null)
|
---|
246 | return;
|
---|
247 |
|
---|
248 | foreach (INode node in startNode.Children)
|
---|
249 | {
|
---|
250 | //(action as Action<INode>).Invoke(node);
|
---|
251 | (this as ITree).ForEach<INode>((action as Action<INode>), node);
|
---|
252 | }
|
---|
253 |
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 |
|
---|
258 |
|
---|
259 | /// <summary>
|
---|
260 | /// Clears the spanning tree.
|
---|
261 | /// </summary>
|
---|
262 | void IGraph.ClearSpanningTree()
|
---|
263 | {
|
---|
264 | mSpanningTree = null;
|
---|
265 | }
|
---|
266 | IEdge ITree.ParentEdge(INode node)
|
---|
267 | {
|
---|
268 | if (mSpanningTree == null)
|
---|
269 | return null;
|
---|
270 | return mSpanningTree.ParentEdge(node);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /// <summary>
|
---|
274 | /// Indicates if the edges of this graph are directed or undirected.
|
---|
275 | /// </summary>
|
---|
276 | /// <value></value>
|
---|
277 | bool ITree.IsDirected
|
---|
278 | {
|
---|
279 | get { return mIsDirected; }
|
---|
280 | set { mIsDirected = value; }
|
---|
281 | }
|
---|
282 |
|
---|
283 | INode ITree.Root
|
---|
284 | {
|
---|
285 | get
|
---|
286 | {
|
---|
287 | return mLayoutRoot as INode;
|
---|
288 | }
|
---|
289 | set
|
---|
290 | {
|
---|
291 | mLayoutRoot = value as IShape;
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | CollectionBase<INode> ITree.Children(INode node)
|
---|
296 | {
|
---|
297 | if (mSpanningTree == null)
|
---|
298 | return null;
|
---|
299 | return mSpanningTree.Children(node);
|
---|
300 | }
|
---|
301 |
|
---|
302 | CollectionBase<IEdge> ITree.ChildEdges(INode node)
|
---|
303 | {
|
---|
304 | if (mSpanningTree == null)
|
---|
305 | return null;
|
---|
306 | return mSpanningTree.ChildEdges(node);
|
---|
307 | }
|
---|
308 |
|
---|
309 | INode ITree.NextSibling(INode node)
|
---|
310 | {
|
---|
311 | if (mSpanningTree == null)
|
---|
312 | return null;
|
---|
313 | return mSpanningTree.NextSibling(node);
|
---|
314 | }
|
---|
315 |
|
---|
316 | INode ITree.PreviousSibling(INode node)
|
---|
317 | {
|
---|
318 | if (mSpanningTree == null)
|
---|
319 | return null;
|
---|
320 | return mSpanningTree.PreviousSibling(node);
|
---|
321 | }
|
---|
322 |
|
---|
323 | INode ITree.LastChild(INode node)
|
---|
324 | {
|
---|
325 | if (mSpanningTree == null)
|
---|
326 | return null;
|
---|
327 | return mSpanningTree.LastChild(node);
|
---|
328 | }
|
---|
329 |
|
---|
330 | INode ITree.FirstChild(INode node)
|
---|
331 | {
|
---|
332 | if (mSpanningTree == null)
|
---|
333 | return null;
|
---|
334 | return mSpanningTree.FirstChild(node);
|
---|
335 | }
|
---|
336 |
|
---|
337 | int ITree.ChildCount(INode node)
|
---|
338 | {
|
---|
339 | if (mSpanningTree == null)
|
---|
340 | return 0;
|
---|
341 | return mSpanningTree.ChildCount(node);
|
---|
342 | }
|
---|
343 |
|
---|
344 | int ITree.Depth(INode node)
|
---|
345 | {
|
---|
346 | if (mSpanningTree == null)
|
---|
347 | return 0;
|
---|
348 | return mSpanningTree.Depth(node);
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | }
|
---|
353 | }
|
---|