Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/CombinedOperator.cs @ 1688

Last change on this file since 1688 was 1673, checked in by epitzer, 16 years ago

Migrate HL.Operators-3.3 to new persistence library. (#603)

File size: 5.7 KB
RevLine 
[2]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;
[1673]28using HeuristicLab.Persistence.Default.Decomposers.Storable;
[2]29
30namespace HeuristicLab.Operators {
[801]31  /// <summary>
32  /// Contains an operator graph and automatically injects its sub-operators into the scope it is
33  /// applied on (useful for modularization to assemble complex operators out of simpler ones).
34  /// </summary>
[89]35  public class CombinedOperator : DelegatingOperator {
[1673]36
37    [Storable]
[2]38    private string myDescription;
[801]39    /// <summary>
40    /// Gets the description of the current instance.
41    /// </summary>
[2]42    public override string Description {
43      get { return myDescription; }
44    }
[1673]45
46    [Storable]
[2]47    private IOperatorGraph myOperatorGraph;
[801]48    /// <summary>
49    /// Gets the operator graph of the current instance.
50    /// </summary>
[2]51    public IOperatorGraph OperatorGraph {
52      get { return myOperatorGraph; }
53    }
54
[801]55    /// <summary>
56    /// Initializes a new instance of <see cref="CombinedOperator"/>.
57    /// </summary>
[2]58    public CombinedOperator()
59      : base() {
60      myDescription =
[48]61        @"A combined operator contains a whole operator graph. It is useful for modularization to assemble complex operators out of simpler ones.
62
63A 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.";
[2]64      myOperatorGraph = new OperatorGraph();
65    }
66
[801]67    /// <summary>
68    /// Sets the description of the current instance.
69    /// </summary>
70    /// <remarks>Calls <see cref="OnDescriptionChanged"/>.</remarks>
71    /// <exception cref="NullReferenceException">Thrown when <paramref name="description"/> is <c>null</c>.</exception>
72    /// <param name="description">The description to set.</param>
[2]73    public void SetDescription(string description) {
74      if (description == null)
75        throw new NullReferenceException("description must not be null");
76
77      if (description != myDescription) {
78        myDescription = description;
79        OnDescriptionChanged();
80      }
81    }
82
[801]83    /// <summary>
84    /// Clones the current instance (deep clone).
85    /// </summary>
86    /// <remarks>Calls <see cref="OperatorBase.Clone
87    /// (System.Collections.Generic.IDictionary&lt;System.Guid, object&gt;)"/>
88    /// of base class <see cref="DelegatingOperator"/>.<br/>
89    /// Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
90    /// <see cref="Auxiliary"/>.</remarks>
91    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
92    /// <returns>The cloned object as <see cref="CombinedOperator"/>.</returns>
[2]93    public override object Clone(IDictionary<Guid, object> clonedObjects) {
94      CombinedOperator clone = (CombinedOperator)base.Clone(clonedObjects);
95      clone.myDescription = Description;
96      clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
97      return clone;
98    }
99
[801]100    /// <summary>
101    /// Adds all sub operators to the specified <paramref name="scope"/>.
102    /// </summary>
103    /// <param name="scope">The scope where to inject the sub operators.</param>
104    /// <returns><c>null</c> if the initial operator is <c>nulll</c>, else a new
105    /// <see cref="AtomicOperation"/> with the initial operator and the given <paramref name="scope"/>.</returns>
[2]106    public override IOperation Apply(IScope scope) {
107      if (OperatorGraph.InitialOperator != null) {
[48]108        for (int i = 0; i < SubOperators.Count; i++) {
109          if (scope.GetVariable(SubOperators[i].Name) != null)
110            scope.RemoveVariable(SubOperators[i].Name);
111          scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
[2]112        }
113        return new AtomicOperation(OperatorGraph.InitialOperator, scope);
114      } else {
115        return null;
116      }
117    }
118
[801]119    /// <summary>
120    /// Creates a new instance of <see cref="CombinedOperatorView"/> to display the current instance.
121    /// </summary>
122    /// <returns>The created view as <see cref="CombinedOperatorView"/>.</returns>
[2]123    public override IView CreateView() {
124      return new CombinedOperatorView(this);
125    }
126
[801]127    /// <summary>
128    /// Occurs when the description of the current instance has been changed.
129    /// </summary>
[2]130    public event EventHandler DescriptionChanged;
[801]131    /// <summary>
132    /// Fires a new <c>DescriptionChanged</c> event.
133    /// </summary>
[2]134    protected virtual void OnDescriptionChanged() {
135      if (DescriptionChanged != null)
136        DescriptionChanged(this, new EventArgs());
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.