Changeset 2653 for trunk/sources/HeuristicLab.Core/3.3/OperatorBase.cs
- Timestamp:
- 01/20/10 05:00:28 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/3.3/OperatorBase.cs
r2526 r2653 24 24 using System.Text; 25 25 using System.Xml; 26 using System.Drawing; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 using HeuristicLab.Common; 29 using HeuristicLab.Collections; 28 30 29 31 namespace HeuristicLab.Core { … … 31 33 /// The base class for all operators. 32 34 /// </summary> 33 public abstract class OperatorBase : ItemBase, IOperator { 35 [Item("OperatorBase", "Base class for operators.")] 36 public abstract class OperatorBase : NamedItemBase, IOperator { 37 public override Image ItemImage { 38 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; } 39 } 34 40 41 private ParameterCollection parameters; 35 42 [Storable] 36 private string myName; 37 /// <summary> 38 /// Gets or sets the name of the operator. 39 /// </summary> 40 /// <remarks>Calls <see cref="OnNameChanged"/> in the setter.</remarks> 41 public string Name { 42 get { return myName; } 43 set { 44 if (myName != value) { 45 myName = value; 46 OnNameChanged(); 47 } 43 protected ParameterCollection Parameters { 44 get { return parameters;} 45 private set { 46 if (parameters != null) parameters.Changed -= new ChangedEventHandler(Parameters_Changed); 47 parameters = value; 48 readOnlyParameters = null; 49 if (parameters != null) parameters.Changed += new ChangedEventHandler(Parameters_Changed); 48 50 } 49 51 } 50 /// <summary>51 /// Gets the description of the current operator.52 /// </summary>53 /// <remarks>Returns "No operator description available" if the method is not overriden.</remarks>54 public virtual string Description {55 get { return "No operator description available.";}52 private ReadOnlyObservableKeyedCollection<string, IParameter> readOnlyParameters; 53 IObservableKeyedCollection<string, IParameter> IOperator.Parameters { 54 get { 55 if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly(); 56 return readOnlyParameters; 57 } 56 58 } 59 57 60 /// <summary> 58 61 /// Flag whether the current instance has been canceled. 59 62 /// </summary> 60 pr otected bool myCanceled;63 private bool canceled; 61 64 /// <inheritdoc/> 62 p ublicbool Canceled {63 get { return myCanceled; }65 protected bool Canceled { 66 get { return canceled; } 64 67 } 65 68 66 69 [Storable] 67 private bool myBreakpoint;70 private bool breakpoint; 68 71 /// <inheritdoc/> 69 72 /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks> 70 73 public bool Breakpoint { 71 get { return myBreakpoint; }74 get { return breakpoint; } 72 75 set { 73 if (value != myBreakpoint) {74 myBreakpoint = value;76 if (value != breakpoint) { 77 breakpoint = value; 75 78 OnBreakpointChanged(); 76 }77 }78 }79 80 [Storable]81 private List<IOperator> mySubOperators;82 /// <summary>83 /// Gets a list of all suboperators.84 /// <note type="caution"> Returns the suboperators read-only!</note>85 /// </summary>86 public virtual IList<IOperator> SubOperators {87 get { return mySubOperators.AsReadOnly(); }88 }89 90 [Storable]91 private Dictionary<string, IVariableInfo> myVariableInfos;92 /// <inheritdoc/>93 public virtual ICollection<IVariableInfo> VariableInfos {94 get { return myVariableInfos.Values; }95 }96 97 private Dictionary<string, IVariable> myVariables;98 /// <inheritdoc/>99 public virtual ICollection<IVariable> Variables {100 get { return myVariables.Values; }101 }102 103 [Storable(Name="Variables")]104 private List<IVariable> VariablePersistence {105 get { return new List<IVariable>(myVariables.Values); }106 set {107 myVariables.Clear();108 foreach (IVariable var in value) {109 AddVariable(var);110 79 } 111 80 } … … 117 86 /// </summary> 118 87 protected OperatorBase() { 119 myName = this.GetType().Name; 120 myCanceled = false; 121 myBreakpoint = false; 122 mySubOperators = new List<IOperator>(); 123 myVariableInfos = new Dictionary<string, IVariableInfo>(); 124 myVariables = new Dictionary<string, IVariable>(); 88 name = ItemName; 89 Parameters = new ParameterCollection(); 90 readOnlyParameters = null; 91 canceled = false; 92 breakpoint = false; 125 93 } 126 94 … … 131 99 /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param> 132 100 /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns> 133 public override I Item Clone(ICloner cloner) {101 public override IDeepCloneable Clone(Cloner cloner) { 134 102 OperatorBase clone = (OperatorBase)base.Clone(cloner); 135 clone.myName = Name; 136 clone.mySubOperators.Clear(); 137 for (int i = 0; i < SubOperators.Count; i++) 138 clone.AddSubOperator((IOperator)cloner.Clone(SubOperators[i])); 139 clone.myVariableInfos.Clear(); 140 foreach (IVariableInfo variableInfo in myVariableInfos.Values) 141 clone.AddVariableInfo((IVariableInfo)cloner.Clone(variableInfo)); 142 clone.myVariables.Clear(); 143 foreach (IVariable variable in myVariables.Values) 144 clone.AddVariable((IVariable)cloner.Clone(variable)); 103 clone.Parameters = (ParameterCollection)cloner.Clone(parameters); 104 clone.canceled = canceled; 105 clone.breakpoint = breakpoint; 145 106 return clone; 146 107 } 147 108 148 #region SubOperator Methods149 /// <inheritdoc cref="HeuristicLab.Core.IOperator.AddSubOperator(HeuristicLab.Core.IOperator)"/>150 /// <param name="subOperator">The sub operator to add.</param>151 /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>152 public virtual void AddSubOperator(IOperator subOperator) {153 mySubOperators.Add(subOperator);154 OnSubOperatorAdded(subOperator, mySubOperators.Count - 1);155 }156 /// <inheritdoc cref="HeuristicLab.Core.IOperator.AddSubOperator(HeuristicLab.Core.IOperator, int)"/>157 /// <param name="subOperator">The sub operator to add.</param>158 /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>159 public virtual void AddSubOperator(IOperator subOperator, int index) {160 mySubOperators.Insert(index, subOperator);161 OnSubOperatorAdded(subOperator, index);162 }163 109 /// <inheritdoc/> 164 /// <remarks>Calls <see cref="OnSubOperatorRemoved"/>.</remarks> 165 public virtual void RemoveSubOperator(int index) { 166 IOperator op = mySubOperators[index]; 167 mySubOperators.RemoveAt(index); 168 OnSubOperatorRemoved(op, index); 169 } 170 #endregion 171 172 #region VariableInfo Methods 173 /// <inheritdoc/> 174 public virtual IVariableInfo GetVariableInfo(string formalName) { 175 IVariableInfo info; 176 if (myVariableInfos.TryGetValue(formalName, out info)) 177 return info; 178 else 179 return null; 180 } 181 /// <inheritdoc/> 182 /// <remarks>Calls <see cref="OnVariableInfoAdded"/>.</remarks> 183 public virtual void AddVariableInfo(IVariableInfo variableInfo) { 184 myVariableInfos.Add(variableInfo.FormalName, variableInfo); 185 OnVariableInfoAdded(variableInfo); 186 } 187 /// <inheritdoc/> 188 /// <remarks>Calls <see cref="OnVariableInfoRemoved"/>.</remarks> 189 public virtual void RemoveVariableInfo(string formalName) { 190 IVariableInfo variableInfo; 191 if (myVariableInfos.TryGetValue(formalName, out variableInfo)) { 192 myVariableInfos.Remove(formalName); 193 OnVariableInfoRemoved(variableInfo); 194 } 195 } 196 #endregion 197 198 #region Variable Methods 199 /// <inheritdoc/> 200 public virtual IVariable GetVariable(string name) { 201 IVariable variable; 202 if (myVariables.TryGetValue(name, out variable)) 203 return variable; 204 else 205 return null; 206 } 207 /// <inheritdoc/> 208 /// <remarks>Calls <see cref="OnVariableAdded"/> and adds <c>NameChanging</c> and <c>NameChanged</c> 209 /// event handlers.</remarks> 210 public virtual void AddVariable(IVariable variable) { 211 myVariables.Add(variable.Name, variable); 212 variable.NameChanging += new EventHandler<CancelEventArgs<string>>(Variable_NameChanging); 213 variable.NameChanged += new EventHandler(Variable_NameChanged); 214 OnVariableAdded(variable); 215 } 216 /// <inheritdoc/> 217 /// <remarks>Calls <see cref="OnVariableRemoved"/> and removes <c>NameChanging</c> and <c>NameChanged</c> 218 /// event handlers.</remarks> 219 public virtual void RemoveVariable(string name) { 220 IVariable variable; 221 if (myVariables.TryGetValue(name, out variable)) { 222 variable.NameChanging -= new EventHandler<CancelEventArgs<string>>(Variable_NameChanging); 223 variable.NameChanged -= new EventHandler(Variable_NameChanged); 224 myVariables.Remove(name); 225 OnVariableRemoved(variable); 226 } 227 } 228 private void Variable_NameChanging(object sender, CancelEventArgs<string> e) { 229 e.Cancel = myVariables.ContainsKey(e.Value); 230 } 231 private void Variable_NameChanged(object sender, EventArgs e) { 232 IVariable variable = (IVariable)sender; 233 string oldName = null; 234 foreach (KeyValuePair<string, IVariable> element in myVariables) { 235 if (element.Value == variable) 236 oldName = element.Key; 237 } 238 myVariables.Remove(oldName); 239 myVariables.Add(variable.Name, variable); 240 } 241 /// <inheritdoc cref="IOperator.GetVariableValue<T>(string, HeuristicLab.Core.IScope, bool)"/> 242 /// <remarks>Calls <see cref="GetVariableValue<T>(string, HeuristicLab.Core.IScope, bool, bool)"/> 243 /// with <c>throwOnError</c> set to <c>false</c>.</remarks> 244 public T GetVariableValue<T>(string formalName, IScope scope, bool recursiveLookup) where T : class, IItem { 245 return GetVariableValue<T>(formalName, scope, recursiveLookup, true); 246 } 247 /// <inheritdoc cref="IOperator.GetVariableValue<T>(string, HeuristicLab.Core.IScope, bool, bool)"/> 248 /// <remarks>Calls 249 /// <see cref="GetVariableValue(string, HeuristicLab.Core.IScope, bool, bool)"/>.</remarks> 250 public T GetVariableValue<T>(string formalName, IScope scope, bool recursiveLookup, bool throwOnError) where T : class, IItem { 251 return (T)GetVariableValue(formalName, scope, recursiveLookup, throwOnError); 252 } 253 /// <inheritdoc cref="IOperator.GetVariableValue(string, HeuristicLab.Core.IScope, bool)"/> 254 /// <remarks>Calls <see cref="GetVariableValue(string, HeuristicLab.Core.IScope, bool, bool)"/> 255 /// with <c>throwOnError</c> set to <c>false</c>.</remarks> 256 public IItem GetVariableValue(string formalName, IScope scope, bool recursiveLookup) { 257 return GetVariableValue(formalName, scope, recursiveLookup, true); 258 } 259 /// <inheritdoc cref="IOperator.GetVariableValue(string, HeuristicLab.Core.IScope, bool, bool)"/> 260 public virtual IItem GetVariableValue(string formalName, IScope scope, bool recursiveLookup, bool throwOnError) { 261 IVariableInfo info = GetVariableInfo(formalName); 262 if (info.Local) { 263 IVariable variable; 264 if (myVariables.TryGetValue(info.ActualName, out variable)) 265 return variable.Value; 266 else { 267 if (throwOnError) 268 throw new ArgumentException("Variable " + info.ActualName + " not found"); 269 else 270 return null; 271 } 272 } else { 273 return scope.GetVariableValue(formalName, recursiveLookup, throwOnError); 274 } 275 } 276 #endregion 277 /// <inheritdoc/> 278 public virtual IOperation Execute(IScope scope) { 279 myCanceled = false; 280 281 foreach (IVariableInfo variableInfo in VariableInfos) 282 scope.AddAlias(variableInfo.FormalName, variableInfo.ActualName); 283 284 IOperation next = Apply(scope); 285 286 foreach (IVariableInfo variableInfo in VariableInfos) 287 scope.RemoveAlias(variableInfo.FormalName); 288 110 public virtual ExecutionContextCollection Execute(ExecutionContext context) { 111 canceled = false; 112 ExecutionContextCollection next = Apply(context); 289 113 OnExecuted(); 290 114 return next; … … 293 117 /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks> 294 118 public virtual void Abort() { 295 myCanceled = true;119 canceled = true; 296 120 } 297 121 /// <summary> … … 300 124 /// <param name="scope">The scope where to execute the operator</param> 301 125 /// <returns><c>null</c>.</returns> 302 public virtual IOperation Apply(IScope scope) {303 return n ull;126 public virtual ExecutionContextCollection Apply(ExecutionContext context) { 127 return new ExecutionContextCollection(); 304 128 } 305 /// <inheritdoc/> 306 public event EventHandler NameChanged; 307 /// <summary> 308 /// Fires a new <c>NameChanged</c> event. 309 /// </summary> 310 protected virtual void OnNameChanged() { 311 if (NameChanged != null) { 312 NameChanged(this, new EventArgs()); 313 } 314 } 129 315 130 /// <inheritdoc/> 316 131 public event EventHandler BreakpointChanged; … … 322 137 BreakpointChanged(this, new EventArgs()); 323 138 } 324 } 325 /// <inheritdoc/> 326 public event EventHandler<EventArgs<IOperator, int>> SubOperatorAdded; 327 /// <summary> 328 /// Fires a new <c>SubOperatorAdded</c> event. 329 /// </summary> 330 /// <param name="subOperator">The sub operator that has been added.</param> 331 /// <param name="index">The position where the operator has been added.</param> 332 protected virtual void OnSubOperatorAdded(IOperator subOperator, int index) { 333 if (SubOperatorAdded != null) 334 SubOperatorAdded(this, new EventArgs<IOperator, int>(subOperator, index)); 335 } 336 /// <inheritdoc/> 337 public event EventHandler<EventArgs<IOperator, int>> SubOperatorRemoved; 338 /// <summary> 339 /// Fires a new <c>SubOperatorRemoved</c> event. 340 /// </summary> 341 /// <param name="subOperator">The sub operator that has been removed.</param> 342 /// <param name="index">The position where the operator has been removed.</param> 343 protected virtual void OnSubOperatorRemoved(IOperator subOperator, int index) { 344 if (SubOperatorRemoved != null) 345 SubOperatorRemoved(this, new EventArgs<IOperator, int>(subOperator, index)); 346 } 347 /// <inheritdoc/> 348 public event EventHandler<EventArgs<IVariableInfo>> VariableInfoAdded; 349 /// <summary> 350 /// Fires a new <c>VariableInfoAdded</c> event. 351 /// </summary> 352 /// <param name="variableInfo">The variable info that has been added.</param> 353 protected virtual void OnVariableInfoAdded(IVariableInfo variableInfo) { 354 if (VariableInfoAdded != null) 355 VariableInfoAdded(this, new EventArgs<IVariableInfo>(variableInfo)); 356 } 357 /// <inheritdoc/> 358 public event EventHandler<EventArgs<IVariableInfo>> VariableInfoRemoved; 359 /// <summary> 360 /// Fires a new <c>VariableInfoRemoved</c> event. 361 /// </summary> 362 /// <param name="variableInfo">The variable info that has been removed.</param> 363 protected virtual void OnVariableInfoRemoved(IVariableInfo variableInfo) { 364 if (VariableInfoRemoved != null) 365 VariableInfoRemoved(this, new EventArgs<IVariableInfo>(variableInfo)); 366 } 367 /// <inheritdoc/> 368 public event EventHandler<EventArgs<IVariable>> VariableAdded; 369 /// <summary> 370 /// Fires a new <c>VariableAdded</c> event. 371 /// </summary> 372 /// <param name="variable">The variable that has been added.</param> 373 protected virtual void OnVariableAdded(IVariable variable) { 374 if (VariableAdded != null) 375 VariableAdded(this, new EventArgs<IVariable>(variable)); 376 } 377 /// <inheritdoc/> 378 public event EventHandler<EventArgs<IVariable>> VariableRemoved; 379 /// <summary> 380 /// Fires a new <c>VariableRemoved</c> event. 381 /// </summary> 382 /// <param name="variable">The variable that has been removed</param> 383 protected virtual void OnVariableRemoved(IVariable variable) { 384 if (VariableRemoved != null) 385 VariableRemoved(this, new EventArgs<IVariable>(variable)); 139 OnChanged(); 386 140 } 387 141 /// <inheritdoc/> … … 395 149 } 396 150 } 151 152 private void Parameters_Changed(object sender, ChangedEventArgs e) { 153 OnChanged(e); 154 } 397 155 } 398 156 }
Note: See TracChangeset
for help on using the changeset viewer.