Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/VariableInjector.cs @ 2526

Last change on this file since 2526 was 2526, checked in by swagner, 14 years ago

Refactored cloning (#806)

File size: 6.4 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="cloner.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 IItem Clone(ICloner cloner) {
60      VariableInjector clone = new VariableInjector();
61      cloner.RegisterClonedObject(this, clone);
62      clone.Name = Name;
63      foreach (IVariable variable in Variables)
64        clone.AddVariable((IVariable)cloner.Clone(variable));
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    [Storable]
106    private KeyValuePair<Dictionary<IVariableInfo, IVariable>, Dictionary<IVariable, IVariableInfo>> VariableMappingPersistence {
107      get {
108        return new KeyValuePair<Dictionary<IVariableInfo, IVariable>, Dictionary<IVariable, IVariableInfo>>(
109          variableInfoVariableTable, variableVariableInfoTable);
110      }
111      set {
112        variableInfoVariableTable = value.Key;
113        variableVariableInfoTable = value.Value;
114        foreach (var pair in variableInfoVariableTable) {
115          pair.Key.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
116          pair.Value.NameChanged += new EventHandler(Variable_NameChanged);         
117        }
118      }
119    }
120
121    private void CreateVariableInfo(IVariable variable) {
122      IVariableInfo info = new VariableInfo(Guid.NewGuid().ToString(), "Injected variable", variable.Value.GetType(), VariableKind.New);
123      info.ActualName = variable.Name;
124      AddVariableInfo(info);
125      variableVariableInfoTable.Add(variable, info);
126      variableInfoVariableTable.Add(info, variable);
127      info.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
128      variable.NameChanged += new EventHandler(Variable_NameChanged);
129    }
130    private void DeleteVariableInfo(string name) {
131      IVariable variable = GetVariable(name);
132      if (variable != null) {
133        IVariableInfo info = variableVariableInfoTable[variable];
134        RemoveVariableInfo(info.FormalName);
135        variableVariableInfoTable.Remove(variable);
136        variableInfoVariableTable.Remove(info);
137        info.ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);
138        variable.NameChanged -= new EventHandler(Variable_NameChanged);
139      }
140    }
141
142    #region VariableInfo and Variable Events
143    private void VariableInfo_ActualNameChanged(object sender, EventArgs e) {
144      IVariableInfo info = (IVariableInfo)sender;
145      IVariable variable = variableInfoVariableTable[info];
146      variable.Name = info.ActualName;
147    }
148    private void Variable_NameChanged(object sender, EventArgs e) {
149      IVariable variable = (IVariable)sender;
150      IVariableInfo info = variableVariableInfoTable[variable];
151      info.ActualName = variable.Name;
152    }
153    #endregion
154  }
155}
Note: See TracBrowser for help on using the repository browser.