Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Core/3.3/Collections/DirectedGraph/DirectedGraph.cs @ 17076

Last change on this file since 17076 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Common.Resources;
28using HEAL.Attic;
29
30namespace HeuristicLab.Core {
31  [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
32  [StorableType("C7DF8A65-95AE-4D73-950B-27A8086D7DA2")]
33  public class DirectedGraph : Item, IDirectedGraph {
34    public override Image ItemImage { get { return VSImageLibrary.Graph; } }
35
36    private HashSet<IVertex> vertices;
37    [Storable]
38    public IEnumerable<IVertex> Vertices {
39      get { return vertices; }
40      private set { vertices = new HashSet<IVertex>(value); }
41    }
42
43    private HashSet<IArc> arcs;
44    [Storable]
45    public IEnumerable<IArc> Arcs {
46      get { return arcs; }
47      private set { arcs = new HashSet<IArc>(value); }
48    }
49
50    public DirectedGraph() {
51      vertices = new HashSet<IVertex>();
52      arcs = new HashSet<IArc>();
53    }
54
55    protected DirectedGraph(DirectedGraph original, Cloner cloner)
56      : base(original, cloner) {
57      vertices = new HashSet<IVertex>(original.vertices.Select(cloner.Clone));
58      arcs = new HashSet<IArc>(original.arcs.Select(cloner.Clone));
59
60      // add the arcs to the newly cloned vertices
61      foreach (var arc in arcs) {
62        arc.Source.AddArc(arc);
63        arc.Target.AddArc(arc);
64      }
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new DirectedGraph(this, cloner);
69    }
70
71    [StorableConstructor]
72    protected DirectedGraph(StorableConstructorFlag _) : base(_) {
73    }
74
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      foreach (var vertex in vertices) {
78        vertex.ArcAdded += Vertex_ArcAdded;
79        vertex.ArcRemoved += Vertex_ArcRemoved;
80      }
81
82      foreach (var arc in arcs) {
83        var source = arc.Source;
84        var target = arc.Target;
85        source.AddArc(arc);
86        target.AddArc(arc);
87      }
88    }
89
90    public virtual void Clear() {
91      vertices.Clear();
92      arcs.Clear();
93    }
94
95    public virtual void AddVertex(IVertex vertex) {
96      if (!vertices.Contains(vertex) && vertex.Degree > 0)
97        throw new ArgumentException("New vertices cannot have any arcs.");
98
99      if (vertices.Add(vertex)) {
100        // register event handlers
101        vertex.ArcAdded += Vertex_ArcAdded;
102        vertex.ArcRemoved += Vertex_ArcRemoved;
103        OnVertexAdded(this, new EventArgs<IVertex>(vertex));
104      }
105    }
106
107    public virtual void AddVertices(IEnumerable<IVertex> vertexList) {
108      foreach (var v in vertexList) { AddVertex(v); }
109    }
110
111    public virtual void RemoveVertices(IEnumerable<IVertex> vertexList) {
112      foreach (var v in vertexList) { RemoveVertex(v); }
113    }
114
115    public virtual void RemoveVertex(IVertex vertex) {
116      vertices.Remove(vertex);
117      // remove connections to/from the removed vertex
118      var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList(); // avoid invalid operation exception: "collection was modified" below
119      foreach (var arc in arcList)
120        RemoveArc(arc);
121      // deregister event handlers
122      vertex.ArcAdded -= Vertex_ArcAdded;
123      vertex.ArcRemoved -= Vertex_ArcRemoved;
124      OnVertexRemoved(this, new EventArgs<IVertex>(vertex));
125    }
126
127    public virtual IArc AddArc(IVertex source, IVertex target) {
128      var arc = new Arc(source, target);
129      AddArc(arc);
130      return arc;
131    }
132
133    public virtual void AddArc(IArc arc) {
134      var source = arc.Source;
135      var target = arc.Target;
136
137      if (source == target)
138        throw new InvalidOperationException("Source and target cannot be the same.");
139
140      if (!vertices.Contains(source) || !vertices.Contains(target))
141        throw new InvalidOperationException("Cannot add arc connecting vertices that are not in the graph.");
142
143      source.AddArc(arc);
144      target.AddArc(arc);
145      arcs.Add(arc);
146    }
147
148    public virtual void AddArcs(IEnumerable<IArc> arcList) {
149      foreach (var a in arcList) { AddArc(a); }
150    }
151
152    public virtual void RemoveArc(IArc arc) {
153      arcs.Remove(arc);
154      var source = (Vertex)arc.Source;
155      var target = (Vertex)arc.Target;
156      source.RemoveArc(arc);
157      target.RemoveArc(arc);
158    }
159
160    public virtual void RemoveArcs(IEnumerable<IArc> arcList) {
161      foreach (var a in arcList) { RemoveArc(a); }
162    }
163
164    protected virtual void Vertex_ArcAdded(object sender, EventArgs<IArc> args) {
165      // the ArcAdded event is fired by a vertex when an arc from/to another vertex is added to its list of connections
166      // because the arc is added in both directions by both the source and the target, this event will get fired twice here
167      var arc = args.Value;
168      if (arcs.Add(arc)) OnArcAdded(this, new EventArgs<IArc>(arc));
169    }
170
171    protected virtual void Vertex_ArcRemoved(object sender, EventArgs<IArc> args) {
172      var arc = args.Value;
173      if (arcs.Remove(arc)) OnArcRemoved(this, new EventArgs<IArc>(arc));
174    }
175
176    // events
177    public event EventHandler<EventArgs<IVertex>> VertexAdded;
178    protected virtual void OnVertexAdded(object sender, EventArgs<IVertex> args) {
179      var added = VertexAdded;
180      if (added != null)
181        added(sender, args);
182    }
183
184    public event EventHandler<EventArgs<IVertex>> VertexRemoved;
185    protected virtual void OnVertexRemoved(object sender, EventArgs<IVertex> args) {
186      var removed = VertexRemoved;
187      if (removed != null)
188        removed(sender, args);
189    }
190
191    public event EventHandler<EventArgs<IArc>> ArcAdded;
192    protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
193      var added = ArcAdded;
194      if (added != null)
195        added(sender, args);
196    }
197
198    public event EventHandler<EventArgs<IArc>> ArcRemoved;
199    protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
200      var removed = ArcRemoved;
201      if (removed != null)
202        removed(sender, args);
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.