Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/DirectedGraph/DirectedGraph.cs @ 11232

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

#2215: Refactored and simplified DirectedGraph and related components API, simplified the BottomUpSimilarityCalculator by not using a directed graph and vertices but a simpler object so that the similarity calculator is self-contained.

File size: 5.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.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      OnVertexAdded(this, EventArgs.Empty);
97    }
98
99    public virtual void RemoveVertex(IVertex vertex) {
100      vertices.Remove(vertex);
101      // remove connections to/from the removed vertex
102      foreach (var arc in vertex.InArcs.Concat(vertex.OutArcs))
103        RemoveArc(arc);
104      // deregister event handlers
105      vertex.ArcAdded -= OnArcAdded;
106      vertex.ArcRemoved -= OnArcRemoved;
107      vertex.Changed -= OnVertexChanged;
108      OnVertexRemoved(this, EventArgs.Empty);
109    }
110
111    public IArc AddArc(IVertex source, IVertex target) {
112      var arc = new Arc(source, target);
113      AddArc(arc);
114      return arc;
115    }
116
117    public void AddArc(IArc arc) {
118      var source = (Vertex)arc.Source;
119      var target = (Vertex)arc.Target;
120      source.AddArc(arc);
121      target.AddArc(arc);
122      arcs.Add(arc);
123    }
124
125    public void RemoveArc(IArc arc) {
126      arcs.Remove(arc);
127      var source = (Vertex)arc.Source;
128      var target = (Vertex)arc.Target;
129      source.RemoveArc(arc);
130      target.RemoveArc(arc);
131    }
132
133    public event EventHandler VertexAdded;
134    protected virtual void OnVertexAdded(object sender, EventArgs args) {
135      var added = VertexAdded;
136      if (added != null)
137        added(sender, args);
138    }
139
140    public event EventHandler VertexChanged;
141    protected virtual void OnVertexChanged(object sender, EventArgs args) {
142      var changed = VertexChanged;
143      if (changed != null)
144        changed(sender, args);
145    }
146
147    public event EventHandler VertexRemoved;
148    protected virtual void OnVertexRemoved(object sender, EventArgs args) {
149      var removed = VertexRemoved;
150      if (removed != null)
151        removed(sender, args);
152    }
153
154    protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
155      var arc = args.Value;
156      // the ArcAdded event is fired by a vertex when an arc from or towards another vertex is added to his list of connections
157      // because the arc is added in both directions by both the source and the target, this event will get fired twice
158      // here, we only want to add the arc once, so if its already contained, we return without complaining
159      if (arcs.Contains(arc)) return;
160      arcs.Add(arc);
161      arc.Changed += OnArcChanged;
162    }
163
164    protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
165      var arc = args.Value;
166      if (!arcs.Contains(arc)) return; // the same rationale as above
167      arcs.Remove(arc);
168      arc.Changed -= OnArcChanged;
169    }
170
171    protected virtual void OnArcChanged(object sender, EventArgs args) { }
172
173    public override Image ItemImage {
174      get { return Common.Resources.VSImageLibrary.Graph; }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.