Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.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: 4.7 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.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.EvolutionTracking {
31  [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
32  [StorableClass]
33  public class DirectedGraph : Item, IDirectedGraph {
34    public bool AllowDuplicateContent { get; set; }
35
36    [Storable]
37    private readonly List<IVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove)
38    public IEnumerable<IVertex> Nodes {
39      get { return nodes; }
40    }
41
42    [Storable]
43    private readonly Dictionary<string, IVertex> idMap;
44
45    [Storable]
46    private readonly Dictionary<object, IVertex> contentMap;
47
48    [StorableConstructor]
49    protected DirectedGraph(bool serializing)
50      : base(serializing) {
51    }
52
53    public DirectedGraph() {
54      nodes = new List<IVertex>();
55      contentMap = new Dictionary<object, IVertex>();
56      idMap = new Dictionary<string, IVertex>();
57    }
58
59    protected DirectedGraph(DirectedGraph original, Cloner cloner)
60      : base(original, cloner) {
61      nodes = new List<IVertex>(original.Nodes);
62      contentMap = new Dictionary<object, IVertex>(original.contentMap);
63      idMap = new Dictionary<string, IVertex>(original.idMap);
64    }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new DirectedGraph(this, cloner);
68    }
69
70    public bool Contains(IVertex t) {
71      return nodes.Contains(t);
72    }
73
74    public bool Contains(object content) {
75      return contentMap.ContainsKey(content);
76    }
77
78    public IVertex GetVertex(string id) {
79      IVertex result;
80      idMap.TryGetValue(id, out result);
81      return result;
82    }
83
84    public IVertex GetVertex(object content) {
85      IVertex result;
86      contentMap.TryGetValue(content, out result);
87      return result;
88    }
89
90    public virtual bool Any(Func<IVertex, bool> predicate) {
91      return nodes.Any(predicate);
92    }
93
94    public virtual bool IsEmpty {
95      get { return !nodes.Any(); }
96    }
97
98    public virtual void Clear() {
99      nodes.Clear();
100      contentMap.Clear();
101      idMap.Clear();
102    }
103
104    public virtual void AddVertex(IVertex vertex) {
105      if (vertex.Content != null) {
106        if (!AllowDuplicateContent && contentMap.ContainsKey(vertex.Content)) {
107          throw new Exception("Content already exists in the graph.");
108        }
109        contentMap.Add(vertex.Content, vertex);
110      }
111      idMap.Add(vertex.Id, vertex);
112      nodes.Add(vertex);
113
114      vertex.PreContentChanged += Vertex_PreContentChanged;
115      vertex.PostContentChanged += Vertex_PostContentChanged;
116    }
117
118    public virtual void RemoveVertex(IVertex vertex) {
119      contentMap.Remove(vertex.Content);
120      idMap.Remove(vertex.Id);
121      nodes.Remove(vertex);
122
123      vertex.PreContentChanged -= Vertex_PreContentChanged;
124      vertex.PostContentChanged -= Vertex_PostContentChanged;
125    }
126
127    public virtual IArc AddArc(IVertex source, IVertex target) {
128      var arc = new Arc(source, target);
129      source.AddForwardArc(arc);
130      target.AddReverseArc(arc);
131      return arc;
132    }
133
134    private void Vertex_PreContentChanged(object sender, EventArgs args) {
135      var vertex = (IVertex)sender;
136      if (contentMap.ContainsKey(vertex.Content)) {
137        contentMap.Remove(vertex.Content);
138      }
139    }
140
141    private void Vertex_PostContentChanged(object sender, EventArgs args) {
142      var vertex = (IVertex)sender;
143      if (vertex.Content != null)
144        contentMap.Add(vertex.Content, vertex);
145    }
146
147    public override Image ItemImage {
148      get { return Common.Resources.VSImageLibrary.Graph; }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.