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;
|
---|
25 | using System;
|
---|
26 |
|
---|
27 | namespace 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 | private void RegisterPortsEvents() {
|
---|
104 | Ports.ItemsAdded += Ports_ItemsAdded;
|
---|
105 | Ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
106 | Ports.ItemsReplaced += Ports_ItemsReplaced;
|
---|
107 | Ports.CollectionReset += Ports_CollectionReset;
|
---|
108 | foreach (var p in Ports)
|
---|
109 | RegisterPortEvents(p);
|
---|
110 | }
|
---|
111 | void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
112 | foreach (var p in e.Items)
|
---|
113 | RegisterPortEvents(p);
|
---|
114 | }
|
---|
115 | void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
116 | foreach (var p in e.Items)
|
---|
117 | DeregisterPortEvents(p);
|
---|
118 | }
|
---|
119 | void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
120 | foreach (var p in e.OldItems)
|
---|
121 | DeregisterPortEvents(p);
|
---|
122 | foreach (var p in e.Items)
|
---|
123 | RegisterPortEvents(p);
|
---|
124 | }
|
---|
125 | void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
126 | foreach (var p in e.OldItems)
|
---|
127 | DeregisterPortEvents(p);
|
---|
128 | foreach (var p in e.Items)
|
---|
129 | RegisterPortEvents(p);
|
---|
130 | }
|
---|
131 | #endregion
|
---|
132 |
|
---|
133 | #region Port Events
|
---|
134 | private void RegisterPortEvents(IPort port) {
|
---|
135 | IInputPort i = port as IInputPort;
|
---|
136 | if (i != null) {
|
---|
137 | i.NameChanged += InputPort_NameChanged;
|
---|
138 | i.ValueChanged += InputPort_ValueChanged;
|
---|
139 | UpdateVariable(i);
|
---|
140 | }
|
---|
141 | IOutputPort o = port as IOutputPort;
|
---|
142 | if (o != null) {
|
---|
143 | o.NameChanged += OutputPort_NameChanged;
|
---|
144 | UpdateOutputPort(o);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | private void DeregisterPortEvents(IPort port) {
|
---|
148 | IInputPort i = port as IInputPort;
|
---|
149 | if (i != null) {
|
---|
150 | i.NameChanged -= InputPort_NameChanged;
|
---|
151 | i.ValueChanged -= InputPort_ValueChanged;
|
---|
152 | }
|
---|
153 | IOutputPort o = port as IOutputPort;
|
---|
154 | if (o != null) {
|
---|
155 | o.NameChanged -= OutputPort_NameChanged;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | private void InputPort_NameChanged(object sender, EventArgs e) {
|
---|
159 | UpdateVariable((IInputPort)sender);
|
---|
160 | }
|
---|
161 | private void InputPort_ValueChanged(object sender, EventArgs e) {
|
---|
162 | UpdateVariable((IInputPort)sender);
|
---|
163 | }
|
---|
164 | private void OutputPort_NameChanged(object sender, EventArgs e) {
|
---|
165 | UpdateOutputPort((IOutputPort)sender);
|
---|
166 | }
|
---|
167 | #endregion
|
---|
168 |
|
---|
169 | #region Variables Events
|
---|
170 | private void RegisterVariablesEvents() {
|
---|
171 | variables.ItemsAdded += Variables_ItemsAdded;
|
---|
172 | variables.ItemsRemoved += Variables_ItemsRemoved;
|
---|
173 | variables.ItemsReplaced += Variables_ItemsReplaced;
|
---|
174 | variables.CollectionReset += Variables_CollectionReset;
|
---|
175 | foreach (var v in variables)
|
---|
176 | RegisterVariableEvents(v);
|
---|
177 | }
|
---|
178 | private void Variables_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
|
---|
179 | foreach (var v in e.Items)
|
---|
180 | RegisterVariableEvents(v);
|
---|
181 | }
|
---|
182 | private void Variables_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
|
---|
183 | foreach (var v in e.Items)
|
---|
184 | DeregisterVariableEvents(v);
|
---|
185 | }
|
---|
186 | private void Variables_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
|
---|
187 | foreach (var v in e.OldItems)
|
---|
188 | DeregisterVariableEvents(v);
|
---|
189 | foreach (var v in e.Items)
|
---|
190 | RegisterVariableEvents(v);
|
---|
191 | }
|
---|
192 | private void Variables_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
|
---|
193 | foreach (var v in e.OldItems)
|
---|
194 | DeregisterVariableEvents(v);
|
---|
195 | foreach (var v in e.Items)
|
---|
196 | RegisterVariableEvents(v);
|
---|
197 | }
|
---|
198 | #endregion
|
---|
199 |
|
---|
200 | #region Variable Events
|
---|
201 | private void RegisterVariableEvents(IVariable variable) {
|
---|
202 | if (variable != null) {
|
---|
203 | variable.ValueChanged += Variable_ValueChanged;
|
---|
204 | variable.ToStringChanged += Variable_ToStringChanged;
|
---|
205 | UpdateOutputPort(variable);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | private void DeregisterVariableEvents(IVariable variable) {
|
---|
209 | if (variable != null) {
|
---|
210 | variable.ValueChanged -= Variable_ValueChanged;
|
---|
211 | variable.ToStringChanged += Variable_ToStringChanged;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | private void Variable_ValueChanged(object sender, EventArgs e) {
|
---|
215 | UpdateOutputPort((IVariable)sender);
|
---|
216 | }
|
---|
217 | private void Variable_ToStringChanged(object sender, EventArgs e) {
|
---|
218 | UpdateOutputPort((IVariable)sender);
|
---|
219 | }
|
---|
220 | #endregion
|
---|
221 | }
|
---|
222 | }
|
---|