Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlReaderWriterBranch/HeuristicLab.Core/Variable.cs @ 175

Last change on this file since 175 was 125, checked in by gkronber, 17 years ago

created a branch that combines the XmlReader and XmlWriter branches

File size: 4.4 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 Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
112      base.Persist(name, writer, persistedObjects);
113      writer.WriteAttributeString("Name", Name);
114      if(Value != null)
115        PersistenceManager.Persist("Value", Value, writer, persistedObjects);
116    }
117    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
118    //  myName = node.Attributes["Name"].Value;
119    //  XmlNode valueNode = node.SelectSingleNode("Value");
120    //  if(valueNode != null)
121    //    myValue = (IItem)PersistenceManager.Restore(valueNode, restoredObjects);
122    //}
123    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
124      myName = reader["Name"];
125      reader.Read();
126      if(reader.IsStartElement())
127        myValue = (IItem)PersistenceManager.Restore(reader, "Value", restoredObjects);
128    }
129    #endregion
130  }
131}
Note: See TracBrowser for help on using the repository browser.