[10267] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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;
|
---|
[17097] | 28 | using HEAL.Attic;
|
---|
[10267] | 29 |
|
---|
[11241] | 30 | namespace HeuristicLab.Core {
|
---|
[10278] | 31 | [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
|
---|
[17097] | 32 | [StorableType("C7DF8A65-95AE-4D73-950B-27A8086D7DA2")]
|
---|
[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));
|
---|
[11935] | 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]
|
---|
[17097] | 72 | protected DirectedGraph(StorableConstructorFlag _) : base(_) {
|
---|
[10897] | 73 | }
|
---|
| 74 |
|
---|
[10903] | 75 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 76 | private void AfterDeserialization() {
|
---|
[11229] | 77 | foreach (var vertex in vertices) {
|
---|
[11935] | 78 | vertex.ArcAdded += Vertex_ArcAdded;
|
---|
| 79 | vertex.ArcRemoved += Vertex_ArcRemoved;
|
---|
[10903] | 80 | }
|
---|
| 81 |
|
---|
[11229] | 82 | foreach (var arc in arcs) {
|
---|
| 83 | var source = arc.Source;
|
---|
| 84 | var target = arc.Target;
|
---|
| 85 | source.AddArc(arc);
|
---|
| 86 | target.AddArc(arc);
|
---|
[10903] | 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[11229] | 90 | public virtual void Clear() {
|
---|
| 91 | vertices.Clear();
|
---|
| 92 | arcs.Clear();
|
---|
[10267] | 93 | }
|
---|
| 94 |
|
---|
[11229] | 95 | public virtual void AddVertex(IVertex vertex) {
|
---|
[11935] | 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 | }
|
---|
[10300] | 105 | }
|
---|
[10884] | 106 |
|
---|
[11935] | 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 |
|
---|
[11229] | 115 | public virtual void RemoveVertex(IVertex vertex) {
|
---|
| 116 | vertices.Remove(vertex);
|
---|
| 117 | // remove connections to/from the removed vertex
|
---|
[11238] | 118 | var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList(); // avoid invalid operation exception: "collection was modified" below
|
---|
[11234] | 119 | foreach (var arc in arcList)
|
---|
[11229] | 120 | RemoveArc(arc);
|
---|
| 121 | // deregister event handlers
|
---|
[11935] | 122 | vertex.ArcAdded -= Vertex_ArcAdded;
|
---|
| 123 | vertex.ArcRemoved -= Vertex_ArcRemoved;
|
---|
| 124 | OnVertexRemoved(this, new EventArgs<IVertex>(vertex));
|
---|
[10884] | 125 | }
|
---|
| 126 |
|
---|
[11241] | 127 | public virtual IArc AddArc(IVertex source, IVertex target) {
|
---|
[11229] | 128 | var arc = new Arc(source, target);
|
---|
| 129 | AddArc(arc);
|
---|
| 130 | return arc;
|
---|
[10300] | 131 | }
|
---|
[10897] | 132 |
|
---|
[11241] | 133 | public virtual void AddArc(IArc arc) {
|
---|
[11935] | 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 |
|
---|
[11229] | 143 | source.AddArc(arc);
|
---|
| 144 | target.AddArc(arc);
|
---|
| 145 | arcs.Add(arc);
|
---|
[10267] | 146 | }
|
---|
[10897] | 147 |
|
---|
[11935] | 148 | public virtual void AddArcs(IEnumerable<IArc> arcList) {
|
---|
| 149 | foreach (var a in arcList) { AddArc(a); }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[11241] | 152 | public virtual void RemoveArc(IArc arc) {
|
---|
[11229] | 153 | arcs.Remove(arc);
|
---|
| 154 | var source = (Vertex)arc.Source;
|
---|
| 155 | var target = (Vertex)arc.Target;
|
---|
| 156 | source.RemoveArc(arc);
|
---|
| 157 | target.RemoveArc(arc);
|
---|
[10267] | 158 | }
|
---|
[10897] | 159 |
|
---|
[11935] | 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
|
---|
[11229] | 167 | var arc = args.Value;
|
---|
[11935] | 168 | if (arcs.Add(arc)) OnArcAdded(this, new EventArgs<IArc>(arc));
|
---|
[10888] | 169 | }
|
---|
| 170 |
|
---|
[11935] | 171 | protected virtual void Vertex_ArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
[11229] | 172 | var arc = args.Value;
|
---|
[11935] | 173 | if (arcs.Remove(arc)) OnArcRemoved(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 174 | }
|
---|
[10888] | 175 |
|
---|
[11238] | 176 | // events
|
---|
[11935] | 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 | }
|
---|
[10267] | 204 | }
|
---|
| 205 | }
|
---|