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 | if (!variables.ContainsKey(port.Name))
|
---|
80 | variables.Add(new Variable(port.Name));
|
---|
81 | variables[port.Name].Value = (IItem)port.Value;
|
---|
82 | }
|
---|
83 | private void UpdateOutputPort(IVariable variable) {
|
---|
84 | IPort port = null;
|
---|
85 | if (Ports.TryGetValue(variable.Name, out port)) {
|
---|
86 | IOutputPort o = port as IOutputPort;
|
---|
87 | if (o != null) {
|
---|
88 | o.Value = variable.Value;
|
---|
89 | }
|
---|
90 | IOutputInputPort oi = port as IOutputInputPort;
|
---|
91 | if (oi != null) {
|
---|
92 | var result = (IItem)oi.SendValue(variable.Value);
|
---|
93 | if (!variables.ContainsKey(oi.Name + "Result"))
|
---|
94 | variables.Add(new Variable(oi.Name + "Result"));
|
---|
95 | variables[oi.Name + "Result"].Value = result;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | private void UpdateOutputPort(IOutputPort port) {
|
---|
100 | IVariable var = null;
|
---|
101 | if (variables.TryGetValue(port.Name, out var)) {
|
---|
102 | port.Value = var.Value;
|
---|
103 | } else {
|
---|
104 | port.Value = null;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | private void UpdateOutputInputPort(IOutputInputPort port) {
|
---|
108 | IVariable var = null;
|
---|
109 | IItem result = null;
|
---|
110 | if (variables.TryGetValue(port.Name, out var)) {
|
---|
111 | result = (IItem)port.SendValue(var.Value);
|
---|
112 | } else {
|
---|
113 | result = (IItem)port.SendValue(null);
|
---|
114 | }
|
---|
115 | if (!variables.ContainsKey(port.Name + "Result"))
|
---|
116 | variables.Add(new Variable(port.Name + "Result"));
|
---|
117 | variables[port.Name + "Result"].Value = result;
|
---|
118 | }
|
---|
119 |
|
---|
120 | #region Ports Events
|
---|
121 | protected override void RegisterPortsEvents() {
|
---|
122 | base.RegisterPortsEvents();
|
---|
123 | foreach (var p in Ports)
|
---|
124 | RegisterPortEvents(p);
|
---|
125 | }
|
---|
126 | protected override void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
127 | base.Ports_ItemsAdded(sender, e);
|
---|
128 | foreach (var p in e.Items)
|
---|
129 | RegisterPortEvents(p);
|
---|
130 | }
|
---|
131 | protected override void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
132 | base.Ports_ItemsRemoved(sender, e);
|
---|
133 | foreach (var p in e.Items)
|
---|
134 | DeregisterPortEvents(p);
|
---|
135 | }
|
---|
136 | protected override void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
137 | base.Ports_ItemsReplaced(sender, e);
|
---|
138 | foreach (var p in e.OldItems)
|
---|
139 | DeregisterPortEvents(p);
|
---|
140 | foreach (var p in e.Items)
|
---|
141 | RegisterPortEvents(p);
|
---|
142 | }
|
---|
143 | protected override void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
144 | base.Ports_CollectionReset(sender, e);
|
---|
145 | foreach (var p in e.OldItems)
|
---|
146 | DeregisterPortEvents(p);
|
---|
147 | foreach (var p in e.Items)
|
---|
148 | RegisterPortEvents(p);
|
---|
149 | }
|
---|
150 | #endregion
|
---|
151 |
|
---|
152 | #region Port Events
|
---|
153 | private void RegisterPortEvents(IPort port) {
|
---|
154 | IInputPort i = port as IInputPort;
|
---|
155 | if (i != null) {
|
---|
156 | i.NameChanged += InputPort_NameChanged;
|
---|
157 | i.ValueChanged += InputPort_ValueChanged;
|
---|
158 | UpdateVariable(i);
|
---|
159 | }
|
---|
160 | IOutputPort o = port as IOutputPort;
|
---|
161 | if (o != null) {
|
---|
162 | o.NameChanged += OutputPort_NameChanged;
|
---|
163 | UpdateOutputPort(o);
|
---|
164 | }
|
---|
165 | IOutputInputPort oi = port as IOutputInputPort;
|
---|
166 | if (oi != null) {
|
---|
167 | oi.NameChanged += OutputInputPort_NameChanged;
|
---|
168 | UpdateOutputInputPort(oi);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | private void DeregisterPortEvents(IPort port) {
|
---|
172 | IInputPort i = port as IInputPort;
|
---|
173 | if (i != null) {
|
---|
174 | i.NameChanged -= InputPort_NameChanged;
|
---|
175 | i.ValueChanged -= InputPort_ValueChanged;
|
---|
176 | }
|
---|
177 | IOutputPort o = port as IOutputPort;
|
---|
178 | if (o != null) {
|
---|
179 | o.NameChanged -= OutputPort_NameChanged;
|
---|
180 | }
|
---|
181 | IOutputInputPort oi = port as IOutputInputPort;
|
---|
182 | if (oi != null) {
|
---|
183 | oi.NameChanged -= OutputInputPort_NameChanged;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | private void InputPort_NameChanged(object sender, EventArgs e) {
|
---|
187 | UpdateVariable((IInputPort)sender);
|
---|
188 | }
|
---|
189 | private void InputPort_ValueChanged(object sender, EventArgs e) {
|
---|
190 | UpdateVariable((IInputPort)sender);
|
---|
191 | }
|
---|
192 | private void OutputPort_NameChanged(object sender, EventArgs e) {
|
---|
193 | UpdateOutputPort((IOutputPort)sender);
|
---|
194 | }
|
---|
195 | private void OutputInputPort_NameChanged(object sender, EventArgs e) {
|
---|
196 | UpdateOutputInputPort((IOutputInputPort)sender);
|
---|
197 | }
|
---|
198 | #endregion
|
---|
199 |
|
---|
200 | #region Variables Events
|
---|
201 | private void RegisterVariablesEvents() {
|
---|
202 | variables.ItemsAdded += Variables_ItemsAdded;
|
---|
203 | variables.ItemsRemoved += Variables_ItemsRemoved;
|
---|
204 | variables.ItemsReplaced += Variables_ItemsReplaced;
|
---|
205 | variables.CollectionReset += Variables_CollectionReset;
|
---|
206 | foreach (var v in variables)
|
---|
207 | RegisterVariableEvents(v);
|
---|
208 | }
|
---|
209 | private void Variables_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
|
---|
210 | foreach (var v in e.Items)
|
---|
211 | RegisterVariableEvents(v);
|
---|
212 | }
|
---|
213 | private void Variables_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
|
---|
214 | foreach (var v in e.Items)
|
---|
215 | DeregisterVariableEvents(v);
|
---|
216 | }
|
---|
217 | private void Variables_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
|
---|
218 | foreach (var v in e.OldItems)
|
---|
219 | DeregisterVariableEvents(v);
|
---|
220 | foreach (var v in e.Items)
|
---|
221 | RegisterVariableEvents(v);
|
---|
222 | }
|
---|
223 | private void Variables_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IVariable> e) {
|
---|
224 | foreach (var v in e.OldItems)
|
---|
225 | DeregisterVariableEvents(v);
|
---|
226 | foreach (var v in e.Items)
|
---|
227 | RegisterVariableEvents(v);
|
---|
228 | }
|
---|
229 | #endregion
|
---|
230 |
|
---|
231 | #region Variable Events
|
---|
232 | private void RegisterVariableEvents(IVariable variable) {
|
---|
233 | if (variable != null) {
|
---|
234 | variable.ValueChanged += Variable_ValueChanged;
|
---|
235 | variable.ToStringChanged += Variable_ToStringChanged;
|
---|
236 | UpdateOutputPort(variable);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | private void DeregisterVariableEvents(IVariable variable) {
|
---|
240 | if (variable != null) {
|
---|
241 | variable.ValueChanged -= Variable_ValueChanged;
|
---|
242 | variable.ToStringChanged += Variable_ToStringChanged;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | private void Variable_ValueChanged(object sender, EventArgs e) {
|
---|
246 | UpdateOutputPort((IVariable)sender);
|
---|
247 | }
|
---|
248 | private void Variable_ToStringChanged(object sender, EventArgs e) {
|
---|
249 | UpdateOutputPort((IVariable)sender);
|
---|
250 | }
|
---|
251 | #endregion
|
---|
252 | }
|
---|
253 | }
|
---|