Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs @ 11233

Last change on this file since 11233 was 11233, checked in by bburlacu, 10 years ago

#1772: Worked towards integrating the new graph api with the tracking operators.

File size: 5.1 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    protected HashSet<IVertex> vertices;
35    [Storable]
36    public IEnumerable<IVertex> Vertices {
37      get { return vertices; }
38      private set { vertices = new HashSet<IVertex>(value); }
39    }
40
41    protected HashSet<IArc> arcs;
42    [Storable]
43    public IEnumerable<IArc> Arcs {
44      get { return arcs; }
45      private set { arcs = new HashSet<IArc>(value); }
46    }
47
48    [StorableConstructor]
49    protected DirectedGraph(bool serializing)
50      : base(serializing) {
51    }
52
53    [StorableHook(HookType.AfterDeserialization)]
54    private void AfterDeserialization() {
55      foreach (var vertex in vertices) {
56        vertex.ArcAdded += OnArcAdded;
57        vertex.ArcRemoved += OnArcRemoved;
58        vertex.Changed += OnVertexChanged;
59      }
60
61      foreach (var arc in arcs) {
62        var source = arc.Source;
63        var target = arc.Target;
64        source.AddArc(arc);
65        target.AddArc(arc);
66        arc.Changed += OnArcChanged;
67      }
68    }
69
70    public DirectedGraph() {
71      vertices = new HashSet<IVertex>();
72      arcs = new HashSet<IArc>();
73    }
74
75    protected DirectedGraph(DirectedGraph original, Cloner cloner)
76      : base(original, cloner) {
77      vertices = new HashSet<IVertex>(original.vertices.Select(cloner.Clone));
78      arcs = new HashSet<IArc>(original.arcs.Select(cloner.Clone));
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new DirectedGraph(this, cloner);
83    }
84
85    public virtual void Clear() {
86      vertices.Clear();
87      arcs.Clear();
88    }
89
90    public virtual void AddVertex(IVertex vertex) {
91      vertices.Add(vertex);
92      // register event handlers
93      vertex.Changed += OnVertexChanged;
94      vertex.ArcAdded += OnArcAdded;
95      vertex.ArcRemoved += OnArcRemoved;
96    }
97
98    public virtual void RemoveVertex(IVertex vertex) {
99      vertices.Remove(vertex);
100      // remove connections to/from the removed vertex
101      foreach (var arc in vertex.InArcs.Concat(vertex.OutArcs))
102        RemoveArc(arc);
103      // deregister event handlers
104      vertex.ArcAdded -= OnArcAdded;
105      vertex.ArcRemoved -= OnArcRemoved;
106      vertex.Changed -= OnVertexChanged;
107    }
108
109    public virtual IArc AddArc(IVertex source, IVertex target) {
110      var arc = new Arc(source, target);
111      AddArc(arc);
112      return arc;
113    }
114
115    public void AddArc(IArc arc) {
116      var source = (Vertex)arc.Source;
117      var target = (Vertex)arc.Target;
118      source.AddArc(arc);
119      target.AddArc(arc);
120      arcs.Add(arc);
121    }
122
123    public void RemoveArc(IArc arc) {
124      arcs.Remove(arc);
125      var source = (Vertex)arc.Source;
126      var target = (Vertex)arc.Target;
127      source.RemoveArc(arc);
128      target.RemoveArc(arc);
129    }
130
131
132    protected virtual void OnVertexChanged(object sender, EventArgs args) { }
133
134    protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
135      var arc = args.Value;
136      // the ArcAdded event is fired by a vertex when an arc from or towards another vertex is added to his list of connections
137      // because the arc is added in both directions by both the source and the target, this event will get fired twice
138      // here, we only want to add the arc once, so if its already contained, we return without complaining
139      if (arcs.Contains(arc)) return;
140      arcs.Add(arc);
141      arc.Changed += OnArcChanged;
142    }
143
144    protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
145      var arc = args.Value;
146      if (!arcs.Contains(arc)) return; // the same rationale as above
147      arcs.Remove(arc);
148      arc.Changed -= OnArcChanged;
149    }
150
151    protected virtual void OnArcChanged(object sender, EventArgs args) { }
152
153    public override Image ItemImage {
154      get { return Common.Resources.VSImageLibrary.Graph; }
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.