Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Core/3.3/Variable.cs @ 2927

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

Refactoring of the operator architecture (#95)

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          if (! OnNameChanging(value).Cancel) {
44            myName = value;
45            OnNameChanged();
46          }
47        }
48      }
49    }
50
51    [Storable]
52    private IItem myValue;
53    /// <inheritdoc/>
54    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
55    public IItem Value {
56      get { return myValue; }
57      set {
58        if (myValue != value) {
59          myValue = value;
60          OnValueChanged();
61        }
62      }
63    }
64
65    /// <summary>
66    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
67    /// and value <c>null</c>.
68    /// </summary>
69    public Variable() {
70      myName = "Anonymous";
71      myValue = null;
72    }
73    /// <summary>
74    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
75    /// and the specified <paramref name="value"/>.
76    /// </summary>
77    /// <param name="name">The name of the current instance.</param>
78    /// <param name="value">The value of the current instance.</param>
79    public Variable(string name, IItem value) {
80      myName = name;
81      myValue = value;
82    }
83
84    /// <inheritdoc cref="IVariable.GetValue&lt;T&gt;"/>
85    public T GetValue<T>() where T : class, IItem {
86      return (T)Value;
87    }
88
89    /// <summary>
90    /// Creates a new instance of <see cref="VariableView"/> to represent the current instance visually.
91    /// </summary>
92    /// <returns>The created view as <see cref="VariableView"/>.</returns>
93    public override IView CreateView() {
94      return new VariableView(this);
95    }
96
97    /// <summary>
98    /// Clones the current instance (deep clone).
99    /// </summary>
100    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
101    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
102    public override ICloneable Clone(ICloner cloner) {
103      Variable clone = new Variable();
104      cloner.AddClonedObject(this, clone);
105      clone.myName = Name;
106      if (Value != null)
107        clone.myValue = (IItem)cloner.Clone(Value);
108      return clone;
109    }
110
111    /// <summary>
112    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
113    /// </summary>
114    /// <returns>The current instance as a string.</returns>
115    public override string ToString() {
116      return Name + ": " + ((Value == null) ? ("null") : (Value.ToString()));
117    }
118
119    /// <inheritdoc/>
120    public event EventHandler<CancelEventArgs<string>> NameChanging;
121    /// <summary>
122    /// Fires a new <c>NameChanging</c> event.
123    /// </summary>
124    /// <param name="e">The event arguments of the changing.</param>
125    protected virtual CancelEventArgs<string> OnNameChanging(string name) {
126      CancelEventArgs<string> eventArgs = new CancelEventArgs<string>(name);
127      if (NameChanging != null)
128        NameChanging(this, eventArgs);
129      return eventArgs;
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.