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