Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Operators/CombinedOperator.cs @ 887

Last change on this file since 887 was 887, checked in by gkronber, 15 years ago

Refactored cloning in all plugins except: HL.Communication, HL.Hive, HL.GP, HL.Routing, HL.Scheduling, HL.SimOpt, HL.Visualization

#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.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;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Operators {
30  /// <summary>
31  /// Contains an operator graph and automatically injects its sub-operators into the scope it is
32  /// applied on (useful for modularization to assemble complex operators out of simpler ones).
33  /// </summary>
34  public class CombinedOperator : DelegatingOperator {
35    private string myDescription;
36    /// <summary>
37    /// Gets the description of the current instance.
38    /// </summary>
39    public override string Description {
40      get { return myDescription; }
41    }
42    private IOperatorGraph myOperatorGraph;
43    /// <summary>
44    /// Gets the operator graph of the current instance.
45    /// </summary>
46    public IOperatorGraph OperatorGraph {
47      get { return myOperatorGraph; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="CombinedOperator"/>.
52    /// </summary>
53    public CombinedOperator()
54      : base() {
55      myDescription =
56        @"A combined operator contains a whole operator graph. It is useful for modularization to assemble complex operators out of simpler ones.
57
58A combined operator automatically inject its sub-operators into the scope it is applied on. Thereby the names of the sub-operators are used as variable names. Those operators can be extracted again in the contained operator graph by using an OperatorExtractor. So it is possible to parameterize a combined operator with custom operators.";
59      myOperatorGraph = new OperatorGraph();
60    }
61
62    /// <summary>
63    /// Copy constructor for CombinedOperators (deep clone).
64    /// </summary>
65    /// <remarks>Calls <see cref="OperatorBase
66    /// (System.Collections.Generic.IDictionary&lt;System.Guid, object&gt;)"/>
67    /// of base class <see cref="DelegatingOperator"/>.<br/>
68    /// Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
69    /// <see cref="Auxiliary"/>.</remarks>
70    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
71    /// <returns>The cloned object as <see cref="CombinedOperator"/>.</returns>
72    public CombinedOperator(CombinedOperator original, IDictionary<Guid, object> clonedObjects)
73      : base(original, clonedObjects) {
74      this.myDescription = original.Description;
75      this.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(original.OperatorGraph, clonedObjects);
76    }
77
78    /// <summary>
79    /// Sets the description of the current instance.
80    /// </summary>
81    /// <remarks>Calls <see cref="OnDescriptionChanged"/>.</remarks>
82    /// <exception cref="NullReferenceException">Thrown when <paramref name="description"/> is <c>null</c>.</exception>
83    /// <param name="description">The description to set.</param>
84    public void SetDescription(string description) {
85      if (description == null)
86        throw new NullReferenceException("description must not be null");
87
88      if (description != myDescription) {
89        myDescription = description;
90        OnDescriptionChanged();
91      }
92    }
93
94    /// <summary>
95    /// Clones the current instance (deep clone).
96    /// </summary>
97    /// <remarks>Calls the copy constructor <see cref="CombinedOperator
98    /// (System.Collections.Generic.IDictionary&lt;System.Guid, object&gt;)"/> .<br/>
99    ///</remarks>
100    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
101    /// <returns>The cloned object as <see cref="CombinedOperator"/>.</returns>
102    public override object Clone(IDictionary<Guid, object> clonedObjects) {
103      return new CombinedOperator(this, clonedObjects);
104    }
105
106    /// <summary>
107    /// Adds all sub operators to the specified <paramref name="scope"/>.
108    /// </summary>
109    /// <param name="scope">The scope where to inject the sub operators.</param>
110    /// <returns><c>null</c> if the initial operator is <c>nulll</c>, else a new
111    /// <see cref="AtomicOperation"/> with the initial operator and the given <paramref name="scope"/>.</returns>
112    public override IOperation Apply(IScope scope) {
113      if (OperatorGraph.InitialOperator != null) {
114        for (int i = 0; i < SubOperators.Count; i++) {
115          if (scope.GetVariable(SubOperators[i].Name) != null)
116            scope.RemoveVariable(SubOperators[i].Name);
117          scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
118        }
119        return new AtomicOperation(OperatorGraph.InitialOperator, scope);
120      } else {
121        return null;
122      }
123    }
124
125    /// <summary>
126    /// Creates a new instance of <see cref="CombinedOperatorView"/> to display the current instance.
127    /// </summary>
128    /// <returns>The created view as <see cref="CombinedOperatorView"/>.</returns>
129    public override IView CreateView() {
130      return new CombinedOperatorView(this);
131    }
132
133    /// <summary>
134    /// Occurs when the description of the current instance has been changed.
135    /// </summary>
136    public event EventHandler DescriptionChanged;
137    /// <summary>
138    /// Fires a new <c>DescriptionChanged</c> event.
139    /// </summary>
140    protected virtual void OnDescriptionChanged() {
141      if (DescriptionChanged != null)
142        DescriptionChanged(this, new EventArgs());
143    }
144
145    #region Persistence Methods
146    /// <summary>
147    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
148    /// </summary>
149    /// <remarks>The description and the operator graph of the current instance are saved as child
150    /// nodes with tag names <c>Description</c> <c>OperatorGraph</c>.<br/>
151    /// Calls <see cref="OperatorBase.GetXmlNode"/> of base class <see cref="DelegatingOperator"/>.</remarks>
152    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
153    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
154    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
155    /// <returns>The saved <see cref="XmlNode"/>.</returns>
156    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
157      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
158      XmlNode descriptionNode = document.CreateNode(XmlNodeType.Element, "Description", null);
159      descriptionNode.InnerText = myDescription;
160      node.AppendChild(descriptionNode);
161      node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
162      return node;
163    }
164    /// <summary>
165    /// Loads the persisted instance from the specified <paramref name="node"/>.
166    /// </summary>
167    /// <remarks>Calls <see cref="OperatorBase.Populate"/> of base class <see cref="DelegatingOperator"/>.
168    /// <br/> See <see cref="GetXmlNode"/> for further information on how the data must be saved.</remarks>
169    /// <param name="node">The <see cref="XmlNode"/> where the operator is saved.</param>
170    /// <param name="restoredObjects">The dictionary of all already restored objects.
171    /// (Needed to avoid cycles.)</param>
172    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
173      base.Populate(node, restoredObjects);
174      XmlNode descriptionNode = node.SelectSingleNode("Description");
175      if (descriptionNode != null) myDescription = descriptionNode.InnerText;
176      myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
177    }
178    #endregion
179  }
180}
Note: See TracBrowser for help on using the repository browser.