Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10833 was 10677, checked in by bburlacu, 10 years ago

#1772: Improved usage of GenealogyGraph and GenealogyGraphNode classes. Made some progress on building block tracing.

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