source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Vertex.cs @ 10886

Last change on this file since 10886 was 10886, checked in by bburlacu, 8 years ago

#1772: Simplified GenealogyGraph (and related components) API in an effort to improve speed and memory consumption (eg., by sharing the same arc when walking the graph in both directions: parent-child and child-parent).

File size: 4.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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.EvolutionTracking {
30  [StorableClass]
31  public class Vertex : Item, IVertex {
32    [Storable]
33    private string id;
34    public string Id {
35      get { return id; }
36      set { id = value; }
37    }
38    [Storable]
39    private string label;
40    public string Label { get { return label; } set { label = value; } }
41    [Storable]
42    private double weight;
43    public double Weight { get { return weight; } set { weight = value; } }
44    [Storable]
45    private List<IArc> inArcs;
46    public IEnumerable<IArc> InArcs {
47      get { return inArcs ?? Enumerable.Empty<IArc>(); }
48      set { inArcs = value.ToList(); }
49    }
50    [Storable]
51    private List<IArc> outArcs;
52    public IEnumerable<IArc> OutArcs {
53      get { return outArcs ?? Enumerable.Empty<IArc>(); }
54      set { outArcs = value.ToList(); }
55    }
56
57    [Storable]
58    public object Content { get; set; }
59
60    [StorableConstructor]
61    public Vertex(bool deserializing) : base(deserializing) { }
62
63    public Vertex() {
64      id = Guid.NewGuid().ToString();
65    }
66
67    protected Vertex(Vertex original, Cloner cloner)
68      : base(original, cloner) {
69      Id = Guid.NewGuid().ToString();
70      Label = original.Label;
71      inArcs = new List<IArc>(original.InArcs);
72      outArcs = new List<IArc>(original.OutArcs);
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new Vertex(this, cloner);
77    }
78
79    [StorableHook(HookType.AfterDeserialization)]
80    protected void AfterDeserialization() {
81      if (Id == null) {
82        Id = Guid.NewGuid().ToString();
83      }
84    }
85
86    // this method can be used to add arcs towards targets that are not visible in the graph
87    // (they do not appear in the nodes Dictionary). It is the programmers responsibility to
88    // enforce the correct and desired behavior.
89    public void AddForwardArc(IArc arc) {
90      if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
91      if (outArcs == null)
92        outArcs = new List<IArc>();
93      outArcs.Add(arc);
94    }
95    public void AddReverseArc(IArc arc) {
96      if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
97      if (inArcs == null)
98        inArcs = new List<IArc>();
99      inArcs.Add(arc);
100    }
101    public int InDegree { get { return InArcs.Count(); } }
102    public int OutDegree { get { return OutArcs.Count(); } }
103    public int Degree { get { return InDegree + OutDegree; } }
104  }
105
106  [StorableClass]
107  public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
108    public new T Content {
109      get { return (T)base.Content; }
110      set { base.Content = value; }
111    }
112
113    public Vertex() { }
114
115    [StorableConstructor]
116    protected Vertex(bool deserializing) : base(deserializing) { }
117
118    protected Vertex(Vertex<T> original, Cloner cloner)
119      : base(original, cloner) {
120      Content = original.Content; // not sure if to Clone()
121    }
122
123    public override IDeepCloneable Clone(Cloner cloner) {
124      return new Vertex<T>(this, cloner);
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.