Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextReaderBranch/HeuristicLab.Operators/VariableInjector.cs @ 176

Last change on this file since 176 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.5 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  public class VariableInjector : OperatorBase {
30    private Dictionary<IVariable, IVariableInfo> variableVariableInfoTable;
31    private Dictionary<IVariableInfo, IVariable> variableInfoVariableTable;
32
33    public override string Description {
34      get { return @"TODO\r\nOperator description still missing ..."; }
35    }
36
37    public VariableInjector()
38      : base() {
39      variableVariableInfoTable = new Dictionary<IVariable, IVariableInfo>();
40      variableInfoVariableTable = new Dictionary<IVariableInfo, IVariable>();
41    }
42
43    public override object Clone(IDictionary<Guid, object> clonedObjects) {
44      VariableInjector clone = new VariableInjector();
45      clonedObjects.Add(Guid, clone);
46      clone.Name = Name;
47      foreach (IVariable variable in Variables)
48        clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
49      return clone;
50    }
51
52    public override void AddVariable(IVariable variable) {
53      base.AddVariable(variable);
54      CreateVariableInfo(variable);
55    }
56
57    public override void RemoveVariable(string name) {
58      DeleteVariableInfo(name);
59      base.RemoveVariable(name);
60    }
61
62    public override IOperation Apply(IScope scope) {
63      foreach (IVariable variable in Variables) {
64        if (scope.GetVariable(variable.Name) != null)
65          scope.RemoveVariable(variable.Name);
66        scope.AddVariable((IVariable)variable.Clone());
67      }
68      return null;
69    }
70
71    public override IView CreateView() {
72      return new VariableInjectorView(this);
73    }
74
75    private void CreateVariableInfo(IVariable variable) {
76      IVariableInfo info = new VariableInfo(Guid.NewGuid().ToString(), "Injected variable", variable.Value.GetType(), VariableKind.New);
77      info.ActualName = variable.Name;
78      AddVariableInfo(info);
79      variableVariableInfoTable.Add(variable, info);
80      variableInfoVariableTable.Add(info, variable);
81      info.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
82      variable.NameChanged += new EventHandler(Variable_NameChanged);
83    }
84    private void DeleteVariableInfo(string name) {
85      IVariable variable = GetVariable(name);
86      if (variable != null) {
87        IVariableInfo info = variableVariableInfoTable[variable];
88        RemoveVariableInfo(info.FormalName);
89        variableVariableInfoTable.Remove(variable);
90        variableInfoVariableTable.Remove(info);
91        info.ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);
92        variable.NameChanged -= new EventHandler(Variable_NameChanged);
93      }
94    }
95
96    #region VariableInfo and Variable Events
97    private void VariableInfo_ActualNameChanged(object sender, EventArgs e) {
98      IVariableInfo info = (IVariableInfo)sender;
99      IVariable variable = variableInfoVariableTable[info];
100      variable.Name = info.ActualName;
101    }
102    private void Variable_NameChanged(object sender, EventArgs e) {
103      IVariable variable = (IVariable)sender;
104      IVariableInfo info = variableVariableInfoTable[variable];
105      info.ActualName = variable.Name;
106    }
107    #endregion
108
109    #region Persistence Methods
110    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
111      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
112      // variable infos should not be persisted
113      XmlNode infosNode = node.SelectSingleNode("VariableInfos");
114      infosNode.RemoveAll();
115      return node;
116    }
117    #endregion
118  }
119}
Note: See TracBrowser for help on using the repository browser.