Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Core/CompositeOperation.cs @ 885

Last change on this file since 885 was 885, checked in by gkronber, 15 years ago

Refactored cloning in HL.Core, HL.Data and HL.Constraints

#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: 7.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  /// <summary>
29  /// Represents a class for operations consisting themselves of several operations.
30  /// Can also be executed in parallel.
31  /// </summary>
32  public class CompositeOperation : ItemBase, IOperation {
33    private bool myExecuteInParallel;
34
35    /// <summary>
36    /// Gets or sets the bool value, whether the operation should be executed in parallel or not. 
37    /// </summary>
38    public bool ExecuteInParallel {
39      get { return myExecuteInParallel; }
40      set { myExecuteInParallel = value; }
41    }
42    private List<IOperation> myOperations;
43    /// <summary>
44    /// Gets all current operations.
45    /// <note type="caution"> Operations are read-only!</note>
46    /// </summary>
47    public IList<IOperation> Operations {
48      get { return myOperations.AsReadOnly(); }
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="CompositeOperation"/>, the <see cref="ExecuteInParallel"/>
53    /// property set to <c>false</c>.
54    /// </summary>
55    public CompositeOperation() {
56      myOperations = new List<IOperation>();
57      myExecuteInParallel = false;
58    }
59
60    /// <summary>
61    /// Copy constructor to create a deep clone of a CompositeOperation instance.
62    /// <remarks>Calls <see cref="CompositeOperation(CompositeOperation original,
63    /// IDictionary<Guid, object> clonedObjects)"/></remarks> with an initially
64    /// empty dictionary of cloned objects to create the clone.
65    /// </summary>
66    /// <param name="original">The original instance to be cloned.</param>
67    public CompositeOperation(CompositeOperation original) : this(original, new Dictionary<Guid, object>()) { }
68
69    /// <summary>
70    /// Copy constructor to create a deep clone of a CompositeOperation instance
71    /// automatically reusing references of already cloned objects.
72    /// </summary>
73    /// <param name="original">The original instance to be cloned</param>
74    /// <param name="clonedObjects">Already cloned object references</param>
75    protected CompositeOperation(CompositeOperation original, IDictionary<Guid, object> clonedObjects)
76      : base(original, clonedObjects) {
77      this.myExecuteInParallel = original.ExecuteInParallel;
78      for (int i = 0; i < original.Operations.Count; i++)
79        this.AddOperation((IOperation)Auxiliary.Clone(original.Operations[i], clonedObjects));
80    }
81
82    /// <summary>
83    /// Adds an operation to the current list of operations.
84    /// </summary>
85    /// <param name="operation">The operation to add.</param>
86    public void AddOperation(IOperation operation) {
87      myOperations.Add(operation);
88    }
89    /// <summary>
90    /// Removes an operation from the current list.
91    /// </summary>
92    /// <param name="operation">The operation to remove.</param>
93    public void RemoveOperation(IOperation operation) {
94      myOperations.Remove(operation);
95    }
96
97    /// <summary>
98    /// Clones the current instance of <see cref="CompositeOperation"/> (deep clone).
99    /// </summary>
100    /// <remarks>All operations of the current instance are cloned, too (deep clone), with the
101    /// copy consturctor <see cref="HeuristicLab.Core.CompositeOperation(
102    /// CompositeOperation original, IDictionary<Guid, object> clonedObjects)"/>.</remarks>
103    /// <param name="clonedObjects">A dictionary of all already cloned objects. (Needed for referential integrity.)</param>
104    /// <returns>The cloned operation as <see cref="CompositeOperation"/>.</returns>
105    public override object Clone(IDictionary<Guid, object> clonedObjects) {
106      return new CompositeOperation(this, clonedObjects);
107    }
108
109    #region Persistence Methods
110    /// <summary>
111    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
112    /// </summary>
113    /// <remarks>Calls <see cref="HeuristicLab.Core.StorableBase.GetXmlNode"/> of base
114    /// class <see cref="ItemBase"/>.<br/>
115    /// The <see cref="ExecuteInParallel"/> property is saved as <see cref="XmlAttribute"/> with the
116    /// tag name <c>ExecuteInParallel</c>. A child node with tag name <c>Operations</c> is created where
117    /// all operations are saved as child nodes.</remarks>
118    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
119    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
120    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
121    /// <returns>The saved <see cref="XmlNode"/>.</returns>
122    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
123      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
124      XmlAttribute parallelAttribute = document.CreateAttribute("ExecuteInParallel");
125      parallelAttribute.Value = ExecuteInParallel.ToString();
126      node.Attributes.Append(parallelAttribute);
127
128      XmlNode operationsNode = document.CreateNode(XmlNodeType.Element, "Operations", null);
129      for (int i = 0; i < Operations.Count; i++)
130        operationsNode.AppendChild(PersistenceManager.Persist(Operations[i], document, persistedObjects));
131      node.AppendChild(operationsNode);
132      return node;
133    }
134    /// <summary>
135    /// Loads the persisted operation from the specified <paramref name="node"/>.
136    /// </summary>
137    /// <remarks>The <see cref="ExecuteInParallel"/> property must be saved as <see cref="XmlAttribute"/>
138    /// with the tag name <c>ExecuteInParallel</c>. <br/>
139    /// The single operations must be saved as child nodes of a node with tag name <c>Operations</c>,
140    /// being a child node of the current instance. <br/>
141    /// Calls <see cref="StorableBase.Populate"/> of base class <see cref="ItemBase"/>.</remarks>
142    /// <param name="node">The <see cref="XmlNode"/> where the operation is saved.</param>
143    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
144    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
145      base.Populate(node, restoredObjects);
146
147      myExecuteInParallel = bool.Parse(node.Attributes["ExecuteInParallel"].Value);
148
149      XmlNode operationsNode = node.SelectSingleNode("Operations");
150      for (int i = 0; i < operationsNode.ChildNodes.Count; i++)
151        AddOperation((IOperation)PersistenceManager.Restore(operationsNode.ChildNodes[i], restoredObjects));
152    }
153    #endregion
154  }
155}
Note: See TracBrowser for help on using the repository browser.