[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;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Core {
|
---|
| 28 | public class Variable : ItemBase, IVariable {
|
---|
| 29 | private string myName;
|
---|
| 30 | public string Name {
|
---|
| 31 | get { return myName; }
|
---|
| 32 | set {
|
---|
| 33 | if (!myName.Equals(value)) {
|
---|
| 34 | NameChangingEventArgs e = new NameChangingEventArgs(value);
|
---|
| 35 | OnNameChanging(e);
|
---|
| 36 | if (!e.Cancel) {
|
---|
| 37 | myName = value;
|
---|
| 38 | OnNameChanged();
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 | private IItem myValue;
|
---|
| 44 | public IItem Value {
|
---|
| 45 | get { return myValue; }
|
---|
| 46 | set {
|
---|
| 47 | if (myValue != value) {
|
---|
| 48 | myValue = value;
|
---|
| 49 | OnValueChanged();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public Variable() {
|
---|
| 55 | myName = "Anonymous";
|
---|
| 56 | myValue = null;
|
---|
| 57 | }
|
---|
| 58 | public Variable(string name, IItem value) {
|
---|
| 59 | myName = name;
|
---|
| 60 | myValue = value;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public T GetValue<T>() where T : class, IItem {
|
---|
| 64 | return (T)Value;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public override IView CreateView() {
|
---|
| 68 | return new VariableView(this);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 72 | Variable clone = new Variable();
|
---|
| 73 | clonedObjects.Add(Guid, clone);
|
---|
| 74 | clone.myName = Name;
|
---|
[701] | 75 | if (Value != null)
|
---|
| 76 | clone.myValue = (IItem)Auxiliary.Clone(Value, clonedObjects);
|
---|
[2] | 77 | return clone;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public override string ToString() {
|
---|
| 81 | return Name + ": " + ((Value == null) ? ("null") : (Value.ToString()));
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public event EventHandler<NameChangingEventArgs> NameChanging;
|
---|
| 85 | protected virtual void OnNameChanging(NameChangingEventArgs e) {
|
---|
| 86 | if (NameChanging != null)
|
---|
| 87 | NameChanging(this, e);
|
---|
| 88 | }
|
---|
| 89 | public event EventHandler NameChanged;
|
---|
| 90 | protected virtual void OnNameChanged() {
|
---|
| 91 | if (NameChanged != null)
|
---|
| 92 | NameChanged(this, new EventArgs());
|
---|
| 93 | OnChanged();
|
---|
| 94 | }
|
---|
| 95 | public event EventHandler ValueChanged;
|
---|
| 96 | protected virtual void OnValueChanged() {
|
---|
| 97 | if (ValueChanged != null)
|
---|
| 98 | ValueChanged(this, new EventArgs());
|
---|
| 99 | OnChanged();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | #region Persistence Methods
|
---|
| 103 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
|
---|
| 104 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 105 | XmlAttribute nameAttribute = document.CreateAttribute("Name");
|
---|
| 106 | nameAttribute.Value = Name;
|
---|
| 107 | node.Attributes.Append(nameAttribute);
|
---|
| 108 | if (Value != null)
|
---|
| 109 | node.AppendChild(PersistenceManager.Persist("Value", Value, document, persistedObjects));
|
---|
| 110 | return node;
|
---|
| 111 | }
|
---|
| 112 | public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
[533] | 113 | base.Populate(node, restoredObjects);
|
---|
[2] | 114 | myName = node.Attributes["Name"].Value;
|
---|
| 115 | XmlNode valueNode = node.SelectSingleNode("Value");
|
---|
| 116 | if (valueNode != null)
|
---|
| 117 | myValue = (IItem)PersistenceManager.Restore(valueNode, restoredObjects);
|
---|
| 118 | }
|
---|
| 119 | #endregion
|
---|
| 120 | }
|
---|
| 121 | }
|
---|