Changeset 2027 for branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/VariableInjector.cs
- Timestamp:
- 06/06/09 02:09:35 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/VariableInjector.cs
r1530 r2027 30 30 /// Class to inject local variables into the scope. 31 31 /// </summary> 32 public class VariableInjector : OperatorBase { 33 private Dictionary<IVariable, IVariableInfo> variableVariableInfoTable; 34 private Dictionary<IVariableInfo, IVariable> variableInfoVariableTable; 32 public class VariableInjector : OperatorBase, IVariablesContainer { 33 private Dictionary<string, IVariable> myVariables; 34 /// <summary> 35 /// Gets a collection of all variables of the current operator. 36 /// </summary> 37 public virtual ICollection<IVariable> Variables { 38 get { return myVariables.Values; } 39 } 35 40 36 41 /// <inheritdoc select="summary"/> 37 42 public override string Description { 38 get { return @"TODO\r\nOperator description still missing ..."; } 43 get { return @"Injects new variable into a scope."; } 44 } 45 46 /// <summary> 47 /// Overrides the Parameters property so that a parameter is returned for each injected variable. 48 /// </summary> 49 public override ICollection<IParameter> Parameters { 50 get { 51 List<IParameter> parameters = new List<IParameter>(); 52 foreach (IVariable variable in Variables) 53 parameters.Add(new Parameter(variable.Name, "Injected variable.", variable.Value.GetType(), ParameterType.Out)); 54 return parameters; 55 } 39 56 } 40 57 … … 44 61 public VariableInjector() 45 62 : base() { 46 variableVariableInfoTable = new Dictionary<IVariable, IVariableInfo>(); 47 variableInfoVariableTable = new Dictionary<IVariableInfo, IVariable>(); 63 myVariables = new Dictionary<string, IVariable>(); 48 64 } 49 65 … … 56 72 /// <returns>The cloned object as <see cref="VariableInjector"/>.</returns> 57 73 public override object Clone(IDictionary<Guid, object> clonedObjects) { 58 VariableInjector clone = new VariableInjector(); 59 clonedObjects.Add(Guid, clone); 60 clone.Name = Name; 74 VariableInjector clone = (VariableInjector)base.Clone(clonedObjects); 61 75 foreach (IVariable variable in Variables) 62 76 clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects)); … … 65 79 66 80 /// <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 81 /// Adds the specified variables to the given <paramref name="scope"/> (and removes them first, 90 82 /// if they already exist in the current scope). … … 92 84 /// <param name="scope">The scope where to inject the variables.</param> 93 85 /// <returns><c>null</c>.</returns> 94 public override IOperation Apply(I Scope scope) {86 public override IOperation Apply(IEnvironment env, IScope scope) { 95 87 foreach (IVariable variable in Variables) { 96 88 if (scope.GetVariable(variable.Name) != null) … … 109 101 } 110 102 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); 103 /// <summary> 104 /// Gets a variable with the given <paramref name="name"/>. 105 /// </summary> 106 /// <param name="name">The name of the variable.</param> 107 /// <returns>The variable with the specified name.</returns> 108 public virtual IVariable GetVariable(string name) { 109 IVariable variable; 110 if (myVariables.TryGetValue(name, out variable)) 111 return variable; 112 else 113 return null; 114 } 115 /// <summary> 116 /// Adds the specified <paramref name="variable"/> to the current instance. 117 /// </summary> 118 /// <param name="variable">The variable to add.</param> 119 public virtual void AddVariable(IVariable variable) { 120 myVariables.Add(variable.Name, variable); 121 variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging); 118 122 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); 123 OnVariableAdded(variable); 124 } 125 /// <summary> 126 /// Deletes the variable with the specified <paramref name="name"/>. 127 /// </summary> 128 /// <param name="name">The name of the variable to delete.</param> 129 public virtual void RemoveVariable(string name) { 130 IVariable variable; 131 if (myVariables.TryGetValue(name, out variable)) { 132 variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging); 128 133 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; 134 myVariables.Remove(name); 135 OnVariableRemoved(variable); 136 } 137 } 138 private void Variable_NameChanging(object sender, NameChangingEventArgs e) { 139 e.Cancel = myVariables.ContainsKey(e.Name); 137 140 } 138 141 private void Variable_NameChanged(object sender, EventArgs e) { 139 142 IVariable variable = (IVariable)sender; 140 IVariableInfo info = variableVariableInfoTable[variable]; 141 info.ActualName = variable.Name; 142 } 143 #endregion 143 string oldName = null; 144 foreach (KeyValuePair<string, IVariable> element in myVariables) { 145 if (element.Value == variable) 146 oldName = element.Key; 147 } 148 myVariables.Remove(oldName); 149 myVariables.Add(variable.Name, variable); 150 } 151 152 /// <summary> 153 /// Occurs when a variable has been added. 154 /// </summary> 155 public event EventHandler<VariableEventArgs> VariableAdded; 156 /// <summary> 157 /// Fires a new <c>VariableAdded</c> event. 158 /// </summary> 159 /// <param name="variable">The variable that has been added.</param> 160 protected virtual void OnVariableAdded(IVariable variable) { 161 if (VariableAdded != null) 162 VariableAdded(this, new VariableEventArgs(variable)); 163 } 164 /// <summary> 165 /// Occurs when a variable has been deleted. 166 /// </summary> 167 public event EventHandler<VariableEventArgs> VariableRemoved; 168 /// <summary> 169 /// Fires a new <c>VariableRemoved</c> event. 170 /// </summary> 171 /// <param name="variable">The variable that has been removed</param> 172 protected virtual void OnVariableRemoved(IVariable variable) { 173 if (VariableRemoved != null) 174 VariableRemoved(this, new VariableEventArgs(variable)); 175 } 144 176 145 177 #region Persistence Methods 146 178 /// <summary> 147 179 /// 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> 180 /// </summary> 181 /// <remarks> 182 /// Calls <see cref="OperatorBase.GetXmlNode"/> of base class <see cref="OperatorBase"/>. 183 /// <br/>A quick overview how the single elements of the current instance are saved: 184 /// <list type="bullet"> 185 /// <item> 186 /// <term>Variables: </term> 187 /// <description>Saved as child node with tag name <c>Variables</c>. All variables are themselves 188 /// saved as child nodes.</description> 189 /// </item> 190 /// </list> 191 /// </remarks> 151 192 /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param> 152 193 /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param> 153 194 /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param> 154 195 /// <returns>The saved <see cref="XmlNode"/>.</returns> 155 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {196 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 156 197 XmlNode node = base.GetXmlNode(name, document, persistedObjects); 157 // variable infos should not be persisted 158 XmlNode infosNode = node.SelectSingleNode("VariableInfos"); 159 infosNode.RemoveAll(); 198 XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null); 199 foreach (IVariable variable in myVariables.Values) 200 variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects)); 201 node.AppendChild(variablesNode); 160 202 return node; 203 } 204 /// <summary> 205 /// Loads the persisted variable injector from the specified <paramref name="node"/>. 206 /// </summary> 207 /// <remarks>Calls <see cref="OperatorBase.Populate"/> of base class 208 /// <see cref="OperatorBase"/>. 209 /// For informations how the different elements must be saved please see <see cref="GetXmlNode"/>.</remarks> 210 /// <param name="node">The <see cref="XmlNode"/> where the operation is saved.</param> 211 /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param> 212 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 213 base.Populate(node, restoredObjects); 214 XmlNode variablesNode = node.SelectSingleNode("Variables"); 215 foreach (XmlNode variableNode in variablesNode.ChildNodes) 216 AddVariable((IVariable)PersistenceManager.Restore(variableNode, restoredObjects)); 161 217 } 162 218 #endregion
Note: See TracChangeset
for help on using the changeset viewer.