Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs @ 11502

Last change on this file since 11502 was 11502, checked in by bburlacu, 8 years ago

#1772: Small refactoring in the GenealogyGraph.

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.IO;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.EvolutionTracking {
33  [StorableClass]
34  [Item("GenealogyGraph", "A class representing a genealogy graph")]
35  public class GenealogyGraph : DirectedGraph, IGenealogyGraph {
36    private Dictionary<object, IGenealogyGraphNode> contentMap;
37    private Dictionary<string, IGenealogyGraphNode> idMap;
38
39    private Dictionary<double, List<IGenealogyGraphNode>> ranks;
40    public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
41      get { return ranks; }
42      set { ranks = value; }
43    }
44
45    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
46      : base(original, cloner) {
47      RebuildDictionaries();
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new GenealogyGraph(this, cloner);
52    }
53
54    [StorableConstructor]
55    protected GenealogyGraph(bool deserializing)
56      : base(deserializing) {
57    }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
61      RebuildDictionaries();
62    }
63
64    public GenealogyGraph() {
65      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
66      contentMap = new Dictionary<object, IGenealogyGraphNode>();
67      idMap = new Dictionary<string, IGenealogyGraphNode>();
68    }
69
70    new public IEnumerable<IGenealogyGraphNode> Vertices {
71      get { return base.Vertices.Select(x => (IGenealogyGraphNode)x); }
72    }
73
74    new public IEnumerable<IGenealogyGraphArc> Arcs {
75      get { return base.Arcs.Select(x => (IGenealogyGraphArc)x); }
76    }
77
78    public override IArc AddArc(IVertex source, IVertex target) {
79      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
80      base.AddArc(arc);
81      return arc;
82    }
83
84    public override void AddVertex(IVertex vertex) {
85      base.AddVertex(vertex);
86      var genealogyGraphNode = (IGenealogyGraphNode)vertex;
87      if (contentMap.ContainsKey(genealogyGraphNode.Data))
88        throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
89      contentMap[genealogyGraphNode.Data] = genealogyGraphNode;
90      if (idMap.ContainsKey(genealogyGraphNode.Id))
91        throw new InvalidOperationException("Duplicate ids are not allowed in the genealogy graph.");
92      idMap[genealogyGraphNode.Id] = genealogyGraphNode;
93      if (!Ranks.ContainsKey(genealogyGraphNode.Rank))
94        Ranks[genealogyGraphNode.Rank] = new List<IGenealogyGraphNode>();
95      Ranks[genealogyGraphNode.Rank].Add(genealogyGraphNode);
96    }
97
98    public override void RemoveVertex(IVertex vertex) {
99      var node = (IGenealogyGraphNode)vertex;
100      contentMap.Remove(node.Data);
101      idMap.Remove(node.Id);
102      if (Ranks.ContainsKey(node.Rank)) {
103        Ranks[node.Rank].Remove(node);
104        if (!Ranks[node.Rank].Any())
105          Ranks.Remove(node.Rank);
106      }
107      base.RemoveVertex(vertex);
108    }
109
110    public IGenealogyGraphNode GetByContent(object content) {
111      IGenealogyGraphNode result;
112      contentMap.TryGetValue(content, out result);
113      return result;
114    }
115
116    public IGenealogyGraphNode GetById(string id) {
117      IGenealogyGraphNode result;
118      idMap.TryGetValue(id, out result);
119      return result;
120    }
121
122    public bool Contains(object content) {
123      return contentMap.ContainsKey(content);
124    }
125
126    public event EventHandler GraphUpdated;
127    private void OnGraphUpdated(object sender, EventArgs args) {
128      var updated = GraphUpdated;
129      if (updated != null) updated(sender, args);
130    }
131
132    public override void Clear() {
133      base.Clear();
134      ranks.Clear();
135      contentMap.Clear();
136      idMap.Clear();
137    }
138
139    private void RebuildDictionaries() {
140      contentMap = new Dictionary<object, IGenealogyGraphNode>();
141      idMap = new Dictionary<string, IGenealogyGraphNode>();
142      ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
143
144      foreach (var v in Vertices) {
145        contentMap[v.Data] = v;
146        idMap[v.Id] = v;
147        if (ranks.ContainsKey(v.Rank)) {
148          ranks[v.Rank].Add(v);
149        } else {
150          ranks[v.Rank] = new List<IGenealogyGraphNode> { v };
151        }
152      }
153    }
154
155    public void ExportDot(string path) {
156      var sb = new StringBuilder();
157      sb.AppendLine("graph fragmentgraph {");
158      foreach (var v in Vertices) {
159        sb.AppendLine("\"" + v.Id + "\"[shape=circle, style = filled, width = " + v.Degree / 2.0 + ", label = " + v.Rank + ", fillcolor = \"" + ColorTranslator.ToHtml(GetColor(v)) + "\"]");
160      }
161      foreach (var a in Arcs) {
162        sb.AppendLine("\"" + a.Source.Id + "\" -- \"" + a.Target.Id + "\"");
163      }
164
165      sb.AppendLine("}");
166      File.WriteAllText(path, sb.ToString());
167    }
168
169    private Color GetColor(IGenealogyGraphNode node) {
170      var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count);
171      if (colorIndex >= ColorGradient.Colors.Count) return ColorGradient.Colors.Last();
172      return ColorGradient.Colors[colorIndex];
173    }
174  }
175
176  [Item("GenealogyGraph", "A genealogy graph in which the vertex data is of type T")]
177  [StorableClass]
178  public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem {
179    new public IEnumerable<IGenealogyGraphNode<T>> Vertices {
180      get { return base.Vertices.Select(x => (IGenealogyGraphNode<T>)x); }
181    }
182    new public IGenealogyGraphNode<T> GetByContent(object content) {
183      return (IGenealogyGraphNode<T>)base.GetByContent(content);
184    }
185    new public IGenealogyGraphNode<T> GetById(string id) {
186      return (IGenealogyGraphNode<T>)base.GetById(id);
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.