Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/Variable.cs @ 1009

Last change on this file since 1009 was 801, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Operators namespace (#331)

File size: 7.2 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 variable of an operator having a name and a value.
30  /// </summary>
31  public class Variable : ItemBase, IVariable {
32    private string myName;
33    /// <inheritdoc/>
34    /// <remarks>Calls <see cref="OnNameChanging"/> and also <see cref="OnNameChanged"/>
35    /// eventually in the setter.</remarks>
36    public string Name {
37      get { return myName; }
38      set {
39        if (!myName.Equals(value)) {
40          NameChangingEventArgs e = new NameChangingEventArgs(value);
41          OnNameChanging(e);
42          if (!e.Cancel) {
43            myName = value;
44            OnNameChanged();
45          }
46        }
47      }
48    }
49    private IItem myValue;
50    /// <inheritdoc/>
51    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
52    public IItem Value {
53      get { return myValue; }
54      set {
55        if (myValue != value) {
56          myValue = value;
57          OnValueChanged();
58        }
59      }
60    }
61   
62    /// <summary>
63    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
64    /// and value <c>null</c>.
65    /// </summary>
66    public Variable() {
67      myName = "Anonymous";
68      myValue = null;
69    }
70    /// <summary>
71    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
72    /// and the specified <paramref name="value"/>.
73    /// </summary>
74    /// <param name="name">The name of the current instance.</param>
75    /// <param name="value">The value of the current instance.</param>
76    public Variable(string name, IItem value) {
77      myName = name;
78      myValue = value;
79    }
80
81    /// <inheritdoc cref="IVariable.GetValue&lt;T&gt;"/>
82    public T GetValue<T>() where T : class, IItem {
83      return (T)Value;
84    }
85
86    /// <summary>
87    /// Creates a new instance of <see cref="VariableView"/> to represent the current instance visually.
88    /// </summary>
89    /// <returns>The created view as <see cref="VariableView"/>.</returns>
90    public override IView CreateView() {
91      return new VariableView(this);
92    }
93
94    /// <summary>
95    /// Clones the current instance (deep clone).
96    /// </summary>
97    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
98    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
99    public override object Clone(IDictionary<Guid, object> clonedObjects) {
100      Variable clone = new Variable();
101      clonedObjects.Add(Guid, clone);
102      clone.myName = Name;
103      if (Value != null)
104        clone.myValue = (IItem)Auxiliary.Clone(Value, clonedObjects);
105      return clone;
106    }
107
108    /// <summary>
109    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
110    /// </summary>
111    /// <returns>The current instance as a string.</returns>
112    public override string ToString() {
113      return Name + ": " + ((Value == null) ? ("null") : (Value.ToString()));
114    }
115
116    /// <inheritdoc/>
117    public event EventHandler<NameChangingEventArgs> NameChanging;
118    /// <summary>
119    /// Fires a new <c>NameChanging</c> event.
120    /// </summary>
121    /// <param name="e">The event arguments of the changing.</param>
122    protected virtual void OnNameChanging(NameChangingEventArgs e) {
123      if (NameChanging != null)
124        NameChanging(this, e);
125    }
126    /// <inheritdoc/>
127    public event EventHandler NameChanged;
128    /// <summary>
129    /// Fires a new <c>NameChanged</c> event.
130    /// </summary>
131    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
132    protected virtual void OnNameChanged() {
133      if (NameChanged != null)
134        NameChanged(this, new EventArgs());
135      OnChanged();
136    }
137    /// <inheritdoc/>
138    public event EventHandler ValueChanged;
139    /// <summary>
140    /// Fires a new <c>ValueChanged</c> even.
141    /// </summary>
142    protected virtual void OnValueChanged() {
143      if (ValueChanged != null)
144        ValueChanged(this, new EventArgs());
145      OnChanged();
146    }
147
148    #region Persistence Methods
149    /// <summary>
150    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
151    /// </summary>
152    /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>.<br/>
153    /// The name of the current instance is saved as an <see cref="XmlAttribute"/> with the
154    /// tag name <c>Name</c>, the value is saved as child node with the tag name <c>Value</c>.</remarks>
155    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
156    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
157    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
158    /// <returns>The saved <see cref="XmlNode"/>.</returns>
159    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
160      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
161      XmlAttribute nameAttribute = document.CreateAttribute("Name");
162      nameAttribute.Value = Name;
163      node.Attributes.Append(nameAttribute);
164      if (Value != null)
165        node.AppendChild(PersistenceManager.Persist("Value", Value, document, persistedObjects));
166      return node;
167    }
168    /// <summary>
169    /// Loads the persisted variable from the specified <paramref name="node"/>.
170    /// </summary>
171    /// <remarks>See <see cref="GetXmlNode"/> to get information on how the variable must be saved.<br/>
172    /// Calls <see cref="StorableBase.Populate"/> of base class <see cref="ItemBase"/>.</remarks>
173    /// <param name="node">The <see cref="XmlNode"/> where the variable is saved.</param>
174    /// <param name="restoredObjects">The dictionary of all already restored objects.
175    /// (Needed to avoid cycles.)</param>
176    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
177      base.Populate(node, restoredObjects);
178      myName = node.Attributes["Name"].Value;
179      XmlNode valueNode = node.SelectSingleNode("Value");
180      if (valueNode != null)
181        myValue = (IItem)PersistenceManager.Restore(valueNode, restoredObjects);
182    }
183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.