Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/DirectedGraph.cs @ 11234

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

#2215: Updated tests project. Fixed a couple of bugs when adding arcs to a vertex. Removed useless events from the directed graph. Worked around invalid operation exception when removing arcs.

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.Problems.DataAnalysis.Symbolic {
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      var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList();
102      foreach (var arc in arcList)
103        RemoveArc(arc);
104      // deregister event handlers
105      vertex.ArcAdded -= OnArcAdded;
106      vertex.ArcRemoved -= OnArcRemoved;
107      vertex.Changed -= OnVertexChanged;
108    }
109
110    public IArc AddArc(IVertex source, IVertex target) {
111      var arc = new Arc(source, target);
112      AddArc(arc);
113      return arc;
114    }
115
116    public void AddArc(IArc arc) {
117      var source = (Vertex)arc.Source;
118      var target = (Vertex)arc.Target;
119      source.AddArc(arc);
120      target.AddArc(arc);
121      arcs.Add(arc);
122    }
123
124    public void RemoveArc(IArc arc) {
125      arcs.Remove(arc);
126      var source = (Vertex)arc.Source;
127      var target = (Vertex)arc.Target;
128      source.RemoveArc(arc);
129      target.RemoveArc(arc);
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.