Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/OperatorGraph.cs @ 800

Last change on this file since 800 was 776, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Core namespace (#331)

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