Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11238


Ignore:
Timestamp:
07/30/14 13:06:52 (10 years ago)
Author:
bburlacu
Message:

#2215: Updated interfaces and graph components according to the reviewer comments (IDirectedGraph, DirectedGraph, Vertex, Arc).

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  
    3131  [StorableClass]
    3232  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 
    4033    [Storable]
    4134    public IVertex Source { get; private set; }
     
    4942      get { return label; }
    5043      set {
     44        if (label.Equals(value)) return;
    5145        label = value;
    5246        OnChanged(this, EventArgs.Empty);
     
    5953      get { return weight; }
    6054      set {
     55        if (weight.Equals(value)) return;
    6156        weight = value;
    6257        OnChanged(this, EventArgs.Empty);
     
    6964      get { return data; }
    7065      set {
     66        if (data == value) return;
    7167        data = value;
    7268        OnChanged(this, EventArgs.Empty);
     
    9490      return new Arc(this, cloner);
    9591    }
     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    }
    9699  }
    97100
  • branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/DirectedGraph.cs

    r11234 r11238  
    5656        vertex.ArcAdded += OnArcAdded;
    5757        vertex.ArcRemoved += OnArcRemoved;
    58         vertex.Changed += OnVertexChanged;
    5958      }
    6059
     
    6463        source.AddArc(arc);
    6564        target.AddArc(arc);
    66         arc.Changed += OnArcChanged;
    6765      }
    6866    }
     
    9189      vertices.Add(vertex);
    9290      // register event handlers
    93       vertex.Changed += OnVertexChanged;
    9491      vertex.ArcAdded += OnArcAdded;
    9592      vertex.ArcRemoved += OnArcRemoved;
     
    9996      vertices.Remove(vertex);
    10097      // 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
    10299      foreach (var arc in arcList)
    103100        RemoveArc(arc);
     
    105102      vertex.ArcAdded -= OnArcAdded;
    106103      vertex.ArcRemoved -= OnArcRemoved;
    107       vertex.Changed -= OnVertexChanged;
    108104    }
    109105
     
    130126    }
    131127
    132     protected virtual void OnVertexChanged(object sender, EventArgs args) { }
    133 
    134128    protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
    135129      var arc = args.Value;
     
    139133      if (arcs.Contains(arc)) return;
    140134      arcs.Add(arc);
    141       arc.Changed += OnArcChanged;
    142135    }
    143136
     
    146139      if (!arcs.Contains(arc)) return; // the same rationale as above
    147140      arcs.Remove(arc);
    148       arc.Changed -= OnArcChanged;
    149141    }
    150 
    151     protected virtual void OnArcChanged(object sender, EventArgs args) { }
    152142
    153143    public override Image ItemImage {
    154144      get { return Common.Resources.VSImageLibrary.Graph; }
    155145    }
     146
     147    // events
     148    public event EventHandler VertexAdded;
     149    public event EventHandler VertexRemoved;
     150    public event EventHandler ArcAdded;
     151    public event EventHandler ArcRemoved;
    156152  }
    157153}
  • branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Interfaces/IDirectedGraph.cs

    r11229 r11238  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Core;
     
    3334    IEnumerable<IVertex> Vertices { get; }
    3435    IEnumerable<IArc> Arcs { get; }
     36
     37    event EventHandler VertexAdded;
     38    event EventHandler VertexRemoved;
     39    event EventHandler ArcAdded;
     40    event EventHandler ArcRemoved;
    3541  }
    3642}
  • branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Interfaces/IVertex.cs

    r11229 r11238  
    4545    void AddArc(IArc arc);
    4646    void RemoveArc(IArc arc);
    47     void RemoveArc(IVertex vertex);
    4847  }
    4948
  • branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Vertex.cs

    r11234 r11238  
    3030  [StorableClass]
    3131  public class Vertex : Item, IVertex {
    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)
    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 
    5432    [Storable]
    5533    protected string label;
     
    5735      get { return label; }
    5836      set {
     37        if (label.Equals(value)) return;
    5938        label = value;
    6039        OnChanged(this, EventArgs.Empty);
     
    6746      get { return weight; }
    6847      set {
     48        if (weight.Equals(value)) return;
    6949        weight = value;
    7050        OnChanged(this, EventArgs.Empty);
     
    7757      get { return content; }
    7858      set {
     59        if (content == value) return;
    7960        content = value;
    8061        OnChanged(this, EventArgs.Empty);
     
    9273    }
    9374
     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
    9479    [StorableConstructor]
    9580    public Vertex(bool deserializing) : base(deserializing) { }
     
    9883    private void AfterDeserialization() { }
    9984
    100     private Vertex() { }
    101 
    102     public Vertex(object content)
    103       : this() {
     85    public Vertex(object content) {
    10486      this.content = content;
    10587    }
     
    126108        if (outArcs == null)
    127109          outArcs = new List<IArc>();
    128         else if (outArcs.Contains(arc) || outArcs.Any(a => a.Target == arc.Target))
     110        else if (outArcs.Contains(arc))
    129111          throw new InvalidOperationException("Arc already added.");
    130112        outArcs.Add(arc);
     
    132114        if (inArcs == null)
    133115          inArcs = new List<IArc>();
    134         else if (inArcs.Contains(arc) || inArcs.Any(a => a.Source == arc.Source))
     116        else if (inArcs.Contains(arc))
    135117          throw new InvalidOperationException("Arc already added.");
    136118        inArcs.Add(arc);
    137119      }
    138120      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       }
    149121    }
    150122
     
    163135    }
    164136
    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
    168160  }
    169161
Note: See TracChangeset for help on using the changeset viewer.