Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/Operator.cs @ 3024

Last change on this file since 3024 was 3024, checked in by swagner, 14 years ago

Restricted name changes to CombinedOperator, Placeholder and ProgrammableOperator (#893)

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Operators {
28  /// <summary>
29  /// The base class for all operators.
30  /// </summary>
31  [Item("Operator", "Base class for operators.")]
32  [StorableClass]
33  public abstract class Operator : ParameterizedNamedItem, IOperator {
34    public override Image ItemImage {
35      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
36    }
37    public override bool CanChangeName {
38      get { return false; }
39    }
40    public override bool CanChangeDescription {
41      get { return false; }
42    }
43
44    [Storable]
45    private IExecutionContext executionContext;
46    protected IExecutionContext ExecutionContext {
47      get { return executionContext; }
48      private set {
49        if (value != executionContext) {
50          executionContext = value;
51          OnExecutionContextChanged();
52        }
53      }
54    }
55
56    /// <summary>
57    /// Flag whether the current instance has been canceled.
58    /// </summary>
59    private bool canceled;
60    /// <inheritdoc/>
61    protected bool Canceled {
62      get { return canceled; }
63      private set {
64        if (value != canceled) {
65          canceled = value;
66          OnCanceledChanged();
67        }
68      }
69    }
70
71    [Storable]
72    private bool breakpoint;
73    /// <inheritdoc/>
74    /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
75    public bool Breakpoint {
76      get { return breakpoint; }
77      set {
78        if (value != breakpoint) {
79          breakpoint = value;
80          OnBreakpointChanged();
81        }
82      }
83    }
84
85    /// <summary>
86    /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
87    /// the canceled flag to <c>false</c> and the name of the operator to the type name.
88    /// </summary>
89    protected Operator()
90      : base() {
91      canceled = false;
92      breakpoint = false;
93    }
94    protected Operator(string name)
95      : base(name) {
96      canceled = false;
97      breakpoint = false;
98    }
99    protected Operator(string name, ParameterCollection parameters)
100      : base(name, parameters) {
101      canceled = false;
102      breakpoint = false;
103    }
104    protected Operator(string name, string description)
105      : base(name, description) {
106      canceled = false;
107      breakpoint = false;
108    }
109    protected Operator(string name, string description, ParameterCollection parameters)
110      : base(name, description, parameters) {
111      canceled = false;
112      breakpoint = false;
113    }
114
115    /// <summary>
116    /// Clones the current instance (deep clone).
117    /// </summary>
118    /// <remarks>Clones also sub operators, variables and variable infos.</remarks>
119    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
120    /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
121    public override IDeepCloneable Clone(Cloner cloner) {
122      Operator clone = (Operator)base.Clone(cloner);
123      clone.canceled = canceled;
124      clone.breakpoint = breakpoint;
125      clone.executionContext = (IExecutionContext)cloner.Clone(executionContext);
126      return clone;
127    }
128
129    /// <inheritdoc/>
130    public virtual IOperation Execute(IExecutionContext context) {
131      try {
132        Canceled = false;
133        ExecutionContext = context;
134        foreach (IParameter param in Parameters)
135          param.ExecutionContext = context;
136        IOperation next = Apply();
137        OnExecuted();
138        return next;
139      }
140      finally {
141        foreach (IParameter param in Parameters)
142          param.ExecutionContext = null;
143        ExecutionContext = null;
144      }
145    }
146    /// <inheritdoc/>
147    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
148    public void Abort() {
149      Canceled = true;
150    }
151    /// <summary>
152    /// Performs the current operator on the specified <paramref name="scope"/>.
153    /// </summary>
154    /// <param name="scope">The scope where to execute the operator</param>
155    /// <returns><c>null</c>.</returns>
156    public abstract IOperation Apply();
157
158    protected virtual void OnExecutionContextChanged() { }
159    protected virtual void OnCanceledChanged() { }
160    /// <inheritdoc/>
161    public event EventHandler BreakpointChanged;
162    /// <summary>
163    /// Fires a new <c>BreakpointChanged</c> event.
164    /// </summary>
165    protected virtual void OnBreakpointChanged() {
166      if (BreakpointChanged != null) {
167        BreakpointChanged(this, EventArgs.Empty);
168      }
169    }
170    /// <inheritdoc/>
171    public event EventHandler Executed;
172    /// <summary>
173    /// Fires a new <c>Executed</c> event.
174    /// </summary>
175    protected virtual void OnExecuted() {
176      if (Executed != null) {
177        Executed(this, EventArgs.Empty);
178      }
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.