[10267] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[10278] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10267] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[11211] | 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[10278] | 31 | [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
|
---|
[10267] | 32 | [StorableClass]
|
---|
[10300] | 33 | public class DirectedGraph : Item, IDirectedGraph {
|
---|
[11229] | 34 | protected HashSet<IVertex> vertices;
|
---|
[10267] | 35 | [Storable]
|
---|
[10903] | 36 | public IEnumerable<IVertex> Vertices {
|
---|
| 37 | get { return vertices; }
|
---|
[11229] | 38 | private set { vertices = new HashSet<IVertex>(value); }
|
---|
[10267] | 39 | }
|
---|
[10884] | 40 |
|
---|
[11229] | 41 | protected HashSet<IArc> arcs;
|
---|
[10300] | 42 | [Storable]
|
---|
[10903] | 43 | public IEnumerable<IArc> Arcs {
|
---|
| 44 | get { return arcs; }
|
---|
[11229] | 45 | private set { arcs = new HashSet<IArc>(value); }
|
---|
[10903] | 46 | }
|
---|
| 47 |
|
---|
[10897] | 48 | [StorableConstructor]
|
---|
| 49 | protected DirectedGraph(bool serializing)
|
---|
| 50 | : base(serializing) {
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[10903] | 53 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 54 | private void AfterDeserialization() {
|
---|
[11229] | 55 | foreach (var vertex in vertices) {
|
---|
| 56 | vertex.ArcAdded += OnArcAdded;
|
---|
| 57 | vertex.ArcRemoved += OnArcRemoved;
|
---|
[10903] | 58 | }
|
---|
| 59 |
|
---|
[11229] | 60 | foreach (var arc in arcs) {
|
---|
| 61 | var source = arc.Source;
|
---|
| 62 | var target = arc.Target;
|
---|
| 63 | source.AddArc(arc);
|
---|
| 64 | target.AddArc(arc);
|
---|
[10903] | 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[10267] | 68 | public DirectedGraph() {
|
---|
[11229] | 69 | vertices = new HashSet<IVertex>();
|
---|
| 70 | arcs = new HashSet<IArc>();
|
---|
[10267] | 71 | }
|
---|
[10897] | 72 |
|
---|
[10300] | 73 | protected DirectedGraph(DirectedGraph original, Cloner cloner)
|
---|
[10267] | 74 | : base(original, cloner) {
|
---|
[11229] | 75 | vertices = new HashSet<IVertex>(original.vertices.Select(cloner.Clone));
|
---|
| 76 | arcs = new HashSet<IArc>(original.arcs.Select(cloner.Clone));
|
---|
[10267] | 77 | }
|
---|
[10897] | 78 |
|
---|
[10267] | 79 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10300] | 80 | return new DirectedGraph(this, cloner);
|
---|
[10267] | 81 | }
|
---|
[10897] | 82 |
|
---|
[11229] | 83 | public virtual void Clear() {
|
---|
| 84 | vertices.Clear();
|
---|
| 85 | arcs.Clear();
|
---|
[10267] | 86 | }
|
---|
| 87 |
|
---|
[11229] | 88 | public virtual void AddVertex(IVertex vertex) {
|
---|
| 89 | vertices.Add(vertex);
|
---|
| 90 | // register event handlers
|
---|
| 91 | vertex.ArcAdded += OnArcAdded;
|
---|
| 92 | vertex.ArcRemoved += OnArcRemoved;
|
---|
[10300] | 93 | }
|
---|
[10884] | 94 |
|
---|
[11229] | 95 | public virtual void RemoveVertex(IVertex vertex) {
|
---|
| 96 | vertices.Remove(vertex);
|
---|
| 97 | // remove connections to/from the removed vertex
|
---|
[11238] | 98 | var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList(); // avoid invalid operation exception: "collection was modified" below
|
---|
[11234] | 99 | foreach (var arc in arcList)
|
---|
[11229] | 100 | RemoveArc(arc);
|
---|
| 101 | // deregister event handlers
|
---|
| 102 | vertex.ArcAdded -= OnArcAdded;
|
---|
| 103 | vertex.ArcRemoved -= OnArcRemoved;
|
---|
[10884] | 104 | }
|
---|
| 105 |
|
---|
[11229] | 106 | public IArc AddArc(IVertex source, IVertex target) {
|
---|
| 107 | var arc = new Arc(source, target);
|
---|
| 108 | AddArc(arc);
|
---|
| 109 | return arc;
|
---|
[10300] | 110 | }
|
---|
[10897] | 111 |
|
---|
[11229] | 112 | public void AddArc(IArc arc) {
|
---|
| 113 | var source = (Vertex)arc.Source;
|
---|
| 114 | var target = (Vertex)arc.Target;
|
---|
| 115 | source.AddArc(arc);
|
---|
| 116 | target.AddArc(arc);
|
---|
| 117 | arcs.Add(arc);
|
---|
[10267] | 118 | }
|
---|
[10897] | 119 |
|
---|
[11229] | 120 | public void RemoveArc(IArc arc) {
|
---|
| 121 | arcs.Remove(arc);
|
---|
| 122 | var source = (Vertex)arc.Source;
|
---|
| 123 | var target = (Vertex)arc.Target;
|
---|
| 124 | source.RemoveArc(arc);
|
---|
| 125 | target.RemoveArc(arc);
|
---|
[10267] | 126 | }
|
---|
[10897] | 127 |
|
---|
[11229] | 128 | protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
|
---|
| 129 | var arc = args.Value;
|
---|
| 130 | // the ArcAdded event is fired by a vertex when an arc from or towards another vertex is added to his list of connections
|
---|
| 131 | // because the arc is added in both directions by both the source and the target, this event will get fired twice
|
---|
| 132 | // here, we only want to add the arc once, so if its already contained, we return without complaining
|
---|
| 133 | if (arcs.Contains(arc)) return;
|
---|
[10903] | 134 | arcs.Add(arc);
|
---|
[10888] | 135 | }
|
---|
| 136 |
|
---|
[11229] | 137 | protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
|
---|
| 138 | var arc = args.Value;
|
---|
| 139 | if (!arcs.Contains(arc)) return; // the same rationale as above
|
---|
| 140 | arcs.Remove(arc);
|
---|
[10267] | 141 | }
|
---|
[10888] | 142 |
|
---|
[10267] | 143 | public override Image ItemImage {
|
---|
| 144 | get { return Common.Resources.VSImageLibrary.Graph; }
|
---|
| 145 | }
|
---|
[11238] | 146 |
|
---|
| 147 | // events
|
---|
| 148 | public event EventHandler VertexAdded;
|
---|
| 149 | public event EventHandler VertexRemoved;
|
---|
| 150 | public event EventHandler ArcAdded;
|
---|
| 151 | public event EventHandler ArcRemoved;
|
---|
[10267] | 152 | }
|
---|
| 153 | }
|
---|