Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Core/3.3/Variable.cs @ 3043

Last change on this file since 3043 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 5.1 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;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace 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
34    [Storable]
35    private string myName;
36    /// <inheritdoc/>
37    /// <remarks>Calls <see cref="OnNameChanging"/> and also <see cref="OnNameChanged"/>
38    /// eventually in the setter.</remarks>
39    public string Name {
40      get { return myName; }
41      set {
42        if (!myName.Equals(value)) {
43          NameChangingEventArgs e = new NameChangingEventArgs(value);
44          OnNameChanging(e);
45          if (!e.Cancel) {
46            myName = value;
47            OnNameChanged();
48          }
49        }
50      }
51    }
52
53    [Storable]
54    private IItem myValue;
55    /// <inheritdoc/>
56    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
57    public IItem Value {
58      get { return myValue; }
59      set {
60        if (myValue != value) {
61          myValue = value;
62          OnValueChanged();
63        }
64      }
65    }
66
67    /// <summary>
68    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
69    /// and value <c>null</c>.
70    /// </summary>
71    public Variable() {
72      myName = "Anonymous";
73      myValue = null;
74    }
75    /// <summary>
76    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
77    /// and the specified <paramref name="value"/>.
78    /// </summary>
79    /// <param name="name">The name of the current instance.</param>
80    /// <param name="value">The value of the current instance.</param>
81    public Variable(string name, IItem value) {
82      myName = name;
83      myValue = value;
84    }
85
86    /// <inheritdoc cref="IVariable.GetValue&lt;T&gt;"/>
87    public T GetValue<T>() where T : class, IItem {
88      return (T)Value;
89    }
90
91    /// <summary>
92    /// Creates a new instance of <see cref="VariableView"/> to represent the current instance visually.
93    /// </summary>
94    /// <returns>The created view as <see cref="VariableView"/>.</returns>
95    public override IView CreateView() {
96      return new VariableView(this);
97    }
98
99    /// <summary>
100    /// Clones the current instance (deep clone).
101    /// </summary>
102    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
103    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
104    public override object Clone(IDictionary<Guid, object> clonedObjects) {
105      Variable clone = new Variable();
106      clonedObjects.Add(Guid, clone);
107      clone.myName = Name;
108      if (Value != null)
109        clone.myValue = (IItem)Auxiliary.Clone(Value, clonedObjects);
110      return clone;
111    }
112
113    /// <summary>
114    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
115    /// </summary>
116    /// <returns>The current instance as a string.</returns>
117    public override string ToString() {
118      return Name + ": " + ((Value == null) ? ("null") : (Value.ToString()));
119    }
120
121    /// <inheritdoc/>
122    public event EventHandler<NameChangingEventArgs> NameChanging;
123    /// <summary>
124    /// Fires a new <c>NameChanging</c> event.
125    /// </summary>
126    /// <param name="e">The event arguments of the changing.</param>
127    protected virtual void OnNameChanging(NameChangingEventArgs e) {
128      if (NameChanging != null)
129        NameChanging(this, e);
130    }
131    /// <inheritdoc/>
132    public event EventHandler NameChanged;
133    /// <summary>
134    /// Fires a new <c>NameChanged</c> event.
135    /// </summary>
136    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
137    protected virtual void OnNameChanged() {
138      if (NameChanged != null)
139        NameChanged(this, new EventArgs());
140      OnChanged();
141    }
142    /// <inheritdoc/>
143    public event EventHandler ValueChanged;
144    /// <summary>
145    /// Fires a new <c>ValueChanged</c> even.
146    /// </summary>
147    protected virtual void OnValueChanged() {
148      if (ValueChanged != null)
149        ValueChanged(this, new EventArgs());
150      OnChanged();
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.