Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Added new SymbolicDataAnalysisGenealogyView and added support for the tracing of building blocks (finding the constituent ancestral elements of a selected subtree).

File size: 4.6 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", "")]
32  public class GenealogyGraph : DirectedGraph, IGenealogyGraph {
33    [Storable]
34    private Dictionary<double, LinkedList<IGenealogyGraphNode>> ranks; // use a linked list for fast insertion/removal
35    public Dictionary<double, LinkedList<IGenealogyGraphNode>> Ranks {
36      get { return ranks; }
37      set { ranks = value; }
38    }
39    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new GenealogyGraph(this, cloner);
44    }
45    [StorableConstructor]
46    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
47    public GenealogyGraph() {
48      Ranks = new Dictionary<double, LinkedList<IGenealogyGraphNode>>();
49    }
50    public override void AddVertex(IVertex vertex) {
51      var node = (IGenealogyGraphNode)vertex;
52      if (!Ranks.ContainsKey(node.Rank))
53        Ranks[node.Rank] = new LinkedList<IGenealogyGraphNode>();
54      Ranks[node.Rank].AddLast(node);
55      base.AddVertex(vertex);
56    }
57    public override void RemoveVertex(IVertex vertex) {
58      var node = (IGenealogyGraphNode)vertex;
59      if (Ranks.ContainsKey(node.Rank)) {
60        Ranks[node.Rank].Remove(node);
61      }
62      base.RemoveVertex(vertex);
63    }
64    public event EventHandler GraphUpdated;
65    private void OnGraphUpdated(object sender, EventArgs args) {
66      var updated = GraphUpdated;
67      if (updated != null) updated(sender, args);
68    }
69
70    public new List<IGenealogyGraphNode> this[object content] {
71      get {
72        var result = base[content];
73        return result != null ? result.Cast<IGenealogyGraphNode>().ToList() : null;
74      }
75    }
76  }
77
78  [StorableClass]
79  [Item("GenealogyGraph", "")]
80  public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
81    [Storable]
82    private Dictionary<double, LinkedList<IGenealogyGraphNode>> ranks;
83    public Dictionary<double, LinkedList<IGenealogyGraphNode>> Ranks {
84      get { return ranks; }
85      set { ranks = value; }
86    }
87    protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
88      : base(original, cloner) {
89    }
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new GenealogyGraph<T>(this, cloner);
92    }
93    [StorableConstructor]
94    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
95    public GenealogyGraph() {
96      Ranks = new Dictionary<double, LinkedList<IGenealogyGraphNode>>();
97    }
98    public override void AddVertex(IVertex vertex) {
99      var node = (IGenealogyGraphNode<T>)vertex;
100      if (!Ranks.ContainsKey(node.Rank))
101        Ranks[node.Rank] = new LinkedList<IGenealogyGraphNode>();
102      Ranks[node.Rank].AddLast(node);
103      base.AddVertex(vertex);
104    }
105    public override void RemoveVertex(IVertex vertex) {
106      var node = (IGenealogyGraphNode<T>)vertex;
107      if (Ranks.ContainsKey(node.Rank)) {
108        Ranks[node.Rank].Remove(node);
109      }
110      base.RemoveVertex(vertex);
111    }
112    public event EventHandler GraphUpdated;
113    private void OnGraphUpdated(object sender, EventArgs args) {
114      var updated = GraphUpdated;
115      if (updated != null) updated(sender, args);
116    }
117
118    public new List<IGenealogyGraphNode<T>> this[object content] {
119      get {
120        var result = base[content];
121        return result != null ? result.Cast<IGenealogyGraphNode<T>>().ToList() : null;
122      }
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.