Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11459


Ignore:
Timestamp:
10/13/14 13:14:25 (10 years ago)
Author:
bburlacu
Message:

#1772: GenealogyGraph: fix serialization
GenealogyGraphNode: Avoid boxing/unboxing with the IEnumerable.Cast<T>

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

    r11390 r11459  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
     25using System.IO;
    2426using System.Linq;
     27using System.Text;
    2528using HeuristicLab.Common;
    2629using HeuristicLab.Core;
     
    3134  [Item("GenealogyGraph", "A class representing a genealogy graph")]
    3235  public class GenealogyGraph : DirectedGraph, IGenealogyGraph {
    33     [Storable]
    34     private readonly Dictionary<object, IGenealogyGraphNode> contentMap;
    35 
    36     [Storable]
    37     private readonly Dictionary<string, IGenealogyGraphNode> idMap;
    38 
    39     [Storable]
     36    private Dictionary<object, IGenealogyGraphNode> contentMap;
     37    private Dictionary<string, IGenealogyGraphNode> idMap;
    4038    protected Dictionary<double, List<IGenealogyGraphNode>> ranks;
    4139
     
    5957    protected GenealogyGraph(bool deserializing)
    6058      : base(deserializing) {
     59    }
     60
     61    [StorableHook(HookType.AfterDeserialization)]
     62    private void AfterDeserialization() {
     63      contentMap = new Dictionary<object, IGenealogyGraphNode>();
     64      idMap = new Dictionary<string, IGenealogyGraphNode>();
     65      ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
     66
     67      foreach (var v in Vertices) {
     68        contentMap[v.Data] = v;
     69        idMap[v.Id] = v;
     70        if (ranks.ContainsKey(v.Rank)) {
     71          ranks[v.Rank].Add(v);
     72        } else {
     73          ranks[v.Rank] = new List<IGenealogyGraphNode> { v };
     74        }
     75      }
    6176    }
    6277
     
    130145      ranks.Clear();
    131146    }
     147
     148    public void ExportDot(string path) {
     149      var sb = new StringBuilder();
     150      sb.AppendLine("graph fragmentgraph {");
     151      foreach (var v in Vertices) {
     152        sb.AppendLine("\"" + v.Id + "\"[shape=circle, style = filled, width = " + v.Degree / 2.0 + ", label = " + v.Rank + ", fillcolor = \"" + ColorTranslator.ToHtml(GetColor(v)) + "\"]");
     153      }
     154      foreach (var a in Arcs.Select(x => (IGenealogyGraphArc)x)) {
     155        sb.AppendLine("\"" + a.Source.Id + "\" -- \"" + a.Target.Id + "\"");
     156      }
     157
     158      sb.AppendLine("}");
     159      File.WriteAllText(path, sb.ToString());
     160    }
     161
     162    private Color GetColor(IGenealogyGraphNode node) {
     163      var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count);
     164      if (colorIndex >= ColorGradient.Colors.Count) return ColorGradient.Colors.Last();
     165      return ColorGradient.Colors[colorIndex];
     166    }
    132167  }
    133168
    134   [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")]
     169  [Item("GenealogyGraph", "A genealogy graph in which the vertex data is of type T")]
    135170  [StorableClass]
    136171  public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs

    r11262 r11459  
    5252
    5353    public new IEnumerable<IGenealogyGraphArc> InArcs {
    54       get { return base.InArcs.Cast<IGenealogyGraphArc>(); }
     54      get { return base.InArcs.Select(x => (IGenealogyGraphArc)x); }
    5555    }
    5656
    5757    public new IEnumerable<IGenealogyGraphArc> OutArcs {
    58       get { return base.OutArcs.Cast<IGenealogyGraphArc>(); }
     58      get { return base.OutArcs.Select(x => (IGenealogyGraphArc)x); }
    5959    }
    6060
     
    148148    }
    149149    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
    150       get { return base.Parents.Cast<IGenealogyGraphNode<T>>(); }
     150      get { return base.Parents.Select(x => (IGenealogyGraphNode<T>)x); }
    151151    }
    152152    public new IEnumerable<IGenealogyGraphNode<T>> Children {
    153       get { return base.Children.Cast<IGenealogyGraphNode<T>>(); }
     153      get { return base.Children.Select(x => (IGenealogyGraphNode<T>)x); }
    154154    }
    155155    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
    156       get { return base.Ancestors.Cast<IGenealogyGraphNode<T>>(); }
     156      get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
    157157    }
    158158    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
    159       get { return base.Descendants.Cast<IGenealogyGraphNode<T>>(); }
     159      get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
    160160    }
    161161  }
Note: See TracChangeset for help on using the changeset viewer.