source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Nodes/AlgorithmNode.cs @ 11500

Last change on this file since 11500 was 11500, checked in by swagner, 8 years ago

#2205: Worked on optimization networks

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