Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.2/VariableInjector.cs @ 2311

Last change on this file since 2311 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

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