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

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

#1772: Introduced separate class for FragmentNodes and adjusted tracing code. Fixed small bug creating the genealogy graph.

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