Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/29/14 16:20:08 (9 years ago)
Author:
bburlacu
Message:

#2215: Refactored and simplified DirectedGraph and related components API, simplified the BottomUpSimilarityCalculator by not using a directed graph and vertices but a simpler object so that the similarity calculator is self-contained.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/Vertex.cs

    r11211 r11229  
    3030  [StorableClass]
    3131  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)
    3637        changed(sender, args);
    37       }
    3838    }
    3939
    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);
    4561      }
    4662    }
    4763
    4864    [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      }
    5372    }
    54 
    55     [Storable]
    56     public string Label { get; set; }
    57 
    58     [Storable]
    59     public double Weight { get; set; }
    6073
    6174    [Storable]
     
    6477      get { return content; }
    6578      set {
    66         OnPreContentChanged(this, EventArgs.Empty);
    6779        content = value;
    68         OnPostContentChanged(this, EventArgs.Empty);
     80        OnChanged(this, EventArgs.Empty);
    6981      }
    7082    }
     
    7385    public IEnumerable<IArc> InArcs {
    7486      get { return inArcs ?? Enumerable.Empty<IArc>(); }
    75       set { inArcs = value.ToList(); }
    7687    }
    7788
     
    7990    public IEnumerable<IArc> OutArcs {
    8091      get { return outArcs ?? Enumerable.Empty<IArc>(); }
    81       set { outArcs = value.ToList(); }
    8292    }
    8393
     
    8595    public Vertex(bool deserializing) : base(deserializing) { }
    8696
    87     public Vertex() {
    88       id = Guid.NewGuid().ToString();
    89     }
     97    [StorableHook(HookType.AfterDeserialization)]
     98    private void AfterDeserialization() { }
     99
     100    private Vertex() { }
    90101
    91102    public Vertex(object content)
     
    96107    protected Vertex(Vertex original, Cloner cloner)
    97108      : base(original, cloner) {
    98       id = Guid.NewGuid().ToString();
    99109      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();
    104115    }
    105116
     
    108119    }
    109120
    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");
    114148      }
    115149    }
    116150
    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));
    125163    }
    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
    132165    public int InDegree { get { return InArcs.Count(); } }
    133166    public int OutDegree { get { return OutArcs.Count(); } }
     
    141174      set { base.Content = value; }
    142175    }
    143 
    144     public Vertex() { }
    145176
    146177    [StorableConstructor]
Note: See TracChangeset for help on using the changeset viewer.