Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11233 was 11233, checked in by bburlacu, 10 years ago

#1772: Worked towards integrating the new graph api with the tracking operators.

File size: 7.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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.EvolutionTracking {
30  [StorableClass]
31  [Item("GenealogyGraph", "A class representing a genealogy graph")]
32  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]
40    private Dictionary<double, List<IGenealogyGraphNode>> ranks; // use a linked list for fast insertion/removal
41    public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
42      get { return ranks; }
43      set { ranks = value; }
44    }
45
46    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
47      : base(original, cloner) {
48      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(original.Ranks);
49      contentMap = new Dictionary<object, IGenealogyGraphNode>(original.contentMap);
50      idMap = new Dictionary<string, IGenealogyGraphNode>(original.idMap);
51    }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new GenealogyGraph(this, cloner);
55    }
56
57    [StorableConstructor]
58    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
59    public GenealogyGraph() {
60      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
61      contentMap = new Dictionary<object, IGenealogyGraphNode>();
62      idMap = new Dictionary<string, IGenealogyGraphNode>();
63    }
64
65    public new IEnumerable<IGenealogyGraphNode> Vertices {
66      get { return from n in base.Vertices select (IGenealogyGraphNode)n; }
67    }
68
69    public override IArc AddArc(IVertex source, IVertex target) {
70      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
71      source.AddArc(arc);
72      target.AddArc(arc);
73      arcs.Add(arc);
74      return arc;
75    }
76
77    public override void AddVertex(IVertex vertex) {
78      base.AddVertex(vertex);
79      var node = (IGenealogyGraphNode)vertex;
80      if (!Ranks.ContainsKey(node.Rank))
81        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
82      Ranks[node.Rank].Add(node);
83
84      if (contentMap.ContainsKey(node.Content))
85        throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
86      contentMap[node.Content] = node;
87
88      if (idMap.ContainsKey(node.Id))
89        throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
90      idMap[node.Id] = node;
91
92      vertex.Changed += OnVertexChanged;
93    }
94
95    public override void RemoveVertex(IVertex vertex) {
96      var node = (IGenealogyGraphNode)vertex;
97      contentMap.Remove(node.Content);
98      idMap.Remove(node.Id);
99      if (Ranks.ContainsKey(node.Rank)) {
100        Ranks[node.Rank].Remove(node);
101      }
102      base.RemoveVertex(vertex);
103    }
104
105    public IGenealogyGraphNode GetByContent(object content) {
106      IGenealogyGraphNode result;
107      contentMap.TryGetValue(content, out result);
108      return result;
109    }
110
111    public IGenealogyGraphNode GetById(string id) {
112      IGenealogyGraphNode result;
113      idMap.TryGetValue(id, out result);
114      return result;
115    }
116
117    public bool Contains(object content) {
118      return contentMap.ContainsKey(content);
119    }
120
121    public event EventHandler GraphUpdated;
122    private void OnGraphUpdated(object sender, EventArgs args) {
123      var updated = GraphUpdated;
124      if (updated != null) updated(sender, args);
125    }
126
127    public override void Clear() {
128      base.Clear();
129      ranks.Clear();
130    }
131
132    protected override void OnVertexChanged(object sender, EventArgs args) {
133      base.OnVertexChanged(sender, args);
134
135      var vertex = (IGenealogyGraphNode)sender;
136      if (!contentMap.ContainsKey(vertex.Content)) {
137        // vertex has received new content
138      }
139    }
140  }
141
142  //  [StorableClass]
143  //  [Item("GenealogyGraph", "A class representing a genealogy graph")]
144  //  public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
145  //    // members and properties
146  //    [Storable]
147  //    private Dictionary<double, List<IGenealogyGraphNode>> ranks;
148  //    public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
149  //      get { return ranks; }
150  //      set { ranks = value; }
151  //    }
152  //    public new IEnumerable<IGenealogyGraphNode<T>> Vertices {
153  //      get { return from n in base.Vertices select (IGenealogyGraphNode<T>)n; }
154  //    }
155  //    // contructors
156  //    protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
157  //      : base(original, cloner) {
158  //    }
159  //    public override IDeepCloneable Clone(Cloner cloner) {
160  //      return new GenealogyGraph<T>(this, cloner);
161  //    }
162  //
163  //    [StorableConstructor]
164  //    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
165  //    public GenealogyGraph() {
166  //      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
167  //    }
168  //
169  //    // methods
170  //    public override void AddVertex(IVertex vertex) {
171  //      base.AddVertex(vertex);
172  //      var node = (IGenealogyGraphNode)vertex;
173  //      if (!Ranks.ContainsKey(node.Rank)) {
174  //        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
175  //      }
176  //      Ranks[node.Rank].Add(node);
177  //    }
178  //
179  //    public override void RemoveVertex(IVertex vertex) {
180  //      var node = (IGenealogyGraphNode<T>)vertex;
181  //      if (Ranks.ContainsKey(node.Rank)) {
182  //        Ranks[node.Rank].Remove(node);
183  //      }
184  //      base.RemoveVertex(vertex);
185  //    }
186  //
187  //    public override IArc AddArc(IVertex source, IVertex target) {
188  //      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
189  //      source.AddArc(arc);
190  //      target.AddArc(arc);
191  //      arcs.Add(arc);
192  //      return arc;
193  //    }
194  //
195  //    IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Vertices {
196  //      get { return Vertices; }
197  //    }
198  //
199  //    public IGenealogyGraphNode GetByContent(object content)
200  //    {
201  //      IGenealogyGraphNode result;
202  //      contentMap.
203  //    }
204  //
205  //    public event EventHandler GraphUpdated;
206  //    private void OnGraphUpdated(object sender, EventArgs args) {
207  //      var updated = GraphUpdated;
208  //      if (updated != null) updated(sender, args);
209  //    }
210  //  }
211  [StorableClass]
212
213  [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")]
214  public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem {
215    public new IEnumerable<IGenealogyGraphNode<T>> Vertices {
216      get { return base.Vertices.Cast<IGenealogyGraphNode<T>>(); }
217    }
218  }
219}
Note: See TracBrowser for help on using the repository browser.