[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 |
|
---|
[10267] | 54 | [Storable]
|
---|
[11241] | 55 | private IDeepCloneable data;
|
---|
| 56 | public IDeepCloneable Data {
|
---|
| 57 | get { return data; }
|
---|
[10897] | 58 | set {
|
---|
[11241] | 59 | if (data == value) return;
|
---|
| 60 | data = value;
|
---|
[11229] | 61 | OnChanged(this, EventArgs.Empty);
|
---|
[10897] | 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[11241] | 65 | private readonly List<IArc> inArcs = new List<IArc>();
|
---|
[10677] | 66 | public IEnumerable<IArc> InArcs {
|
---|
[11241] | 67 | get { return inArcs; }
|
---|
[10677] | 68 | }
|
---|
[10897] | 69 |
|
---|
[11241] | 70 | private readonly List<IArc> outArcs = new List<IArc>();
|
---|
[10677] | 71 | public IEnumerable<IArc> OutArcs {
|
---|
[11241] | 72 | get { return outArcs; }
|
---|
[10677] | 73 | }
|
---|
[10834] | 74 |
|
---|
[11238] | 75 | public int InDegree { get { return InArcs.Count(); } }
|
---|
| 76 | public int OutDegree { get { return OutArcs.Count(); } }
|
---|
| 77 | public int Degree { get { return InDegree + OutDegree; } }
|
---|
| 78 |
|
---|
[10267] | 79 | [StorableConstructor]
|
---|
| 80 | public Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 81 |
|
---|
[11229] | 82 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 83 | private void AfterDeserialization() { }
|
---|
[10267] | 84 |
|
---|
[11241] | 85 | public Vertex(IDeepCloneable data) {
|
---|
| 86 | this.data = data;
|
---|
[10897] | 87 | }
|
---|
| 88 |
|
---|
[10267] | 89 | protected Vertex(Vertex original, Cloner cloner)
|
---|
| 90 | : base(original, cloner) {
|
---|
[11241] | 91 | data = cloner.Clone(original.Data);
|
---|
[11229] | 92 | label = original.Label;
|
---|
| 93 | weight = original.Weight;
|
---|
| 94 |
|
---|
[11263] | 95 | // we do not clone the arcs here (would cause too much recursion and ultimately a stack overflow)
|
---|
[10267] | 96 | }
|
---|
| 97 |
|
---|
| 98 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 99 | return new Vertex(this, cloner);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[11229] | 102 | public void AddArc(IArc arc) {
|
---|
| 103 | if (this != arc.Source && this != arc.Target)
|
---|
[11263] | 104 | throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
|
---|
[11229] | 105 |
|
---|
[11263] | 106 | if (arc.Source == arc.Target)
|
---|
| 107 | throw new ArgumentException("Arc source and target must be different.");
|
---|
| 108 |
|
---|
[11229] | 109 | if (this == arc.Source) {
|
---|
[11241] | 110 | if (outArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
|
---|
[11229] | 111 | outArcs.Add(arc);
|
---|
[11241] | 112 | }
|
---|
| 113 | if (this == arc.Target) {
|
---|
| 114 | if (inArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
|
---|
[11229] | 115 | inArcs.Add(arc);
|
---|
[10267] | 116 | }
|
---|
[11229] | 117 | OnArcAdded(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 118 | }
|
---|
| 119 |
|
---|
[11229] | 120 | public void RemoveArc(IArc arc) {
|
---|
| 121 | if (this != arc.Source && this != arc.Target)
|
---|
[11263] | 122 | throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
|
---|
[11229] | 123 |
|
---|
[11241] | 124 | if (this == arc.Source) {
|
---|
| 125 | if (!outArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs.");
|
---|
[11229] | 126 | }
|
---|
[11241] | 127 | if (this == arc.Target) {
|
---|
| 128 | if (!inArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs.");
|
---|
| 129 | }
|
---|
[11229] | 130 | OnArcRemoved(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 131 | }
|
---|
[11229] | 132 |
|
---|
[11238] | 133 | #region events
|
---|
| 134 | // use the same event to signal a change in the content, weight or label
|
---|
| 135 | public event EventHandler Changed;
|
---|
| 136 | protected virtual void OnChanged(object sender, EventArgs args) {
|
---|
| 137 | var changed = Changed;
|
---|
| 138 | if (changed != null)
|
---|
| 139 | changed(sender, args);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public event EventHandler<EventArgs<IArc>> ArcAdded;
|
---|
| 143 | protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
|
---|
| 144 | var added = ArcAdded;
|
---|
| 145 | if (added != null)
|
---|
| 146 | added(sender, args);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | public event EventHandler<EventArgs<IArc>> ArcRemoved;
|
---|
| 150 | protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
| 151 | var removed = ArcRemoved;
|
---|
| 152 | if (removed != null)
|
---|
| 153 | removed(sender, args);
|
---|
| 154 | }
|
---|
| 155 | #endregion
|
---|
[10267] | 156 | }
|
---|
| 157 |
|
---|
| 158 | [StorableClass]
|
---|
| 159 | public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
|
---|
[11241] | 160 | public new T Data {
|
---|
| 161 | get { return (T)base.Data; }
|
---|
| 162 | set { base.Data = value; }
|
---|
[10267] | 163 | }
|
---|
| 164 |
|
---|
| 165 | [StorableConstructor]
|
---|
| 166 | protected Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 167 |
|
---|
| 168 | protected Vertex(Vertex<T> original, Cloner cloner)
|
---|
| 169 | : base(original, cloner) {
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[11263] | 172 | public Vertex(IDeepCloneable data)
|
---|
| 173 | : base(data) {
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[10267] | 176 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 177 | return new Vertex<T>(this, cloner);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|