Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Analysis/Model.Analysis.cs @ 11301

Last change on this file since 11301 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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