[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.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
[11211] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[10267] | 30 | [StorableClass]
|
---|
| 31 | public class Vertex : Item, IVertex {
|
---|
| 32 | [Storable]
|
---|
[11229] | 33 | protected string label;
|
---|
| 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]
|
---|
[11229] | 44 | protected double weight;
|
---|
| 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]
|
---|
[10897] | 55 | protected object content;
|
---|
| 56 | public object Content {
|
---|
| 57 | get { return content; }
|
---|
| 58 | set {
|
---|
[11238] | 59 | if (content == value) return;
|
---|
[10897] | 60 | content = value;
|
---|
[11229] | 61 | OnChanged(this, EventArgs.Empty);
|
---|
[10897] | 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[10267] | 65 | private List<IArc> inArcs;
|
---|
[10677] | 66 | public IEnumerable<IArc> InArcs {
|
---|
| 67 | get { return inArcs ?? Enumerable.Empty<IArc>(); }
|
---|
| 68 | }
|
---|
[10897] | 69 |
|
---|
[10267] | 70 | private List<IArc> outArcs;
|
---|
[10677] | 71 | public IEnumerable<IArc> OutArcs {
|
---|
| 72 | get { return outArcs ?? Enumerable.Empty<IArc>(); }
|
---|
| 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 |
|
---|
[11238] | 85 | public Vertex(object content) {
|
---|
[10897] | 86 | this.content = content;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[10267] | 89 | protected Vertex(Vertex original, Cloner cloner)
|
---|
| 90 | : base(original, cloner) {
|
---|
[10897] | 91 | content = original.content;
|
---|
[11229] | 92 | label = original.Label;
|
---|
| 93 | weight = original.Weight;
|
---|
| 94 |
|
---|
| 95 | inArcs = original.InArcs.Select(cloner.Clone).ToList();
|
---|
| 96 | outArcs = original.OutArcs.Select(cloner.Clone).ToList();
|
---|
[10267] | 97 | }
|
---|
| 98 |
|
---|
| 99 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 100 | return new Vertex(this, cloner);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[11229] | 103 | public void AddArc(IArc arc) {
|
---|
| 104 | if (this != arc.Source && this != arc.Target)
|
---|
| 105 | throw new InvalidOperationException("The current vertex must be either the arc source or the arc target.");
|
---|
| 106 |
|
---|
| 107 | if (this == arc.Source) {
|
---|
| 108 | if (outArcs == null)
|
---|
| 109 | outArcs = new List<IArc>();
|
---|
[11238] | 110 | else if (outArcs.Contains(arc))
|
---|
[11229] | 111 | throw new InvalidOperationException("Arc already added.");
|
---|
| 112 | outArcs.Add(arc);
|
---|
| 113 | } else if (this == arc.Target) {
|
---|
| 114 | if (inArcs == null)
|
---|
| 115 | inArcs = new List<IArc>();
|
---|
[11238] | 116 | else if (inArcs.Contains(arc))
|
---|
[11229] | 117 | throw new InvalidOperationException("Arc already added.");
|
---|
| 118 | inArcs.Add(arc);
|
---|
[10267] | 119 | }
|
---|
[11229] | 120 | OnArcAdded(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 121 | }
|
---|
| 122 |
|
---|
[11229] | 123 | public void RemoveArc(IArc arc) {
|
---|
| 124 | if (this != arc.Source && this != arc.Target)
|
---|
| 125 | throw new InvalidOperationException("The current vertex must be either the arc source or the arc target.");
|
---|
| 126 |
|
---|
| 127 | if (this == arc.Source && outArcs != null) {
|
---|
| 128 | if (!outArcs.Remove(arc))
|
---|
| 129 | throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs.");
|
---|
| 130 | } else if (this == arc.Target && inArcs != null) {
|
---|
| 131 | if (!inArcs.Remove(arc))
|
---|
| 132 | throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs.");
|
---|
| 133 | }
|
---|
| 134 | OnArcRemoved(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 135 | }
|
---|
[11229] | 136 |
|
---|
[11238] | 137 | #region events
|
---|
| 138 | // use the same event to signal a change in the content, weight or label
|
---|
| 139 | public event EventHandler Changed;
|
---|
| 140 | protected virtual void OnChanged(object sender, EventArgs args) {
|
---|
| 141 | var changed = Changed;
|
---|
| 142 | if (changed != null)
|
---|
| 143 | changed(sender, args);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public event EventHandler<EventArgs<IArc>> ArcAdded;
|
---|
| 147 | protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
|
---|
| 148 | var added = ArcAdded;
|
---|
| 149 | if (added != null)
|
---|
| 150 | added(sender, args);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | public event EventHandler<EventArgs<IArc>> ArcRemoved;
|
---|
| 154 | protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
| 155 | var removed = ArcRemoved;
|
---|
| 156 | if (removed != null)
|
---|
| 157 | removed(sender, args);
|
---|
| 158 | }
|
---|
| 159 | #endregion
|
---|
[10267] | 160 | }
|
---|
| 161 |
|
---|
| 162 | [StorableClass]
|
---|
| 163 | public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
|
---|
[10300] | 164 | public new T Content {
|
---|
| 165 | get { return (T)base.Content; }
|
---|
| 166 | set { base.Content = value; }
|
---|
[10267] | 167 | }
|
---|
| 168 |
|
---|
| 169 | [StorableConstructor]
|
---|
| 170 | protected Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 171 |
|
---|
| 172 | protected Vertex(Vertex<T> original, Cloner cloner)
|
---|
| 173 | : base(original, cloner) {
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 177 | return new Vertex<T>(this, cloner);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|