[13368] | 1 | #region License Information
|
---|
[10267] | 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10267] | 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;
|
---|
[11241] | 27 | using HeuristicLab.Common.Resources;
|
---|
[10267] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[11241] | 30 | namespace HeuristicLab.Core {
|
---|
[10278] | 31 | [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
|
---|
[14711] | 32 | [StorableType("57BB1C46-28A7-482A-8BAE-437AD14B0158")]
|
---|
[10300] | 33 | public class DirectedGraph : Item, IDirectedGraph {
|
---|
[11241] | 34 | public override Image ItemImage { get { return VSImageLibrary.Graph; } }
|
---|
| 35 |
|
---|
| 36 | private HashSet<IVertex> vertices;
|
---|
[10267] | 37 | [Storable]
|
---|
[10903] | 38 | public IEnumerable<IVertex> Vertices {
|
---|
| 39 | get { return vertices; }
|
---|
[11229] | 40 | private set { vertices = new HashSet<IVertex>(value); }
|
---|
[10267] | 41 | }
|
---|
[10884] | 42 |
|
---|
[11241] | 43 | private HashSet<IArc> arcs;
|
---|
[10300] | 44 | [Storable]
|
---|
[10903] | 45 | public IEnumerable<IArc> Arcs {
|
---|
| 46 | get { return arcs; }
|
---|
[11229] | 47 | private set { arcs = new HashSet<IArc>(value); }
|
---|
[10903] | 48 | }
|
---|
| 49 |
|
---|
[11241] | 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));
|
---|
[11263] | 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 | }
|
---|
[11241] | 65 | }
|
---|
| 66 |
|
---|
| 67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 68 | return new DirectedGraph(this, cloner);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[10897] | 71 | [StorableConstructor]
|
---|
| 72 | protected DirectedGraph(bool serializing)
|
---|
| 73 | : base(serializing) {
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[10903] | 76 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 77 | private void AfterDeserialization() {
|
---|
[11229] | 78 | foreach (var vertex in vertices) {
|
---|
[11249] | 79 | vertex.ArcAdded += Vertex_ArcAdded;
|
---|
| 80 | vertex.ArcRemoved += Vertex_ArcRemoved;
|
---|
[10903] | 81 | }
|
---|
| 82 |
|
---|
[11229] | 83 | foreach (var arc in arcs) {
|
---|
| 84 | var source = arc.Source;
|
---|
| 85 | var target = arc.Target;
|
---|
| 86 | source.AddArc(arc);
|
---|
| 87 | target.AddArc(arc);
|
---|
[10903] | 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[11229] | 91 | public virtual void Clear() {
|
---|
| 92 | vertices.Clear();
|
---|
| 93 | arcs.Clear();
|
---|
[10267] | 94 | }
|
---|
| 95 |
|
---|
[11229] | 96 | public virtual void AddVertex(IVertex vertex) {
|
---|
[11263] | 97 | if (!vertices.Contains(vertex) && vertex.Degree > 0)
|
---|
| 98 | throw new ArgumentException("New vertices cannot have any arcs.");
|
---|
| 99 |
|
---|
| 100 | if (vertices.Add(vertex)) {
|
---|
| 101 | // register event handlers
|
---|
| 102 | vertex.ArcAdded += Vertex_ArcAdded;
|
---|
| 103 | vertex.ArcRemoved += Vertex_ArcRemoved;
|
---|
[11912] | 104 | OnVertexAdded(this, new EventArgs<IVertex>(vertex));
|
---|
[11263] | 105 | }
|
---|
[10300] | 106 | }
|
---|
[10884] | 107 |
|
---|
[11324] | 108 | public virtual void AddVertices(IEnumerable<IVertex> vertexList) {
|
---|
[11912] | 109 | foreach (var v in vertexList) { AddVertex(v); }
|
---|
[11324] | 110 | }
|
---|
| 111 |
|
---|
[11391] | 112 | public virtual void RemoveVertices(IEnumerable<IVertex> vertexList) {
|
---|
[11912] | 113 | foreach (var v in vertexList) { RemoveVertex(v); }
|
---|
[11391] | 114 | }
|
---|
| 115 |
|
---|
[11229] | 116 | public virtual void RemoveVertex(IVertex vertex) {
|
---|
| 117 | vertices.Remove(vertex);
|
---|
| 118 | // remove connections to/from the removed vertex
|
---|
[11238] | 119 | var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList(); // avoid invalid operation exception: "collection was modified" below
|
---|
[11234] | 120 | foreach (var arc in arcList)
|
---|
[11229] | 121 | RemoveArc(arc);
|
---|
| 122 | // deregister event handlers
|
---|
[11249] | 123 | vertex.ArcAdded -= Vertex_ArcAdded;
|
---|
| 124 | vertex.ArcRemoved -= Vertex_ArcRemoved;
|
---|
| 125 | OnVertexRemoved(this, new EventArgs<IVertex>(vertex));
|
---|
[10884] | 126 | }
|
---|
| 127 |
|
---|
[11241] | 128 | public virtual IArc AddArc(IVertex source, IVertex target) {
|
---|
[11229] | 129 | var arc = new Arc(source, target);
|
---|
| 130 | AddArc(arc);
|
---|
| 131 | return arc;
|
---|
[10300] | 132 | }
|
---|
[10897] | 133 |
|
---|
[11241] | 134 | public virtual void AddArc(IArc arc) {
|
---|
[11324] | 135 | var source = arc.Source;
|
---|
| 136 | var target = arc.Target;
|
---|
| 137 |
|
---|
[11444] | 138 | if (source == target)
|
---|
| 139 | throw new InvalidOperationException("Source and target cannot be the same.");
|
---|
| 140 |
|
---|
[11324] | 141 | if (!vertices.Contains(source) || !vertices.Contains(target))
|
---|
| 142 | throw new InvalidOperationException("Cannot add arc connecting vertices that are not in the graph.");
|
---|
| 143 |
|
---|
[11229] | 144 | source.AddArc(arc);
|
---|
| 145 | target.AddArc(arc);
|
---|
| 146 | arcs.Add(arc);
|
---|
[10267] | 147 | }
|
---|
[10897] | 148 |
|
---|
[11912] | 149 | public virtual void AddArcs(IEnumerable<IArc> arcList) {
|
---|
| 150 | foreach (var a in arcList) { AddArc(a); }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[11241] | 153 | public virtual void RemoveArc(IArc arc) {
|
---|
[11229] | 154 | arcs.Remove(arc);
|
---|
| 155 | var source = (Vertex)arc.Source;
|
---|
| 156 | var target = (Vertex)arc.Target;
|
---|
| 157 | source.RemoveArc(arc);
|
---|
| 158 | target.RemoveArc(arc);
|
---|
[10267] | 159 | }
|
---|
[10897] | 160 |
|
---|
[11912] | 161 | public virtual void RemoveArcs(IEnumerable<IArc> arcList) {
|
---|
| 162 | foreach (var a in arcList) { RemoveArc(a); }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[11249] | 165 | protected virtual void Vertex_ArcAdded(object sender, EventArgs<IArc> args) {
|
---|
| 166 | // the ArcAdded event is fired by a vertex when an arc from/to another vertex is added to its list of connections
|
---|
| 167 | // because the arc is added in both directions by both the source and the target, this event will get fired twice here
|
---|
[11229] | 168 | var arc = args.Value;
|
---|
[11249] | 169 | if (arcs.Add(arc)) OnArcAdded(this, new EventArgs<IArc>(arc));
|
---|
[10888] | 170 | }
|
---|
| 171 |
|
---|
[11249] | 172 | protected virtual void Vertex_ArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
[11229] | 173 | var arc = args.Value;
|
---|
[11249] | 174 | if (arcs.Remove(arc)) OnArcRemoved(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 175 | }
|
---|
[10888] | 176 |
|
---|
[11238] | 177 | // events
|
---|
[11251] | 178 | public event EventHandler<EventArgs<IVertex>> VertexAdded;
|
---|
[11912] | 179 | protected virtual void OnVertexAdded(object sender, EventArgs<IVertex> args) {
|
---|
[11248] | 180 | var added = VertexAdded;
|
---|
| 181 | if (added != null)
|
---|
| 182 | added(sender, args);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[11250] | 185 | public event EventHandler<EventArgs<IVertex>> VertexRemoved;
|
---|
[11249] | 186 | protected virtual void OnVertexRemoved(object sender, EventArgs<IVertex> args) {
|
---|
[11248] | 187 | var removed = VertexRemoved;
|
---|
| 188 | if (removed != null)
|
---|
| 189 | removed(sender, args);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[11250] | 192 | public event EventHandler<EventArgs<IArc>> ArcAdded;
|
---|
[11249] | 193 | protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
|
---|
[11248] | 194 | var added = ArcAdded;
|
---|
| 195 | if (added != null)
|
---|
| 196 | added(sender, args);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[11250] | 199 | public event EventHandler<EventArgs<IArc>> ArcRemoved;
|
---|
[11249] | 200 | protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
[11248] | 201 | var removed = ArcRemoved;
|
---|
| 202 | if (removed != null)
|
---|
| 203 | removed(sender, args);
|
---|
| 204 | }
|
---|
[10267] | 205 | }
|
---|
| 206 | }
|
---|