Changeset 11233 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph
- Timestamp:
- 07/30/14 01:41:54 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs
r10903 r11233 32 32 public class GenealogyGraph : DirectedGraph, IGenealogyGraph { 33 33 [Storable] 34 private readonly Dictionary<object, IGenealogyGraphNode> contentMap; 35 36 [Storable] 37 private readonly Dictionary<string, IGenealogyGraphNode> idMap; 38 39 [Storable] 34 40 private Dictionary<double, List<IGenealogyGraphNode>> ranks; // use a linked list for fast insertion/removal 35 41 public Dictionary<double, List<IGenealogyGraphNode>> Ranks { … … 37 43 set { ranks = value; } 38 44 } 39 public new IEnumerable<IGenealogyGraphNode> Vertices {40 get { return from n in base.Vertices select (IGenealogyGraphNode)n; }41 }42 45 43 46 protected GenealogyGraph(GenealogyGraph original, Cloner cloner) 44 47 : base(original, cloner) { 45 48 Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(original.Ranks); 46 } 49 contentMap = new Dictionary<object, IGenealogyGraphNode>(original.contentMap); 50 idMap = new Dictionary<string, IGenealogyGraphNode>(original.idMap); 51 } 52 47 53 public override IDeepCloneable Clone(Cloner cloner) { 48 54 return new GenealogyGraph(this, cloner); 49 55 } 56 50 57 [StorableConstructor] 51 58 protected GenealogyGraph(bool deserializing) : base(deserializing) { } 52 59 public GenealogyGraph() { 53 60 Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(); 61 contentMap = new Dictionary<object, IGenealogyGraphNode>(); 62 idMap = new Dictionary<string, IGenealogyGraphNode>(); 63 } 64 65 public new IEnumerable<IGenealogyGraphNode> Vertices { 66 get { return from n in base.Vertices select (IGenealogyGraphNode)n; } 54 67 } 55 68 56 69 public override IArc AddArc(IVertex source, IVertex target) { 57 70 var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target); 58 source.Add ForwardArc(arc);59 target.Add ReverseArc(arc);71 source.AddArc(arc); 72 target.AddArc(arc); 60 73 arcs.Add(arc); 61 74 return arc; … … 68 81 Ranks[node.Rank] = new List<IGenealogyGraphNode>(); 69 82 Ranks[node.Rank].Add(node); 70 } 83 84 if (contentMap.ContainsKey(node.Content)) 85 throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph."); 86 contentMap[node.Content] = node; 87 88 if (idMap.ContainsKey(node.Id)) 89 throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph."); 90 idMap[node.Id] = node; 91 92 vertex.Changed += OnVertexChanged; 93 } 94 71 95 public override void RemoveVertex(IVertex vertex) { 72 96 var node = (IGenealogyGraphNode)vertex; 97 contentMap.Remove(node.Content); 98 idMap.Remove(node.Id); 73 99 if (Ranks.ContainsKey(node.Rank)) { 74 100 Ranks[node.Rank].Remove(node); … … 76 102 base.RemoveVertex(vertex); 77 103 } 104 105 public IGenealogyGraphNode GetByContent(object content) { 106 IGenealogyGraphNode result; 107 contentMap.TryGetValue(content, out result); 108 return result; 109 } 110 111 public IGenealogyGraphNode GetById(string id) { 112 IGenealogyGraphNode result; 113 idMap.TryGetValue(id, out result); 114 return result; 115 } 116 117 public bool Contains(object content) { 118 return contentMap.ContainsKey(content); 119 } 120 78 121 public event EventHandler GraphUpdated; 79 122 private void OnGraphUpdated(object sender, EventArgs args) { … … 86 129 ranks.Clear(); 87 130 } 131 132 protected override void OnVertexChanged(object sender, EventArgs args) { 133 base.OnVertexChanged(sender, args); 134 135 var vertex = (IGenealogyGraphNode)sender; 136 if (!contentMap.ContainsKey(vertex.Content)) { 137 // vertex has received new content 138 } 139 } 88 140 } 89 141 142 // [StorableClass] 143 // [Item("GenealogyGraph", "A class representing a genealogy graph")] 144 // public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem { 145 // // members and properties 146 // [Storable] 147 // private Dictionary<double, List<IGenealogyGraphNode>> ranks; 148 // public Dictionary<double, List<IGenealogyGraphNode>> Ranks { 149 // get { return ranks; } 150 // set { ranks = value; } 151 // } 152 // public new IEnumerable<IGenealogyGraphNode<T>> Vertices { 153 // get { return from n in base.Vertices select (IGenealogyGraphNode<T>)n; } 154 // } 155 // // contructors 156 // protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner) 157 // : base(original, cloner) { 158 // } 159 // public override IDeepCloneable Clone(Cloner cloner) { 160 // return new GenealogyGraph<T>(this, cloner); 161 // } 162 // 163 // [StorableConstructor] 164 // protected GenealogyGraph(bool deserializing) : base(deserializing) { } 165 // public GenealogyGraph() { 166 // Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(); 167 // } 168 // 169 // // methods 170 // public override void AddVertex(IVertex vertex) { 171 // base.AddVertex(vertex); 172 // var node = (IGenealogyGraphNode)vertex; 173 // if (!Ranks.ContainsKey(node.Rank)) { 174 // Ranks[node.Rank] = new List<IGenealogyGraphNode>(); 175 // } 176 // Ranks[node.Rank].Add(node); 177 // } 178 // 179 // public override void RemoveVertex(IVertex vertex) { 180 // var node = (IGenealogyGraphNode<T>)vertex; 181 // if (Ranks.ContainsKey(node.Rank)) { 182 // Ranks[node.Rank].Remove(node); 183 // } 184 // base.RemoveVertex(vertex); 185 // } 186 // 187 // public override IArc AddArc(IVertex source, IVertex target) { 188 // var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target); 189 // source.AddArc(arc); 190 // target.AddArc(arc); 191 // arcs.Add(arc); 192 // return arc; 193 // } 194 // 195 // IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Vertices { 196 // get { return Vertices; } 197 // } 198 // 199 // public IGenealogyGraphNode GetByContent(object content) 200 // { 201 // IGenealogyGraphNode result; 202 // contentMap. 203 // } 204 // 205 // public event EventHandler GraphUpdated; 206 // private void OnGraphUpdated(object sender, EventArgs args) { 207 // var updated = GraphUpdated; 208 // if (updated != null) updated(sender, args); 209 // } 210 // } 90 211 [StorableClass] 91 [Item("GenealogyGraph", "A class representing a genealogy graph")] 92 public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem { 93 // members and properties 94 [Storable] 95 private Dictionary<double, List<IGenealogyGraphNode>> ranks; 96 public Dictionary<double, List<IGenealogyGraphNode>> Ranks { 97 get { return ranks; } 98 set { ranks = value; } 99 } 212 213 [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")] 214 public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem { 100 215 public new IEnumerable<IGenealogyGraphNode<T>> Vertices { 101 get { return from n in base.Vertices select (IGenealogyGraphNode<T>)n; } 102 } 103 // contructors 104 protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner) 105 : base(original, cloner) { 106 } 107 public override IDeepCloneable Clone(Cloner cloner) { 108 return new GenealogyGraph<T>(this, cloner); 109 } 110 [StorableConstructor] 111 protected GenealogyGraph(bool deserializing) : base(deserializing) { } 112 public GenealogyGraph() { 113 Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(); 114 } 115 // methods 116 public override void AddVertex(IVertex vertex) { 117 base.AddVertex(vertex); 118 var node = (IGenealogyGraphNode)vertex; 119 if (!Ranks.ContainsKey(node.Rank)) { 120 Ranks[node.Rank] = new List<IGenealogyGraphNode>(); 121 } 122 Ranks[node.Rank].Add(node); 123 } 124 public override void RemoveVertex(IVertex vertex) { 125 var node = (IGenealogyGraphNode<T>)vertex; 126 if (Ranks.ContainsKey(node.Rank)) { 127 Ranks[node.Rank].Remove(node); 128 } 129 base.RemoveVertex(vertex); 130 } 131 public override IArc AddArc(IVertex source, IVertex target) { 132 var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target); 133 source.AddForwardArc(arc); 134 target.AddReverseArc(arc); 135 arcs.Add(arc); 136 return arc; 137 } 138 IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Vertices { 139 get { return Vertices; } 140 } 141 public event EventHandler GraphUpdated; 142 private void OnGraphUpdated(object sender, EventArgs args) { 143 var updated = GraphUpdated; 144 if (updated != null) updated(sender, args); 216 get { return base.Vertices.Cast<IGenealogyGraphNode<T>>(); } 145 217 } 146 218 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphArc.cs
r10903 r11233 33 33 : base(original, cloner) { } 34 34 35 protected GenealogyGraphArc() { } 36 37 public GenealogyGraphArc(IGenealogyGraphNode source, IGenealogyGraphNode target, object data = null) { 38 Source = source; 39 Target = target; 40 Data = data; 35 public GenealogyGraphArc(IGenealogyGraphNode source, IGenealogyGraphNode target) 36 : base(source, target) { 41 37 } 42 38 … … 47 43 public new IGenealogyGraphNode Source { 48 44 get { return (IGenealogyGraphNode)base.Source; } 49 set { base.Source = value; }50 45 } 51 46 52 47 public new IGenealogyGraphNode Target { 53 48 get { return (IGenealogyGraphNode)base.Target; } 54 set { base.Target = value; }55 49 } 56 50 } … … 63 57 protected GenealogyGraphArc(GenealogyGraphArc<T> original, Cloner cloner) 64 58 : base(original, cloner) { 65 Data = (T)original.Data;59 Data = original.Data; 66 60 } 67 61 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs
r10897 r11233 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 39 40 } 40 41 41 public GenealogyGraphNode() { } 42 public GenealogyGraphNode(object content) : base(content) { } 42 public GenealogyGraphNode(object content) 43 : base(content) { 44 Id = Guid.NewGuid().ToString(); 45 } 43 46 44 47 public new IEnumerable<IGenealogyGraphArc> InArcs { 45 48 get { return base.InArcs.Cast<IGenealogyGraphArc>(); } 46 set { base.InArcs = value; }47 49 } 48 50 public new IEnumerable<IGenealogyGraphArc> OutArcs { 49 51 get { return base.OutArcs.Cast<IGenealogyGraphArc>(); } 50 set { base.OutArcs = value; }51 52 } 52 53 public IEnumerable<IGenealogyGraphNode> Ancestors { … … 88 89 } 89 90 [Storable] 91 public string Id { get; private set; } 92 93 [Storable] 90 94 private double rank; 91 95 public double Rank { … … 124 128 } 125 129 126 public GenealogyGraphNode() { }127 130 public GenealogyGraphNode(object content) : base(content) { } 128 131 129 132 protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner) 130 133 : base(original, cloner) { 134 // we do not clone the content 131 135 Content = original.Content; 132 136 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraph.cs
r10903 r11233 27 27 Dictionary<double, List<IGenealogyGraphNode>> Ranks { get; } 28 28 new IEnumerable<IGenealogyGraphNode> Vertices { get; } 29 IGenealogyGraphNode GetByContent(object content); 30 IGenealogyGraphNode GetById(string id); 31 bool Contains(object content); 29 32 } 30 33 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphArc.cs
r10888 r11233 20 20 #endregion 21 21 22 using HeuristicLab.Core;23 24 22 namespace HeuristicLab.EvolutionTracking { 25 23 public interface IGenealogyGraphArc : IArc { 26 new IGenealogyGraphNode Source { get; set; } 27 new IGenealogyGraphNode Target { get; set; } 28 } 29 30 public interface IGenealogyGraphArc<T> : IGenealogyGraphArc where T : class, IItem { 31 new T Data { get; set; } 24 new IGenealogyGraphNode Source { get; } 25 new IGenealogyGraphNode Target { get; } 32 26 } 33 27 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs
r10890 r11233 28 28 double Quality { get; set; } 29 29 bool IsElite { get; set; } 30 string Id { get; } 30 31 31 32 IEnumerable<IGenealogyGraphNode> Parents { get; } … … 33 34 IEnumerable<IGenealogyGraphNode> Ancestors { get; } 34 35 IEnumerable<IGenealogyGraphNode> Descendants { get; } 35 new IEnumerable<IGenealogyGraphArc> InArcs { get; set;}36 new IEnumerable<IGenealogyGraphArc> OutArcs { get; set;}36 new IEnumerable<IGenealogyGraphArc> InArcs { get; } 37 new IEnumerable<IGenealogyGraphArc> OutArcs { get; } 37 38 } 38 39
Note: See TracChangeset
for help on using the changeset viewer.