#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.EvolutionTracking { [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")] [StorableClass] public class DirectedGraph : Item, IDirectedGraph { public bool AllowDuplicateContent { get; set; } [Storable] protected readonly List vertices; // for performance reasons, maybe this should be a linked list (fast remove) public IEnumerable Vertices { get { return vertices; } } [Storable] private readonly Dictionary idMap; [Storable] private readonly Dictionary contentMap; [Storable] protected readonly List arcs; public IEnumerable Arcs { get { return arcs; } } [StorableConstructor] protected DirectedGraph(bool serializing) : base(serializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { foreach (var arc in arcs) { arc.Source.AddForwardArc(arc); arc.Target.AddReverseArc(arc); } foreach (var vertex in vertices) { vertex.PreContentChanged += Vertex_PreContentChanged; vertex.PostContentChanged += Vertex_PostContentChanged; } } public DirectedGraph() { vertices = new List(); arcs = new List(); contentMap = new Dictionary(); idMap = new Dictionary(); } protected DirectedGraph(DirectedGraph original, Cloner cloner) : base(original, cloner) { vertices = new List(original.vertices); arcs = new List(original.arcs); contentMap = new Dictionary(original.contentMap); idMap = new Dictionary(original.idMap); } public override IDeepCloneable Clone(Cloner cloner) { return new DirectedGraph(this, cloner); } public bool Contains(IVertex t) { return vertices.Contains(t); } public bool Contains(object content) { return contentMap.ContainsKey(content); } public IVertex GetVertex(string id) { IVertex result; idMap.TryGetValue(id, out result); return result; } public IVertex GetVertex(object content) { IVertex result; contentMap.TryGetValue(content, out result); return result; } public virtual bool Any(Func predicate) { return vertices.Any(predicate); } public virtual bool IsEmpty { get { return !vertices.Any(); } } public virtual void Clear() { vertices.Clear(); contentMap.Clear(); idMap.Clear(); } public virtual void AddVertex(IVertex vertex) { if (vertex.Content != null) { if (!AllowDuplicateContent && contentMap.ContainsKey(vertex.Content)) { throw new Exception("Content already exists in the graph."); } contentMap[vertex.Content] = vertex; } idMap.Add(vertex.Id, vertex); vertices.Add(vertex); vertex.PreContentChanged += Vertex_PreContentChanged; vertex.PostContentChanged += Vertex_PostContentChanged; } public virtual void RemoveVertex(IVertex vertex) { contentMap.Remove(vertex.Content); idMap.Remove(vertex.Id); vertices.Remove(vertex); // remove connections to/from the removed vertex foreach (var arc in vertex.InArcs) { arc.Source.OutArcs = arc.Source.OutArcs.Where(x => x.Target != vertex); } foreach (var arc in vertex.OutArcs) { arc.Target.InArcs = arc.Target.InArcs.Where(x => x.Source != vertex); } // de-register event handlers vertex.PreContentChanged -= Vertex_PreContentChanged; vertex.PostContentChanged -= Vertex_PostContentChanged; } public virtual IArc AddArc(IVertex source, IVertex target) { var arc = new Arc(source, target); source.AddForwardArc(arc); target.AddReverseArc(arc); arcs.Add(arc); return arc; } private void Vertex_PreContentChanged(object sender, EventArgs args) { var vertex = (IVertex)sender; if (contentMap.ContainsKey(vertex.Content)) { contentMap.Remove(vertex.Content); } } private void Vertex_PostContentChanged(object sender, EventArgs args) { var vertex = (IVertex)sender; if (vertex.Content != null) contentMap.Add(vertex.Content, vertex); } public override Image ItemImage { get { return Common.Resources.VSImageLibrary.Graph; } } } }