Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Core/3.2/Parameter.cs @ 2030

Last change on this file since 2030 was 2030, checked in by swagner, 15 years ago

Refactoring of the Operator Architecture (#95)

File size: 12.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;
26
27namespace HeuristicLab.Core {
28  /// <summary>
29  /// Represents a parameter of an operator.
30  /// </summary>
31  /// <typeparam name="T">The data type of the parameter, which has to be an <see cref="IItem"/>.</typeparam>
32  public class Parameter<T> : ItemBase, IParameter, IParamter<T> where T : class, IItem {
33    private string myActualName;
34    /// <summary>
35    /// Gets or sets the actual name of the current instance.
36    /// </summary>
37    /// <remarks>Calls <see cref="OnActualNameChanged"/> in the setter.</remarks>
38    public string ActualName {
39      get { return myActualName; }
40      set {
41        if (! myActualName.Equals(value)) {
42          myActualName = value;
43          OnActualNameChanged();
44        }
45      }
46    }
47    private string myFormalName;
48    /// <summary>
49    /// Gets the formal name of the current instance.
50    /// </summary>
51    public string FormalName {
52      get { return myFormalName; }
53    }
54    private string myDescription;
55    /// <summary>
56    /// Gets or sets the description of the current instance.
57    /// </summary>
58    /// <remarks>Calls <see cref="OnDescriptionChanged"/> in the setter.</remarks>
59    public string Description {
60      get { return myDescription; }
61      set {
62        if (! myDescription.Equals(value)) {
63          myDescription = value;
64          OnDescriptionChanged();
65        }
66      }
67    }
68    /// <summary>
69    /// Gets the data type of the parameter.
70    /// </summary>
71    public Type DataType {
72      get { return typeof(T); }
73    }
74    private ParameterType myType;
75    /// <summary>
76    /// Gets the type of the parameter (input, output, both).
77    /// </summary>
78    public ParameterType Type {
79      get { return myType; }
80    }
81    private bool myConstant;
82    /// <summary>
83    /// Gets or sets, if the parameter is constant (only valid for input parameters).
84    /// </summary>
85    /// <remarks>Calls <see cref="OnConstantChanged"/> in the setter.</remarks>
86    public bool Constant {
87      get { return myConstant; }
88      set {
89        if (Type != ParameterType.In) throw new InvalidOperationException("only input parameters can be constant");
90        if (myConstant != value) {
91          myConstant = value;
92          OnConstantChanged();
93        }
94      }
95    }
96    private bool myCached;
97    /// <summary>
98    /// Gets or sets, if the parameter should be cached by operators.
99    /// </summary>
100    /// <remarks>Calls <see cref="OnCachedChanged"/> in the setter.</remarks>
101    public bool Cached {
102      get { return myCached; }
103      set {
104        if (myCached != value) {
105          myCached = value;
106          OnCachedChanged();
107        }
108      }
109    }
110    private T myValue;
111    /// <summary>
112    /// Gets or sets the value of the parameter.
113    /// </summary>
114    public T Value {
115      get { return myValue; }
116      set {
117        if (! myValue.Equals(value)) {
118          myValue = value;
119          OnValueChanged();
120        }
121      }
122    }
123    /// <summary>
124    /// Gets or sets the value of the parameter.
125    /// </summary>
126    public IItem IParameter.Value {
127      get { return myValue; }
128      set { Value = (T)value; }
129    }
130
131    /// <summary>
132    /// Initializes a new instance of <see cref="Parameter"/> with name "Anonymous",
133    /// no description, and a <c>null</c> data type. The type of
134    /// the parameter is input.
135    /// </summary>
136    public Parameter() {
137      myActualName = "Anonymous";
138      myFormalName = "Anonymous";
139      myDescription = "";
140      myType = ParameterType.In;
141      myConstant = false;
142      myCached = false;
143      myValue = null;
144    }
145    /// <summary>
146    /// Initializes a new instance of <see cref="Parameter"/> with the given parameters.
147    /// </summary>
148    /// <remarks>Calls <see cref="Parameter()"/>.<br/>
149    /// The formal name is assigned to the actual name, too.</remarks>
150    /// <param name="formalName">The formal name of the current instance.</param>
151    /// <param name="description">The description of the current instance.</param>
152    /// <param name="kind">The type of the parameter.</param>
153    public Parameter(string formalName, string description, ParameterType type, bool constant, bool cached)
154      : this() {
155      myActualName = formalName;
156      myFormalName = formalName;
157      myDescription = description;
158      myType = type;
159      myConstant = constant;
160      myCached = cached;
161    }
162
163    /// <summary>
164    /// Creates a new instance of <see cref="ParameterView"/> to represent the current instance
165    /// visually.
166    /// </summary>
167    /// <returns>The created view as <see cref="ParameterView"/>.</returns>
168    public override IView CreateView() {
169      return new ParameterView(this);
170    }
171
172    /// <summary>
173    /// Clones the current instance (deep clone).
174    /// </summary>
175    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
176    /// <returns>The cloned object as <see cref="Parameter"/>.</returns>
177    public override object Clone(IDictionary<Guid, object> clonedObjects) {
178      Parameter<T> clone = (Parameter<T>)base.Clone(clonedObjects);
179      clonedObjects.Add(Guid, clone);
180      clone.myActualName = ActualName;
181      clone.myFormalName = FormalName;
182      clone.myDescription = Description;
183      clone.myType = Type;
184      clone.myConstant = Constant;
185      clone.myCached = Cached;
186      clone.myValue = (T)Auxiliary.Clone(Value, clonedObjects);
187      return clone;
188    }
189
190    /// <inheritdoc/>
191    public event EventHandler ActualNameChanged;
192    /// <summary>
193    /// Fires a new <c>ActualNameChanged</c> event.
194    /// </summary>
195    protected virtual void OnActualNameChanged() {
196      if (ActualNameChanged != null)
197        ActualNameChanged(this, new EventArgs());
198    }
199    /// <inheritdoc />
200    public event EventHandler DescriptionChanged;
201    /// <summary>
202    /// Fires a new <c>DescriptionChanged</c> event.
203    /// </summary>
204    protected virtual void OnDescriptionChanged() {
205      if (DescriptionChanged != null)
206        DescriptionChanged(this, new EventArgs());
207    }
208    public event EventHandler ConstantChanged;
209    /// <summary>
210    /// Fires a new <c>ConstantChanged</c> event.
211    /// </summary>
212    protected virtual void OnConstantChanged() {
213      if (ConstantChanged != null)
214        ConstantChanged(this, new EventArgs());
215    }
216    public event EventHandler CachedChanged;
217    /// <summary>
218    /// Fires a new <c>CachedChanged</c> event.
219    /// </summary>
220    protected virtual void OnCachedChanged() {
221      if (CachedChanged != null)
222        CachedChanged(this, new EventArgs());
223    }
224    public event EventHandler ValueChanged;
225    /// <summary>
226    /// Fires a new <c>ValueChanged</c> event.
227    /// </summary>
228    protected virtual void OnValueChanged() {
229      if (ValueChanged != null)
230        ValueChanged(this, new EventArgs());
231    }
232
233    #region Persistence Methods
234    /// <summary>
235    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
236    /// </summary>
237    /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>. <br/>
238    /// A quick overview how the single elements of the current instance are saved:
239    /// <list type="bullet">
240    /// <item>
241    /// <term>ActualName: </term>
242    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>ActualName</c>.</description>
243    /// </item>
244    /// <item>
245    /// <term>FormalName: </term>
246    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>FormalName</c>.</description>
247    /// </item>
248    /// <item>
249    /// <term>Description: </term>
250    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>Description</c>.</description>
251    /// </item>
252    /// <item>
253    /// <term>Type: </term>
254    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>ParameterType</c>.</description>
255    /// </item>
256    /// <item>
257    /// <term>Constant: </term>
258    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>Constant</c>.</description>
259    /// </item>
260    /// <item>
261    /// <term>Cached: </term>
262    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>Cached</c>.</description>
263    /// </item>
264    /// <item>
265    /// <term>Value: </term>
266    /// <description>Saved as child node with tag name <c>Value</c>.</description>
267    /// </item>
268    /// </list></remarks>
269    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
270    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
271    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
272    /// <returns>The saved <see cref="XmlNode"/>.</returns>
273    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
274      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
275      XmlAttribute actualNameAttribute = document.CreateAttribute("ActualName");
276      actualNameAttribute.Value = ActualName;
277      node.Attributes.Append(actualNameAttribute);
278
279      XmlAttribute formalNameAttribute = document.CreateAttribute("FormalName");
280      formalNameAttribute.Value = FormalName;
281      node.Attributes.Append(formalNameAttribute);
282
283      XmlAttribute descriptionAttribute = document.CreateAttribute("Description");
284      descriptionAttribute.Value = Description;
285      node.Attributes.Append(descriptionAttribute);
286
287      XmlAttribute typeAttribute = document.CreateAttribute("ParameterType");
288      typeAttribute.Value = Type.ToString();
289      node.Attributes.Append(typeAttribute);
290
291      XmlAttribute constantAttribute = document.CreateAttribute("Constant");
292      constantAttribute.Value = Constant.ToString();
293      node.Attributes.Append(constantAttribute);
294
295      XmlAttribute cachedAttribute = document.CreateAttribute("Cached");
296      cachedAttribute.Value = Cached.ToString();
297      node.Attributes.Append(cachedAttribute);
298
299      if (myValue != null)
300        node.AppendChild(PersistenceManager.Persist("Value", Value, document, persistedObjects));
301
302      return node;
303    }
304    /// <summary>
305    /// Loads the persisted parameter from the specified <paramref name="node"/>.
306    /// </summary>
307    /// <remarks>See <see cref="GetXmlNode"/> for further information on how the parameter must be
308    /// saved. <br/>
309    /// Calls <see cref="StorableBase.Populate"/> of base class <see cref="ItemBase"/>.</remarks>
310    /// <param name="node">The <see cref="XmlNode"/> where the parameter is saved.</param>
311    /// <param name="restoredObjects">The dictionary of all already restored objects.
312    /// (Needed to avoid cycles.)</param>
313    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
314      base.Populate(node, restoredObjects);
315      myActualName = node.Attributes["ActualName"].Value;
316      myFormalName = node.Attributes["FormalName"].Value;
317      myDescription = node.Attributes["Description"].Value;
318      myType = (ParameterType)Enum.Parse(typeof(ParameterType), node.Attributes["ParameterType"].Value);
319      myConstant = bool.Parse(node.Attributes["Constant"].Value);
320      myCached = bool.Parse(node.Attributes["Cached"].Value);
321
322      XmlNode valueNode = node.SelectSingleNode("Value");
323      if (valueNode != null)
324        myValue = (T)PersistenceManager.Restore(valueNode, restoredObjects);
325    }
326    #endregion
327  }
328}
Note: See TracBrowser for help on using the repository browser.