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 |
|
---|
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 |
|
---|
30 | namespace 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 | }
|
---|