1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace 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 | [StorableType("DC0C1D1F-82DF-4150-94CC-A10FD3667F72")]
|
---|
31 | public class Vertex : Item, IVertex {
|
---|
32 | [Storable]
|
---|
33 | private string label;
|
---|
34 | public string Label {
|
---|
35 | get { return label; }
|
---|
36 | set {
|
---|
37 | if (String.Equals(Label, 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 | private readonly List<IArc> inArcs = new List<IArc>();
|
---|
55 | public IEnumerable<IArc> InArcs {
|
---|
56 | get { return inArcs; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | private readonly List<IArc> outArcs = new List<IArc>();
|
---|
60 | public IEnumerable<IArc> OutArcs {
|
---|
61 | get { return outArcs; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public int InDegree { get { return InArcs.Count(); } }
|
---|
65 | public int OutDegree { get { return OutArcs.Count(); } }
|
---|
66 | public int Degree { get { return InDegree + OutDegree; } }
|
---|
67 |
|
---|
68 | [StorableConstructor]
|
---|
69 | public Vertex(bool deserializing) : base(deserializing) { }
|
---|
70 |
|
---|
71 | [StorableHook(HookType.AfterDeserialization)]
|
---|
72 | private void AfterDeserialization() { }
|
---|
73 |
|
---|
74 | public Vertex() { }
|
---|
75 |
|
---|
76 | protected Vertex(Vertex original, Cloner cloner)
|
---|
77 | : base(original, cloner) {
|
---|
78 | label = original.Label;
|
---|
79 | weight = original.Weight;
|
---|
80 | // we do not clone the arcs here (would cause too much recursion and ultimately a stack overflow)
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
84 | return new Vertex(this, cloner);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void AddArc(IArc arc) {
|
---|
88 | if (this != arc.Source && this != arc.Target)
|
---|
89 | throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
|
---|
90 |
|
---|
91 | if (arc.Source == arc.Target)
|
---|
92 | throw new ArgumentException("Arc source and target must be different.");
|
---|
93 |
|
---|
94 | if (this == arc.Source) {
|
---|
95 | if (outArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
|
---|
96 | outArcs.Add(arc);
|
---|
97 | }
|
---|
98 | if (this == arc.Target) {
|
---|
99 | if (inArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
|
---|
100 | inArcs.Add(arc);
|
---|
101 | }
|
---|
102 | OnArcAdded(this, new EventArgs<IArc>(arc));
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void RemoveArc(IArc arc) {
|
---|
106 | if (this != arc.Source && this != arc.Target)
|
---|
107 | throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
|
---|
108 |
|
---|
109 | if (this == arc.Source) {
|
---|
110 | if (!outArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs.");
|
---|
111 | }
|
---|
112 | if (this == arc.Target) {
|
---|
113 | if (!inArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs.");
|
---|
114 | }
|
---|
115 | OnArcRemoved(this, new EventArgs<IArc>(arc));
|
---|
116 | }
|
---|
117 |
|
---|
118 | #region events
|
---|
119 | // use the same event to signal a change in the content, weight or label
|
---|
120 | public event EventHandler Changed;
|
---|
121 | protected virtual void OnChanged(object sender, EventArgs args) {
|
---|
122 | var changed = Changed;
|
---|
123 | if (changed != null)
|
---|
124 | changed(sender, args);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public event EventHandler<EventArgs<IArc>> ArcAdded;
|
---|
128 | protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
|
---|
129 | var added = ArcAdded;
|
---|
130 | if (added != null)
|
---|
131 | added(sender, args);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public event EventHandler<EventArgs<IArc>> ArcRemoved;
|
---|
135 | protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
136 | var removed = ArcRemoved;
|
---|
137 | if (removed != null)
|
---|
138 | removed(sender, args);
|
---|
139 | }
|
---|
140 | #endregion
|
---|
141 | }
|
---|
142 |
|
---|
143 | [StorableType("0B8261A0-8829-4BB8-B7EF-940A9A0AB1D9")]
|
---|
144 | public class Vertex<T> : Vertex, IVertex<T> where T : class,IDeepCloneable {
|
---|
145 | [Storable]
|
---|
146 | private T data;
|
---|
147 | public T Data {
|
---|
148 | get { return data; }
|
---|
149 | set {
|
---|
150 | if (data == value) return;
|
---|
151 | data = value;
|
---|
152 | OnChanged(this, EventArgs.Empty);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | [StorableConstructor]
|
---|
157 | protected Vertex(bool deserializing) : base(deserializing) { }
|
---|
158 |
|
---|
159 | protected Vertex(Vertex<T> original, Cloner cloner)
|
---|
160 | : base(original, cloner) {
|
---|
161 | if (original.Data != null)
|
---|
162 | Data = cloner.Clone(original.Data);
|
---|
163 | }
|
---|
164 |
|
---|
165 | public Vertex() : base() { }
|
---|
166 |
|
---|
167 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
168 | return new Vertex<T>(this, cloner);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|