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