1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Core {
|
---|
29 | /// <summary>
|
---|
30 | /// The base class for all operators.
|
---|
31 | /// </summary>
|
---|
32 | public abstract class OperatorBase : ConstrainedItemBase, IOperator {
|
---|
33 |
|
---|
34 | [Storable]
|
---|
35 | private string myName;
|
---|
36 | /// <summary>
|
---|
37 | /// Gets or sets the name of the operator.
|
---|
38 | /// </summary>
|
---|
39 | /// <remarks>Calls <see cref="OnNameChanged"/> in the setter.</remarks>
|
---|
40 | public string Name {
|
---|
41 | get { return myName; }
|
---|
42 | set {
|
---|
43 | if (myName != value) {
|
---|
44 | myName = value;
|
---|
45 | OnNameChanged();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 | /// <summary>
|
---|
50 | /// Gets the description of the current operator.
|
---|
51 | /// </summary>
|
---|
52 | /// <remarks>Returns "No operator description available" if the method is not overriden.</remarks>
|
---|
53 | public virtual string Description {
|
---|
54 | get { return "No operator description available."; }
|
---|
55 | }
|
---|
56 | /// <summary>
|
---|
57 | /// Flag whether the current instance has been canceled.
|
---|
58 | /// </summary>
|
---|
59 | protected bool myCanceled;
|
---|
60 | /// <inheritdoc/>
|
---|
61 | public bool Canceled {
|
---|
62 | get { return myCanceled; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | [Storable]
|
---|
66 | private bool myBreakpoint;
|
---|
67 | /// <inheritdoc/>
|
---|
68 | /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
|
---|
69 | public bool Breakpoint {
|
---|
70 | get { return myBreakpoint; }
|
---|
71 | set {
|
---|
72 | if (value != myBreakpoint) {
|
---|
73 | myBreakpoint = value;
|
---|
74 | OnBreakpointChanged();
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | [Storable]
|
---|
80 | private List<IOperator> mySubOperators;
|
---|
81 | /// <summary>
|
---|
82 | /// Gets a list of all suboperators.
|
---|
83 | /// <note type="caution"> Returns the suboperators read-only!</note>
|
---|
84 | /// </summary>
|
---|
85 | public virtual IList<IOperator> SubOperators {
|
---|
86 | get { return mySubOperators.AsReadOnly(); }
|
---|
87 | }
|
---|
88 |
|
---|
89 | [Storable]
|
---|
90 | private Dictionary<string, IVariableInfo> myVariableInfos;
|
---|
91 | /// <inheritdoc/>
|
---|
92 | public virtual ICollection<IVariableInfo> VariableInfos {
|
---|
93 | get { return myVariableInfos.Values; }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private Dictionary<string, IVariable> myVariables;
|
---|
97 | /// <inheritdoc/>
|
---|
98 | public virtual ICollection<IVariable> Variables {
|
---|
99 | get { return myVariables.Values; }
|
---|
100 | }
|
---|
101 |
|
---|
102 | [Storable(Name="Variables")]
|
---|
103 | private List<IVariable> VariablePersistence {
|
---|
104 | get { return new List<IVariable>(myVariables.Values); }
|
---|
105 | set {
|
---|
106 | myVariables.Clear();
|
---|
107 | foreach (IVariable var in value) {
|
---|
108 | AddVariable(var);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | /// <summary>
|
---|
114 | /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
|
---|
115 | /// the canceled flag to <c>false</c> and the name of the operator to the type name.
|
---|
116 | /// </summary>
|
---|
117 | protected OperatorBase() {
|
---|
118 | myName = this.GetType().Name;
|
---|
119 | myCanceled = false;
|
---|
120 | myBreakpoint = false;
|
---|
121 | mySubOperators = new List<IOperator>();
|
---|
122 | myVariableInfos = new Dictionary<string, IVariableInfo>();
|
---|
123 | myVariables = new Dictionary<string, IVariable>();
|
---|
124 | }
|
---|
125 |
|
---|
126 | /// <summary>
|
---|
127 | /// Clones the current instance (deep clone).
|
---|
128 | /// </summary>
|
---|
129 | /// <remarks>Clones also sub operators, variables and variable infos.</remarks>
|
---|
130 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
131 | /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
|
---|
132 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
133 | OperatorBase clone = (OperatorBase)base.Clone(clonedObjects);
|
---|
134 | clone.myName = Name;
|
---|
135 | clone.mySubOperators.Clear();
|
---|
136 | for (int i = 0; i < SubOperators.Count; i++)
|
---|
137 | clone.AddSubOperator((IOperator)Auxiliary.Clone(SubOperators[i], clonedObjects));
|
---|
138 | clone.myVariableInfos.Clear();
|
---|
139 | foreach (IVariableInfo variableInfo in myVariableInfos.Values)
|
---|
140 | clone.AddVariableInfo((IVariableInfo)Auxiliary.Clone(variableInfo, clonedObjects));
|
---|
141 | clone.myVariables.Clear();
|
---|
142 | foreach (IVariable variable in myVariables.Values)
|
---|
143 | clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
|
---|
144 | return clone;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /// <summary>
|
---|
148 | /// Creates a new instance of <see cref="OperatorBaseView"/> to represent the current operator
|
---|
149 | /// visually.
|
---|
150 | /// </summary>
|
---|
151 | /// <returns>The created view as <see cref="OperatorBaseView"/>.</returns>
|
---|
152 | public override IView CreateView() {
|
---|
153 | return new OperatorBaseView(this);
|
---|
154 | }
|
---|
155 |
|
---|
156 | #region SubOperator Methods
|
---|
157 | /// <inheritdoc cref="HeuristicLab.Core.IOperator.AddSubOperator(HeuristicLab.Core.IOperator)"/>
|
---|
158 | /// <param name="subOperator">The sub operator to add.</param>
|
---|
159 | /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
|
---|
160 | public virtual void AddSubOperator(IOperator subOperator) {
|
---|
161 | mySubOperators.Add(subOperator);
|
---|
162 | OnSubOperatorAdded(subOperator, mySubOperators.Count - 1);
|
---|
163 | }
|
---|
164 | /// <inheritdoc cref="IOperator.TryAddSubOperator(HeuristicLab.Core.IOperator)"/>
|
---|
165 | /// <param name="subOperator">The sub operator to add.</param>
|
---|
166 | /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
|
---|
167 | public virtual bool TryAddSubOperator(IOperator subOperator) {
|
---|
168 | mySubOperators.Add(subOperator);
|
---|
169 | if (IsValid()) {
|
---|
170 | OnSubOperatorAdded(subOperator, mySubOperators.Count - 1);
|
---|
171 | return true;
|
---|
172 | } else {
|
---|
173 | mySubOperators.RemoveAt(mySubOperators.Count - 1);
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | /// <inheritdoc cref="HeuristicLab.Core.IOperator.TryAddSubOperator(HeuristicLab.Core.IOperator,
|
---|
178 | /// out System.Collections.Generic.ICollection<HeuristicLab.Core.IConstraint>)"/>
|
---|
179 | /// <param name="subOperator">The sub operator to add.</param>
|
---|
180 | /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
|
---|
181 | public virtual bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) {
|
---|
182 | mySubOperators.Add(subOperator);
|
---|
183 | if (IsValid(out violatedConstraints)) {
|
---|
184 | OnSubOperatorAdded(subOperator, mySubOperators.Count - 1);
|
---|
185 | return true;
|
---|
186 | } else {
|
---|
187 | mySubOperators.RemoveAt(mySubOperators.Count - 1);
|
---|
188 | return false;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | /// <inheritdoc cref="HeuristicLab.Core.IOperator.AddSubOperator(HeuristicLab.Core.IOperator, int)"/>
|
---|
192 | /// <param name="subOperator">The sub operator to add.</param>
|
---|
193 | /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
|
---|
194 | public virtual void AddSubOperator(IOperator subOperator, int index) {
|
---|
195 | mySubOperators.Insert(index, subOperator);
|
---|
196 | OnSubOperatorAdded(subOperator, index);
|
---|
197 | }
|
---|
198 | /// <inheritdoc cref="IOperator.TryAddSubOperator(HeuristicLab.Core.IOperator, int)"/>
|
---|
199 | /// <param name="subOperator">The sub operator to add.</param>
|
---|
200 | /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
|
---|
201 | public virtual bool TryAddSubOperator(IOperator subOperator, int index) {
|
---|
202 | mySubOperators.Insert(index, subOperator);
|
---|
203 | if (IsValid()) {
|
---|
204 | OnSubOperatorAdded(subOperator, index);
|
---|
205 | return true;
|
---|
206 | } else {
|
---|
207 | mySubOperators.RemoveAt(index);
|
---|
208 | return false;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | /// <inheritdoc cref="IOperator.TryAddSubOperator(HeuristicLab.Core.IOperator, int, out
|
---|
212 | /// System.Collections.Generic.ICollection<HeuristicLab.Core.IConstraint>)"/>
|
---|
213 | /// <param name="subOperator">The sub operator to add.</param>
|
---|
214 | /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
|
---|
215 | public virtual bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) {
|
---|
216 | mySubOperators.Insert(index, subOperator);
|
---|
217 | if (IsValid(out violatedConstraints)) {
|
---|
218 | OnSubOperatorAdded(subOperator, index);
|
---|
219 | return true;
|
---|
220 | } else {
|
---|
221 | mySubOperators.RemoveAt(index);
|
---|
222 | return false;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | /// <inheritdoc/>
|
---|
226 | /// <remarks>Calls <see cref="OnSubOperatorRemoved"/>.</remarks>
|
---|
227 | public virtual void RemoveSubOperator(int index) {
|
---|
228 | IOperator op = mySubOperators[index];
|
---|
229 | mySubOperators.RemoveAt(index);
|
---|
230 | OnSubOperatorRemoved(op, index);
|
---|
231 | }
|
---|
232 | /// <inheritdoc/>
|
---|
233 | /// <remarks>Calls <see cref="OnSubOperatorRemoved"/>.</remarks>
|
---|
234 | public virtual bool TryRemoveSubOperator(int index) {
|
---|
235 | IOperator op = mySubOperators[index];
|
---|
236 | mySubOperators.RemoveAt(index);
|
---|
237 | if (IsValid()) {
|
---|
238 | OnSubOperatorRemoved(op, index);
|
---|
239 | return true;
|
---|
240 | } else {
|
---|
241 | mySubOperators.Insert(index, op);
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | /// <inheritdoc/>
|
---|
246 | /// <remarks>Calls <see cref="OnSubOperatorRemoved"/>.</remarks>
|
---|
247 | public virtual bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) {
|
---|
248 | IOperator op = mySubOperators[index];
|
---|
249 | mySubOperators.RemoveAt(index);
|
---|
250 | if (IsValid(out violatedConstraints)) {
|
---|
251 | OnSubOperatorRemoved(op, index);
|
---|
252 | return true;
|
---|
253 | } else {
|
---|
254 | mySubOperators.Insert(index, op);
|
---|
255 | return false;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | #endregion
|
---|
259 |
|
---|
260 | #region VariableInfo Methods
|
---|
261 | /// <inheritdoc/>
|
---|
262 | public virtual IVariableInfo GetVariableInfo(string formalName) {
|
---|
263 | IVariableInfo info;
|
---|
264 | if (myVariableInfos.TryGetValue(formalName, out info))
|
---|
265 | return info;
|
---|
266 | else
|
---|
267 | return null;
|
---|
268 | }
|
---|
269 | /// <inheritdoc/>
|
---|
270 | /// <remarks>Calls <see cref="OnVariableInfoAdded"/>.</remarks>
|
---|
271 | public virtual void AddVariableInfo(IVariableInfo variableInfo) {
|
---|
272 | myVariableInfos.Add(variableInfo.FormalName, variableInfo);
|
---|
273 | OnVariableInfoAdded(variableInfo);
|
---|
274 | }
|
---|
275 | /// <inheritdoc/>
|
---|
276 | /// <remarks>Calls <see cref="OnVariableInfoAdded"/>.</remarks>
|
---|
277 | public virtual bool TryAddVariableInfo(IVariableInfo variableInfo) {
|
---|
278 | myVariableInfos.Add(variableInfo.FormalName, variableInfo);
|
---|
279 | if (IsValid()) {
|
---|
280 | OnVariableInfoAdded(variableInfo);
|
---|
281 | return true;
|
---|
282 | } else {
|
---|
283 | myVariableInfos.Remove(variableInfo.FormalName);
|
---|
284 | return false;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | /// <inheritdoc/>
|
---|
288 | /// <remarks>Calls <see cref="OnVariableInfoAdded"/>.</remarks>
|
---|
289 | public virtual bool TryAddVariableInfo(IVariableInfo variableInfo, out ICollection<IConstraint> violatedConstraints) {
|
---|
290 | myVariableInfos.Add(variableInfo.FormalName, variableInfo);
|
---|
291 | if (IsValid(out violatedConstraints)) {
|
---|
292 | OnVariableInfoAdded(variableInfo);
|
---|
293 | return true;
|
---|
294 | } else {
|
---|
295 | myVariableInfos.Remove(variableInfo.FormalName);
|
---|
296 | return false;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | /// <inheritdoc/>
|
---|
300 | /// <remarks>Calls <see cref="OnVariableInfoRemoved"/>.</remarks>
|
---|
301 | public virtual void RemoveVariableInfo(string formalName) {
|
---|
302 | IVariableInfo variableInfo;
|
---|
303 | if (myVariableInfos.TryGetValue(formalName, out variableInfo)) {
|
---|
304 | myVariableInfos.Remove(formalName);
|
---|
305 | OnVariableInfoRemoved(variableInfo);
|
---|
306 | }
|
---|
307 | }
|
---|
308 | /// <inheritdoc/>
|
---|
309 | /// <remarks>Calls <see cref="OnVariableInfoRemoved"/>.</remarks>
|
---|
310 | public virtual bool TryRemoveVariableInfo(string formalName) {
|
---|
311 | IVariableInfo variableInfo;
|
---|
312 | if (myVariableInfos.TryGetValue(formalName, out variableInfo)) {
|
---|
313 | myVariableInfos.Remove(formalName);
|
---|
314 | if (IsValid()) {
|
---|
315 | OnVariableInfoRemoved(variableInfo);
|
---|
316 | return true;
|
---|
317 | } else {
|
---|
318 | myVariableInfos.Add(formalName, variableInfo);
|
---|
319 | return false;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | return true;
|
---|
323 | }
|
---|
324 | /// <inheritdoc/>
|
---|
325 | /// <remarks>Calls <see cref="OnVariableInfoRemoved"/>.</remarks>
|
---|
326 | public virtual bool TryRemoveVariableInfo(string formalName, out ICollection<IConstraint> violatedConstraints) {
|
---|
327 | IVariableInfo variableInfo;
|
---|
328 | if (myVariableInfos.TryGetValue(formalName, out variableInfo)) {
|
---|
329 | myVariableInfos.Remove(formalName);
|
---|
330 | if (IsValid(out violatedConstraints)) {
|
---|
331 | OnVariableInfoRemoved(variableInfo);
|
---|
332 | return true;
|
---|
333 | } else {
|
---|
334 | myVariableInfos.Add(formalName, variableInfo);
|
---|
335 | return false;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | violatedConstraints = new List<IConstraint>();
|
---|
339 | return true;
|
---|
340 | }
|
---|
341 | #endregion
|
---|
342 |
|
---|
343 | #region Variable Methods
|
---|
344 | /// <inheritdoc/>
|
---|
345 | public virtual IVariable GetVariable(string name) {
|
---|
346 | IVariable variable;
|
---|
347 | if (myVariables.TryGetValue(name, out variable))
|
---|
348 | return variable;
|
---|
349 | else
|
---|
350 | return null;
|
---|
351 | }
|
---|
352 | /// <inheritdoc/>
|
---|
353 | /// <remarks>Calls <see cref="OnVariableAdded"/> and adds <c>NameChanging</c> and <c>NameChanged</c>
|
---|
354 | /// event handlers.</remarks>
|
---|
355 | public virtual void AddVariable(IVariable variable) {
|
---|
356 | myVariables.Add(variable.Name, variable);
|
---|
357 | variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
358 | variable.NameChanged += new EventHandler(Variable_NameChanged);
|
---|
359 | OnVariableAdded(variable);
|
---|
360 | }
|
---|
361 | /// <inheritdoc/>
|
---|
362 | /// <remarks>Calls <see cref="OnVariableAdded"/> and adds <c>NameChanging</c> and <c>NameChanged</c>
|
---|
363 | /// event handlers.</remarks>
|
---|
364 | public virtual bool TryAddVariable(IVariable variable) {
|
---|
365 | myVariables.Add(variable.Name, variable);
|
---|
366 | if (IsValid()) {
|
---|
367 | variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
368 | variable.NameChanged += new EventHandler(Variable_NameChanged);
|
---|
369 | OnVariableAdded(variable);
|
---|
370 | return true;
|
---|
371 | } else {
|
---|
372 | myVariableInfos.Remove(variable.Name);
|
---|
373 | return false;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | /// <inheritdoc/>
|
---|
377 | /// <remarks>Calls <see cref="OnVariableAdded"/> and adds <c>NameChanging</c> and <c>NameChanged</c>
|
---|
378 | /// event handlers.</remarks>
|
---|
379 | public virtual bool TryAddVariable(IVariable variable, out ICollection<IConstraint> violatedConstraints) {
|
---|
380 | myVariables.Add(variable.Name, variable);
|
---|
381 | if (IsValid(out violatedConstraints)) {
|
---|
382 | variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
383 | variable.NameChanged += new EventHandler(Variable_NameChanged);
|
---|
384 | OnVariableAdded(variable);
|
---|
385 | return true;
|
---|
386 | } else {
|
---|
387 | myVariableInfos.Remove(variable.Name);
|
---|
388 | return false;
|
---|
389 | }
|
---|
390 | }
|
---|
391 | /// <inheritdoc/>
|
---|
392 | /// <remarks>Calls <see cref="OnVariableRemoved"/> and removes <c>NameChanging</c> and <c>NameChanged</c>
|
---|
393 | /// event handlers.</remarks>
|
---|
394 | public virtual void RemoveVariable(string name) {
|
---|
395 | IVariable variable;
|
---|
396 | if (myVariables.TryGetValue(name, out variable)) {
|
---|
397 | variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
398 | variable.NameChanged -= new EventHandler(Variable_NameChanged);
|
---|
399 | myVariables.Remove(name);
|
---|
400 | OnVariableRemoved(variable);
|
---|
401 | }
|
---|
402 | }
|
---|
403 | /// <inheritdoc/>
|
---|
404 | /// <remarks>Calls <see cref="OnVariableRemoved"/> and removes <c>NameChanging</c> and <c>NameChanged</c>
|
---|
405 | /// event handlers.</remarks>
|
---|
406 | public virtual bool TryRemoveVariable(string name) {
|
---|
407 | IVariable variable;
|
---|
408 | if (myVariables.TryGetValue(name, out variable)) {
|
---|
409 | myVariables.Remove(name);
|
---|
410 | if (IsValid()) {
|
---|
411 | variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
412 | variable.NameChanged -= new EventHandler(Variable_NameChanged);
|
---|
413 | OnVariableRemoved(variable);
|
---|
414 | return true;
|
---|
415 | } else {
|
---|
416 | myVariables.Add(name, variable);
|
---|
417 | return false;
|
---|
418 | }
|
---|
419 | }
|
---|
420 | return true;
|
---|
421 | }
|
---|
422 | /// <inheritdoc/>
|
---|
423 | /// <remarks>Calls <see cref="OnVariableRemoved"/> and removes <c>NameChanging</c> and <c>NameChanged</c>
|
---|
424 | /// event handlers.</remarks>
|
---|
425 | public virtual bool TryRemoveVariable(string name, out ICollection<IConstraint> violatedConstraints) {
|
---|
426 | IVariable variable;
|
---|
427 | if (myVariables.TryGetValue(name, out variable)) {
|
---|
428 | myVariables.Remove(name);
|
---|
429 | if (IsValid(out violatedConstraints)) {
|
---|
430 | variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
431 | variable.NameChanged -= new EventHandler(Variable_NameChanged);
|
---|
432 | OnVariableRemoved(variable);
|
---|
433 | return true;
|
---|
434 | } else {
|
---|
435 | myVariables.Add(name, variable);
|
---|
436 | return false;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | violatedConstraints = new List<IConstraint>();
|
---|
440 | return true;
|
---|
441 | }
|
---|
442 | private void Variable_NameChanging(object sender, NameChangingEventArgs e) {
|
---|
443 | e.Cancel = myVariables.ContainsKey(e.Name);
|
---|
444 | }
|
---|
445 | private void Variable_NameChanged(object sender, EventArgs e) {
|
---|
446 | IVariable variable = (IVariable)sender;
|
---|
447 | string oldName = null;
|
---|
448 | foreach (KeyValuePair<string, IVariable> element in myVariables) {
|
---|
449 | if (element.Value == variable)
|
---|
450 | oldName = element.Key;
|
---|
451 | }
|
---|
452 | myVariables.Remove(oldName);
|
---|
453 | myVariables.Add(variable.Name, variable);
|
---|
454 | }
|
---|
455 | /// <inheritdoc cref="IOperator.GetVariableValue<T>(string, HeuristicLab.Core.IScope, bool)"/>
|
---|
456 | /// <remarks>Calls <see cref="GetVariableValue<T>(string, HeuristicLab.Core.IScope, bool, bool)"/>
|
---|
457 | /// with <c>throwOnError</c> set to <c>false</c>.</remarks>
|
---|
458 | public T GetVariableValue<T>(string formalName, IScope scope, bool recursiveLookup) where T : class, IItem {
|
---|
459 | return GetVariableValue<T>(formalName, scope, recursiveLookup, true);
|
---|
460 | }
|
---|
461 | /// <inheritdoc cref="IOperator.GetVariableValue<T>(string, HeuristicLab.Core.IScope, bool, bool)"/>
|
---|
462 | /// <remarks>Calls
|
---|
463 | /// <see cref="GetVariableValue(string, HeuristicLab.Core.IScope, bool, bool)"/>.</remarks>
|
---|
464 | public T GetVariableValue<T>(string formalName, IScope scope, bool recursiveLookup, bool throwOnError) where T : class, IItem {
|
---|
465 | return (T)GetVariableValue(formalName, scope, recursiveLookup, throwOnError);
|
---|
466 | }
|
---|
467 | /// <inheritdoc cref="IOperator.GetVariableValue(string, HeuristicLab.Core.IScope, bool)"/>
|
---|
468 | /// <remarks>Calls <see cref="GetVariableValue(string, HeuristicLab.Core.IScope, bool, bool)"/>
|
---|
469 | /// with <c>throwOnError</c> set to <c>false</c>.</remarks>
|
---|
470 | public IItem GetVariableValue(string formalName, IScope scope, bool recursiveLookup) {
|
---|
471 | return GetVariableValue(formalName, scope, recursiveLookup, true);
|
---|
472 | }
|
---|
473 | /// <inheritdoc cref="IOperator.GetVariableValue(string, HeuristicLab.Core.IScope, bool, bool)"/>
|
---|
474 | public virtual IItem GetVariableValue(string formalName, IScope scope, bool recursiveLookup, bool throwOnError) {
|
---|
475 | IVariableInfo info = GetVariableInfo(formalName);
|
---|
476 | if (info.Local) {
|
---|
477 | IVariable variable;
|
---|
478 | if (myVariables.TryGetValue(info.ActualName, out variable))
|
---|
479 | return variable.Value;
|
---|
480 | else {
|
---|
481 | if (throwOnError)
|
---|
482 | throw new ArgumentException("Variable " + info.ActualName + " not found");
|
---|
483 | else
|
---|
484 | return null;
|
---|
485 | }
|
---|
486 | } else {
|
---|
487 | return scope.GetVariableValue(formalName, recursiveLookup, throwOnError);
|
---|
488 | }
|
---|
489 | }
|
---|
490 | #endregion
|
---|
491 | /// <inheritdoc/>
|
---|
492 | public virtual IOperation Execute(IScope scope) {
|
---|
493 | myCanceled = false;
|
---|
494 |
|
---|
495 | foreach (IVariableInfo variableInfo in VariableInfos)
|
---|
496 | scope.AddAlias(variableInfo.FormalName, variableInfo.ActualName);
|
---|
497 |
|
---|
498 | IOperation next = Apply(scope);
|
---|
499 |
|
---|
500 | foreach (IVariableInfo variableInfo in VariableInfos)
|
---|
501 | scope.RemoveAlias(variableInfo.FormalName);
|
---|
502 |
|
---|
503 | OnExecuted();
|
---|
504 | return next;
|
---|
505 | }
|
---|
506 | /// <inheritdoc/>
|
---|
507 | /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
|
---|
508 | public virtual void Abort() {
|
---|
509 | myCanceled = true;
|
---|
510 | }
|
---|
511 | /// <summary>
|
---|
512 | /// Performs the current operator on the specified <paramref name="scope"/>.
|
---|
513 | /// </summary>
|
---|
514 | /// <param name="scope">The scope where to execute the operator</param>
|
---|
515 | /// <returns><c>null</c>.</returns>
|
---|
516 | public virtual IOperation Apply(IScope scope) {
|
---|
517 | return null;
|
---|
518 | }
|
---|
519 | /// <inheritdoc/>
|
---|
520 | public event EventHandler NameChanged;
|
---|
521 | /// <summary>
|
---|
522 | /// Fires a new <c>NameChanged</c> event.
|
---|
523 | /// </summary>
|
---|
524 | protected virtual void OnNameChanged() {
|
---|
525 | if (NameChanged != null) {
|
---|
526 | NameChanged(this, new EventArgs());
|
---|
527 | }
|
---|
528 | }
|
---|
529 | /// <inheritdoc/>
|
---|
530 | public event EventHandler BreakpointChanged;
|
---|
531 | /// <summary>
|
---|
532 | /// Fires a new <c>BreakpointChanged</c> event.
|
---|
533 | /// </summary>
|
---|
534 | protected virtual void OnBreakpointChanged() {
|
---|
535 | if (BreakpointChanged != null) {
|
---|
536 | BreakpointChanged(this, new EventArgs());
|
---|
537 | }
|
---|
538 | }
|
---|
539 | /// <inheritdoc/>
|
---|
540 | public event EventHandler<OperatorIndexEventArgs> SubOperatorAdded;
|
---|
541 | /// <summary>
|
---|
542 | /// Fires a new <c>SubOperatorAdded</c> event.
|
---|
543 | /// </summary>
|
---|
544 | /// <param name="subOperator">The sub operator that has been added.</param>
|
---|
545 | /// <param name="index">The position where the operator has been added.</param>
|
---|
546 | protected virtual void OnSubOperatorAdded(IOperator subOperator, int index) {
|
---|
547 | if (SubOperatorAdded != null)
|
---|
548 | SubOperatorAdded(this, new OperatorIndexEventArgs(subOperator, index));
|
---|
549 | }
|
---|
550 | /// <inheritdoc/>
|
---|
551 | public event EventHandler<OperatorIndexEventArgs> SubOperatorRemoved;
|
---|
552 | /// <summary>
|
---|
553 | /// Fires a new <c>SubOperatorRemoved</c> event.
|
---|
554 | /// </summary>
|
---|
555 | /// <param name="subOperator">The sub operator that has been removed.</param>
|
---|
556 | /// <param name="index">The position where the operator has been removed.</param>
|
---|
557 | protected virtual void OnSubOperatorRemoved(IOperator subOperator, int index) {
|
---|
558 | if (SubOperatorRemoved != null)
|
---|
559 | SubOperatorRemoved(this, new OperatorIndexEventArgs(subOperator, index));
|
---|
560 | }
|
---|
561 | /// <inheritdoc/>
|
---|
562 | public event EventHandler<VariableInfoEventArgs> VariableInfoAdded;
|
---|
563 | /// <summary>
|
---|
564 | /// Fires a new <c>VariableInfoAdded</c> event.
|
---|
565 | /// </summary>
|
---|
566 | /// <param name="variableInfo">The variable info that has been added.</param>
|
---|
567 | protected virtual void OnVariableInfoAdded(IVariableInfo variableInfo) {
|
---|
568 | if (VariableInfoAdded != null)
|
---|
569 | VariableInfoAdded(this, new VariableInfoEventArgs(variableInfo));
|
---|
570 | }
|
---|
571 | /// <inheritdoc/>
|
---|
572 | public event EventHandler<VariableInfoEventArgs> VariableInfoRemoved;
|
---|
573 | /// <summary>
|
---|
574 | /// Fires a new <c>VariableInfoRemoved</c> event.
|
---|
575 | /// </summary>
|
---|
576 | /// <param name="variableInfo">The variable info that has been removed.</param>
|
---|
577 | protected virtual void OnVariableInfoRemoved(IVariableInfo variableInfo) {
|
---|
578 | if (VariableInfoRemoved != null)
|
---|
579 | VariableInfoRemoved(this, new VariableInfoEventArgs(variableInfo));
|
---|
580 | }
|
---|
581 | /// <inheritdoc/>
|
---|
582 | public event EventHandler<VariableEventArgs> VariableAdded;
|
---|
583 | /// <summary>
|
---|
584 | /// Fires a new <c>VariableAdded</c> event.
|
---|
585 | /// </summary>
|
---|
586 | /// <param name="variable">The variable that has been added.</param>
|
---|
587 | protected virtual void OnVariableAdded(IVariable variable) {
|
---|
588 | if (VariableAdded != null)
|
---|
589 | VariableAdded(this, new VariableEventArgs(variable));
|
---|
590 | }
|
---|
591 | /// <inheritdoc/>
|
---|
592 | public event EventHandler<VariableEventArgs> VariableRemoved;
|
---|
593 | /// <summary>
|
---|
594 | /// Fires a new <c>VariableRemoved</c> event.
|
---|
595 | /// </summary>
|
---|
596 | /// <param name="variable">The variable that has been removed</param>
|
---|
597 | protected virtual void OnVariableRemoved(IVariable variable) {
|
---|
598 | if (VariableRemoved != null)
|
---|
599 | VariableRemoved(this, new VariableEventArgs(variable));
|
---|
600 | }
|
---|
601 | /// <inheritdoc/>
|
---|
602 | public event EventHandler Executed;
|
---|
603 | /// <summary>
|
---|
604 | /// Fires a new <c>Executed</c> event.
|
---|
605 | /// </summary>
|
---|
606 | protected virtual void OnExecuted() {
|
---|
607 | if (Executed != null) {
|
---|
608 | Executed(this, new EventArgs());
|
---|
609 | }
|
---|
610 | }
|
---|
611 | }
|
---|
612 | }
|
---|