[10267] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[10278] | 3 | * Copyright (C) 2002-2014 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;
|
---|
[10677] | 24 | using System.Linq;
|
---|
[10267] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
[11241] | 28 | namespace HeuristicLab.Core {
|
---|
[11248] | 29 | [Item("Vertex", "An object representing a vertex in the graph. It can have a text label, a weight, and an additional data object.")]
|
---|
[10267] | 30 | [StorableClass]
|
---|
| 31 | public class Vertex : Item, IVertex {
|
---|
| 32 | [Storable]
|
---|
[11241] | 33 | private string label;
|
---|
[11229] | 34 | public string Label {
|
---|
| 35 | get { return label; }
|
---|
| 36 | set {
|
---|
[11238] | 37 | if (label.Equals(value)) return;
|
---|
[11229] | 38 | label = value;
|
---|
| 39 | OnChanged(this, EventArgs.Empty);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
[10897] | 42 |
|
---|
[10267] | 43 | [Storable]
|
---|
[11241] | 44 | private double weight;
|
---|
[11229] | 45 | public double Weight {
|
---|
| 46 | get { return weight; }
|
---|
| 47 | set {
|
---|
[11238] | 48 | if (weight.Equals(value)) return;
|
---|
[11229] | 49 | weight = value;
|
---|
| 50 | OnChanged(this, EventArgs.Empty);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[10897] | 53 |
|
---|
[11241] | 54 | private readonly List<IArc> inArcs = new List<IArc>();
|
---|
[10677] | 55 | public IEnumerable<IArc> InArcs {
|
---|
[11241] | 56 | get { return inArcs; }
|
---|
[10677] | 57 | }
|
---|
[10897] | 58 |
|
---|
[11241] | 59 | private readonly List<IArc> outArcs = new List<IArc>();
|
---|
[10677] | 60 | public IEnumerable<IArc> OutArcs {
|
---|
[11241] | 61 | get { return outArcs; }
|
---|
[10677] | 62 | }
|
---|
[10834] | 63 |
|
---|
[11238] | 64 | public int InDegree { get { return InArcs.Count(); } }
|
---|
| 65 | public int OutDegree { get { return OutArcs.Count(); } }
|
---|
| 66 | public int Degree { get { return InDegree + OutDegree; } }
|
---|
| 67 |
|
---|
[10267] | 68 | [StorableConstructor]
|
---|
| 69 | public Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 70 |
|
---|
[11229] | 71 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 72 | private void AfterDeserialization() { }
|
---|
[10267] | 73 |
|
---|
[11912] | 74 | public Vertex() { }
|
---|
[10897] | 75 |
|
---|
[10267] | 76 | protected Vertex(Vertex original, Cloner cloner)
|
---|
| 77 | : base(original, cloner) {
|
---|
[11229] | 78 | label = original.Label;
|
---|
| 79 | weight = original.Weight;
|
---|
[11263] | 80 | // we do not clone the arcs here (would cause too much recursion and ultimately a stack overflow)
|
---|
[10267] | 81 | }
|
---|
| 82 |
|
---|
| 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new Vertex(this, cloner);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[11229] | 87 | public void AddArc(IArc arc) {
|
---|
| 88 | if (this != arc.Source && this != arc.Target)
|
---|
[11263] | 89 | throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
|
---|
[11229] | 90 |
|
---|
[11263] | 91 | if (arc.Source == arc.Target)
|
---|
| 92 | throw new ArgumentException("Arc source and target must be different.");
|
---|
| 93 |
|
---|
[11229] | 94 | if (this == arc.Source) {
|
---|
[11241] | 95 | if (outArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
|
---|
[11229] | 96 | outArcs.Add(arc);
|
---|
[11241] | 97 | }
|
---|
| 98 | if (this == arc.Target) {
|
---|
| 99 | if (inArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
|
---|
[11229] | 100 | inArcs.Add(arc);
|
---|
[10267] | 101 | }
|
---|
[11229] | 102 | OnArcAdded(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 103 | }
|
---|
| 104 |
|
---|
[11229] | 105 | public void RemoveArc(IArc arc) {
|
---|
| 106 | if (this != arc.Source && this != arc.Target)
|
---|
[11263] | 107 | throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
|
---|
[11229] | 108 |
|
---|
[11241] | 109 | if (this == arc.Source) {
|
---|
| 110 | if (!outArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs.");
|
---|
[11229] | 111 | }
|
---|
[11241] | 112 | if (this == arc.Target) {
|
---|
| 113 | if (!inArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs.");
|
---|
| 114 | }
|
---|
[11229] | 115 | OnArcRemoved(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 116 | }
|
---|
[11229] | 117 |
|
---|
[11238] | 118 | #region events
|
---|
| 119 | // use the same event to signal a change in the content, weight or label
|
---|
| 120 | public event EventHandler Changed;
|
---|
| 121 | protected virtual void OnChanged(object sender, EventArgs args) {
|
---|
| 122 | var changed = Changed;
|
---|
| 123 | if (changed != null)
|
---|
| 124 | changed(sender, args);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | public event EventHandler<EventArgs<IArc>> ArcAdded;
|
---|
| 128 | protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
|
---|
| 129 | var added = ArcAdded;
|
---|
| 130 | if (added != null)
|
---|
| 131 | added(sender, args);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public event EventHandler<EventArgs<IArc>> ArcRemoved;
|
---|
| 135 | protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
| 136 | var removed = ArcRemoved;
|
---|
| 137 | if (removed != null)
|
---|
| 138 | removed(sender, args);
|
---|
| 139 | }
|
---|
| 140 | #endregion
|
---|
[10267] | 141 | }
|
---|
| 142 |
|
---|
| 143 | [StorableClass]
|
---|
[11912] | 144 | public class Vertex<T> : Vertex, IVertex<T> where T : class,IDeepCloneable {
|
---|
| 145 | [Storable]
|
---|
| 146 | private T data;
|
---|
| 147 | public T Data {
|
---|
| 148 | get { return data; }
|
---|
| 149 | set {
|
---|
| 150 | if (data == value) return;
|
---|
| 151 | data = value;
|
---|
| 152 | OnChanged(this, EventArgs.Empty);
|
---|
| 153 | }
|
---|
[10267] | 154 | }
|
---|
| 155 |
|
---|
| 156 | [StorableConstructor]
|
---|
| 157 | protected Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 158 |
|
---|
| 159 | protected Vertex(Vertex<T> original, Cloner cloner)
|
---|
| 160 | : base(original, cloner) {
|
---|
[11912] | 161 | if (original.Data != null)
|
---|
| 162 | Data = cloner.Clone(original.Data);
|
---|
[10267] | 163 | }
|
---|
| 164 |
|
---|
[11912] | 165 | public Vertex() : base() { }
|
---|
[11263] | 166 |
|
---|
[10267] | 167 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 168 | return new Vertex<T>(this, cloner);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|