Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2215: Updated interfaces and graph components according to the reviewer comments (IDirectedGraph, DirectedGraph, Vertex, Arc).

File size: 5.7 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    [Storable]
33    protected string label;
34    public string Label {
35      get { return label; }
36      set {
37        if (label.Equals(value)) return;
38        label = value;
39        OnChanged(this, EventArgs.Empty);
40      }
41    }
42
43    [Storable]
44    protected double weight;
45    public double Weight {
46      get { return weight; }
47      set {
48        if (weight.Equals(value)) return;
49        weight = value;
50        OnChanged(this, EventArgs.Empty);
51      }
52    }
53
54    [Storable]
55    protected object content;
56    public object Content {
57      get { return content; }
58      set {
59        if (content == value) return;
60        content = value;
61        OnChanged(this, EventArgs.Empty);
62      }
63    }
64
65    private List<IArc> inArcs;
66    public IEnumerable<IArc> InArcs {
67      get { return inArcs ?? Enumerable.Empty<IArc>(); }
68    }
69
70    private List<IArc> outArcs;
71    public IEnumerable<IArc> OutArcs {
72      get { return outArcs ?? Enumerable.Empty<IArc>(); }
73    }
74
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
79    [StorableConstructor]
80    public Vertex(bool deserializing) : base(deserializing) { }
81
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() { }
84
85    public Vertex(object content) {
86      this.content = content;
87    }
88
89    protected Vertex(Vertex original, Cloner cloner)
90      : base(original, cloner) {
91      content = original.content;
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();
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new Vertex(this, cloner);
101    }
102
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>();
110        else if (outArcs.Contains(arc))
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>();
116        else if (inArcs.Contains(arc))
117          throw new InvalidOperationException("Arc already added.");
118        inArcs.Add(arc);
119      }
120      OnArcAdded(this, new EventArgs<IArc>(arc));
121    }
122
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));
135    }
136
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
160  }
161
162  [StorableClass]
163  public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
164    public new T Content {
165      get { return (T)base.Content; }
166      set { base.Content = value; }
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}
Note: See TracBrowser for help on using the repository browser.