[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 System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Optimization.Networks {
|
---|
| 30 | [Item("AlgorithmNode", "A node of an optimization network which contains a HeuristicLab algorithm.")]
|
---|
| 31 | [StorableClass]
|
---|
| 32 | public class AlgorithmNode : Node {
|
---|
| 33 | [Storable]
|
---|
| 34 | private IAlgorithm algorithm;
|
---|
| 35 | public IAlgorithm Algorithm {
|
---|
| 36 | get { return algorithm; }
|
---|
| 37 | set {
|
---|
| 38 | if (value != algorithm) {
|
---|
| 39 | DeregisterAlgorithmEvents();
|
---|
| 40 | algorithm = value;
|
---|
| 41 | RegisterAlgorithmEvents();
|
---|
| 42 | OnAlgorithmChanged();
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | [StorableConstructor]
|
---|
| 48 | protected AlgorithmNode(bool deserializing) : base(deserializing) { }
|
---|
| 49 | protected AlgorithmNode(AlgorithmNode original, Cloner cloner)
|
---|
| 50 | : base(original, cloner) {
|
---|
| 51 | algorithm = cloner.Clone(original.algorithm);
|
---|
| 52 | RegisterPortsEvents();
|
---|
| 53 | RegisterAlgorithmEvents();
|
---|
| 54 | }
|
---|
| 55 | public AlgorithmNode()
|
---|
| 56 | : base("Node") {
|
---|
| 57 | RegisterPortsEvents();
|
---|
| 58 | }
|
---|
| 59 | public AlgorithmNode(string name)
|
---|
| 60 | : base(name) {
|
---|
| 61 | RegisterPortsEvents();
|
---|
| 62 | }
|
---|
| 63 | public AlgorithmNode(string name, string description)
|
---|
| 64 | : base(name, description) {
|
---|
| 65 | RegisterPortsEvents();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 69 | private void AfterDeserialization() {
|
---|
| 70 | RegisterPortsEvents();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 74 | return new AlgorithmNode(this, cloner);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private void UpdateParameter(IInputPort port) {
|
---|
| 78 | if (algorithm != null) {
|
---|
| 79 | foreach (var param in algorithm.Parameters) {
|
---|
| 80 | IValueParameter p = param as IValueParameter;
|
---|
| 81 | if ((p != null) && (p.Name == port.Name)) {
|
---|
| 82 | p.Value = (IItem)port.Value;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | private void UpdateOutputPort(IResult result) {
|
---|
| 88 | foreach (var port in Ports) {
|
---|
| 89 | IOutputPort p = port as IOutputPort;
|
---|
| 90 | if ((p != null) && (p.Name == result.Name)) {
|
---|
| 91 | p.Value = result.Value;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public event EventHandler AlgorithmChanged;
|
---|
| 97 | protected virtual void OnAlgorithmChanged() {
|
---|
| 98 | var handler = AlgorithmChanged;
|
---|
| 99 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | #region Ports Events
|
---|
| 103 | private void RegisterPortsEvents() {
|
---|
| 104 | Ports.ItemsAdded += Ports_ItemsAdded;
|
---|
| 105 | Ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
| 106 | Ports.ItemsReplaced += Ports_ItemsReplaced;
|
---|
| 107 | Ports.CollectionReset += Ports_CollectionReset;
|
---|
| 108 | }
|
---|
| 109 | void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 110 | foreach (var p in e.Items)
|
---|
| 111 | RegisterPortEvents(p);
|
---|
| 112 | }
|
---|
| 113 | void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 114 | foreach (var p in e.Items)
|
---|
| 115 | DeregisterPortEvents(p);
|
---|
| 116 | }
|
---|
| 117 | void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 118 | foreach (var p in e.OldItems)
|
---|
| 119 | DeregisterPortEvents(p);
|
---|
| 120 | foreach (var p in e.Items)
|
---|
| 121 | RegisterPortEvents(p);
|
---|
| 122 | }
|
---|
| 123 | void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 124 | foreach (var p in e.OldItems)
|
---|
| 125 | DeregisterPortEvents(p);
|
---|
| 126 | foreach (var p in e.Items)
|
---|
| 127 | RegisterPortEvents(p);
|
---|
| 128 | }
|
---|
| 129 | #endregion
|
---|
| 130 |
|
---|
| 131 | #region Port Events
|
---|
| 132 | private void RegisterPortEvents(IPort port) {
|
---|
| 133 | IInputPort p = port as IInputPort;
|
---|
| 134 | if (p != null) {
|
---|
| 135 | p.ValueChanged += InputPort_ValueChanged;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | private void DeregisterPortEvents(IPort port) {
|
---|
| 139 | IInputPort p = port as IInputPort;
|
---|
| 140 | if (p != null) {
|
---|
| 141 | p.ValueChanged -= InputPort_ValueChanged;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | private void InputPort_ValueChanged(object sender, EventArgs e) {
|
---|
| 145 | UpdateParameter((IInputPort)sender);
|
---|
| 146 | }
|
---|
| 147 | #endregion
|
---|
| 148 |
|
---|
| 149 | #region Algorithm Events
|
---|
| 150 | private void RegisterAlgorithmEvents() {
|
---|
| 151 | if (algorithm != null) {
|
---|
| 152 | algorithm.Results.ItemsAdded += Results_ItemsAdded;
|
---|
| 153 | algorithm.Results.ItemsRemoved += Results_ItemsRemoved;
|
---|
| 154 | algorithm.Results.ItemsReplaced += Results_ItemsReplaced;
|
---|
| 155 | algorithm.Results.CollectionReset += Results_CollectionReset;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | private void DeregisterAlgorithmEvents() {
|
---|
| 159 | if (algorithm != null) {
|
---|
| 160 | algorithm.Results.ItemsAdded -= Results_ItemsAdded;
|
---|
| 161 | algorithm.Results.ItemsRemoved -= Results_ItemsRemoved;
|
---|
| 162 | algorithm.Results.ItemsReplaced -= Results_ItemsReplaced;
|
---|
| 163 | algorithm.Results.CollectionReset -= Results_CollectionReset;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | private void Results_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 167 | foreach (var r in e.Items)
|
---|
| 168 | RegisterResultEvents(r);
|
---|
| 169 | }
|
---|
| 170 | private void Results_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 171 | foreach (var r in e.Items)
|
---|
| 172 | DeregisterResultEvents(r);
|
---|
| 173 | }
|
---|
| 174 | private void Results_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 175 | foreach (var r in e.OldItems)
|
---|
| 176 | DeregisterResultEvents(r);
|
---|
| 177 | foreach (var r in e.Items)
|
---|
| 178 | RegisterResultEvents(r);
|
---|
| 179 | }
|
---|
| 180 | private void Results_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 181 | foreach (var r in e.OldItems)
|
---|
| 182 | DeregisterResultEvents(r);
|
---|
| 183 | foreach (var r in e.Items)
|
---|
| 184 | RegisterResultEvents(r);
|
---|
| 185 | }
|
---|
| 186 | #endregion
|
---|
| 187 |
|
---|
| 188 | #region Result Events
|
---|
| 189 | private void RegisterResultEvents(IResult result) {
|
---|
| 190 | if (result != null) {
|
---|
| 191 | result.ValueChanged += Result_ValueChanged;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | private void DeregisterResultEvents(IResult result) {
|
---|
| 195 | if (result != null) {
|
---|
| 196 | result.ValueChanged -= Result_ValueChanged;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | private void Result_ValueChanged(object sender, EventArgs e) {
|
---|
| 200 | UpdateOutputPort((IResult)sender);
|
---|
| 201 | }
|
---|
| 202 | #endregion
|
---|
| 203 | }
|
---|
| 204 | }
|
---|