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 | using HeuristicLab.Common;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Core {
|
---|
29 | /// <summary>
|
---|
30 | /// Represents a graph of operators.
|
---|
31 | /// </summary>
|
---|
32 | public class OperatorGraph : ItemBase, IOperatorGraph {
|
---|
33 | private IDictionary<Guid, IOperator> myOperators;
|
---|
34 | /// <summary>
|
---|
35 | /// Gets all operators of the current instance.
|
---|
36 | /// </summary>
|
---|
37 | public ICollection<IOperator> Operators {
|
---|
38 | get { return myOperators.Values; }
|
---|
39 | }
|
---|
40 | private IOperator myInitialOperator;
|
---|
41 | /// <summary>
|
---|
42 | /// Gets or sets the initial operator (the starting one).
|
---|
43 | /// </summary>
|
---|
44 | /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks>
|
---|
45 | public IOperator InitialOperator {
|
---|
46 | get { return myInitialOperator; }
|
---|
47 | set {
|
---|
48 | if (myInitialOperator != value) {
|
---|
49 | myInitialOperator = value;
|
---|
50 | OnInitialOperatorChanged();
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Initializes a new instance of <see cref="OperatorGraph"/>.
|
---|
57 | /// </summary>
|
---|
58 | public OperatorGraph() {
|
---|
59 | myOperators = new Dictionary<Guid, IOperator>();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Creates a new instance of <see cref="OperatorGraphView"/> to represent the current instance
|
---|
64 | /// visually.
|
---|
65 | /// </summary>
|
---|
66 | /// <returns>The created view as <see cref="OperatorGraphView"/>.</returns>
|
---|
67 | public override IView CreateView() {
|
---|
68 | return new OperatorGraphView(this);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Clones the current instance (deep clone).
|
---|
73 | /// </summary>
|
---|
74 | /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
|
---|
75 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
76 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
77 | /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
|
---|
78 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
79 | OperatorGraph clone = new OperatorGraph();
|
---|
80 | clonedObjects.Add(Guid, clone);
|
---|
81 | foreach (IOperator op in Operators)
|
---|
82 | clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
|
---|
83 | if (InitialOperator != null)
|
---|
84 | clone.myInitialOperator = (IOperator)Auxiliary.Clone(InitialOperator, clonedObjects);
|
---|
85 | return clone;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /// <inheritdoc/>
|
---|
89 | /// <remarks>Calls <see cref="OnOperatorAdded"/>.</remarks>
|
---|
90 | public void AddOperator(IOperator op) {
|
---|
91 | if (!myOperators.ContainsKey(op.Guid)) {
|
---|
92 | myOperators.Add(op.Guid, op);
|
---|
93 | OnOperatorAdded(op);
|
---|
94 |
|
---|
95 | foreach (IOperator subOperator in op.SubOperators)
|
---|
96 | AddOperator(subOperator);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | /// <inheritdoc/>
|
---|
100 | /// <remarks>Calls <see cref="OnOperatorRemoved"/>.</remarks>
|
---|
101 | public void RemoveOperator(Guid guid) {
|
---|
102 | IOperator op = GetOperator(guid);
|
---|
103 | if (op != null) {
|
---|
104 | foreach (IOperator o in Operators) {
|
---|
105 | int i = 0;
|
---|
106 | while (i < o.SubOperators.Count) {
|
---|
107 | if (o.SubOperators[i] == op)
|
---|
108 | o.RemoveSubOperator(i);
|
---|
109 | else
|
---|
110 | i++;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | if (InitialOperator == op)
|
---|
114 | InitialOperator = null;
|
---|
115 | myOperators.Remove(op.Guid);
|
---|
116 | OnOperatorRemoved(op);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | /// <inheritdoc/>
|
---|
120 | public IOperator GetOperator(Guid guid) {
|
---|
121 | IOperator op;
|
---|
122 | if (myOperators.TryGetValue(guid, out op))
|
---|
123 | return op;
|
---|
124 | else
|
---|
125 | return null;
|
---|
126 | }
|
---|
127 | /// <inheritdoc/>
|
---|
128 | public void Clear() {
|
---|
129 | Guid[] guids = new Guid[Operators.Count];
|
---|
130 | int i = 0;
|
---|
131 | foreach (IOperator op in Operators) {
|
---|
132 | guids[i] = op.Guid;
|
---|
133 | i++;
|
---|
134 | }
|
---|
135 | for (int j = 0; j < guids.Length; j++)
|
---|
136 | RemoveOperator(guids[j]);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /// <inheritdoc/>
|
---|
140 | public event EventHandler<EventArgs<IOperator>> OperatorAdded;
|
---|
141 | /// <summary>
|
---|
142 | /// Fires a new <c>OperatorAdded</c> event.
|
---|
143 | /// </summary>
|
---|
144 | /// <param name="op">The operator that has been added.</param>
|
---|
145 | protected virtual void OnOperatorAdded(IOperator op) {
|
---|
146 | if (OperatorAdded != null)
|
---|
147 | OperatorAdded(this, new EventArgs<IOperator>(op));
|
---|
148 | }
|
---|
149 | /// <inheritdoc/>
|
---|
150 | public event EventHandler<EventArgs<IOperator>> OperatorRemoved;
|
---|
151 | /// <summary>
|
---|
152 | /// Fires a new <c>OperatorRemoved</c> event.
|
---|
153 | /// </summary>
|
---|
154 | /// <param name="op">The operator that has been removed.</param>
|
---|
155 | protected virtual void OnOperatorRemoved(IOperator op) {
|
---|
156 | if (OperatorRemoved != null)
|
---|
157 | OperatorRemoved(this, new EventArgs<IOperator>(op));
|
---|
158 | }
|
---|
159 | /// <inheritdoc/>
|
---|
160 | public event EventHandler InitialOperatorChanged;
|
---|
161 | /// <summary>
|
---|
162 | /// Fires a new <c>InitialOperatorChanged</c> event.
|
---|
163 | /// </summary>
|
---|
164 | protected virtual void OnInitialOperatorChanged() {
|
---|
165 | if (InitialOperatorChanged != null)
|
---|
166 | InitialOperatorChanged(this, new EventArgs());
|
---|
167 | }
|
---|
168 |
|
---|
169 | #region Persistence Methods
|
---|
170 | /// <summary>
|
---|
171 | /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
|
---|
172 | /// </summary>
|
---|
173 | /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>.<br/>
|
---|
174 | /// To save the operators of the current instance a child node is created with the tag name
|
---|
175 | /// <c>Operators</c>. Beyond this child node all operators are saved as child nodes themselves.<br/>
|
---|
176 | /// The initial operator is saved as child node with the tag name <c>InitialOperator</c>.</remarks>
|
---|
177 | /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
|
---|
178 | /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
|
---|
179 | /// <param name="persistedObjects">The dictionary of all already persisted objects.
|
---|
180 | /// (Needed to avoid cycles.)</param>
|
---|
181 | /// <returns>The saved <see cref="XmlNode"/>.</returns>
|
---|
182 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
|
---|
183 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
184 | XmlNode ops = document.CreateNode(XmlNodeType.Element, "Operators", null);
|
---|
185 | foreach (IOperator op in myOperators.Values)
|
---|
186 | ops.AppendChild(PersistenceManager.Persist(op, document, persistedObjects));
|
---|
187 | node.AppendChild(ops);
|
---|
188 | if (InitialOperator != null)
|
---|
189 | node.AppendChild(PersistenceManager.Persist("InitialOperator", InitialOperator, document, persistedObjects));
|
---|
190 | return node;
|
---|
191 | }
|
---|
192 | /// <summary>
|
---|
193 | /// Loads the persisted operator graph from the specified <paramref name="node"/>.
|
---|
194 | /// </summary>
|
---|
195 | /// <remarks>See <see cref="GetXmlNode"/> to get more information about how the graph must be saved. <br/>
|
---|
196 | /// Calls <see cref="StorableBase.Populate"/> of base class <see cref="ItemBase"/>.</remarks>
|
---|
197 | /// <param name="node">The <see cref="XmlNode"/> where the operator graph is saved.</param>
|
---|
198 | /// <param name="restoredObjects">The dictionary of all already restored objects.
|
---|
199 | /// (Needed to avoid cycles.)</param>
|
---|
200 | public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
201 | base.Populate(node, restoredObjects);
|
---|
202 |
|
---|
203 | XmlNode ops = node.SelectSingleNode("Operators");
|
---|
204 | for (int i = 0; i < ops.ChildNodes.Count; i++) {
|
---|
205 | XmlNode opNode = ops.ChildNodes[i];
|
---|
206 | IOperator op = (IOperator)PersistenceManager.Restore(opNode, restoredObjects);
|
---|
207 | myOperators.Add(op.Guid, op);
|
---|
208 | }
|
---|
209 |
|
---|
210 | XmlNode initial = node.SelectSingleNode("InitialOperator");
|
---|
211 | if (initial != null)
|
---|
212 | myInitialOperator = (IOperator)PersistenceManager.Restore(initial, restoredObjects);
|
---|
213 | }
|
---|
214 | #endregion
|
---|
215 | }
|
---|
216 | }
|
---|