[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 {
|
---|
[11229] | 32 | // use the same event to signal a change in the content, weight or label
|
---|
| 33 | public event EventHandler Changed;
|
---|
| 34 | protected virtual void OnChanged(object sender, EventArgs args) {
|
---|
| 35 | var changed = Changed;
|
---|
| 36 | if (changed != null)
|
---|
[10897] | 37 | changed(sender, args);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[11229] | 40 | public event EventHandler<EventArgs<IArc>> ArcAdded;
|
---|
| 41 | protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
|
---|
| 42 | var added = ArcAdded;
|
---|
| 43 | if (added != null)
|
---|
| 44 | added(sender, args);
|
---|
[10897] | 45 | }
|
---|
| 46 |
|
---|
[11229] | 47 | public event EventHandler<EventArgs<IArc>> ArcRemoved;
|
---|
| 48 | protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
| 49 | var removed = ArcRemoved;
|
---|
| 50 | if (removed != null)
|
---|
| 51 | removed(sender, args);
|
---|
[10267] | 52 | }
|
---|
[10897] | 53 |
|
---|
[10267] | 54 | [Storable]
|
---|
[11229] | 55 | protected string label;
|
---|
| 56 | public string Label {
|
---|
| 57 | get { return label; }
|
---|
| 58 | set {
|
---|
| 59 | label = value;
|
---|
| 60 | OnChanged(this, EventArgs.Empty);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
[10897] | 63 |
|
---|
[10267] | 64 | [Storable]
|
---|
[11229] | 65 | protected double weight;
|
---|
| 66 | public double Weight {
|
---|
| 67 | get { return weight; }
|
---|
| 68 | set {
|
---|
| 69 | weight = value;
|
---|
| 70 | OnChanged(this, EventArgs.Empty);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
[10897] | 73 |
|
---|
[10267] | 74 | [Storable]
|
---|
[10897] | 75 | protected object content;
|
---|
| 76 | public object Content {
|
---|
| 77 | get { return content; }
|
---|
| 78 | set {
|
---|
| 79 | content = value;
|
---|
[11229] | 80 | OnChanged(this, EventArgs.Empty);
|
---|
[10897] | 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[10267] | 84 | private List<IArc> inArcs;
|
---|
[10677] | 85 | public IEnumerable<IArc> InArcs {
|
---|
| 86 | get { return inArcs ?? Enumerable.Empty<IArc>(); }
|
---|
| 87 | }
|
---|
[10897] | 88 |
|
---|
[10267] | 89 | private List<IArc> outArcs;
|
---|
[10677] | 90 | public IEnumerable<IArc> OutArcs {
|
---|
| 91 | get { return outArcs ?? Enumerable.Empty<IArc>(); }
|
---|
| 92 | }
|
---|
[10834] | 93 |
|
---|
[10267] | 94 | [StorableConstructor]
|
---|
| 95 | public Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 96 |
|
---|
[11229] | 97 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 98 | private void AfterDeserialization() { }
|
---|
[10267] | 99 |
|
---|
[11229] | 100 | private Vertex() { }
|
---|
| 101 |
|
---|
[10897] | 102 | public Vertex(object content)
|
---|
| 103 | : this() {
|
---|
| 104 | this.content = content;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[10267] | 107 | protected Vertex(Vertex original, Cloner cloner)
|
---|
| 108 | : base(original, cloner) {
|
---|
[10897] | 109 | content = original.content;
|
---|
[11229] | 110 | label = original.Label;
|
---|
| 111 | weight = original.Weight;
|
---|
| 112 |
|
---|
| 113 | inArcs = original.InArcs.Select(cloner.Clone).ToList();
|
---|
| 114 | outArcs = original.OutArcs.Select(cloner.Clone).ToList();
|
---|
[10267] | 115 | }
|
---|
| 116 |
|
---|
| 117 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 118 | return new Vertex(this, cloner);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[11229] | 121 | public void AddArc(IArc arc) {
|
---|
| 122 | if (this != arc.Source && this != arc.Target)
|
---|
| 123 | throw new InvalidOperationException("The current vertex must be either the arc source or the arc target.");
|
---|
| 124 |
|
---|
| 125 | if (this == arc.Source) {
|
---|
| 126 | if (outArcs == null)
|
---|
| 127 | outArcs = new List<IArc>();
|
---|
| 128 | else if (outArcs.Contains(arc) || outArcs.Any(a => a.Target == arc.Target && a.Source == arc.Source))
|
---|
| 129 | throw new InvalidOperationException("Arc already added.");
|
---|
| 130 | outArcs.Add(arc);
|
---|
| 131 | } else if (this == arc.Target) {
|
---|
| 132 | if (inArcs == null)
|
---|
| 133 | inArcs = new List<IArc>();
|
---|
| 134 | else if (inArcs.Contains(arc) || inArcs.Any(a => a.Target == arc.Target && a.Source == arc.Source))
|
---|
| 135 | throw new InvalidOperationException("Arc already added.");
|
---|
| 136 | inArcs.Add(arc);
|
---|
[10267] | 137 | }
|
---|
[11229] | 138 | OnArcAdded(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 139 | }
|
---|
| 140 |
|
---|
[11229] | 141 | public void RemoveArc(IVertex vertex) {
|
---|
| 142 | try {
|
---|
| 143 | var arc = inArcs.Concat(outArcs).SingleOrDefault(x => x.Target == vertex || x.Source == vertex);
|
---|
| 144 | RemoveArc(arc);
|
---|
| 145 | }
|
---|
| 146 | catch (Exception) {
|
---|
| 147 | throw new InvalidOperationException("Only one arc allowed between two vertices");
|
---|
| 148 | }
|
---|
[10267] | 149 | }
|
---|
[11229] | 150 |
|
---|
| 151 | public void RemoveArc(IArc arc) {
|
---|
| 152 | if (this != arc.Source && this != arc.Target)
|
---|
| 153 | throw new InvalidOperationException("The current vertex must be either the arc source or the arc target.");
|
---|
| 154 |
|
---|
| 155 | if (this == arc.Source && outArcs != null) {
|
---|
| 156 | if (!outArcs.Remove(arc))
|
---|
| 157 | throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs.");
|
---|
| 158 | } else if (this == arc.Target && inArcs != null) {
|
---|
| 159 | if (!inArcs.Remove(arc))
|
---|
| 160 | throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs.");
|
---|
| 161 | }
|
---|
| 162 | OnArcRemoved(this, new EventArgs<IArc>(arc));
|
---|
[10267] | 163 | }
|
---|
[11229] | 164 |
|
---|
[10677] | 165 | public int InDegree { get { return InArcs.Count(); } }
|
---|
| 166 | public int OutDegree { get { return OutArcs.Count(); } }
|
---|
[10267] | 167 | public int Degree { get { return InDegree + OutDegree; } }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | [StorableClass]
|
---|
| 171 | public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
|
---|
[10300] | 172 | public new T Content {
|
---|
| 173 | get { return (T)base.Content; }
|
---|
| 174 | set { base.Content = value; }
|
---|
[10267] | 175 | }
|
---|
| 176 |
|
---|
| 177 | [StorableConstructor]
|
---|
| 178 | protected Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 179 |
|
---|
| 180 | protected Vertex(Vertex<T> original, Cloner cloner)
|
---|
| 181 | : base(original, cloner) {
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 185 | return new Vertex<T>(this, cloner);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|