Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented reviewers' comments (#893).

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