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 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;
|
---|
41 |
|
---|
42 | public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
|
---|
43 | get { return ranks; }
|
---|
44 | set { ranks = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
|
---|
48 | : base(original, cloner) {
|
---|
49 | Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(original.Ranks);
|
---|
50 | contentMap = new Dictionary<object, IGenealogyGraphNode>(original.contentMap);
|
---|
51 | idMap = new Dictionary<string, IGenealogyGraphNode>(original.idMap);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
55 | return new GenealogyGraph(this, cloner);
|
---|
56 | }
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected GenealogyGraph(bool deserializing)
|
---|
60 | : base(deserializing) {
|
---|
61 | }
|
---|
62 |
|
---|
63 | public GenealogyGraph() {
|
---|
64 | Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
65 | contentMap = new Dictionary<object, IGenealogyGraphNode>();
|
---|
66 | idMap = new Dictionary<string, IGenealogyGraphNode>();
|
---|
67 | }
|
---|
68 |
|
---|
69 | public new IEnumerable<IGenealogyGraphNode> Vertices {
|
---|
70 | get { return from n in base.Vertices select (IGenealogyGraphNode)n; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override IArc AddArc(IVertex source, IVertex target) {
|
---|
74 | var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
|
---|
75 | base.AddArc(arc);
|
---|
76 | return arc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override void AddVertex(IVertex vertex) {
|
---|
80 | base.AddVertex(vertex);
|
---|
81 | var node = (IGenealogyGraphNode)vertex;
|
---|
82 | if (!Ranks.ContainsKey(node.Rank))
|
---|
83 | Ranks[node.Rank] = new List<IGenealogyGraphNode>();
|
---|
84 | Ranks[node.Rank].Add(node);
|
---|
85 |
|
---|
86 | if (contentMap.ContainsKey(node.Data))
|
---|
87 | throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
|
---|
88 | contentMap[node.Data] = node;
|
---|
89 |
|
---|
90 | if (idMap.ContainsKey(node.Id))
|
---|
91 | throw new InvalidOperationException("Duplicate ids are not allowed in the genealogy graph.");
|
---|
92 | idMap[node.Id] = node;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override void RemoveVertex(IVertex vertex) {
|
---|
96 | var node = (IGenealogyGraphNode)vertex;
|
---|
97 | contentMap.Remove(node.Data);
|
---|
98 | idMap.Remove(node.Id);
|
---|
99 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
100 | Ranks[node.Rank].Remove(node);
|
---|
101 | if (!Ranks[node.Rank].Any())
|
---|
102 | Ranks.Remove(node.Rank);
|
---|
103 | }
|
---|
104 | base.RemoveVertex(vertex);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public IGenealogyGraphNode GetByContent(object content) {
|
---|
108 | IGenealogyGraphNode result;
|
---|
109 | contentMap.TryGetValue(content, out result);
|
---|
110 | return result;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public IGenealogyGraphNode GetById(string id) {
|
---|
114 | IGenealogyGraphNode result;
|
---|
115 | idMap.TryGetValue(id, out result);
|
---|
116 | return result;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public bool Contains(object content) {
|
---|
120 | return contentMap.ContainsKey(content);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public event EventHandler GraphUpdated;
|
---|
124 |
|
---|
125 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
126 | var updated = GraphUpdated;
|
---|
127 | if (updated != null) updated(sender, args);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public override void Clear() {
|
---|
131 | base.Clear();
|
---|
132 | ranks.Clear();
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")]
|
---|
137 | [StorableClass]
|
---|
138 | public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem {
|
---|
139 | public new IEnumerable<IGenealogyGraphNode<T>> Vertices {
|
---|
140 | get { return base.Vertices.Cast<IGenealogyGraphNode<T>>(); }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public new IGenealogyGraphNode<T> GetByContent(object content) {
|
---|
144 | return (IGenealogyGraphNode<T>)base.GetByContent(content);
|
---|
145 | }
|
---|
146 |
|
---|
147 | public new IGenealogyGraphNode<T> GetById(string id) {
|
---|
148 | return (IGenealogyGraphNode<T>)base.GetById(id);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|