Changeset 11935
- Timestamp:
- 02/06/15 11:59:12 (10 years ago)
- Location:
- stable
- Files:
-
- 8 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 11241,11248-11251,11256,11263,11324,11389,11391,11444,11879,11912-11913
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Core/3.3/Collections/DirectedGraph/Arc.cs
r11241 r11935 25 25 26 26 namespace HeuristicLab.Core { 27 /// <summary> 28 /// An arc that can have a weight, a label, and a data object for holding additional info 29 /// </summary> 27 [Item("Arc", "A graph arc connecting two graph vertices, that can have a weight, label, and data object for holding additional info")] 30 28 [StorableClass] 31 29 public class Arc : Item, IArc { … … 58 56 } 59 57 60 [Storable]61 protected IDeepCloneable data;62 public IDeepCloneable Data {63 get { return data; }64 set {65 if (data == value) return;66 data = value;67 OnChanged(this, EventArgs.Empty);68 }69 }70 58 71 59 [StorableConstructor] 72 p ublicArc(bool deserializing) : base(deserializing) { }60 protected Arc(bool deserializing) : base(deserializing) { } 73 61 74 62 public Arc(IVertex source, IVertex target) { … … 83 71 label = original.Label; 84 72 weight = original.Weight; 85 data = cloner.Clone(data);86 73 } 87 74 public override IDeepCloneable Clone(Cloner cloner) { return new Arc(this, cloner); } … … 96 83 97 84 [StorableClass] 98 public class Arc<T> : Arc, IArc<T> where T : class,IItem { 85 public class Arc<T> : Arc, IArc<T> where T : class,IDeepCloneable { 86 [Storable] 87 protected T data; 88 public T Data { 89 get { return data; } 90 set { 91 if (data == value) return; 92 data = value; 93 OnChanged(this, EventArgs.Empty); 94 } 95 } 96 97 public override IDeepCloneable Clone(Cloner cloner) { 98 return new Arc<T>(this, cloner); 99 } 100 101 protected Arc(Arc<T> original, Cloner cloner) 102 : base(original, cloner) { 103 if (original.Data != null) 104 Data = cloner.Clone(original.Data); 105 } 106 99 107 public Arc(bool deserializing) 100 108 : base(deserializing) { … … 108 116 : base(original, cloner) { 109 117 } 110 111 new public IVertex<T> Source {112 get { return (IVertex<T>)base.Source; }113 }114 new public IVertex<T> Target {115 get { return (IVertex<T>)base.Target; }116 }117 118 } 118 119 } -
stable/HeuristicLab.Core/3.3/Collections/DirectedGraph/DirectedGraph.cs
r11241 r11935 57 57 vertices = new HashSet<IVertex>(original.vertices.Select(cloner.Clone)); 58 58 arcs = new HashSet<IArc>(original.arcs.Select(cloner.Clone)); 59 60 // add the arcs to the newly cloned vertices 61 foreach (var arc in arcs) { 62 arc.Source.AddArc(arc); 63 arc.Target.AddArc(arc); 64 } 59 65 } 60 66 … … 71 77 private void AfterDeserialization() { 72 78 foreach (var vertex in vertices) { 73 vertex.ArcAdded += OnArcAdded;74 vertex.ArcRemoved += OnArcRemoved;79 vertex.ArcAdded += Vertex_ArcAdded; 80 vertex.ArcRemoved += Vertex_ArcRemoved; 75 81 } 76 82 … … 89 95 90 96 public virtual void AddVertex(IVertex vertex) { 91 vertices.Add(vertex); 92 // register event handlers 93 vertex.ArcAdded += OnArcAdded; 94 vertex.ArcRemoved += OnArcRemoved; 97 if (!vertices.Contains(vertex) && vertex.Degree > 0) 98 throw new ArgumentException("New vertices cannot have any arcs."); 99 100 if (vertices.Add(vertex)) { 101 // register event handlers 102 vertex.ArcAdded += Vertex_ArcAdded; 103 vertex.ArcRemoved += Vertex_ArcRemoved; 104 OnVertexAdded(this, new EventArgs<IVertex>(vertex)); 105 } 106 } 107 108 public virtual void AddVertices(IEnumerable<IVertex> vertexList) { 109 foreach (var v in vertexList) { AddVertex(v); } 110 } 111 112 public virtual void RemoveVertices(IEnumerable<IVertex> vertexList) { 113 foreach (var v in vertexList) { RemoveVertex(v); } 95 114 } 96 115 … … 102 121 RemoveArc(arc); 103 122 // deregister event handlers 104 vertex.ArcAdded -= OnArcAdded; 105 vertex.ArcRemoved -= OnArcRemoved; 123 vertex.ArcAdded -= Vertex_ArcAdded; 124 vertex.ArcRemoved -= Vertex_ArcRemoved; 125 OnVertexRemoved(this, new EventArgs<IVertex>(vertex)); 106 126 } 107 127 … … 113 133 114 134 public virtual void AddArc(IArc arc) { 115 var source = (Vertex)arc.Source; 116 var target = (Vertex)arc.Target; 135 var source = arc.Source; 136 var target = arc.Target; 137 138 if (source == target) 139 throw new InvalidOperationException("Source and target cannot be the same."); 140 141 if (!vertices.Contains(source) || !vertices.Contains(target)) 142 throw new InvalidOperationException("Cannot add arc connecting vertices that are not in the graph."); 143 117 144 source.AddArc(arc); 118 145 target.AddArc(arc); 119 146 arcs.Add(arc); 147 } 148 149 public virtual void AddArcs(IEnumerable<IArc> arcList) { 150 foreach (var a in arcList) { AddArc(a); } 120 151 } 121 152 … … 128 159 } 129 160 130 public event EventHandler ArcAdded; 161 public virtual void RemoveArcs(IEnumerable<IArc> arcList) { 162 foreach (var a in arcList) { RemoveArc(a); } 163 } 164 165 protected virtual void Vertex_ArcAdded(object sender, EventArgs<IArc> args) { 166 // the ArcAdded event is fired by a vertex when an arc from/to another vertex is added to its list of connections 167 // because the arc is added in both directions by both the source and the target, this event will get fired twice here 168 var arc = args.Value; 169 if (arcs.Add(arc)) OnArcAdded(this, new EventArgs<IArc>(arc)); 170 } 171 172 protected virtual void Vertex_ArcRemoved(object sender, EventArgs<IArc> args) { 173 var arc = args.Value; 174 if (arcs.Remove(arc)) OnArcRemoved(this, new EventArgs<IArc>(arc)); 175 } 176 177 // events 178 public event EventHandler<EventArgs<IVertex>> VertexAdded; 179 protected virtual void OnVertexAdded(object sender, EventArgs<IVertex> args) { 180 var added = VertexAdded; 181 if (added != null) 182 added(sender, args); 183 } 184 185 public event EventHandler<EventArgs<IVertex>> VertexRemoved; 186 protected virtual void OnVertexRemoved(object sender, EventArgs<IVertex> args) { 187 var removed = VertexRemoved; 188 if (removed != null) 189 removed(sender, args); 190 } 191 192 public event EventHandler<EventArgs<IArc>> ArcAdded; 131 193 protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) { 132 var arc = args.Value; 133 // the ArcAdded event is fired by a vertex when an arc from or towards another vertex is added to his list of connections 134 // because the arc is added in both directions by both the source and the target, this event will get fired twice 135 // here, we only want to add the arc once, so if its already contained, we return without complaining 136 if (arcs.Contains(arc)) return; 137 arcs.Add(arc); 138 } 139 140 141 public event EventHandler ArcRemoved; 194 var added = ArcAdded; 195 if (added != null) 196 added(sender, args); 197 } 198 199 public event EventHandler<EventArgs<IArc>> ArcRemoved; 142 200 protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) { 143 var arc = args.Value; 144 if (!arcs.Contains(arc)) return; // the same rationale as above 145 arcs.Remove(arc); 146 } 147 148 // events 149 public event EventHandler VertexAdded; 150 public event EventHandler VertexRemoved; 201 var removed = ArcRemoved; 202 if (removed != null) 203 removed(sender, args); 204 } 151 205 } 152 206 } -
stable/HeuristicLab.Core/3.3/Collections/DirectedGraph/Vertex.cs
r11241 r11935 27 27 28 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.")] 29 30 [StorableClass] 30 31 public class Vertex : Item, IVertex { … … 51 52 } 52 53 53 [Storable]54 private IDeepCloneable data;55 public IDeepCloneable Data {56 get { return data; }57 set {58 if (data == value) return;59 data = value;60 OnChanged(this, EventArgs.Empty);61 }62 }63 64 54 private readonly List<IArc> inArcs = new List<IArc>(); 65 55 public IEnumerable<IArc> InArcs { … … 82 72 private void AfterDeserialization() { } 83 73 84 public Vertex(IDeepCloneable data) { 85 this.data = data; 86 } 74 public Vertex() { } 87 75 88 76 protected Vertex(Vertex original, Cloner cloner) 89 77 : base(original, cloner) { 90 data = cloner.Clone(original.Data);91 78 label = original.Label; 92 79 weight = original.Weight; 93 94 inArcs = original.InArcs.Select(cloner.Clone).ToList(); 95 outArcs = original.OutArcs.Select(cloner.Clone).ToList(); 80 // we do not clone the arcs here (would cause too much recursion and ultimately a stack overflow) 96 81 } 97 82 … … 102 87 public void AddArc(IArc arc) { 103 88 if (this != arc.Source && this != arc.Target) 104 throw new InvalidOperationException("The current vertex must be either the arc source or the 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."); 105 93 106 94 if (this == arc.Source) { … … 117 105 public void RemoveArc(IArc arc) { 118 106 if (this != arc.Source && this != arc.Target) 119 throw new InvalidOperationException("The current vertex must be either the arc source or the arc target.");107 throw new ArgumentException("The current vertex must be either the arc source or the arc target."); 120 108 121 109 if (this == arc.Source) { … … 154 142 155 143 [StorableClass] 156 public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem { 157 public new T Data { 158 get { return (T)base.Data; } 159 set { base.Data = value; } 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 } 160 154 } 161 155 … … 165 159 protected Vertex(Vertex<T> original, Cloner cloner) 166 160 : base(original, cloner) { 161 if (original.Data != null) 162 Data = cloner.Clone(original.Data); 167 163 } 164 165 public Vertex() : base() { } 168 166 169 167 public override IDeepCloneable Clone(Cloner cloner) { -
stable/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj
r11920 r11935 122 122 <Compile Include="Collections\CheckedItemCollection.cs" /> 123 123 <Compile Include="Collections\CheckedItemList.cs" /> 124 <Compile Include="Collections\DirectedGraph\Arc.cs" /> 125 <Compile Include="Collections\DirectedGraph\DirectedGraph.cs" /> 126 <Compile Include="Collections\DirectedGraph\Vertex.cs" /> 124 127 <Compile Include="Collections\ReadOnlyCheckedItemCollection.cs" /> 125 128 <Compile Include="Collections\ReadOnlyCheckedItemList.cs"> … … 154 157 <Compile Include="Constraints\IConstraint.cs" /> 155 158 <Compile Include="Constraints\TypeCompatibilityConstraint.cs" /> 159 <Compile Include="Interfaces\DirectedGraph\IArc.cs" /> 160 <Compile Include="Interfaces\DirectedGraph\IDirectedGraph.cs" /> 161 <Compile Include="Interfaces\DirectedGraph\IVertex.cs" /> 156 162 <Compile Include="Interfaces\ICheckedMultiOperator.cs" /> 157 163 <Compile Include="Interfaces\IConstrainedValueParameter.cs" /> -
stable/HeuristicLab.Core/3.3/Interfaces/DirectedGraph/IArc.cs
r11241 r11935 29 29 string Label { get; set; } 30 30 double Weight { get; set; } 31 IDeepCloneable Data { get; set; }32 31 33 32 event EventHandler Changed; // generic event for when the label, weight or data were changed 34 33 } 35 34 36 public interface IArc<T> : IArc where T : class,IItem { 37 new IVertex<T> Source { get; } 38 new IVertex<T> Target { get; } 35 public interface IArc<T> : IArc where T : class,IDeepCloneable { 36 T Data { get; set; } 39 37 } 40 38 } -
stable/HeuristicLab.Core/3.3/Interfaces/DirectedGraph/IDirectedGraph.cs
r11241 r11935 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Core { 26 27 public interface IDirectedGraph : IItem { 27 void Clear();28 void AddVertex(IVertex vertex);29 IArc AddArc(IVertex source, IVertex target);30 void AddArc(IArc arc);31 void RemoveVertex(IVertex vertex);32 void RemoveArc(IArc arc);33 28 IEnumerable<IVertex> Vertices { get; } 34 29 IEnumerable<IArc> Arcs { get; } 35 30 36 event EventHandler VertexAdded; 37 event EventHandler VertexRemoved; 38 event EventHandler ArcAdded; 39 event EventHandler ArcRemoved; 31 void Clear(); 32 void AddVertex(IVertex vertex); 33 void RemoveVertex(IVertex vertex); 34 35 void AddVertices(IEnumerable<IVertex> vertexList); 36 void RemoveVertices(IEnumerable<IVertex> vertexList); 37 38 IArc AddArc(IVertex source, IVertex target); 39 void AddArc(IArc arc); 40 void RemoveArc(IArc arc); 41 42 void AddArcs(IEnumerable<IArc> arcs); 43 void RemoveArcs(IEnumerable<IArc> removeArcs); 44 45 event EventHandler<EventArgs<IVertex>> VertexAdded; 46 event EventHandler<EventArgs<IVertex>> VertexRemoved; 47 event EventHandler<EventArgs<IArc>> ArcAdded; 48 event EventHandler<EventArgs<IArc>> ArcRemoved; 40 49 } 41 50 } -
stable/HeuristicLab.Core/3.3/Interfaces/DirectedGraph/IVertex.cs
r11241 r11935 36 36 double Weight { get; set; } 37 37 38 IDeepCloneable Data { get; set; }39 40 38 void AddArc(IArc arc); 41 39 void RemoveArc(IArc arc); … … 46 44 } 47 45 48 public interface IVertex<T> : IVertex 49 where T : class { 50 new T Data { get; set; } 46 public interface IVertex<T> : IVertex where T : class, IDeepCloneable { 47 T Data { get; set; } 51 48 } 52 49 }
Note: See TracChangeset
for help on using the changeset viewer.