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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
30 | [StorableClass]
|
---|
31 | public class Vertex : Item, IVertex {
|
---|
32 | public event EventHandler PreContentChanged;
|
---|
33 | protected virtual void OnPreContentChanged(object sender, EventArgs args) {
|
---|
34 | var changed = PreContentChanged;
|
---|
35 | if (changed != null) {
|
---|
36 | changed(sender, args);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public event EventHandler PostContentChanged;
|
---|
41 | protected virtual void OnPostContentChanged(object sender, EventArgs args) {
|
---|
42 | var changed = PostContentChanged;
|
---|
43 | if (changed != null) {
|
---|
44 | changed(sender, args);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | [Storable]
|
---|
49 | private string id;
|
---|
50 |
|
---|
51 | public string Id {
|
---|
52 | get { return id; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | [Storable]
|
---|
56 | public string Label { get; set; }
|
---|
57 |
|
---|
58 | [Storable]
|
---|
59 | public double Weight { get; set; }
|
---|
60 |
|
---|
61 | [Storable]
|
---|
62 | protected object content;
|
---|
63 | public object Content {
|
---|
64 | get { return content; }
|
---|
65 | set {
|
---|
66 | OnPreContentChanged(this, EventArgs.Empty);
|
---|
67 | content = value;
|
---|
68 | OnPostContentChanged(this, EventArgs.Empty);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | private List<IArc> inArcs;
|
---|
73 | public IEnumerable<IArc> InArcs {
|
---|
74 | get { return inArcs ?? Enumerable.Empty<IArc>(); }
|
---|
75 | set { inArcs = value.ToList(); }
|
---|
76 | }
|
---|
77 |
|
---|
78 | private List<IArc> outArcs;
|
---|
79 | public IEnumerable<IArc> OutArcs {
|
---|
80 | get { return outArcs ?? Enumerable.Empty<IArc>(); }
|
---|
81 | set { outArcs = value.ToList(); }
|
---|
82 | }
|
---|
83 |
|
---|
84 | [StorableConstructor]
|
---|
85 | public Vertex(bool deserializing) : base(deserializing) { }
|
---|
86 |
|
---|
87 | public Vertex() {
|
---|
88 | id = Guid.NewGuid().ToString();
|
---|
89 | }
|
---|
90 |
|
---|
91 | public Vertex(object content)
|
---|
92 | : this() {
|
---|
93 | this.content = content;
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected Vertex(Vertex original, Cloner cloner)
|
---|
97 | : base(original, cloner) {
|
---|
98 | id = Guid.NewGuid().ToString();
|
---|
99 | content = original.content;
|
---|
100 | Label = original.Label;
|
---|
101 | Weight = original.Weight;
|
---|
102 | inArcs = new List<IArc>(original.inArcs);
|
---|
103 | outArcs = new List<IArc>(original.outArcs);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
107 | return new Vertex(this, cloner);
|
---|
108 | }
|
---|
109 |
|
---|
110 | [StorableHook(HookType.AfterDeserialization)]
|
---|
111 | private void AfterDeserialization() {
|
---|
112 | if (Id == null) {
|
---|
113 | id = Guid.NewGuid().ToString();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | // this method can be used to add arcs towards targets that are not visible in the graph
|
---|
118 | // (they do not appear in the nodes Dictionary). It is the programmers responsibility to
|
---|
119 | // enforce the correct and desired behavior.
|
---|
120 | public void AddForwardArc(IArc arc) {
|
---|
121 | if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
|
---|
122 | if (outArcs == null)
|
---|
123 | outArcs = new List<IArc>();
|
---|
124 | outArcs.Add(arc);
|
---|
125 | }
|
---|
126 | public void AddReverseArc(IArc arc) {
|
---|
127 | if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
|
---|
128 | if (inArcs == null)
|
---|
129 | inArcs = new List<IArc>();
|
---|
130 | inArcs.Add(arc);
|
---|
131 | }
|
---|
132 | public int InDegree { get { return InArcs.Count(); } }
|
---|
133 | public int OutDegree { get { return OutArcs.Count(); } }
|
---|
134 | public int Degree { get { return InDegree + OutDegree; } }
|
---|
135 | }
|
---|
136 |
|
---|
137 | [StorableClass]
|
---|
138 | public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
|
---|
139 | public new T Content {
|
---|
140 | get { return (T)base.Content; }
|
---|
141 | set { base.Content = value; }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public Vertex() { }
|
---|
145 |
|
---|
146 | [StorableConstructor]
|
---|
147 | protected Vertex(bool deserializing) : base(deserializing) { }
|
---|
148 |
|
---|
149 | protected Vertex(Vertex<T> original, Cloner cloner)
|
---|
150 | : base(original, cloner) {
|
---|
151 | }
|
---|
152 |
|
---|
153 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
154 | return new Vertex<T>(this, cloner);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|