Changeset 11238
- Timestamp:
- 07/30/14 13:06:52 (10 years ago)
- Location:
- branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Arc.cs
r11229 r11238 31 31 [StorableClass] 32 32 public class Arc : Item, IArc { 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 33 [Storable] 41 34 public IVertex Source { get; private set; } … … 49 42 get { return label; } 50 43 set { 44 if (label.Equals(value)) return; 51 45 label = value; 52 46 OnChanged(this, EventArgs.Empty); … … 59 53 get { return weight; } 60 54 set { 55 if (weight.Equals(value)) return; 61 56 weight = value; 62 57 OnChanged(this, EventArgs.Empty); … … 69 64 get { return data; } 70 65 set { 66 if (data == value) return; 71 67 data = value; 72 68 OnChanged(this, EventArgs.Empty); … … 94 90 return new Arc(this, cloner); 95 91 } 92 93 public event EventHandler Changed; 94 protected virtual void OnChanged(object sender, EventArgs args) { 95 var changed = Changed; 96 if (changed != null) 97 changed(sender, args); 98 } 96 99 } 97 100 -
branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/DirectedGraph.cs
r11234 r11238 56 56 vertex.ArcAdded += OnArcAdded; 57 57 vertex.ArcRemoved += OnArcRemoved; 58 vertex.Changed += OnVertexChanged;59 58 } 60 59 … … 64 63 source.AddArc(arc); 65 64 target.AddArc(arc); 66 arc.Changed += OnArcChanged;67 65 } 68 66 } … … 91 89 vertices.Add(vertex); 92 90 // register event handlers 93 vertex.Changed += OnVertexChanged;94 91 vertex.ArcAdded += OnArcAdded; 95 92 vertex.ArcRemoved += OnArcRemoved; … … 99 96 vertices.Remove(vertex); 100 97 // remove connections to/from the removed vertex 101 var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList(); 98 var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList(); // avoid invalid operation exception: "collection was modified" below 102 99 foreach (var arc in arcList) 103 100 RemoveArc(arc); … … 105 102 vertex.ArcAdded -= OnArcAdded; 106 103 vertex.ArcRemoved -= OnArcRemoved; 107 vertex.Changed -= OnVertexChanged;108 104 } 109 105 … … 130 126 } 131 127 132 protected virtual void OnVertexChanged(object sender, EventArgs args) { }133 134 128 protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) { 135 129 var arc = args.Value; … … 139 133 if (arcs.Contains(arc)) return; 140 134 arcs.Add(arc); 141 arc.Changed += OnArcChanged;142 135 } 143 136 … … 146 139 if (!arcs.Contains(arc)) return; // the same rationale as above 147 140 arcs.Remove(arc); 148 arc.Changed -= OnArcChanged;149 141 } 150 151 protected virtual void OnArcChanged(object sender, EventArgs args) { }152 142 153 143 public override Image ItemImage { 154 144 get { return Common.Resources.VSImageLibrary.Graph; } 155 145 } 146 147 // events 148 public event EventHandler VertexAdded; 149 public event EventHandler VertexRemoved; 150 public event EventHandler ArcAdded; 151 public event EventHandler ArcRemoved; 156 152 } 157 153 } -
branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Interfaces/IDirectedGraph.cs
r11229 r11238 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Core; … … 33 34 IEnumerable<IVertex> Vertices { get; } 34 35 IEnumerable<IArc> Arcs { get; } 36 37 event EventHandler VertexAdded; 38 event EventHandler VertexRemoved; 39 event EventHandler ArcAdded; 40 event EventHandler ArcRemoved; 35 41 } 36 42 } -
branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Interfaces/IVertex.cs
r11229 r11238 45 45 void AddArc(IArc arc); 46 46 void RemoveArc(IArc arc); 47 void RemoveArc(IVertex vertex);48 47 } 49 48 -
branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Vertex.cs
r11234 r11238 30 30 [StorableClass] 31 31 public class Vertex : Item, IVertex { 32 // use the same event to signal a change in the content, weight or label33 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 32 [Storable] 55 33 protected string label; … … 57 35 get { return label; } 58 36 set { 37 if (label.Equals(value)) return; 59 38 label = value; 60 39 OnChanged(this, EventArgs.Empty); … … 67 46 get { return weight; } 68 47 set { 48 if (weight.Equals(value)) return; 69 49 weight = value; 70 50 OnChanged(this, EventArgs.Empty); … … 77 57 get { return content; } 78 58 set { 59 if (content == value) return; 79 60 content = value; 80 61 OnChanged(this, EventArgs.Empty); … … 92 73 } 93 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 94 79 [StorableConstructor] 95 80 public Vertex(bool deserializing) : base(deserializing) { } … … 98 83 private void AfterDeserialization() { } 99 84 100 private Vertex() { } 101 102 public Vertex(object content) 103 : this() { 85 public Vertex(object content) { 104 86 this.content = content; 105 87 } … … 126 108 if (outArcs == null) 127 109 outArcs = new List<IArc>(); 128 else if (outArcs.Contains(arc) || outArcs.Any(a => a.Target == arc.Target))110 else if (outArcs.Contains(arc)) 129 111 throw new InvalidOperationException("Arc already added."); 130 112 outArcs.Add(arc); … … 132 114 if (inArcs == null) 133 115 inArcs = new List<IArc>(); 134 else if (inArcs.Contains(arc) || inArcs.Any(a => a.Source == arc.Source))116 else if (inArcs.Contains(arc)) 135 117 throw new InvalidOperationException("Arc already added."); 136 118 inArcs.Add(arc); 137 119 } 138 120 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 121 } 150 122 … … 163 135 } 164 136 165 public int InDegree { get { return InArcs.Count(); } } 166 public int OutDegree { get { return OutArcs.Count(); } } 167 public int Degree { get { return InDegree + OutDegree; } } 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 168 160 } 169 161
Note: See TracChangeset
for help on using the changeset viewer.