Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Core/AtomicOperation.cs @ 846

Last change on this file since 846 was 836, checked in by gkronber, 16 years ago

worked on #285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

File size: 5.2 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  /// <summary>
29  /// Represents a single operation with one operator and one scope.
30  /// </summary>
31  public class AtomicOperation : ItemBase, IOperation {
32    private IOperator myOperator;
33    /// <summary>
34    /// Gets the current operator as <see cref="IOperator"/>.
35    /// </summary>
36    public IOperator Operator {
37      get { return myOperator; }
38    }
39    private IScope myScope; 
40    /// <summary>
41    /// Gets the current scope as <see cref="IScope"/>.
42    /// </summary>
43    public IScope Scope {
44      get { return myScope; }
45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="AtomicOperation"/>.
49    /// Empty constructor is needed for the persistence mechanism.
50    /// </summary>
51    public AtomicOperation() { }
52    /// <summary>
53    /// Initializes a new instance of <see cref="AtomicOperation"/> with the given <paramref name="op"/>
54    /// and the given <paramref name="scope"/>.
55    /// </summary>
56    /// <param name="op">The operator to assign.</param>
57    /// <param name="scope">The scope to assign.</param>
58    public AtomicOperation(IOperator op, IScope scope) {
59      myOperator = op;
60      myScope = scope;
61    }
62
63    /// <summary>
64    /// Clones the current instance.
65    /// </summary>
66    /// <remarks>The operator and the scope objects are cloned with the
67    /// <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of the <see cref="Auxiliary"/> class.</remarks>
68    /// <param name="clonedObjects">All already cloned objects. (Needed to avoid cycles.)</param>
69    /// <returns>The cloned object as <see cref="AtomicOperation"/>.</returns>
70    public override object Clone(IDictionary<Guid, object> clonedObjects) {
71      AtomicOperation clone = new AtomicOperation();
72      clonedObjects.Add(Guid, clone);
73      clone.myOperator = (IOperator)Auxiliary.Clone(Operator, clonedObjects);
74      clone.myScope = (IScope)Auxiliary.Clone(Scope, clonedObjects);
75      return clone;
76    }
77
78    #region Persistence Methods
79
80    /// <summary>
81    ///  Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
82    /// </summary>
83    /// <remarks>Calls <see cref="HeuristicLab.Core.StorableBase.GetXmlNode"/> of base
84    /// class <see cref="ItemBase"/>. <br/>
85    /// The operator is saved as child node having the tag name <c>Operator</c>.<br/>
86    /// The scope is also saved as a child node having the tag name <c>Scope</c>.</remarks>
87    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
88    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
89    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
90    /// <returns>The saved <see cref="XmlNode"/>.</returns>
91    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
92      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
93      node.AppendChild(PersistenceManager.Persist("Operator", Operator, document, persistedObjects));
94      node.AppendChild(PersistenceManager.Persist("Scope", Scope, document, persistedObjects));
95      return node;
96    }
97    /// <summary>
98    /// Loads the persisted operation from the specified <paramref name="node"/>.
99    /// </summary>
100    /// <remarks>Calls <see cref="HeuristicLab.Core.StorableBase.Populate"/> of base class
101    /// <see cref="ItemBase"/>.<br/>
102    /// The operator must be saved as a child node with the tag name <c>Operator</c>, also the scope must
103    /// be saved as a child node having the tag name <c>Scope</c> (see <see cref="GetXmlNode"/>).</remarks>
104    /// <param name="node">The <see cref="XmlNode"/> where the operation is saved.</param>
105    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
106    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
107      base.Populate(node, restoredObjects);
108      myOperator = (IOperator)PersistenceManager.Restore(node.SelectSingleNode("Operator"), restoredObjects);
109      myScope = (IScope)PersistenceManager.Restore(node.SelectSingleNode("Scope"), restoredObjects);
110    }
111    #endregion
112  }
113}
Note: See TracBrowser for help on using the repository browser.