Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Vertex.cs @ 11234

Last change on this file since 11234 was 11234, checked in by bburlacu, 10 years ago

#2215: Updated tests project. Fixed a couple of bugs when adding arcs to a vertex. Removed useless events from the directed graph. Worked around invalid operation exception when removing arcs.

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableClass]
31  public class Vertex : Item, IVertex {
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)
37        changed(sender, args);
38    }
39
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);
45    }
46
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);
52    }
53
54    [Storable]
55    protected string label;
56    public string Label {
57      get { return label; }
58      set {
59        label = value;
60        OnChanged(this, EventArgs.Empty);
61      }
62    }
63
64    [Storable]
65    protected double weight;
66    public double Weight {
67      get { return weight; }
68      set {
69        weight = value;
70        OnChanged(this, EventArgs.Empty);
71      }
72    }
73
74    [Storable]
75    protected object content;
76    public object Content {
77      get { return content; }
78      set {
79        content = value;
80        OnChanged(this, EventArgs.Empty);
81      }
82    }
83
84    private List<IArc> inArcs;
85    public IEnumerable<IArc> InArcs {
86      get { return inArcs ?? Enumerable.Empty<IArc>(); }
87    }
88
89    private List<IArc> outArcs;
90    public IEnumerable<IArc> OutArcs {
91      get { return outArcs ?? Enumerable.Empty<IArc>(); }
92    }
93
94    [StorableConstructor]
95    public Vertex(bool deserializing) : base(deserializing) { }
96
97    [StorableHook(HookType.AfterDeserialization)]
98    private void AfterDeserialization() { }
99
100    private Vertex() { }
101
102    public Vertex(object content)
103      : this() {
104      this.content = content;
105    }
106
107    protected Vertex(Vertex original, Cloner cloner)
108      : base(original, cloner) {
109      content = original.content;
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();
115    }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new Vertex(this, cloner);
119    }
120
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))
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.Source == arc.Source))
135          throw new InvalidOperationException("Arc already added.");
136        inArcs.Add(arc);
137      }
138      OnArcAdded(this, new EventArgs<IArc>(arc));
139    }
140
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      }
149    }
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));
163    }
164
165    public int InDegree { get { return InArcs.Count(); } }
166    public int OutDegree { get { return OutArcs.Count(); } }
167    public int Degree { get { return InDegree + OutDegree; } }
168  }
169
170  [StorableClass]
171  public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
172    public new T Content {
173      get { return (T)base.Content; }
174      set { base.Content = value; }
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}
Note: See TracBrowser for help on using the repository browser.