#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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.EvolutionTracking {
[StorableClass]
public class Vertex : Item, IVertex {
[Storable]
private string id;
public string Id {
get { return id; }
set { id = value; }
}
[Storable]
private string label;
public string Label { get { return label; } set { label = value; } }
[Storable]
private double weight;
public double Weight { get { return weight; } set { weight = value; } }
[Storable]
private List inArcs;
public IEnumerable InArcs {
get { return inArcs ?? Enumerable.Empty(); }
set { inArcs = value.ToList(); }
}
[Storable]
private List outArcs;
public IEnumerable OutArcs {
get { return outArcs ?? Enumerable.Empty(); }
set { outArcs = value.ToList(); }
}
[Storable]
public object Content { get; set; }
[StorableConstructor]
public Vertex(bool deserializing) : base(deserializing) { }
public Vertex() {
id = Guid.NewGuid().ToString();
}
protected Vertex(Vertex original, Cloner cloner)
: base(original, cloner) {
Id = Guid.NewGuid().ToString();
Label = original.Label;
inArcs = new List(original.InArcs);
outArcs = new List(original.OutArcs);
}
public override IDeepCloneable Clone(Cloner cloner) {
return new Vertex(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
protected void AfterDeserialization() {
if (Id == null) {
Id = Guid.NewGuid().ToString();
}
}
// this method can be used to add arcs towards targets that are not visible in the graph
// (they do not appear in the nodes Dictionary). It is the programmers responsibility to
// enforce the correct and desired behavior.
public void AddForwardArc(IArc arc) {
if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
if (outArcs == null)
outArcs = new List();
outArcs.Add(arc);
}
public void AddReverseArc(IArc arc) {
if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
if (inArcs == null)
inArcs = new List();
inArcs.Add(arc);
}
public int InDegree { get { return InArcs.Count(); } }
public int OutDegree { get { return OutArcs.Count(); } }
public int Degree { get { return InDegree + OutDegree; } }
}
[StorableClass]
public class Vertex : Vertex, IVertex where T : class,IItem {
public new T Content {
get { return (T)base.Content; }
set { base.Content = value; }
}
public Vertex() { }
[StorableConstructor]
protected Vertex(bool deserializing) : base(deserializing) { }
protected Vertex(Vertex original, Cloner cloner)
: base(original, cloner) {
Content = original.Content; // not sure if to Clone()
}
public override IDeepCloneable Clone(Cloner cloner) {
return new Vertex(this, cloner);
}
}
}