Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Operators/3.3/VariableInjector.cs @ 2491

Last change on this file since 2491 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

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