Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.2/CombinedOperator.cs @ 2148

Last change on this file since 2148 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 7.9 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    /// Sets the description of the current instance.
64    /// </summary>
65    /// <remarks>Calls <see cref="OnDescriptionChanged"/>.</remarks>
66    /// <exception cref="NullReferenceException">Thrown when <paramref name="description"/> is <c>null</c>.</exception>
67    /// <param name="description">The description to set.</param>
68    public void SetDescription(string description) {
69      if (description == null)
70        throw new NullReferenceException("description must not be null");
71
72      if (description != myDescription) {
73        myDescription = description;
74        OnDescriptionChanged();
75      }
76    }
77
78    /// <summary>
79    /// Clones the current instance (deep clone).
80    /// </summary>
81    /// <remarks>Calls <see cref="OperatorBase.Clone
82    /// (System.Collections.Generic.IDictionary&lt;System.Guid, object&gt;)"/>
83    /// of base class <see cref="DelegatingOperator"/>.<br/>
84    /// Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
85    /// <see cref="Auxiliary"/>.</remarks>
86    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
87    /// <returns>The cloned object as <see cref="CombinedOperator"/>.</returns>
88    public override object Clone(IDictionary<Guid, object> clonedObjects) {
89      CombinedOperator clone = (CombinedOperator)base.Clone(clonedObjects);
90      clone.myDescription = Description;
91      clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
92      return clone;
93    }
94
95    /// <summary>
96    /// Adds all sub operators to the specified <paramref name="scope"/>.
97    /// </summary>
98    /// <param name="scope">The scope where to inject the sub operators.</param>
99    /// <returns><c>null</c> if the initial operator is <c>nulll</c>, else a new
100    /// <see cref="AtomicOperation"/> with the initial operator and the given <paramref name="scope"/>.</returns>
101    public override IOperation Apply(IScope scope) {
102      if (OperatorGraph.InitialOperator != null) {
103        for (int i = 0; i < SubOperators.Count; i++) {
104          if (scope.GetVariable(SubOperators[i].Name) != null)
105            scope.RemoveVariable(SubOperators[i].Name);
106          scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
107        }
108        return new AtomicOperation(OperatorGraph.InitialOperator, scope);
109      } else {
110        return null;
111      }
112    }
113
114    /// <summary>
115    /// Creates a new instance of <see cref="CombinedOperatorView"/> to display the current instance.
116    /// </summary>
117    /// <returns>The created view as <see cref="CombinedOperatorView"/>.</returns>
118    public override IView CreateView() {
119      return new CombinedOperatorView(this);
120    }
121
122    /// <summary>
123    /// Occurs when the description of the current instance has been changed.
124    /// </summary>
125    public event EventHandler DescriptionChanged;
126    /// <summary>
127    /// Fires a new <c>DescriptionChanged</c> event.
128    /// </summary>
129    protected virtual void OnDescriptionChanged() {
130      if (DescriptionChanged != null)
131        DescriptionChanged(this, new EventArgs());
132    }
133
134    #region Persistence Methods
135    /// <summary>
136    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
137    /// </summary>
138    /// <remarks>The description and the operator graph of the current instance are saved as child
139    /// nodes with tag names <c>Description</c> <c>OperatorGraph</c>.<br/>
140    /// Calls <see cref="OperatorBase.GetXmlNode"/> of base class <see cref="DelegatingOperator"/>.</remarks>
141    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
142    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
143    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
144    /// <returns>The saved <see cref="XmlNode"/>.</returns>
145    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
146      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
147      XmlNode descriptionNode = document.CreateNode(XmlNodeType.Element, "Description", null);
148      descriptionNode.InnerText = myDescription;
149      node.AppendChild(descriptionNode);
150      node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
151      return node;
152    }
153    /// <summary>
154    /// Loads the persisted instance from the specified <paramref name="node"/>.
155    /// </summary>
156    /// <remarks>Calls <see cref="OperatorBase.Populate"/> of base class <see cref="DelegatingOperator"/>.
157    /// <br/> See <see cref="GetXmlNode"/> for further information on how the data must be saved.</remarks>
158    /// <param name="node">The <see cref="XmlNode"/> where the operator is saved.</param>
159    /// <param name="restoredObjects">The dictionary of all already restored objects.
160    /// (Needed to avoid cycles.)</param>
161    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
162      base.Populate(node, restoredObjects);
163      XmlNode descriptionNode = node.SelectSingleNode("Description");
164      if (descriptionNode != null) myDescription = descriptionNode.InnerText;
165      myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
166    }
167    #endregion
168  }
169}
Note: See TracBrowser for help on using the repository browser.