Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/VariableInfo.cs @ 3136

Last change on this file since 3136 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 9.8 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  /// Class for storing meta-information about variables/parameters of an operator.
30  /// </summary>
31  public class VariableInfo : ItemBase, IVariableInfo {
32    private string myActualName;
33    /// <summary>
34    /// Gets or sets the actual name of the current instance.
35    /// </summary>
36    /// <remarks>Calls <see cref="OnActualNameChanged"/> in the setter.</remarks>
37    public string ActualName {
38      get { return myActualName; }
39      set {
40        if (myActualName != value) {
41          myActualName = value;
42          OnActualNameChanged();
43        }
44      }
45    }
46    private string myFormalName;
47    /// <summary>
48    /// Gets the formal name of the current instance.
49    /// </summary>
50    public string FormalName {
51      get { return myFormalName; }
52    }
53    private string myDescription;
54    /// <summary>
55    /// Gets the description of the current instance.
56    /// </summary>
57    public string Description {
58      get { return myDescription; }
59    }
60    private Type myDataType;
61    /// <summary>
62    /// Gets the data type of the parameter.
63    /// </summary>
64    public Type DataType {
65      get { return myDataType; }
66    }
67    private VariableKind myKind;
68    /// <summary>
69    /// Gets the kind of the parameter (input parameter, output parameter,...).
70    /// </summary>
71    public VariableKind Kind {
72      get { return myKind; }
73    }
74    private bool myLocal;
75    /// <summary>
76    /// Gets or sets a boolean value, whether the variable is a local one.
77    /// </summary>
78    /// <remarks>Calls <see cref="OnLocalChanged"/> in the setter.</remarks>
79    public bool Local {
80      get { return myLocal; }
81      set {
82        if (myLocal != value) {
83          myLocal = value;
84          OnLocalChanged();
85        }
86      }
87    }
88
89    /// <summary>
90    /// Initializes a new instance of <see cref="VariableInfo"/> with actual and formal name "Anonymous",
91    /// no description, a <c>null</c> data type and the <c>local</c> flag set to <c>false</c>. The type of
92    /// the variable is an input parameter.
93    /// </summary>
94    public VariableInfo() {
95      myActualName = "Anonymous";
96      myFormalName = "Anonymous";
97      myDescription = "";
98      myDataType = null;
99      myKind = VariableKind.In;
100      myLocal = false;
101    }
102    /// <summary>
103    /// Initializes a new instance of <see cref="VariableInfo"/> with the given parameters.
104    /// </summary>
105    /// <remarks>Calls <see cref="VariableInfo()"/>.<br/>
106    /// The formal name is assigned to the actual name, too.</remarks>
107    /// <param name="formalName">The formal name of the current instance.</param>
108    /// <param name="description">The description of the current instance.</param>
109    /// <param name="dataType">The data type of the parameter.</param>
110    /// <param name="kind">The type of the parameter.</param>
111    public VariableInfo(string formalName, string description, Type dataType, VariableKind kind)
112      : this() {
113      myActualName = formalName;
114      myFormalName = formalName;
115      myDescription = description;
116      myDataType = dataType;
117      myKind = kind;
118    }
119
120    /// <summary>
121    /// Creates a new instance of <see cref="VariableInfoView"/> to represent the current instance
122    /// visually.
123    /// </summary>
124    /// <returns>The created view as <see cref="VariableInfoView"/>.</returns>
125    public override IView CreateView() {
126      return new VariableInfoView(this);
127    }
128
129    /// <summary>
130    /// Clones the current instance (deep clone).
131    /// </summary>
132    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
133    /// <returns>The cloned object as <see cref="VariableInfo"/>.</returns>
134    public override object Clone(IDictionary<Guid, object> clonedObjects) {
135      VariableInfo clone = new VariableInfo();
136      clonedObjects.Add(Guid, clone);
137      clone.myActualName = ActualName;
138      clone.myFormalName = FormalName;
139      clone.myDescription = Description;
140      clone.myDataType = DataType;
141      clone.myKind = Kind;
142      clone.myLocal = Local;
143      return clone;
144    }
145
146    /// <inheritdoc/>
147    public event EventHandler ActualNameChanged;
148    /// <summary>
149    /// Fires a new <c>ActualNameChanged</c> event.
150    /// </summary>
151    protected virtual void OnActualNameChanged() {
152      if (ActualNameChanged != null)
153        ActualNameChanged(this, new EventArgs());
154    }
155    /// <inheritdoc />
156    public event EventHandler LocalChanged;
157    /// <summary>
158    /// Fires a new <c>LocalChanged</c> event.
159    /// </summary>
160    protected virtual void OnLocalChanged() {
161      if (LocalChanged != null)
162        LocalChanged(this, new EventArgs());
163    }
164
165    #region Persistence Methods
166    /// <summary>
167    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
168    /// </summary>
169    /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>. <br/>
170    /// A quick overview how the single elements of the current instance are saved:
171    /// <list type="bullet">
172    /// <item>
173    /// <term>Actual name: </term>
174    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>ActualName</c>.</description>
175    /// </item>
176    /// <item>
177    /// <term>Formal name: </term>
178    /// <description>Saves as <see cref="XmlAttribute"/> with tag name <c>FormalName</c>.</description>
179    /// </item>
180    /// <item>
181    /// <term>Description: </term>
182    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>Description</c>.</description>
183    /// </item>
184    /// <item><term>Data type: </term>
185    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>DataType</c>.</description>
186    /// </item>
187    /// <item>
188    /// <term>Kind: </term>
189    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>Kind</c>.</description>
190    /// </item>
191    /// <item>
192    /// <term>Local: </term>
193    /// <description>Saved as <see cref="XmlAttribute"/> with tag name <c>Local</c>.</description>
194    /// </item>
195    /// </list></remarks>
196    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
197    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
198    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
199    /// <returns>The saved <see cref="XmlNode"/>.</returns>
200    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
201      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
202      XmlAttribute actualNameAttribute = document.CreateAttribute("ActualName");
203      actualNameAttribute.Value = ActualName;
204      node.Attributes.Append(actualNameAttribute);
205
206      XmlAttribute formalNameAttribute = document.CreateAttribute("FormalName");
207      formalNameAttribute.Value = FormalName;
208      node.Attributes.Append(formalNameAttribute);
209
210      XmlAttribute descriptionAttribute = document.CreateAttribute("Description");
211      descriptionAttribute.Value = Description;
212      node.Attributes.Append(descriptionAttribute);
213
214      XmlAttribute dataTypeAttribute = document.CreateAttribute("DataType");
215      dataTypeAttribute.Value = PersistenceManager.BuildTypeString(DataType);
216      node.Attributes.Append(dataTypeAttribute);
217
218      XmlAttribute kindAttribute = document.CreateAttribute("Kind");
219      kindAttribute.Value = Kind.ToString();
220      node.Attributes.Append(kindAttribute);
221
222      XmlAttribute localAttribute = document.CreateAttribute("Local");
223      localAttribute.Value = Local.ToString();
224      node.Attributes.Append(localAttribute);
225
226      return node;
227    }
228    /// <summary>
229    /// Loads the persisted variable info from the specified <paramref name="node"/>.
230    /// </summary>
231    /// <remarks>See <see cref="GetXmlNode"/> for further information on how the variable info must be
232    /// saved. <br/>
233    /// Calls <see cref="StorableBase.Populate"/> of base class <see cref="ItemBase"/>.</remarks>
234    /// <param name="node">The <see cref="XmlNode"/> where the variable info is saved.</param>
235    /// <param name="restoredObjects">The dictionary of all already restored objects.
236    /// (Needed to avoid cycles.)</param>
237    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
238      base.Populate(node, restoredObjects);
239      myActualName = node.Attributes["ActualName"].Value;
240      myFormalName = node.Attributes["FormalName"].Value;
241      myDescription = node.Attributes["Description"].Value;
242      myDataType = Type.GetType(node.Attributes["DataType"].Value, true);
243      myKind = (VariableKind)Enum.Parse(typeof(VariableKind), node.Attributes["Kind"].Value);
244      myLocal = bool.Parse(node.Attributes["Local"].Value);
245    }
246    #endregion
247  }
248}
Note: See TracBrowser for help on using the repository browser.