[11406] | 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;
|
---|
[11409] | 25 | using System;
|
---|
[11406] | 26 |
|
---|
| 27 | namespace HeuristicLab.Optimization.Networks {
|
---|
| 28 | [Item("AlgorithmNode", "A node of an optimization network which contains a HeuristicLab algorithm.")]
|
---|
| 29 | [StorableClass]
|
---|
[11409] | 30 | public class AlgorithmNode : Node, IAlgorithmNode {
|
---|
| 31 | new public PortCollection Ports {
|
---|
| 32 | get { return base.Ports; }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[11406] | 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()
|
---|
[11409] | 58 | : base("AlgorithmNode") {
|
---|
[11406] | 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();
|
---|
[11409] | 73 | RegisterAlgorithmEvents();
|
---|
[11406] | 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) {
|
---|
[11409] | 82 | IParameter param = null;
|
---|
| 83 | if (algorithm.Parameters.TryGetValue(port.Name, out param)) {
|
---|
[11406] | 84 | IValueParameter p = param as IValueParameter;
|
---|
[11409] | 85 | if (p != null) {
|
---|
[11406] | 86 | p.Value = (IItem)port.Value;
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | private void UpdateOutputPort(IResult result) {
|
---|
[11409] | 92 | IPort port = null;
|
---|
| 93 | if (Ports.TryGetValue(result.Name, out port)) {
|
---|
[11406] | 94 | IOutputPort p = port as IOutputPort;
|
---|
[11409] | 95 | if (p != null) {
|
---|
[11406] | 96 | p.Value = result.Value;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[11409] | 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 | }
|
---|
[11406] | 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 | private void RegisterPortsEvents() {
|
---|
| 119 | Ports.ItemsAdded += Ports_ItemsAdded;
|
---|
| 120 | Ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
| 121 | Ports.ItemsReplaced += Ports_ItemsReplaced;
|
---|
| 122 | Ports.CollectionReset += Ports_CollectionReset;
|
---|
[11409] | 123 | foreach (var p in Ports)
|
---|
| 124 | RegisterPortEvents(p);
|
---|
[11406] | 125 | }
|
---|
| 126 | void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 127 | foreach (var p in e.Items)
|
---|
| 128 | RegisterPortEvents(p);
|
---|
| 129 | }
|
---|
| 130 | void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 131 | foreach (var p in e.Items)
|
---|
| 132 | DeregisterPortEvents(p);
|
---|
| 133 | }
|
---|
| 134 | void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 135 | foreach (var p in e.OldItems)
|
---|
| 136 | DeregisterPortEvents(p);
|
---|
| 137 | foreach (var p in e.Items)
|
---|
| 138 | RegisterPortEvents(p);
|
---|
| 139 | }
|
---|
| 140 | void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 141 | foreach (var p in e.OldItems)
|
---|
| 142 | DeregisterPortEvents(p);
|
---|
| 143 | foreach (var p in e.Items)
|
---|
| 144 | RegisterPortEvents(p);
|
---|
| 145 | }
|
---|
| 146 | #endregion
|
---|
| 147 |
|
---|
| 148 | #region Port Events
|
---|
| 149 | private void RegisterPortEvents(IPort port) {
|
---|
[11409] | 150 | IInputPort i = port as IInputPort;
|
---|
| 151 | if (i != null) {
|
---|
| 152 | i.NameChanged += InputPort_NameChanged;
|
---|
| 153 | i.ValueChanged += InputPort_ValueChanged;
|
---|
| 154 | UpdateParameter(i);
|
---|
[11406] | 155 | }
|
---|
[11409] | 156 | IOutputPort o = port as IOutputPort;
|
---|
| 157 | if (o != null) {
|
---|
| 158 | o.NameChanged += OutputPort_NameChanged;
|
---|
| 159 | UpdateOutputPort(o);
|
---|
| 160 | }
|
---|
[11406] | 161 | }
|
---|
| 162 | private void DeregisterPortEvents(IPort port) {
|
---|
[11409] | 163 | IInputPort i = port as IInputPort;
|
---|
| 164 | if (i != null) {
|
---|
| 165 | i.NameChanged -= InputPort_NameChanged;
|
---|
| 166 | i.ValueChanged -= InputPort_ValueChanged;
|
---|
[11406] | 167 | }
|
---|
[11409] | 168 | IOutputPort o = port as IOutputPort;
|
---|
| 169 | if (o != null) {
|
---|
| 170 | o.NameChanged -= OutputPort_NameChanged;
|
---|
| 171 | }
|
---|
[11406] | 172 | }
|
---|
[11409] | 173 | private void InputPort_NameChanged(object sender, EventArgs e) {
|
---|
| 174 | UpdateParameter((IInputPort)sender);
|
---|
| 175 | }
|
---|
[11406] | 176 | private void InputPort_ValueChanged(object sender, EventArgs e) {
|
---|
| 177 | UpdateParameter((IInputPort)sender);
|
---|
| 178 | }
|
---|
[11409] | 179 | private void OutputPort_NameChanged(object sender, EventArgs e) {
|
---|
| 180 | UpdateOutputPort((IOutputPort)sender);
|
---|
| 181 | }
|
---|
[11406] | 182 | #endregion
|
---|
| 183 |
|
---|
| 184 | #region Algorithm Events
|
---|
| 185 | private void RegisterAlgorithmEvents() {
|
---|
| 186 | if (algorithm != null) {
|
---|
[11409] | 187 | algorithm.Prepared += Algorithm_Prepared;
|
---|
| 188 | RegisterResultsEvents();
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | private void DeregisterAlgorithmEvents() {
|
---|
| 193 | if (algorithm != null) {
|
---|
| 194 | algorithm.Prepared -= Algorithm_Prepared;
|
---|
| 195 | DeregisterResultsEvents();
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | private void Algorithm_Prepared(object sender, EventArgs e) {
|
---|
| 199 | RegisterResultsEvents();
|
---|
| 200 | }
|
---|
| 201 | #endregion
|
---|
| 202 |
|
---|
| 203 | #region Results Events
|
---|
| 204 | private void RegisterResultsEvents() {
|
---|
| 205 | if ((algorithm != null) && (algorithm.Results != null)) {
|
---|
[11406] | 206 | algorithm.Results.ItemsAdded += Results_ItemsAdded;
|
---|
| 207 | algorithm.Results.ItemsRemoved += Results_ItemsRemoved;
|
---|
| 208 | algorithm.Results.ItemsReplaced += Results_ItemsReplaced;
|
---|
| 209 | algorithm.Results.CollectionReset += Results_CollectionReset;
|
---|
[11409] | 210 | foreach (var r in algorithm.Results)
|
---|
| 211 | RegisterResultEvents(r);
|
---|
[11406] | 212 | }
|
---|
| 213 | }
|
---|
[11409] | 214 | private void DeregisterResultsEvents() {
|
---|
| 215 | if ((algorithm != null) && (algorithm.Results != null)) {
|
---|
[11406] | 216 | algorithm.Results.ItemsAdded -= Results_ItemsAdded;
|
---|
| 217 | algorithm.Results.ItemsRemoved -= Results_ItemsRemoved;
|
---|
| 218 | algorithm.Results.ItemsReplaced -= Results_ItemsReplaced;
|
---|
| 219 | algorithm.Results.CollectionReset -= Results_CollectionReset;
|
---|
[11409] | 220 | foreach (var r in algorithm.Results)
|
---|
| 221 | DeregisterResultEvents(r);
|
---|
[11406] | 222 | }
|
---|
| 223 | }
|
---|
| 224 | private void Results_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 225 | foreach (var r in e.Items)
|
---|
| 226 | RegisterResultEvents(r);
|
---|
| 227 | }
|
---|
| 228 | private void Results_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 229 | foreach (var r in e.Items)
|
---|
| 230 | DeregisterResultEvents(r);
|
---|
| 231 | }
|
---|
| 232 | private void Results_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 233 | foreach (var r in e.OldItems)
|
---|
| 234 | DeregisterResultEvents(r);
|
---|
| 235 | foreach (var r in e.Items)
|
---|
| 236 | RegisterResultEvents(r);
|
---|
| 237 | }
|
---|
| 238 | private void Results_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 239 | foreach (var r in e.OldItems)
|
---|
| 240 | DeregisterResultEvents(r);
|
---|
| 241 | foreach (var r in e.Items)
|
---|
| 242 | RegisterResultEvents(r);
|
---|
| 243 | }
|
---|
| 244 | #endregion
|
---|
| 245 |
|
---|
| 246 | #region Result Events
|
---|
| 247 | private void RegisterResultEvents(IResult result) {
|
---|
| 248 | if (result != null) {
|
---|
| 249 | result.ValueChanged += Result_ValueChanged;
|
---|
[11409] | 250 | result.ToStringChanged += Result_ToStringChanged;
|
---|
| 251 | UpdateOutputPort(result);
|
---|
[11406] | 252 | }
|
---|
| 253 | }
|
---|
| 254 | private void DeregisterResultEvents(IResult result) {
|
---|
| 255 | if (result != null) {
|
---|
| 256 | result.ValueChanged -= Result_ValueChanged;
|
---|
[11409] | 257 | result.ToStringChanged += Result_ToStringChanged;
|
---|
[11406] | 258 | }
|
---|
| 259 | }
|
---|
| 260 | private void Result_ValueChanged(object sender, EventArgs e) {
|
---|
| 261 | UpdateOutputPort((IResult)sender);
|
---|
| 262 | }
|
---|
[11409] | 263 | private void Result_ToStringChanged(object sender, EventArgs e) {
|
---|
| 264 | UpdateOutputPort((IResult)sender);
|
---|
| 265 | }
|
---|
[11406] | 266 | #endregion
|
---|
| 267 | }
|
---|
| 268 | }
|
---|