Changeset 11409 for branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/AlgorithmNode.cs
- Timestamp:
- 10/02/14 03:30:30 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/AlgorithmNode.cs
r11406 r11409 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 using System.Drawing;25 22 using HeuristicLab.Common; 26 23 using HeuristicLab.Core; 27 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 using System; 28 26 29 27 namespace HeuristicLab.Optimization.Networks { 30 28 [Item("AlgorithmNode", "A node of an optimization network which contains a HeuristicLab algorithm.")] 31 29 [StorableClass] 32 public class AlgorithmNode : Node { 30 public class AlgorithmNode : Node, IAlgorithmNode { 31 new public PortCollection Ports { 32 get { return base.Ports; } 33 } 34 33 35 [Storable] 34 36 private IAlgorithm algorithm; … … 54 56 } 55 57 public AlgorithmNode() 56 : base(" Node") {58 : base("AlgorithmNode") { 57 59 RegisterPortsEvents(); 58 60 } … … 69 71 private void AfterDeserialization() { 70 72 RegisterPortsEvents(); 73 RegisterAlgorithmEvents(); 71 74 } 72 75 … … 77 80 private void UpdateParameter(IInputPort port) { 78 81 if (algorithm != null) { 79 foreach (var param in algorithm.Parameters) { 82 IParameter param = null; 83 if (algorithm.Parameters.TryGetValue(port.Name, out param)) { 80 84 IValueParameter p = param as IValueParameter; 81 if ( (p != null) && (p.Name == port.Name)) {85 if (p != null) { 82 86 p.Value = (IItem)port.Value; 83 87 } … … 86 90 } 87 91 private void UpdateOutputPort(IResult result) { 88 foreach (var port in Ports) { 92 IPort port = null; 93 if (Ports.TryGetValue(result.Name, out port)) { 89 94 IOutputPort p = port as IOutputPort; 90 if ( (p != null) && (p.Name == result.Name)) {95 if (p != null) { 91 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; 92 107 } 93 108 } … … 106 121 Ports.ItemsReplaced += Ports_ItemsReplaced; 107 122 Ports.CollectionReset += Ports_CollectionReset; 123 foreach (var p in Ports) 124 RegisterPortEvents(p); 108 125 } 109 126 void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) { … … 131 148 #region Port Events 132 149 private void RegisterPortEvents(IPort port) { 133 IInputPort p = port as IInputPort; 134 if (p != null) { 135 p.ValueChanged += InputPort_ValueChanged; 150 IInputPort i = port as IInputPort; 151 if (i != null) { 152 i.NameChanged += InputPort_NameChanged; 153 i.ValueChanged += InputPort_ValueChanged; 154 UpdateParameter(i); 155 } 156 IOutputPort o = port as IOutputPort; 157 if (o != null) { 158 o.NameChanged += OutputPort_NameChanged; 159 UpdateOutputPort(o); 136 160 } 137 161 } 138 162 private void DeregisterPortEvents(IPort port) { 139 IInputPort p = port as IInputPort; 140 if (p != null) { 141 p.ValueChanged -= InputPort_ValueChanged; 142 } 163 IInputPort i = port as IInputPort; 164 if (i != null) { 165 i.NameChanged -= InputPort_NameChanged; 166 i.ValueChanged -= InputPort_ValueChanged; 167 } 168 IOutputPort o = port as IOutputPort; 169 if (o != null) { 170 o.NameChanged -= OutputPort_NameChanged; 171 } 172 } 173 private void InputPort_NameChanged(object sender, EventArgs e) { 174 UpdateParameter((IInputPort)sender); 143 175 } 144 176 private void InputPort_ValueChanged(object sender, EventArgs e) { 145 177 UpdateParameter((IInputPort)sender); 178 } 179 private void OutputPort_NameChanged(object sender, EventArgs e) { 180 UpdateOutputPort((IOutputPort)sender); 146 181 } 147 182 #endregion … … 150 185 private void RegisterAlgorithmEvents() { 151 186 if (algorithm != null) { 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)) { 152 206 algorithm.Results.ItemsAdded += Results_ItemsAdded; 153 207 algorithm.Results.ItemsRemoved += Results_ItemsRemoved; 154 208 algorithm.Results.ItemsReplaced += Results_ItemsReplaced; 155 209 algorithm.Results.CollectionReset += Results_CollectionReset; 156 } 157 } 158 private void DeregisterAlgorithmEvents() { 159 if (algorithm != null) { 210 foreach (var r in algorithm.Results) 211 RegisterResultEvents(r); 212 } 213 } 214 private void DeregisterResultsEvents() { 215 if ((algorithm != null) && (algorithm.Results != null)) { 160 216 algorithm.Results.ItemsAdded -= Results_ItemsAdded; 161 217 algorithm.Results.ItemsRemoved -= Results_ItemsRemoved; 162 218 algorithm.Results.ItemsReplaced -= Results_ItemsReplaced; 163 219 algorithm.Results.CollectionReset -= Results_CollectionReset; 220 foreach (var r in algorithm.Results) 221 DeregisterResultEvents(r); 164 222 } 165 223 } … … 190 248 if (result != null) { 191 249 result.ValueChanged += Result_ValueChanged; 250 result.ToStringChanged += Result_ToStringChanged; 251 UpdateOutputPort(result); 192 252 } 193 253 } … … 195 255 if (result != null) { 196 256 result.ValueChanged -= Result_ValueChanged; 257 result.ToStringChanged += Result_ToStringChanged; 197 258 } 198 259 } … … 200 261 UpdateOutputPort((IResult)sender); 201 262 } 263 private void Result_ToStringChanged(object sender, EventArgs e) { 264 UpdateOutputPort((IResult)sender); 265 } 202 266 #endregion 203 267 }
Note: See TracChangeset
for help on using the changeset viewer.