1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Common.Resources;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Core {
|
---|
31 | [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
|
---|
32 | [StorableClass]
|
---|
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 |
|
---|
61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
62 | return new DirectedGraph(this, cloner);
|
---|
63 | }
|
---|
64 |
|
---|
65 | [StorableConstructor]
|
---|
66 | protected DirectedGraph(bool serializing)
|
---|
67 | : base(serializing) {
|
---|
68 | }
|
---|
69 |
|
---|
70 | [StorableHook(HookType.AfterDeserialization)]
|
---|
71 | private void AfterDeserialization() {
|
---|
72 | foreach (var vertex in vertices) {
|
---|
73 | vertex.ArcAdded += OnVertexArcAdded;
|
---|
74 | vertex.ArcRemoved += OnVertexArcRemoved;
|
---|
75 | }
|
---|
76 |
|
---|
77 | foreach (var arc in arcs) {
|
---|
78 | var source = arc.Source;
|
---|
79 | var target = arc.Target;
|
---|
80 | source.AddArc(arc);
|
---|
81 | target.AddArc(arc);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public virtual void Clear() {
|
---|
86 | vertices.Clear();
|
---|
87 | arcs.Clear();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public virtual void AddVertex(IVertex vertex) {
|
---|
91 | vertices.Add(vertex);
|
---|
92 | // register event handlers
|
---|
93 | vertex.ArcAdded += OnVertexArcAdded;
|
---|
94 | vertex.ArcRemoved += OnVertexArcRemoved;
|
---|
95 | OnVertedAdded(this, EventArgs.Empty);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public virtual void RemoveVertex(IVertex vertex) {
|
---|
99 | vertices.Remove(vertex);
|
---|
100 | // remove connections to/from the removed vertex
|
---|
101 | var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList(); // avoid invalid operation exception: "collection was modified" below
|
---|
102 | foreach (var arc in arcList)
|
---|
103 | RemoveArc(arc);
|
---|
104 | // deregister event handlers
|
---|
105 | vertex.ArcAdded -= OnVertexArcAdded;
|
---|
106 | vertex.ArcRemoved -= OnVertexArcRemoved;
|
---|
107 | OnVertexRemoved(this, EventArgs.Empty); // do not pass the removed vertex in the event as we don't need it anymore
|
---|
108 | }
|
---|
109 |
|
---|
110 | public virtual IArc AddArc(IVertex source, IVertex target) {
|
---|
111 | var arc = new Arc(source, target);
|
---|
112 | AddArc(arc);
|
---|
113 | return arc;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public virtual void AddArc(IArc arc) {
|
---|
117 | var source = (Vertex)arc.Source;
|
---|
118 | var target = (Vertex)arc.Target;
|
---|
119 | source.AddArc(arc);
|
---|
120 | target.AddArc(arc);
|
---|
121 | arcs.Add(arc);
|
---|
122 | OnArcAdded(this, EventArgs.Empty);
|
---|
123 | }
|
---|
124 |
|
---|
125 | public virtual void RemoveArc(IArc arc) {
|
---|
126 | arcs.Remove(arc);
|
---|
127 | var source = (Vertex)arc.Source;
|
---|
128 | var target = (Vertex)arc.Target;
|
---|
129 | source.RemoveArc(arc);
|
---|
130 | target.RemoveArc(arc);
|
---|
131 | OnArcRemoved(this, EventArgs.Empty);
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected virtual void OnVertexArcAdded(object sender, EventArgs<IArc> args) {
|
---|
135 | var arc = args.Value;
|
---|
136 | // the ArcAdded event is fired by a vertex when an arc from/to another vertex is added to its list of connections
|
---|
137 | // because the arc is added in both directions by both the source and the target, this event will get fired twice
|
---|
138 | // here, we only want to add the arc once, so if its already contained, we return without complaining
|
---|
139 | if (arcs.Contains(arc)) return;
|
---|
140 | arcs.Add(arc);
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected virtual void OnVertexArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
144 | var arc = args.Value;
|
---|
145 | if (!arcs.Contains(arc)) return; // the same rationale as above
|
---|
146 | arcs.Remove(arc);
|
---|
147 | }
|
---|
148 |
|
---|
149 | // events
|
---|
150 | public event EventHandler VertexAdded;
|
---|
151 | protected virtual void OnVertedAdded(object sender, EventArgs args) {
|
---|
152 | var added = VertexAdded;
|
---|
153 | if (added != null)
|
---|
154 | added(sender, args);
|
---|
155 | }
|
---|
156 |
|
---|
157 | public event EventHandler VertexRemoved;
|
---|
158 | protected virtual void OnVertexRemoved(object sender, EventArgs args) {
|
---|
159 | var removed = VertexRemoved;
|
---|
160 | if (removed != null)
|
---|
161 | removed(sender, args);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public event EventHandler ArcAdded;
|
---|
165 | protected virtual void OnArcAdded(object sender, EventArgs args) {
|
---|
166 | var added = ArcAdded;
|
---|
167 | if (added != null)
|
---|
168 | added(sender, args);
|
---|
169 | }
|
---|
170 |
|
---|
171 | public event EventHandler ArcRemoved;
|
---|
172 | protected virtual void OnArcRemoved(object sender, EventArgs args) {
|
---|
173 | var removed = ArcRemoved;
|
---|
174 | if (removed != null)
|
---|
175 | removed(sender, args);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|