Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Core/3.2/Environment.cs @ 2553

Last change on this file since 2553 was 2027, checked in by swagner, 15 years ago

Refactoring of the operator architecture (#95)

File size: 4.7 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 Environment : ItemBase, IEnvironment {
29    private IEnvironment parent;
30    private Dictionary<string, string> aliases;
31
32    /// <summary>
33    /// Initializes a new instance of <see cref="Environment"/>.
34    /// </summary>
35    public Environment() {
36      aliases = new Dictionary<string, string>();
37    }
38    /// <summary>
39    /// Initializes a new instance of <see cref="Environment"/> with the given <paramref name="parent"/>.
40    /// </summary>
41    /// <param name="parnet">The parent environment.</param>
42    public Environment(IEnvironment parent)
43      : this() {
44      this.parent = parent;
45    }
46
47    /// <summary>
48    /// Clones the current instance.
49    /// </summary>
50    /// <remarks>The parent environment is cloned with the
51    /// <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of the <see cref="Auxiliary"/> class.</remarks>
52    /// <param name="clonedObjects">All already cloned objects. (Needed to avoid cycles.)</param>
53    /// <returns>The cloned object as <see cref="AtomicOperation"/>.</returns>
54    public override object Clone(IDictionary<Guid, object> clonedObjects) {
55      Environment clone = new Environment();
56      clonedObjects.Add(Guid, clone);
57      clone.parent = (IEnvironment)Auxiliary.Clone(parent, clonedObjects);
58      return clone;
59    }
60
61    public void AddAlias(string oldName, string newName) {
62      aliases.Add(oldName, newName);
63    }
64    public void RemoveAlias(string name) {
65      aliases.Remove(name);
66    }
67
68    public string TranslateName(string name) {
69      string translatedName = name;
70      if (aliases.ContainsKey(translatedName))
71        translatedName = aliases[translatedName];
72      if (parent != null)
73        translatedName = parent.TranslateName(translatedName);
74      return translatedName;
75    }
76
77    #region Persistence Methods
78
79    /// <summary>
80    ///  Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
81    /// </summary>
82    /// <remarks>Calls <see cref="HeuristicLab.Core.StorableBase.GetXmlNode"/> of base
83    /// class <see cref="ItemBase"/>. <br/>
84    /// The parent environment is saved as child node having the tag name <c>ParentEnvironment</c>.</remarks>
85    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
86    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
87    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
88    /// <returns>The saved <see cref="XmlNode"/>.</returns>
89    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
90      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
91      node.AppendChild(PersistenceManager.Persist("ParentEnvironment", parent, document, persistedObjects));
92      return node;
93    }
94    /// <summary>
95    /// Loads the persisted operation from the specified <paramref name="node"/>.
96    /// </summary>
97    /// <remarks>Calls <see cref="HeuristicLab.Core.StorableBase.Populate"/> of base class
98    /// <see cref="ItemBase"/>.<br/>
99    /// The parent environment must be saved as a child node with the tag name <c>ParentEnvironment</c>
100    /// (see <see cref="GetXmlNode"/>).</remarks>
101    /// <param name="node">The <see cref="XmlNode"/> where the operation is saved.</param>
102    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
103    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
104      base.Populate(node, restoredObjects);
105      parent = (IEnvironment)PersistenceManager.Restore(node.SelectSingleNode("ParentEnvironment"), restoredObjects);
106    }
107    #endregion
108  }
109}
Note: See TracBrowser for help on using the repository browser.