Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Collections/DirectedGraph/Vertex.cs @ 11248

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

#2223: Added item attributes and firing of events in the DirectedGraph.

File size: 5.5 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.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Core {
29  [Item("Vertex", "An object representing a vertex in the graph. It can have a text label, a weight, and an additional data object.")]
30  [StorableClass]
31  public class Vertex : Item, IVertex {
32    [Storable]
33    private 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    private 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    private IDeepCloneable data;
56    public IDeepCloneable Data {
57      get { return data; }
58      set {
59        if (data == value) return;
60        data = value;
61        OnChanged(this, EventArgs.Empty);
62      }
63    }
64
65    private readonly List<IArc> inArcs = new List<IArc>();
66    public IEnumerable<IArc> InArcs {
67      get { return inArcs; }
68    }
69
70    private readonly List<IArc> outArcs = new List<IArc>();
71    public IEnumerable<IArc> OutArcs {
72      get { return outArcs; }
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(IDeepCloneable data) {
86      this.data = data;
87    }
88
89    protected Vertex(Vertex original, Cloner cloner)
90      : base(original, cloner) {
91      data = cloner.Clone(original.Data);
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.Contains(arc)) throw new InvalidOperationException("Arc already added.");
109        outArcs.Add(arc);
110      }
111      if (this == arc.Target) {
112        if (inArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
113        inArcs.Add(arc);
114      }
115      OnArcAdded(this, new EventArgs<IArc>(arc));
116    }
117
118    public void RemoveArc(IArc arc) {
119      if (this != arc.Source && this != arc.Target)
120        throw new InvalidOperationException("The current vertex must be either the arc source or the arc target.");
121
122      if (this == arc.Source) {
123        if (!outArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs.");
124      }
125      if (this == arc.Target) {
126        if (!inArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs.");
127      }
128      OnArcRemoved(this, new EventArgs<IArc>(arc));
129    }
130
131    #region events
132    // use the same event to signal a change in the content, weight or label
133    public event EventHandler Changed;
134    protected virtual void OnChanged(object sender, EventArgs args) {
135      var changed = Changed;
136      if (changed != null)
137        changed(sender, args);
138    }
139
140    public event EventHandler<EventArgs<IArc>> ArcAdded;
141    protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
142      var added = ArcAdded;
143      if (added != null)
144        added(sender, args);
145    }
146
147    public event EventHandler<EventArgs<IArc>> ArcRemoved;
148    protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
149      var removed = ArcRemoved;
150      if (removed != null)
151        removed(sender, args);
152    }
153    #endregion
154  }
155
156  [StorableClass]
157  public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
158    public new T Data {
159      get { return (T)base.Data; }
160      set { base.Data = value; }
161    }
162
163    [StorableConstructor]
164    protected Vertex(bool deserializing) : base(deserializing) { }
165
166    protected Vertex(Vertex<T> original, Cloner cloner)
167      : base(original, cloner) {
168    }
169
170    public override IDeepCloneable Clone(Cloner cloner) {
171      return new Vertex<T>(this, cloner);
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.