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