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 {
|
---|
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 | /// Clones the current instance (deep clone).
|
---|
74 | /// </summary>
|
---|
75 | /// <remarks>Deep clone with <see cref="Auxiliary.Clone"/> method of helper class
|
---|
76 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
77 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
78 | /// <returns>The cloned object as <see cref="OperatorGroup"/>.</returns>
|
---|
79 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
80 | OperatorGroup clone = (OperatorGroup)base.Clone(clonedObjects);
|
---|
81 | clone.myName = Name;
|
---|
82 | foreach (IOperatorGroup group in SubGroups)
|
---|
83 | clone.AddSubGroup((IOperatorGroup)Auxiliary.Clone(group, clonedObjects));
|
---|
84 | foreach (IOperator op in Operators)
|
---|
85 | clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
|
---|
86 | return clone;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /// <summary>
|
---|
90 | /// Adds the given subgroup (<paramref name="group"/>) to the current instance.
|
---|
91 | /// </summary>
|
---|
92 | /// <param name="group">The subgroup to add.</param>
|
---|
93 | public virtual void AddSubGroup(IOperatorGroup group) {
|
---|
94 | mySubGroups.Add(group);
|
---|
95 | }
|
---|
96 | /// <summary>
|
---|
97 | /// Removes the given subgroup (<paramref name="group"/>) from the current instance.
|
---|
98 | /// </summary>
|
---|
99 | /// <param name="group">The subgroup to remove.</param>
|
---|
100 | public virtual void RemoveSubGroup(IOperatorGroup group) {
|
---|
101 | mySubGroups.Remove(group);
|
---|
102 | }
|
---|
103 | /// <summary>
|
---|
104 | /// Ads the given operator <paramref name="op"/> to the current instance.
|
---|
105 | /// </summary>
|
---|
106 | /// <param name="op">The operator to add.</param>
|
---|
107 | public virtual void AddOperator(IOperator op) {
|
---|
108 | myOperators.Add(op);
|
---|
109 | }
|
---|
110 | /// <summary>
|
---|
111 | /// Removes the given operator <paramref name="op"/> from the current instance.
|
---|
112 | /// </summary>
|
---|
113 | /// <param name="op">The operator to remove.</param>
|
---|
114 | public virtual void RemoveOperator(IOperator op) {
|
---|
115 | myOperators.Remove(op);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /// <summary>
|
---|
119 | /// Occurs when the name of the operator was changed.
|
---|
120 | /// </summary>
|
---|
121 | public event EventHandler NameChanged;
|
---|
122 | /// <summary>
|
---|
123 | /// Fires a new <c>NameChanged</c> event.
|
---|
124 | /// </summary>
|
---|
125 | protected virtual void OnNameChanged() {
|
---|
126 | if (NameChanged != null) {
|
---|
127 | NameChanged(this, new EventArgs());
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | #region Persistence Methods
|
---|
132 | /// <summary>
|
---|
133 | /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
|
---|
134 | /// </summary>
|
---|
135 | /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>.<br/>
|
---|
136 | /// A quick overview how the single elements of the current instance are saved:
|
---|
137 | /// <list type="bullet">
|
---|
138 | /// <item>
|
---|
139 | /// <term>Name: </term>
|
---|
140 | /// <description>Saved as an <see cref="XmlAttribute"/> with attribute name <c>Name</c>.</description>
|
---|
141 | /// </item>
|
---|
142 | /// <item>
|
---|
143 | /// <term>Sub groups: </term>
|
---|
144 | /// <description>A child node is created with tag name <c>SubGroups</c>. Beyond this child node
|
---|
145 | /// all sub operator groups are saved as child nodes themselves.</description>
|
---|
146 | /// </item>
|
---|
147 | /// <item>
|
---|
148 | /// <term>Operators: </term>
|
---|
149 | /// <description>A child node is created with tag name <c>Operators</c>. Beyond this child node
|
---|
150 | /// all operators are saved as child nodes themselves.</description>
|
---|
151 | /// </item>
|
---|
152 | /// </list>
|
---|
153 | /// </remarks>
|
---|
154 | /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
|
---|
155 | /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
|
---|
156 | /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
|
---|
157 | /// <returns>The saved <see cref="XmlNode"/>.</returns>
|
---|
158 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
|
---|
159 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
160 | XmlAttribute nameAttribute = document.CreateAttribute("Name");
|
---|
161 | nameAttribute.Value = Name;
|
---|
162 | node.Attributes.Append(nameAttribute);
|
---|
163 | XmlNode subGroupsNode = document.CreateNode(XmlNodeType.Element, "SubGroups", null);
|
---|
164 | foreach (IOperatorGroup group in SubGroups)
|
---|
165 | subGroupsNode.AppendChild(PersistenceManager.Persist(group, document, persistedObjects));
|
---|
166 | node.AppendChild(subGroupsNode);
|
---|
167 | XmlNode operatorsNode = document.CreateNode(XmlNodeType.Element, "Operators", null);
|
---|
168 | foreach (IOperator op in Operators)
|
---|
169 | operatorsNode.AppendChild(PersistenceManager.Persist(op, document, persistedObjects));
|
---|
170 | node.AppendChild(operatorsNode);
|
---|
171 | return node;
|
---|
172 | }
|
---|
173 | /// <summary>
|
---|
174 | /// Loads the persisted operator group from the specified <paramref name="node"/>.
|
---|
175 | /// </summary>
|
---|
176 | /// <remarks>See <see cref="GetXmlNode"/> to get information about how the data must be saved. <br/>
|
---|
177 | /// Calls <see cref="StorableBase.Populate"/> of base class <see cref="StorableBase"/>.</remarks>
|
---|
178 | /// <param name="node">The <see cref="XmlNode"/> where the boolean value is saved.</param>
|
---|
179 | /// <param name="restoredObjects">The dictionary of all already restored objects.
|
---|
180 | /// (Needed to avoid cycles.)</param>
|
---|
181 | public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
182 | base.Populate(node, restoredObjects);
|
---|
183 | myName = node.Attributes["Name"].Value;
|
---|
184 | XmlNode subGroupsNode = node.SelectSingleNode("SubGroups");
|
---|
185 | foreach (XmlNode subGroupNode in subGroupsNode.ChildNodes)
|
---|
186 | AddSubGroup((IOperatorGroup)PersistenceManager.Restore(subGroupNode, restoredObjects));
|
---|
187 | XmlNode operatorsNode = node.SelectSingleNode("Operators");
|
---|
188 | foreach (XmlNode operatorNode in operatorsNode.ChildNodes)
|
---|
189 | AddOperator((IOperator)PersistenceManager.Restore(operatorNode, restoredObjects));
|
---|
190 | }
|
---|
191 | #endregion
|
---|
192 | }
|
---|
193 | }
|
---|