Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11935


Ignore:
Timestamp:
02/06/15 11:59:12 (10 years ago)
Author:
jkarder
Message:

#2223: merged r11241, r11243, r11248, r11249, r11250, r11251, r11256, r11263, r11324, r11389, r11391, r11444, r11853, r11879, r11912 and r11913 into stable

Location:
stable
Files:
8 edited
2 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Core/3.3/Collections/DirectedGraph/Arc.cs

    r11241 r11935  
    2525
    2626namespace 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")]
    3028  [StorableClass]
    3129  public class Arc : Item, IArc {
     
    5856    }
    5957
    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     }
    7058
    7159    [StorableConstructor]
    72     public Arc(bool deserializing) : base(deserializing) { }
     60    protected Arc(bool deserializing) : base(deserializing) { }
    7361
    7462    public Arc(IVertex source, IVertex target) {
     
    8371      label = original.Label;
    8472      weight = original.Weight;
    85       data = cloner.Clone(data);
    8673    }
    8774    public override IDeepCloneable Clone(Cloner cloner) { return new Arc(this, cloner); }
     
    9683
    9784  [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
    99107    public Arc(bool deserializing)
    100108      : base(deserializing) {
     
    108116      : base(original, cloner) {
    109117    }
    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     }
    117118  }
    118119}
  • stable/HeuristicLab.Core/3.3/Collections/DirectedGraph/DirectedGraph.cs

    r11241 r11935  
    5757      vertices = new HashSet<IVertex>(original.vertices.Select(cloner.Clone));
    5858      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      }
    5965    }
    6066
     
    7177    private void AfterDeserialization() {
    7278      foreach (var vertex in vertices) {
    73         vertex.ArcAdded += OnArcAdded;
    74         vertex.ArcRemoved += OnArcRemoved;
     79        vertex.ArcAdded += Vertex_ArcAdded;
     80        vertex.ArcRemoved += Vertex_ArcRemoved;
    7581      }
    7682
     
    8995
    9096    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); }
    95114    }
    96115
     
    102121        RemoveArc(arc);
    103122      // 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));
    106126    }
    107127
     
    113133
    114134    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
    117144      source.AddArc(arc);
    118145      target.AddArc(arc);
    119146      arcs.Add(arc);
     147    }
     148
     149    public virtual void AddArcs(IEnumerable<IArc> arcList) {
     150      foreach (var a in arcList) { AddArc(a); }
    120151    }
    121152
     
    128159    }
    129160
    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;
    131193    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;
    142200    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    }
    151205  }
    152206}
  • stable/HeuristicLab.Core/3.3/Collections/DirectedGraph/Vertex.cs

    r11241 r11935  
    2727
    2828namespace 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.")]
    2930  [StorableClass]
    3031  public class Vertex : Item, IVertex {
     
    5152    }
    5253
    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 
    6454    private readonly List<IArc> inArcs = new List<IArc>();
    6555    public IEnumerable<IArc> InArcs {
     
    8272    private void AfterDeserialization() { }
    8373
    84     public Vertex(IDeepCloneable data) {
    85       this.data = data;
    86     }
     74    public Vertex() { }
    8775
    8876    protected Vertex(Vertex original, Cloner cloner)
    8977      : base(original, cloner) {
    90       data = cloner.Clone(original.Data);
    9178      label = original.Label;
    9279      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)
    9681    }
    9782
     
    10287    public void AddArc(IArc arc) {
    10388      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.");
    10593
    10694      if (this == arc.Source) {
     
    117105    public void RemoveArc(IArc arc) {
    118106      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.");
    120108
    121109      if (this == arc.Source) {
     
    154142
    155143  [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      }
    160154    }
    161155
     
    165159    protected Vertex(Vertex<T> original, Cloner cloner)
    166160      : base(original, cloner) {
     161      if (original.Data != null)
     162        Data = cloner.Clone(original.Data);
    167163    }
     164
     165    public Vertex() : base() { }
    168166
    169167    public override IDeepCloneable Clone(Cloner cloner) {
  • stable/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj

    r11920 r11935  
    122122    <Compile Include="Collections\CheckedItemCollection.cs" />
    123123    <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" />
    124127    <Compile Include="Collections\ReadOnlyCheckedItemCollection.cs" />
    125128    <Compile Include="Collections\ReadOnlyCheckedItemList.cs">
     
    154157    <Compile Include="Constraints\IConstraint.cs" />
    155158    <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" />
    156162    <Compile Include="Interfaces\ICheckedMultiOperator.cs" />
    157163    <Compile Include="Interfaces\IConstrainedValueParameter.cs" />
  • stable/HeuristicLab.Core/3.3/Interfaces/DirectedGraph/IArc.cs

    r11241 r11935  
    2929    string Label { get; set; }
    3030    double Weight { get; set; }
    31     IDeepCloneable Data { get; set; }
    3231
    3332    event EventHandler Changed; // generic event for when the label, weight or data were changed
    3433  }
    3534
    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; }
    3937  }
    4038}
  • stable/HeuristicLab.Core/3.3/Interfaces/DirectedGraph/IDirectedGraph.cs

    r11241 r11935  
    2222using System;
    2323using System.Collections.Generic;
     24using HeuristicLab.Common;
    2425
    2526namespace HeuristicLab.Core {
    2627  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);
    3328    IEnumerable<IVertex> Vertices { get; }
    3429    IEnumerable<IArc> Arcs { get; }
    3530
    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;
    4049  }
    4150}
  • stable/HeuristicLab.Core/3.3/Interfaces/DirectedGraph/IVertex.cs

    r11241 r11935  
    3636    double Weight { get; set; }
    3737
    38     IDeepCloneable Data { get; set; }
    39 
    4038    void AddArc(IArc arc);
    4139    void RemoveArc(IArc arc);
     
    4644  }
    4745
    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; }
    5148  }
    5249}
Note: See TracChangeset for help on using the changeset viewer.