#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 { /// /// Representation of a group of operators (can also include subgroups). /// public class OperatorGroup : StorableBase, IOperatorGroup { private string myName; /// /// Gets or sets the name of the current operator group. /// /// Calls in the setter. public string Name { get { return myName; } set { if (myName != value) { myName = value; OnNameChanged(); } } } private List mySubGroups; /// /// Gets all subgroups of the current instance. /// The subgroups are returned read-only. /// public ICollection SubGroups { get { return mySubGroups.AsReadOnly(); } } private List myOperators; /// /// Gets all operators of the current instance. /// The operators are returned read-only. /// public ICollection Operators { get { return myOperators.AsReadOnly(); } } /// /// Initializes a new instance of having "Anonymous" as name. /// public OperatorGroup() { myName = "Anonymous"; mySubGroups = new List(); myOperators = new List(); } /// /// Clones the current instance (deep clone). /// /// Deep clone with method of helper class /// . /// Dictionary of all already cloned objects. (Needed to avoid cycles.) /// The cloned object as . public override object Clone(IDictionary clonedObjects) { OperatorGroup clone = (OperatorGroup)base.Clone(clonedObjects); clone.myName = Name; foreach (IOperatorGroup group in SubGroups) clone.AddSubGroup((IOperatorGroup)Auxiliary.Clone(group, clonedObjects)); foreach (IOperator op in Operators) clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects)); return clone; } /// /// Adds the given subgroup () to the current instance. /// /// The subgroup to add. public virtual void AddSubGroup(IOperatorGroup group) { mySubGroups.Add(group); } /// /// Removes the given subgroup () from the current instance. /// /// The subgroup to remove. public virtual void RemoveSubGroup(IOperatorGroup group) { mySubGroups.Remove(group); } /// /// Ads the given operator to the current instance. /// /// The operator to add. public virtual void AddOperator(IOperator op) { myOperators.Add(op); } /// /// Removes the given operator from the current instance. /// /// The operator to remove. public virtual void RemoveOperator(IOperator op) { myOperators.Remove(op); } /// /// Occurs when the name of the operator was changed. /// public event EventHandler NameChanged; /// /// Fires a new NameChanged event. /// protected virtual void OnNameChanged() { if (NameChanged != null) { NameChanged(this, new EventArgs()); } } #region Persistence Methods /// /// Saves the current instance as in the specified . /// /// Calls of base class .
/// A quick overview how the single elements of the current instance are saved: /// /// /// Name: /// Saved as an with attribute name Name. /// /// /// Sub groups: /// A child node is created with tag name SubGroups. Beyond this child node /// all sub operator groups are saved as child nodes themselves. /// /// /// Operators: /// A child node is created with tag name Operators. Beyond this child node /// all operators are saved as child nodes themselves. /// /// ///
/// 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 nameAttribute = document.CreateAttribute("Name"); nameAttribute.Value = Name; node.Attributes.Append(nameAttribute); XmlNode subGroupsNode = document.CreateNode(XmlNodeType.Element, "SubGroups", null); foreach (IOperatorGroup group in SubGroups) subGroupsNode.AppendChild(PersistenceManager.Persist(group, document, persistedObjects)); node.AppendChild(subGroupsNode); XmlNode operatorsNode = document.CreateNode(XmlNodeType.Element, "Operators", null); foreach (IOperator op in Operators) operatorsNode.AppendChild(PersistenceManager.Persist(op, document, persistedObjects)); node.AppendChild(operatorsNode); return node; } /// /// Loads the persisted operator group from the specified . /// /// See to get information about how the data must be saved.
/// Calls of base class .
/// The where the boolean value is saved. /// The dictionary of all already restored objects. /// (Needed to avoid cycles.) public override void Populate(XmlNode node, IDictionary restoredObjects) { base.Populate(node, restoredObjects); myName = node.Attributes["Name"].Value; XmlNode subGroupsNode = node.SelectSingleNode("SubGroups"); foreach (XmlNode subGroupNode in subGroupsNode.ChildNodes) AddSubGroup((IOperatorGroup)PersistenceManager.Restore(subGroupNode, restoredObjects)); XmlNode operatorsNode = node.SelectSingleNode("Operators"); foreach (XmlNode operatorNode in operatorsNode.ChildNodes) AddOperator((IOperator)PersistenceManager.Restore(operatorNode, restoredObjects)); } #endregion } }