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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace 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 new IGenealogyGraphNode this[object content] {
|
---|
84 | get {
|
---|
85 | var result = base[content];
|
---|
86 | return result == null ? null : (IGenealogyGraphNode)result;
|
---|
87 | }
|
---|
88 | set { base[content] = value; }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public override void Clear() {
|
---|
92 | base.Clear();
|
---|
93 | ranks.Clear();
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | [StorableClass]
|
---|
98 | [Item("GenealogyGraph", "A class representing a genealogy graph")]
|
---|
99 | public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
|
---|
100 | // members and properties
|
---|
101 | [Storable]
|
---|
102 | private Dictionary<double, List<IGenealogyGraphNode>> ranks;
|
---|
103 | public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
|
---|
104 | get { return ranks; }
|
---|
105 | set { ranks = value; }
|
---|
106 | }
|
---|
107 | public new IEnumerable<IGenealogyGraphNode<T>> Nodes {
|
---|
108 | get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; }
|
---|
109 | }
|
---|
110 | // contructors
|
---|
111 | protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
|
---|
112 | : base(original, cloner) {
|
---|
113 | }
|
---|
114 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
115 | return new GenealogyGraph<T>(this, cloner);
|
---|
116 | }
|
---|
117 | [StorableConstructor]
|
---|
118 | protected GenealogyGraph(bool deserializing) : base(deserializing) { }
|
---|
119 | public GenealogyGraph() {
|
---|
120 | Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
121 | }
|
---|
122 | // methods
|
---|
123 | public override void AddVertex(IVertex vertex) {
|
---|
124 | base.AddVertex(vertex);
|
---|
125 | var node = (IGenealogyGraphNode<T>)vertex;
|
---|
126 | if (!Ranks.ContainsKey(node.Rank)) {
|
---|
127 | Ranks[node.Rank] = new List<IGenealogyGraphNode>();
|
---|
128 | }
|
---|
129 | Ranks[node.Rank].Add(node);
|
---|
130 | }
|
---|
131 | public override void RemoveVertex(IVertex vertex) {
|
---|
132 | var node = (IGenealogyGraphNode<T>)vertex;
|
---|
133 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
134 | Ranks[node.Rank].Remove(node);
|
---|
135 | }
|
---|
136 | base.RemoveVertex(vertex);
|
---|
137 | }
|
---|
138 | public override IArc AddArc(IVertex source, IVertex target) {
|
---|
139 | var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
|
---|
140 | source.AddForwardArc(arc);
|
---|
141 | target.AddReverseArc(arc);
|
---|
142 | return arc;
|
---|
143 | }
|
---|
144 | IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes {
|
---|
145 | get { return Nodes; }
|
---|
146 | }
|
---|
147 | public event EventHandler GraphUpdated;
|
---|
148 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
149 | var updated = GraphUpdated;
|
---|
150 | if (updated != null) updated(sender, args);
|
---|
151 | }
|
---|
152 | public new IGenealogyGraphNode<T> this[object content] {
|
---|
153 | get {
|
---|
154 | var result = base[content];
|
---|
155 | return result == null ? null : (IGenealogyGraphNode<T>)result;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|