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", "")]
|
---|
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 | public IGenealogyGraphArc AddArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
|
---|
44 | var arc = new GenealogyGraphArc(source, target);
|
---|
45 | source.AddForwardArc(arc);
|
---|
46 | target.AddReverseArc(arc);
|
---|
47 | return arc;
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
|
---|
51 | : base(original, cloner) {
|
---|
52 | }
|
---|
53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | return new GenealogyGraph(this, cloner);
|
---|
55 | }
|
---|
56 | [StorableConstructor]
|
---|
57 | protected GenealogyGraph(bool deserializing) : base(deserializing) { }
|
---|
58 | public GenealogyGraph() {
|
---|
59 | Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
60 | }
|
---|
61 | public override void AddVertex(IVertex vertex) {
|
---|
62 | var node = (IGenealogyGraphNode)vertex;
|
---|
63 | if (!Ranks.ContainsKey(node.Rank))
|
---|
64 | Ranks[node.Rank] = new List<IGenealogyGraphNode>();
|
---|
65 | Ranks[node.Rank].Add(node);
|
---|
66 | base.AddVertex(vertex);
|
---|
67 | }
|
---|
68 | public override void RemoveVertex(IVertex vertex) {
|
---|
69 | var node = (IGenealogyGraphNode)vertex;
|
---|
70 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
71 | Ranks[node.Rank].Remove(node);
|
---|
72 | }
|
---|
73 | base.RemoveVertex(vertex);
|
---|
74 | }
|
---|
75 | public event EventHandler GraphUpdated;
|
---|
76 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
77 | var updated = GraphUpdated;
|
---|
78 | if (updated != null) updated(sender, args);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public new IGenealogyGraphNode this[object content] {
|
---|
82 | get {
|
---|
83 | var result = base[content];
|
---|
84 | return result == null ? null : (IGenealogyGraphNode)result;
|
---|
85 | }
|
---|
86 | set { base[content] = value; }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override void Clear() {
|
---|
90 | base.Clear();
|
---|
91 | ranks.Clear();
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | [StorableClass]
|
---|
96 | [Item("GenealogyGraph", "")]
|
---|
97 | public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
|
---|
98 | // members and properties
|
---|
99 | [Storable]
|
---|
100 | private Dictionary<double, List<IGenealogyGraphNode>> ranks;
|
---|
101 | public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
|
---|
102 | get { return ranks; }
|
---|
103 | set { ranks = value; }
|
---|
104 | }
|
---|
105 | public new IEnumerable<IGenealogyGraphNode<T>> Nodes {
|
---|
106 | get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public IGenealogyGraphArc AddArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
|
---|
110 | var arc = new GenealogyGraphArc(source, target);
|
---|
111 | source.AddForwardArc(arc);
|
---|
112 | target.AddReverseArc(arc);
|
---|
113 | return arc;
|
---|
114 | }
|
---|
115 |
|
---|
116 | // contructors
|
---|
117 | protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
|
---|
118 | : base(original, cloner) {
|
---|
119 | }
|
---|
120 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
121 | return new GenealogyGraph<T>(this, cloner);
|
---|
122 | }
|
---|
123 | [StorableConstructor]
|
---|
124 | protected GenealogyGraph(bool deserializing) : base(deserializing) { }
|
---|
125 | public GenealogyGraph() {
|
---|
126 | Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
127 | }
|
---|
128 | // methods
|
---|
129 | public override void AddVertex(IVertex vertex) {
|
---|
130 | var node = (IGenealogyGraphNode<T>)vertex;
|
---|
131 | if (!Ranks.ContainsKey(node.Rank)) {
|
---|
132 | Ranks[node.Rank] = new List<IGenealogyGraphNode>();
|
---|
133 | }
|
---|
134 | Ranks[node.Rank].Add(node);
|
---|
135 | base.AddVertex(vertex);
|
---|
136 | }
|
---|
137 | public override void RemoveVertex(IVertex vertex) {
|
---|
138 | var node = (IGenealogyGraphNode<T>)vertex;
|
---|
139 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
140 | Ranks[node.Rank].Remove(node);
|
---|
141 | }
|
---|
142 | base.RemoveVertex(vertex);
|
---|
143 | }
|
---|
144 |
|
---|
145 | IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes {
|
---|
146 | get { return Nodes; }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public event EventHandler GraphUpdated;
|
---|
150 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
151 | var updated = GraphUpdated;
|
---|
152 | if (updated != null) updated(sender, args);
|
---|
153 | }
|
---|
154 | public new IGenealogyGraphNode<T> this[object content] {
|
---|
155 | get {
|
---|
156 | var result = base[content];
|
---|
157 | return result == null ? null : (IGenealogyGraphNode<T>)result;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|