Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/VariablesNode.cs @ 11423

Last change on this file since 11423 was 11423, checked in by swagner, 10 years ago

#2205: Worked on optimization networks

File size: 7.7 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("VariablesNode", "A node of an optimization network which contains variables.")]
29  [StorableClass]
30  public class VariablesNode : Node, IVariablesNode {
31    new public PortCollection Ports {
32      get { return base.Ports; }
33    }
34
35    [Storable]
36    private VariableCollection variables;
37    public VariableCollection Variables {
38      get { return variables; }
39    }
40
41    [StorableConstructor]
42    protected VariablesNode(bool deserializing) : base(deserializing) { }
43    protected VariablesNode(VariablesNode original, Cloner cloner)
44      : base(original, cloner) {
45      variables = cloner.Clone(original.variables);
46      RegisterPortsEvents();
47      RegisterVariablesEvents();
48    }
49    public VariablesNode()
50      : base("VariablesNode") {
51      variables = new VariableCollection();
52      RegisterPortsEvents();
53      RegisterVariablesEvents();
54    }
55    public VariablesNode(string name)
56      : base(name) {
57      variables = new VariableCollection();
58      RegisterPortsEvents();
59      RegisterVariablesEvents();
60    }
61    public VariablesNode(string name, string description)
62      : base(name, description) {
63      variables = new VariableCollection();
64      RegisterPortsEvents();
65      RegisterVariablesEvents();
66    }
67
68    [StorableHook(HookType.AfterDeserialization)]
69    private void AfterDeserialization() {
70      RegisterPortsEvents();
71      RegisterVariablesEvents();
72    }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new VariablesNode(this, cloner);
76    }
77
78    private void UpdateVariable(IInputPort port) {
79      IVariable var = null;
80      if (variables.TryGetValue(port.Name, out var)) {
81        var.Value = (IItem)port.Value;
82      }
83    }
84    private void UpdateOutputPort(IVariable variable) {
85      IPort port = null;
86      if (Ports.TryGetValue(variable.Name, out port)) {
87        IOutputPort p = port as IOutputPort;
88        if (p != null) {
89          p.Value = variable.Value;
90        }
91      }
92    }
93    private void UpdateOutputPort(IOutputPort port) {
94      IVariable var = null;
95      if (variables.TryGetValue(port.Name, out var)) {
96        port.Value = var.Value;
97      } else {
98        port.Value = null;
99      }
100    }
101
102    #region Ports Events
103    protected override void RegisterPortsEvents() {
104      base.RegisterPortsEvents();
105      foreach (var p in Ports)
106        RegisterPortEvents(p);
107    }
108    protected override void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
109      base.Ports_ItemsAdded(sender, e);
110      foreach (var p in e.Items)
111        RegisterPortEvents(p);
112    }
113    protected override void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
114      base.Ports_ItemsRemoved(sender, e);
115      foreach (var p in e.Items)
116        DeregisterPortEvents(p);
117    }
118    protected override void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
119      base.Ports_ItemsReplaced(sender, e);
120      foreach (var p in e.OldItems)
121        DeregisterPortEvents(p);
122      foreach (var p in e.Items)
123        RegisterPortEvents(p);
124    }
125    protected override void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
126      base.Ports_CollectionReset(sender, e);
127      foreach (var p in e.OldItems)
128        DeregisterPortEvents(p);
129      foreach (var p in e.Items)
130        RegisterPortEvents(p);
131    }
132    #endregion
133
134    #region Port Events
135    private void RegisterPortEvents(IPort port) {
136      IInputPort i = port as IInputPort;
137      if (i != null) {
138        i.NameChanged += InputPort_NameChanged;
139        i.ValueChanged += InputPort_ValueChanged;
140        UpdateVariable(i);
141      }
142      IOutputPort o = port as IOutputPort;
143      if (o != null) {
144        o.NameChanged += OutputPort_NameChanged;
145        UpdateOutputPort(o);
146      }
147    }
148    private void DeregisterPortEvents(IPort port) {
149      IInputPort i = port as IInputPort;
150      if (i != null) {
151        i.NameChanged -= InputPort_NameChanged;
152        i.ValueChanged -= InputPort_ValueChanged;
153      }
154      IOutputPort o = port as IOutputPort;
155      if (o != null) {
156        o.NameChanged -= OutputPort_NameChanged;
157      }
158    }
159    private void InputPort_NameChanged(object sender, EventArgs e) {
160      UpdateVariable((IInputPort)sender);
161    }
162    private void InputPort_ValueChanged(object sender, EventArgs e) {
163      UpdateVariable((IInputPort)sender);
164    }
165    private void OutputPort_NameChanged(object sender, EventArgs e) {
166      UpdateOutputPort((IOutputPort)sender);
167    }
168    #endregion
169
170    #region Variables Events
171    private void RegisterVariablesEvents() {
172      variables.ItemsAdded += Variables_ItemsAdded;
173      variables.ItemsRemoved += Variables_ItemsRemoved;
174      variables.ItemsReplaced += Variables_ItemsReplaced;
175      variables.CollectionReset += Variables_CollectionReset;
176      foreach (var v in variables)
177        RegisterVariableEvents(v);
178    }
179    private void Variables_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
180      foreach (var v in e.Items)
181        RegisterVariableEvents(v);
182    }
183    private void Variables_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
184      foreach (var v in e.Items)
185        DeregisterVariableEvents(v);
186    }
187    private void Variables_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
188      foreach (var v in e.OldItems)
189        DeregisterVariableEvents(v);
190      foreach (var v in e.Items)
191        RegisterVariableEvents(v);
192    }
193    private void Variables_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
194      foreach (var v in e.OldItems)
195        DeregisterVariableEvents(v);
196      foreach (var v in e.Items)
197        RegisterVariableEvents(v);
198    }
199    #endregion
200
201    #region Variable Events
202    private void RegisterVariableEvents(IVariable variable) {
203      if (variable != null) {
204        variable.ValueChanged += Variable_ValueChanged;
205        variable.ToStringChanged += Variable_ToStringChanged;
206        UpdateOutputPort(variable);
207      }
208    }
209    private void DeregisterVariableEvents(IVariable variable) {
210      if (variable != null) {
211        variable.ValueChanged -= Variable_ValueChanged;
212        variable.ToStringChanged += Variable_ToStringChanged;
213      }
214    }
215    private void Variable_ValueChanged(object sender, EventArgs e) {
216      UpdateOutputPort((IVariable)sender);
217    }
218    private void Variable_ToStringChanged(object sender, EventArgs e) {
219      UpdateOutputPort((IVariable)sender);
220    }
221    #endregion
222  }
223}
Note: See TracBrowser for help on using the repository browser.