Changeset 11229 for branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Vertex.cs
- Timestamp:
- 07/29/14 16:20:08 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Vertex.cs
r11211 r11229 30 30 [StorableClass] 31 31 public class Vertex : Item, IVertex { 32 public event EventHandler PreContentChanged; 33 protected virtual void OnPreContentChanged(object sender, EventArgs args) { 34 var changed = PreContentChanged; 35 if (changed != null) { 32 // use the same event to signal a change in the content, weight or label 33 public event EventHandler Changed; 34 protected virtual void OnChanged(object sender, EventArgs args) { 35 var changed = Changed; 36 if (changed != null) 36 37 changed(sender, args); 37 }38 38 } 39 39 40 public event EventHandler PostContentChanged; 41 protected virtual void OnPostContentChanged(object sender, EventArgs args) { 42 var changed = PostContentChanged; 43 if (changed != null) { 44 changed(sender, args); 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 [Storable] 55 protected string label; 56 public string Label { 57 get { return label; } 58 set { 59 label = value; 60 OnChanged(this, EventArgs.Empty); 45 61 } 46 62 } 47 63 48 64 [Storable] 49 private string id; 50 51 public string Id { 52 get { return id; } 65 protected double weight; 66 public double Weight { 67 get { return weight; } 68 set { 69 weight = value; 70 OnChanged(this, EventArgs.Empty); 71 } 53 72 } 54 55 [Storable]56 public string Label { get; set; }57 58 [Storable]59 public double Weight { get; set; }60 73 61 74 [Storable] … … 64 77 get { return content; } 65 78 set { 66 OnPreContentChanged(this, EventArgs.Empty);67 79 content = value; 68 On PostContentChanged(this, EventArgs.Empty);80 OnChanged(this, EventArgs.Empty); 69 81 } 70 82 } … … 73 85 public IEnumerable<IArc> InArcs { 74 86 get { return inArcs ?? Enumerable.Empty<IArc>(); } 75 set { inArcs = value.ToList(); }76 87 } 77 88 … … 79 90 public IEnumerable<IArc> OutArcs { 80 91 get { return outArcs ?? Enumerable.Empty<IArc>(); } 81 set { outArcs = value.ToList(); }82 92 } 83 93 … … 85 95 public Vertex(bool deserializing) : base(deserializing) { } 86 96 87 public Vertex() { 88 id = Guid.NewGuid().ToString(); 89 } 97 [StorableHook(HookType.AfterDeserialization)] 98 private void AfterDeserialization() { } 99 100 private Vertex() { } 90 101 91 102 public Vertex(object content) … … 96 107 protected Vertex(Vertex original, Cloner cloner) 97 108 : base(original, cloner) { 98 id = Guid.NewGuid().ToString();99 109 content = original.content; 100 Label = original.Label; 101 Weight = original.Weight; 102 inArcs = new List<IArc>(original.inArcs); 103 outArcs = new List<IArc>(original.outArcs); 110 label = original.Label; 111 weight = original.Weight; 112 113 inArcs = original.InArcs.Select(cloner.Clone).ToList(); 114 outArcs = original.OutArcs.Select(cloner.Clone).ToList(); 104 115 } 105 116 … … 108 119 } 109 120 110 [StorableHook(HookType.AfterDeserialization)] 111 private void AfterDeserialization() { 112 if (Id == null) { 113 id = Guid.NewGuid().ToString(); 121 public void AddArc(IArc arc) { 122 if (this != arc.Source && this != arc.Target) 123 throw new InvalidOperationException("The current vertex must be either the arc source or the arc target."); 124 125 if (this == arc.Source) { 126 if (outArcs == null) 127 outArcs = new List<IArc>(); 128 else if (outArcs.Contains(arc) || outArcs.Any(a => a.Target == arc.Target && a.Source == arc.Source)) 129 throw new InvalidOperationException("Arc already added."); 130 outArcs.Add(arc); 131 } else if (this == arc.Target) { 132 if (inArcs == null) 133 inArcs = new List<IArc>(); 134 else if (inArcs.Contains(arc) || inArcs.Any(a => a.Target == arc.Target && a.Source == arc.Source)) 135 throw new InvalidOperationException("Arc already added."); 136 inArcs.Add(arc); 137 } 138 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"); 114 148 } 115 149 } 116 150 117 // this method can be used to add arcs towards targets that are not visible in the graph 118 // (they do not appear in the nodes Dictionary). It is the programmers responsibility to 119 // enforce the correct and desired behavior. 120 public void AddForwardArc(IArc arc) { 121 if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); } 122 if (outArcs == null) 123 outArcs = new List<IArc>(); 124 outArcs.Add(arc); 151 public void RemoveArc(IArc arc) { 152 if (this != arc.Source && this != arc.Target) 153 throw new InvalidOperationException("The current vertex must be either the arc source or the arc target."); 154 155 if (this == arc.Source && outArcs != null) { 156 if (!outArcs.Remove(arc)) 157 throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs."); 158 } else if (this == arc.Target && inArcs != null) { 159 if (!inArcs.Remove(arc)) 160 throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs."); 161 } 162 OnArcRemoved(this, new EventArgs<IArc>(arc)); 125 163 } 126 public void AddReverseArc(IArc arc) { 127 if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); }; 128 if (inArcs == null) 129 inArcs = new List<IArc>(); 130 inArcs.Add(arc); 131 } 164 132 165 public int InDegree { get { return InArcs.Count(); } } 133 166 public int OutDegree { get { return OutArcs.Count(); } } … … 141 174 set { base.Content = value; } 142 175 } 143 144 public Vertex() { }145 176 146 177 [StorableConstructor]
Note: See TracChangeset
for help on using the changeset viewer.