#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace HeuristicLab.Core { /// /// Represents a class for operations consisting themselves of several operations. /// Can also be executed in parallel. /// public class CompositeOperation : ItemBase, IOperation { private bool myExecuteInParallel; /// /// Gets or sets the bool value, whether the operation should be executed in parallel or not. /// public bool ExecuteInParallel { get { return myExecuteInParallel; } set { myExecuteInParallel = value; } } private List myOperations; /// /// Gets all current operations. /// Operations are read-only! /// public IList Operations { get { return myOperations.AsReadOnly(); } } /// /// Initializes a new instance of , the /// property set to false. /// public CompositeOperation() { myOperations = new List(); myExecuteInParallel = false; } /// /// Adds an operation to the current list of operations. /// /// The operation to add. public void AddOperation(IOperation operation) { myOperations.Add(operation); } /// /// Removes an operation from the current list. /// /// The operation to remove. public void RemoveOperation(IOperation operation) { myOperations.Remove(operation); } /// /// Clones the current instance of (deep clone). /// /// All operations of the current instance are cloned, too (deep clone), with the /// method of the class . /// A dictionary of all already cloned objects. (Needed to avoid cycles.) /// The cloned operation as . public override object Clone(IDictionary clonedObjects) { CompositeOperation clone = new CompositeOperation(); clonedObjects.Add(Guid, clone); clone.myExecuteInParallel = ExecuteInParallel; for (int i = 0; i < Operations.Count; i++) clone.AddOperation((IOperation)Auxiliary.Clone(Operations[i], clonedObjects)); return clone; } #region Persistence Methods /// /// Saves the current instance as in the specified . /// /// Calls of base /// class .
/// The property is saved as with the /// tag name ExecuteInParallel. A child node with tag name Operations is created where /// all operations are saved as child nodes.
/// The (tag)name of the . /// The where to save the data. /// The dictionary of all already persisted objects. (Needed to avoid cycles.) /// The saved . public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary persistedObjects) { XmlNode node = base.GetXmlNode(name, document, persistedObjects); XmlAttribute parallelAttribute = document.CreateAttribute("ExecuteInParallel"); parallelAttribute.Value = ExecuteInParallel.ToString(); node.Attributes.Append(parallelAttribute); XmlNode operationsNode = document.CreateNode(XmlNodeType.Element, "Operations", null); for (int i = 0; i < Operations.Count; i++) operationsNode.AppendChild(PersistenceManager.Persist(Operations[i], document, persistedObjects)); node.AppendChild(operationsNode); return node; } /// /// Loads the persisted operation from the specified . /// /// The property must be saved as /// with the tag name ExecuteInParallel.
/// The single operations must be saved as child nodes of a node with tag name Operations, /// being a child node of the current instance.
/// Calls of base class .
/// The where the operation is saved. /// A dictionary of all already restored objects. (Needed to avoid cycles.) public override void Populate(XmlNode node, IDictionary restoredObjects) { base.Populate(node, restoredObjects); myExecuteInParallel = bool.Parse(node.Attributes["ExecuteInParallel"].Value); XmlNode operationsNode = node.SelectSingleNode("Operations"); for (int i = 0; i < operationsNode.ChildNodes.Count; i++) AddOperation((IOperation)PersistenceManager.Restore(operationsNode.ChildNodes[i], restoredObjects)); } #endregion } }