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.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.Decomposers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Operators {
|
---|
30 | /// <summary>
|
---|
31 | /// Class to inject local variables into the scope.
|
---|
32 | /// </summary>
|
---|
33 | public class VariableInjector : OperatorBase {
|
---|
34 |
|
---|
35 | [Storable]
|
---|
36 | private Dictionary<IVariable, IVariableInfo> variableVariableInfoTable;
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | private Dictionary<IVariableInfo, IVariable> variableInfoVariableTable;
|
---|
40 |
|
---|
41 | /// <inheritdoc select="summary"/>
|
---|
42 | public override string Description {
|
---|
43 | get { return @"TODO\r\nOperator description still missing ..."; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Initializes a new instance of <see cref="VariableInjector"/>.
|
---|
48 | /// </summary>
|
---|
49 | public VariableInjector()
|
---|
50 | : base() {
|
---|
51 | variableVariableInfoTable = new Dictionary<IVariable, IVariableInfo>();
|
---|
52 | variableInfoVariableTable = new Dictionary<IVariableInfo, IVariable>();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Clones the current instance (deep clone).
|
---|
57 | /// </summary>
|
---|
58 | /// <remarks>Deep clone performed with <see cref="Auxiliary.Clone"/> of helper class
|
---|
59 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
60 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
61 | /// <returns>The cloned object as <see cref="VariableInjector"/>.</returns>
|
---|
62 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
63 | VariableInjector clone = new VariableInjector();
|
---|
64 | clonedObjects.Add(Guid, clone);
|
---|
65 | clone.Name = Name;
|
---|
66 | foreach (IVariable variable in Variables)
|
---|
67 | clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
|
---|
68 | return clone;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Adds the specified <paramref name="variable"/> to the current instance to get injected.
|
---|
73 | /// </summary>
|
---|
74 | /// <param name="variable">The variable to add.</param>
|
---|
75 | /// <remarks>Calls private method <see cref="CreateVariableInfo"/> and <see cref="OperatorBase.AddVariable"/>
|
---|
76 | /// of base class <see cref="OperatorBase"/>.</remarks>
|
---|
77 | public override void AddVariable(IVariable variable) {
|
---|
78 | base.AddVariable(variable);
|
---|
79 | CreateVariableInfo(variable);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// Removes a variable with the specified <paramref name="name"/> from the current injector.
|
---|
84 | /// </summary>
|
---|
85 | /// <remarks>Calls private method <see cref="DeleteVariableInfo"/> and <see cref="OperatorBase.RemoveVariable"/>
|
---|
86 | /// of base class <see cref="OperatorBase"/>.</remarks>
|
---|
87 | /// <param name="name">The name of the </param>
|
---|
88 | public override void RemoveVariable(string name) {
|
---|
89 | DeleteVariableInfo(name);
|
---|
90 | base.RemoveVariable(name);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /// <summary>
|
---|
94 | /// Adds the specified variables to the given <paramref name="scope"/> (and removes them first,
|
---|
95 | /// if they already exist in the current scope).
|
---|
96 | /// </summary>
|
---|
97 | /// <param name="scope">The scope where to inject the variables.</param>
|
---|
98 | /// <returns><c>null</c>.</returns>
|
---|
99 | public override IOperation Apply(IScope scope) {
|
---|
100 | foreach (IVariable variable in Variables) {
|
---|
101 | if (scope.GetVariable(variable.Name) != null)
|
---|
102 | scope.RemoveVariable(variable.Name);
|
---|
103 | scope.AddVariable((IVariable)variable.Clone());
|
---|
104 | }
|
---|
105 | return null;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /// <summary>
|
---|
109 | /// Creates a new instance of <see cref="VariableInjectorView"/> to display the current instance.
|
---|
110 | /// </summary>
|
---|
111 | /// <returns>The created view as <see cref="VariableInjectorView"/>.</returns>
|
---|
112 | public override IView CreateView() {
|
---|
113 | return new VariableInjectorView(this);
|
---|
114 | }
|
---|
115 |
|
---|
116 | private void CreateVariableInfo(IVariable variable) {
|
---|
117 | IVariableInfo info = new VariableInfo(Guid.NewGuid().ToString(), "Injected variable", variable.Value.GetType(), VariableKind.New);
|
---|
118 | info.ActualName = variable.Name;
|
---|
119 | AddVariableInfo(info);
|
---|
120 | variableVariableInfoTable.Add(variable, info);
|
---|
121 | variableInfoVariableTable.Add(info, variable);
|
---|
122 | info.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
|
---|
123 | variable.NameChanged += new EventHandler(Variable_NameChanged);
|
---|
124 | }
|
---|
125 | private void DeleteVariableInfo(string name) {
|
---|
126 | IVariable variable = GetVariable(name);
|
---|
127 | if (variable != null) {
|
---|
128 | IVariableInfo info = variableVariableInfoTable[variable];
|
---|
129 | RemoveVariableInfo(info.FormalName);
|
---|
130 | variableVariableInfoTable.Remove(variable);
|
---|
131 | variableInfoVariableTable.Remove(info);
|
---|
132 | info.ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);
|
---|
133 | variable.NameChanged -= new EventHandler(Variable_NameChanged);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | #region VariableInfo and Variable Events
|
---|
138 | private void VariableInfo_ActualNameChanged(object sender, EventArgs e) {
|
---|
139 | IVariableInfo info = (IVariableInfo)sender;
|
---|
140 | IVariable variable = variableInfoVariableTable[info];
|
---|
141 | variable.Name = info.ActualName;
|
---|
142 | }
|
---|
143 | private void Variable_NameChanged(object sender, EventArgs e) {
|
---|
144 | IVariable variable = (IVariable)sender;
|
---|
145 | IVariableInfo info = variableVariableInfoTable[variable];
|
---|
146 | info.ActualName = variable.Name;
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 | }
|
---|
150 | }
|
---|