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