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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using System;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Optimization.Networks {
|
---|
28 | [Item("AlgorithmNode", "A node of an optimization network which contains a HeuristicLab algorithm.")]
|
---|
29 | [StorableClass]
|
---|
30 | public class AlgorithmNode : Node, IAlgorithmNode {
|
---|
31 | new public PortCollection Ports {
|
---|
32 | get { return base.Ports; }
|
---|
33 | }
|
---|
34 |
|
---|
35 | [Storable]
|
---|
36 | private IAlgorithm algorithm;
|
---|
37 | public IAlgorithm Algorithm {
|
---|
38 | get { return algorithm; }
|
---|
39 | set {
|
---|
40 | if (value != algorithm) {
|
---|
41 | DeregisterAlgorithmEvents();
|
---|
42 | algorithm = value;
|
---|
43 | RegisterAlgorithmEvents();
|
---|
44 | OnAlgorithmChanged();
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected AlgorithmNode(bool deserializing) : base(deserializing) { }
|
---|
51 | protected AlgorithmNode(AlgorithmNode original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | algorithm = cloner.Clone(original.algorithm);
|
---|
54 | RegisterPortsEvents();
|
---|
55 | RegisterAlgorithmEvents();
|
---|
56 | }
|
---|
57 | public AlgorithmNode()
|
---|
58 | : base("AlgorithmNode") {
|
---|
59 | RegisterPortsEvents();
|
---|
60 | }
|
---|
61 | public AlgorithmNode(string name)
|
---|
62 | : base(name) {
|
---|
63 | RegisterPortsEvents();
|
---|
64 | }
|
---|
65 | public AlgorithmNode(string name, string description)
|
---|
66 | : base(name, description) {
|
---|
67 | RegisterPortsEvents();
|
---|
68 | }
|
---|
69 |
|
---|
70 | [StorableHook(HookType.AfterDeserialization)]
|
---|
71 | private void AfterDeserialization() {
|
---|
72 | RegisterPortsEvents();
|
---|
73 | RegisterAlgorithmEvents();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | return new AlgorithmNode(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void UpdateParameter(IInputPort port) {
|
---|
81 | if (algorithm != null) {
|
---|
82 | IParameter param = null;
|
---|
83 | if (algorithm.Parameters.TryGetValue(port.Name, out param)) {
|
---|
84 | IValueParameter p = param as IValueParameter;
|
---|
85 | if ((p != null) && (port.Value != null)) {
|
---|
86 | p.Value = (IItem)port.Value;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | private void UpdateOutputPort(IResult result) {
|
---|
92 | IPort port = null;
|
---|
93 | if (Ports.TryGetValue(result.Name, out port)) {
|
---|
94 | IOutputPort p = port as IOutputPort;
|
---|
95 | if (p != null) {
|
---|
96 | p.Value = result.Value;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 | private void UpdateOutputPort(IOutputPort port) {
|
---|
101 | if ((algorithm != null) && (algorithm.Results != null)) {
|
---|
102 | IResult result = null;
|
---|
103 | if (algorithm.Results.TryGetValue(port.Name, out result)) {
|
---|
104 | port.Value = result != null ? result.Value : null;
|
---|
105 | } else {
|
---|
106 | port.Value = null;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public event EventHandler AlgorithmChanged;
|
---|
112 | protected virtual void OnAlgorithmChanged() {
|
---|
113 | var handler = AlgorithmChanged;
|
---|
114 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
115 | }
|
---|
116 |
|
---|
117 | #region Ports Events
|
---|
118 | protected override void RegisterPortsEvents() {
|
---|
119 | base.RegisterPortsEvents();
|
---|
120 | foreach (var p in Ports)
|
---|
121 | RegisterPortEvents(p);
|
---|
122 | }
|
---|
123 | protected override void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
124 | base.Ports_ItemsAdded(sender, e);
|
---|
125 | foreach (var p in e.Items)
|
---|
126 | RegisterPortEvents(p);
|
---|
127 | }
|
---|
128 | protected override void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
129 | base.Ports_ItemsRemoved(sender, e);
|
---|
130 | foreach (var p in e.Items)
|
---|
131 | DeregisterPortEvents(p);
|
---|
132 | }
|
---|
133 | protected override void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
134 | base.Ports_ItemsReplaced(sender, e);
|
---|
135 | foreach (var p in e.OldItems)
|
---|
136 | DeregisterPortEvents(p);
|
---|
137 | foreach (var p in e.Items)
|
---|
138 | RegisterPortEvents(p);
|
---|
139 | }
|
---|
140 | protected override void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
141 | base.Ports_CollectionReset(sender, e);
|
---|
142 | foreach (var p in e.OldItems)
|
---|
143 | DeregisterPortEvents(p);
|
---|
144 | foreach (var p in e.Items)
|
---|
145 | RegisterPortEvents(p);
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region Port Events
|
---|
150 | private void RegisterPortEvents(IPort port) {
|
---|
151 | IInputPort i = port as IInputPort;
|
---|
152 | if (i != null) {
|
---|
153 | i.NameChanged += InputPort_NameChanged;
|
---|
154 | i.ValueChanged += InputPort_ValueChanged;
|
---|
155 | UpdateParameter(i);
|
---|
156 | }
|
---|
157 | IOutputPort o = port as IOutputPort;
|
---|
158 | if (o != null) {
|
---|
159 | o.NameChanged += OutputPort_NameChanged;
|
---|
160 | UpdateOutputPort(o);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | private void DeregisterPortEvents(IPort port) {
|
---|
164 | IInputPort i = port as IInputPort;
|
---|
165 | if (i != null) {
|
---|
166 | i.NameChanged -= InputPort_NameChanged;
|
---|
167 | i.ValueChanged -= InputPort_ValueChanged;
|
---|
168 | }
|
---|
169 | IOutputPort o = port as IOutputPort;
|
---|
170 | if (o != null) {
|
---|
171 | o.NameChanged -= OutputPort_NameChanged;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | private void InputPort_NameChanged(object sender, EventArgs e) {
|
---|
175 | UpdateParameter((IInputPort)sender);
|
---|
176 | }
|
---|
177 | private void InputPort_ValueChanged(object sender, EventArgs e) {
|
---|
178 | UpdateParameter((IInputPort)sender);
|
---|
179 | }
|
---|
180 | private void OutputPort_NameChanged(object sender, EventArgs e) {
|
---|
181 | UpdateOutputPort((IOutputPort)sender);
|
---|
182 | }
|
---|
183 | #endregion
|
---|
184 |
|
---|
185 | #region Algorithm Events
|
---|
186 | private void RegisterAlgorithmEvents() {
|
---|
187 | if (algorithm != null) {
|
---|
188 | algorithm.Prepared += Algorithm_Prepared;
|
---|
189 | RegisterResultsEvents();
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | private void DeregisterAlgorithmEvents() {
|
---|
194 | if (algorithm != null) {
|
---|
195 | algorithm.Prepared -= Algorithm_Prepared;
|
---|
196 | DeregisterResultsEvents();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | private void Algorithm_Prepared(object sender, EventArgs e) {
|
---|
200 | RegisterResultsEvents();
|
---|
201 | }
|
---|
202 | #endregion
|
---|
203 |
|
---|
204 | #region Results Events
|
---|
205 | private void RegisterResultsEvents() {
|
---|
206 | if ((algorithm != null) && (algorithm.Results != null)) {
|
---|
207 | algorithm.Results.ItemsAdded += Results_ItemsAdded;
|
---|
208 | algorithm.Results.ItemsRemoved += Results_ItemsRemoved;
|
---|
209 | algorithm.Results.ItemsReplaced += Results_ItemsReplaced;
|
---|
210 | algorithm.Results.CollectionReset += Results_CollectionReset;
|
---|
211 | foreach (var r in algorithm.Results)
|
---|
212 | RegisterResultEvents(r);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | private void DeregisterResultsEvents() {
|
---|
216 | if ((algorithm != null) && (algorithm.Results != null)) {
|
---|
217 | algorithm.Results.ItemsAdded -= Results_ItemsAdded;
|
---|
218 | algorithm.Results.ItemsRemoved -= Results_ItemsRemoved;
|
---|
219 | algorithm.Results.ItemsReplaced -= Results_ItemsReplaced;
|
---|
220 | algorithm.Results.CollectionReset -= Results_CollectionReset;
|
---|
221 | foreach (var r in algorithm.Results)
|
---|
222 | DeregisterResultEvents(r);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | private void Results_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
226 | foreach (var r in e.Items)
|
---|
227 | RegisterResultEvents(r);
|
---|
228 | }
|
---|
229 | private void Results_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
230 | foreach (var r in e.Items)
|
---|
231 | DeregisterResultEvents(r);
|
---|
232 | }
|
---|
233 | private void Results_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
234 | foreach (var r in e.OldItems)
|
---|
235 | DeregisterResultEvents(r);
|
---|
236 | foreach (var r in e.Items)
|
---|
237 | RegisterResultEvents(r);
|
---|
238 | }
|
---|
239 | private void Results_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
240 | foreach (var r in e.OldItems)
|
---|
241 | DeregisterResultEvents(r);
|
---|
242 | foreach (var r in e.Items)
|
---|
243 | RegisterResultEvents(r);
|
---|
244 | }
|
---|
245 | #endregion
|
---|
246 |
|
---|
247 | #region Result Events
|
---|
248 | private void RegisterResultEvents(IResult result) {
|
---|
249 | if (result != null) {
|
---|
250 | result.ValueChanged += Result_ValueChanged;
|
---|
251 | result.ToStringChanged += Result_ToStringChanged;
|
---|
252 | UpdateOutputPort(result);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | private void DeregisterResultEvents(IResult result) {
|
---|
256 | if (result != null) {
|
---|
257 | result.ValueChanged -= Result_ValueChanged;
|
---|
258 | result.ToStringChanged += Result_ToStringChanged;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | private void Result_ValueChanged(object sender, EventArgs e) {
|
---|
262 | UpdateOutputPort((IResult)sender);
|
---|
263 | }
|
---|
264 | private void Result_ToStringChanged(object sender, EventArgs e) {
|
---|
265 | UpdateOutputPort((IResult)sender);
|
---|
266 | }
|
---|
267 | #endregion
|
---|
268 | }
|
---|
269 | }
|
---|