Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Adjusted namespace in Plugin.cs.frame for HeuristicLab.EvolutionTracking.Views. Simplified DirectedGraph and GenealogyGraph API. Added public events for the Vertex content (so that the graph itself can be notified when the content changes and can adjust it's content-to-vertex map. Adjusted instrumented operators code to reflect api changes.

File size: 5.3 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 Dictionary<double, List<IGenealogyGraphNode>> ranks; // use a linked list for fast insertion/removal
35    public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
36      get { return ranks; }
37      set { ranks = value; }
38    }
39    public new IEnumerable<IGenealogyGraphNode> Nodes {
40      get { return from n in base.Nodes select (IGenealogyGraphNode)n; }
41    }
42
43    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
44      : base(original, cloner) {
45      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(original.Ranks);
46    }
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new GenealogyGraph(this, cloner);
49    }
50    [StorableConstructor]
51    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
52    public GenealogyGraph() {
53      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
54    }
55
56    public override IArc AddArc(IVertex source, IVertex target) {
57      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
58      source.AddForwardArc(arc);
59      target.AddReverseArc(arc);
60      return arc;
61    }
62
63    public override void AddVertex(IVertex vertex) {
64      base.AddVertex(vertex);
65      var node = (IGenealogyGraphNode)vertex;
66      if (!Ranks.ContainsKey(node.Rank))
67        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
68      Ranks[node.Rank].Add(node);
69    }
70    public override void RemoveVertex(IVertex vertex) {
71      var node = (IGenealogyGraphNode)vertex;
72      if (Ranks.ContainsKey(node.Rank)) {
73        Ranks[node.Rank].Remove(node);
74      }
75      base.RemoveVertex(vertex);
76    }
77    public event EventHandler GraphUpdated;
78    private void OnGraphUpdated(object sender, EventArgs args) {
79      var updated = GraphUpdated;
80      if (updated != null) updated(sender, args);
81    }
82
83    public override void Clear() {
84      base.Clear();
85      ranks.Clear();
86    }
87  }
88
89  [StorableClass]
90  [Item("GenealogyGraph", "A class representing a genealogy graph")]
91  public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
92    // members and properties
93    [Storable]
94    private Dictionary<double, List<IGenealogyGraphNode>> ranks;
95    public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
96      get { return ranks; }
97      set { ranks = value; }
98    }
99    public new IEnumerable<IGenealogyGraphNode<T>> Nodes {
100      get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; }
101    }
102    // contructors
103    protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
104      : base(original, cloner) {
105    }
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new GenealogyGraph<T>(this, cloner);
108    }
109    [StorableConstructor]
110    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
111    public GenealogyGraph() {
112      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
113    }
114    // methods
115    public override void AddVertex(IVertex vertex) {
116      base.AddVertex(vertex);
117      var node = (IGenealogyGraphNode)vertex;
118      if (!Ranks.ContainsKey(node.Rank)) {
119        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
120      }
121      Ranks[node.Rank].Add(node);
122    }
123    public override void RemoveVertex(IVertex vertex) {
124      var node = (IGenealogyGraphNode<T>)vertex;
125      if (Ranks.ContainsKey(node.Rank)) {
126        Ranks[node.Rank].Remove(node);
127      }
128      base.RemoveVertex(vertex);
129    }
130    public override IArc AddArc(IVertex source, IVertex target) {
131      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
132      source.AddForwardArc(arc);
133      target.AddReverseArc(arc);
134      return arc;
135    }
136    IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes {
137      get { return Nodes; }
138    }
139    public event EventHandler GraphUpdated;
140    private void OnGraphUpdated(object sender, EventArgs args) {
141      var updated = GraphUpdated;
142      if (updated != null) updated(sender, args);
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.