Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged cloning refactoring branch back into trunk (#922)

File size: 5.6 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;
[3376]24using HeuristicLab.Common;
[2805]25using HeuristicLab.Core;
[1823]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]27
[2805]28namespace HeuristicLab.Operators {
[776]29  /// <summary>
30  /// The base class for all operators.
31  /// </summary>
[2664]32  [Item("Operator", "Base class for operators.")]
[3017]33  [StorableClass]
[2845]34  public abstract class Operator : ParameterizedNamedItem, IOperator {
[2653]35    public override Image ItemImage {
[3306]36      get {
37        if (Breakpoint) return HeuristicLab.Common.Resources.VS2008ImageLibrary.BreakpointActive;
38        else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method;
39      }
[2653]40    }
[2684]41    public override bool CanChangeDescription {
42      get { return false; }
43    }
[1667]44
45    [Storable]
[2834]46    private IExecutionContext executionContext;
47    protected IExecutionContext ExecutionContext {
[2757]48      get { return executionContext; }
49      private set {
50        if (value != executionContext) {
51          executionContext = value;
52          OnExecutionContextChanged();
53        }
54      }
55    }
56
[776]57    /// <summary>
58    /// Flag whether the current instance has been canceled.
59    /// </summary>
[2653]60    private bool canceled;
[776]61    /// <inheritdoc/>
[2653]62    protected bool Canceled {
63      get { return canceled; }
[2757]64      private set {
65        if (value != canceled) {
66          canceled = value;
67          OnCanceledChanged();
68        }
69      }
[2]70    }
[1667]71
72    [Storable]
[2653]73    private bool breakpoint;
[776]74    /// <inheritdoc/>
75    /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
[2]76    public bool Breakpoint {
[2653]77      get { return breakpoint; }
[2]78      set {
[2653]79        if (value != breakpoint) {
80          breakpoint = value;
[2]81          OnBreakpointChanged();
[3306]82          OnItemImageChanged();
[2]83        }
84      }
85    }
86
[4722]87    [StorableConstructor]
88    protected Operator(bool deserializing) : base(deserializing) { }
89    protected Operator(Operator original, Cloner cloner)
90      : base(original, cloner) {
91      this.canceled = original.canceled;
92      this.breakpoint = original.breakpoint;
93      this.executionContext = cloner.Clone<IExecutionContext>(original.executionContext);
94    }
[776]95    /// <summary>
96    /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
97    /// the canceled flag to <c>false</c> and the name of the operator to the type name.
98    /// </summary>
[2845]99    protected Operator()
100      : base() {
[2653]101      canceled = false;
102      breakpoint = false;
[2]103    }
[2845]104    protected Operator(string name)
105      : base(name) {
106      canceled = false;
107      breakpoint = false;
108    }
109    protected Operator(string name, ParameterCollection parameters)
110      : base(name, parameters) {
111      canceled = false;
112      breakpoint = false;
113    }
114    protected Operator(string name, string description)
115      : base(name, description) {
116      canceled = false;
117      breakpoint = false;
118    }
119    protected Operator(string name, string description, ParameterCollection parameters)
120      : base(name, description, parameters) {
121      canceled = false;
122      breakpoint = false;
123    }
[2]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      }
165    }
[776]166    /// <inheritdoc/>
[2]167    public event EventHandler Executed;
[776]168    /// <summary>
169    /// Fires a new <c>Executed</c> event.
170    /// </summary>
[2]171    protected virtual void OnExecuted() {
172      if (Executed != null) {
[2793]173        Executed(this, EventArgs.Empty);
[2]174      }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.