Changeset 11752


Ignore:
Timestamp:
01/13/15 17:17:42 (5 years ago)
Author:
bburlacu
Message:

#1772: GenealogyGraph: minor refactoring, added a fix/hack to restore correct arcs order for each vertex after deserialization.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs

    r11694 r11752  
    3737    private Dictionary<string, IGenealogyGraphNode> idMap;
    3838
    39     private Dictionary<double, List<IGenealogyGraphNode>> ranks;
    40     public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
    41       get { return ranks; }
    42       set { ranks = value; }
     39    private readonly Comparison<IArc> compareArcs = (a, b) => {
     40      if (a.Data == b.Data)
     41        return 0;
     42      if (a.Data == null)
     43        return -1;
     44      return 1;
     45    };
     46
     47    protected Dictionary<double, List<IGenealogyGraphNode>> ranks;
     48    public IEnumerable<KeyValuePair<double, IEnumerable<IGenealogyGraphNode>>> Ranks {
     49      get { return ranks.Select(x => new KeyValuePair<double, IEnumerable<IGenealogyGraphNode>>(x.Key, x.Value)); }
    4350    }
    44 
     51    public IEnumerable<IGenealogyGraphNode> GetByRank(double rank) {
     52      return ranks.ContainsKey(rank) ? ranks[rank] : Enumerable.Empty<IGenealogyGraphNode>();
     53    }
    4554    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
    4655      : base(original, cloner) {
    4756      RebuildDictionaries();
    4857
    49       foreach (var arcs in Vertices.Select(v => (List<IArc>)((Vertex)v).InArcs)) {
    50         arcs.Sort((a, b) => {
    51           if (a.Data == b.Data)
    52             return 0;
    53           if (a.Data == null)
    54             return -1;
    55           return 1;
    56         });
    57       }
     58      foreach (var arcs in Vertices.Select(v => (List<IArc>)((IVertex)v).InArcs)) { arcs.Sort(compareArcs); }
    5859    }
    59 
    6060    public override IDeepCloneable Clone(Cloner cloner) {
    6161      return new GenealogyGraph(this, cloner);
     
    6666      : base(deserializing) {
    6767    }
    68 
    6968    [StorableHook(HookType.AfterDeserialization)]
    7069    private void AfterDeserialization() {
    7170      RebuildDictionaries();
    72 
    73       foreach (var arcs in Vertices.Select(v => (List<IArc>)((Vertex)v).InArcs)) {
    74         arcs.Sort((a, b) => {
    75           if (a.Data == b.Data)
    76             return 0;
    77           if (a.Data == null)
    78             return -1;
    79           return 1;
    80         });
    81       }
     71      foreach (var arcs in Vertices.Select(v => (List<IArc>)((Vertex)v).InArcs)) { arcs.Sort(compareArcs); }
    8272    }
    83 
    8473    public GenealogyGraph() {
    85       Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
     74      ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
    8675      contentMap = new Dictionary<object, IGenealogyGraphNode>();
    8776      idMap = new Dictionary<string, IGenealogyGraphNode>();
    8877    }
    89 
    9078    new public IEnumerable<IGenealogyGraphNode> Vertices {
    9179      get { return base.Vertices.Select(x => (IGenealogyGraphNode)x); }
    9280    }
    93 
    9481    new public IEnumerable<IGenealogyGraphArc> Arcs {
    9582      get { return base.Arcs.Select(x => (IGenealogyGraphArc)x); }
    9683    }
    97 
    9884    public override IArc AddArc(IVertex source, IVertex target) {
    9985      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
     
    10187      return arc;
    10288    }
    103 
    10489    public override void AddVertex(IVertex vertex) {
    10590      base.AddVertex(vertex);
     
    115100      ranks[genealogyGraphNode.Rank].Add(genealogyGraphNode);
    116101    }
    117 
    118102    public override void RemoveVertex(IVertex vertex) {
    119103      var node = (IGenealogyGraphNode)vertex;
    120104      contentMap.Remove(node.Data);
    121105      idMap.Remove(node.Id);
    122       if (Ranks.ContainsKey(node.Rank)) {
    123         Ranks[node.Rank].Remove(node);
    124         if (!Ranks[node.Rank].Any())
    125           Ranks.Remove(node.Rank);
     106      if (ranks.ContainsKey(node.Rank)) {
     107        ranks[node.Rank].Remove(node); // this call will be slow, use with caution
     108        if (!ranks[node.Rank].Any())
     109          ranks.Remove(node.Rank);
    126110      }
    127111      base.RemoveVertex(vertex);
    128112    }
    129 
    130113    public IGenealogyGraphNode GetByContent(object content) {
    131114      IGenealogyGraphNode result;
     
    133116      return result;
    134117    }
    135 
    136118    public IGenealogyGraphNode GetById(string id) {
    137119      IGenealogyGraphNode result;
     
    139121      return result;
    140122    }
    141 
    142123    public bool Contains(object content) {
    143124      return contentMap.ContainsKey(content);
    144125    }
    145 
    146126    public event EventHandler GraphUpdated;
    147127    private void OnGraphUpdated(object sender, EventArgs args) {
     
    149129      if (updated != null) updated(sender, args);
    150130    }
    151 
    152131    public override void Clear() {
    153132      base.Clear();
     
    156135      idMap.Clear();
    157136    }
    158 
    159137    private void RebuildDictionaries() {
    160138      contentMap = new Dictionary<object, IGenealogyGraphNode>();
     
    172150      }
    173151    }
    174 
    175152    public void ExportDot(string path) {
    176153      var sb = new StringBuilder();
     
    186163      File.WriteAllText(path, sb.ToString());
    187164    }
    188 
    189165    private Color GetColor(IGenealogyGraphNode node) {
    190166      var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count);
     
    213189      return (IGenealogyGraphNode<T>)base.GetById(id);
    214190    }
     191    new public IEnumerable<IGenealogyGraphNode<T>> GetByRank(double rank) {
     192      return base.GetByRank(rank).Select(x => (IGenealogyGraphNode<T>)x);
     193    }
     194    public new IEnumerable<KeyValuePair<double, IEnumerable<IGenealogyGraphNode<T>>>> Ranks {
     195      get { return base.Ranks.Select(x => new KeyValuePair<double, IEnumerable<IGenealogyGraphNode<T>>>(x.Key, x.Value.Select(n => (IGenealogyGraphNode<T>)n))); }
     196    }
    215197  }
    216198}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraph.cs

    r11390 r11752  
    2525namespace HeuristicLab.EvolutionTracking {
    2626  public interface IGenealogyGraph : IDirectedGraph {
    27     Dictionary<double, List<IGenealogyGraphNode>> Ranks { get; }
     27    IEnumerable<IGenealogyGraphNode> GetByRank(double rank);
     28    IEnumerable<KeyValuePair<double, IEnumerable<IGenealogyGraphNode>>> Ranks { get; }
    2829    IGenealogyGraphNode GetByContent(object content);
    2930    IGenealogyGraphNode GetById(string id);
Note: See TracChangeset for help on using the changeset viewer.