Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Core/OperatorGroup.cs @ 836

Last change on this file since 836 was 836, checked in by gkronber, 15 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: 8.7 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  /// Representation of a group of operators (can also include subgroups).
30  /// </summary>
31  public class OperatorGroup : StorableBase, IOperatorGroup {
32    private string myName;
33    /// <summary>
34    /// Gets or sets the name of the current operator group.
35    /// </summary>
36    /// <remarks>Calls <see cref="OnNameChanged"/> in the setter.</remarks>
37    public string Name {
38      get { return myName; }
39      set {
40        if (myName != value) {
41          myName = value;
42          OnNameChanged();
43        }
44      }
45    }
46    private List<IOperatorGroup> mySubGroups;
47    /// <summary>
48    /// Gets all subgroups of the current instance.
49    /// <note type="caution"> The subgroups are returned read-only.</note>
50    /// </summary>
51    public ICollection<IOperatorGroup> SubGroups {
52      get { return mySubGroups.AsReadOnly(); }
53    }
54    private List<IOperator> myOperators;
55    /// <summary>
56    /// Gets all operators of the current instance.
57    /// <note type="caution"> The operators are returned read-only.</note>
58    /// </summary>
59    public ICollection<IOperator> Operators {
60      get { return myOperators.AsReadOnly(); }
61    }
62
63    /// <summary>
64    /// Initializes a new instance of <see cref="OperatorGroup"/> having "Anonymous" as name.
65    /// </summary>
66    public OperatorGroup() {
67      myName = "Anonymous";
68      mySubGroups = new List<IOperatorGroup>();
69      myOperators = new List<IOperator>();
70    }
71
72    /// <summary>
73    /// Copy constructor for deep cloning
74    /// </summary>
75    /// <param name="original"></param>
76    /// <param name="clonedObjects"></param>
77    protected OperatorGroup(OperatorGroup original, IDictionary<Guid, object> clonedObjects) : base(original, clonedObjects) {
78      this.myName = original.myName;
79      mySubGroups = new List<IOperatorGroup>();
80      myOperators = new List<IOperator>();
81      foreach (IOperatorGroup group in original.SubGroups)
82        AddSubGroup((IOperatorGroup)Auxiliary.Clone(group, clonedObjects));
83      foreach (IOperator op in original.Operators)
84        AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
85    }
86
87    /// <summary>
88    /// Clones the current instance (deep clone).
89    /// </summary>
90    /// <remarks>Deep clone with <see cref="Auxiliary.Clone"/> method of helper class
91    /// <see cref="Auxiliary"/>.</remarks>
92    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
93    /// <returns>The cloned object as <see cref="OperatorGroup"/>.</returns>
94    public override object Clone(IDictionary<Guid, object> clonedObjects) {
95      OperatorGroup clone = new OperatorGroup(this, clonedObjects);
96      return clone;
97    }
98
99    /// <summary>
100    /// Adds the given subgroup (<paramref name="group"/>) to the current instance.
101    /// </summary>
102    /// <param name="group">The subgroup to add.</param>
103    public virtual void AddSubGroup(IOperatorGroup group) {
104      mySubGroups.Add(group);
105    }
106    /// <summary>
107    /// Removes the given subgroup (<paramref name="group"/>) from the current instance.
108    /// </summary>
109    /// <param name="group">The subgroup to remove.</param>
110    public virtual void RemoveSubGroup(IOperatorGroup group) {
111      mySubGroups.Remove(group);
112    }
113    /// <summary>
114    /// Ads the given operator <paramref name="op"/> to the current instance.
115    /// </summary>
116    /// <param name="op">The operator to add.</param>
117    public virtual void AddOperator(IOperator op) {
118      myOperators.Add(op);
119    }
120    /// <summary>
121    /// Removes the given operator <paramref name="op"/> from the current instance.
122    /// </summary>
123    /// <param name="op">The operator to remove.</param>
124    public virtual void RemoveOperator(IOperator op) {
125      myOperators.Remove(op);
126    }
127
128    /// <summary>
129    /// Occurs when the name of the operator was changed.
130    /// </summary>
131    public event EventHandler NameChanged;
132    /// <summary>
133    /// Fires a new <c>NameChanged</c> event.
134    /// </summary>
135    protected virtual void OnNameChanged() {
136      if (NameChanged != null) {
137        NameChanged(this, new EventArgs());
138      }
139    }
140
141    #region Persistence Methods
142    /// <summary>
143    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
144    /// </summary>
145    /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>.<br/>
146    /// A quick overview how the single elements of the current instance are saved:
147    /// <list type="bullet">
148    /// <item>
149    /// <term>Name: </term>
150    /// <description>Saved as an <see cref="XmlAttribute"/> with attribute name <c>Name</c>.</description>
151    /// </item>
152    /// <item>
153    /// <term>Sub groups: </term>
154    /// <description>A child node is created with tag name <c>SubGroups</c>. Beyond this child node
155    /// all sub operator groups are saved as child nodes themselves.</description>
156    /// </item>
157    /// <item>
158    /// <term>Operators: </term>
159    /// <description>A child node is created with tag name <c>Operators</c>. Beyond this child node
160    /// all operators are saved as child nodes themselves.</description>
161    /// </item>
162    /// </list>
163    /// </remarks>
164    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
165    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
166    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
167    /// <returns>The saved <see cref="XmlNode"/>.</returns>
168    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
169      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
170      XmlAttribute nameAttribute = document.CreateAttribute("Name");
171      nameAttribute.Value = Name;
172      node.Attributes.Append(nameAttribute);
173      XmlNode subGroupsNode = document.CreateNode(XmlNodeType.Element, "SubGroups", null);
174      foreach (IOperatorGroup group in SubGroups)
175        subGroupsNode.AppendChild(PersistenceManager.Persist(group, document, persistedObjects));
176      node.AppendChild(subGroupsNode);
177      XmlNode operatorsNode = document.CreateNode(XmlNodeType.Element, "Operators", null);
178      foreach (IOperator op in Operators)
179        operatorsNode.AppendChild(PersistenceManager.Persist(op, document, persistedObjects));
180      node.AppendChild(operatorsNode);
181      return node;
182    }
183    /// <summary>
184    /// Loads the persisted operator group from the specified <paramref name="node"/>.
185    /// </summary>
186    /// <remarks>See <see cref="GetXmlNode"/> to get information about how the data must be saved. <br/>
187    /// Calls <see cref="StorableBase.Populate"/> of base class <see cref="StorableBase"/>.</remarks>
188    /// <param name="node">The <see cref="XmlNode"/> where the boolean value is saved.</param>
189    /// <param name="restoredObjects">The dictionary of all already restored objects.
190    /// (Needed to avoid cycles.)</param>
191    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
192      base.Populate(node, restoredObjects);
193      myName = node.Attributes["Name"].Value;
194      XmlNode subGroupsNode = node.SelectSingleNode("SubGroups");
195      foreach (XmlNode subGroupNode in subGroupsNode.ChildNodes)
196        AddSubGroup((IOperatorGroup)PersistenceManager.Restore(subGroupNode, restoredObjects));
197      XmlNode operatorsNode = node.SelectSingleNode("Operators");
198      foreach (XmlNode operatorNode in operatorsNode.ChildNodes)
199        AddOperator((IOperator)PersistenceManager.Restore(operatorNode, restoredObjects));
200    }
201    #endregion
202  }
203}
Note: See TracBrowser for help on using the repository browser.