Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Core/Variable.cs @ 17607

Last change on this file since 17607 was 533, checked in by gkronber, 16 years ago

fixed #266 (GUID is not restored (persistence) for Variables)

File size: 3.8 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;
26
27namespace 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;
75      clone.myValue = (IItem)Auxiliary.Clone(Value, clonedObjects);
76      return clone;
77    }
78
79    public override string ToString() {
80      return Name + ": " + ((Value == null) ? ("null") : (Value.ToString()));
81    }
82
83    public event EventHandler<NameChangingEventArgs> NameChanging;
84    protected virtual void OnNameChanging(NameChangingEventArgs e) {
85      if (NameChanging != null)
86        NameChanging(this, e);
87    }
88    public event EventHandler NameChanged;
89    protected virtual void OnNameChanged() {
90      if (NameChanged != null)
91        NameChanged(this, new EventArgs());
92      OnChanged();
93    }
94    public event EventHandler ValueChanged;
95    protected virtual void OnValueChanged() {
96      if (ValueChanged != null)
97        ValueChanged(this, new EventArgs());
98      OnChanged();
99    }
100
101    #region Persistence Methods
102    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
103      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
104      XmlAttribute nameAttribute = document.CreateAttribute("Name");
105      nameAttribute.Value = Name;
106      node.Attributes.Append(nameAttribute);
107      if (Value != null)
108        node.AppendChild(PersistenceManager.Persist("Value", Value, document, persistedObjects));
109      return node;
110    }
111    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
112      base.Populate(node, restoredObjects);
113      myName = node.Attributes["Name"].Value;
114      XmlNode valueNode = node.SelectSingleNode("Value");
115      if (valueNode != null)
116        myValue = (IItem)PersistenceManager.Restore(valueNode, restoredObjects);
117    }
118    #endregion
119  }
120}
Note: See TracBrowser for help on using the repository browser.