Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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