Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4501 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 5.9 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
[776]87    /// <summary>
88    /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
89    /// the canceled flag to <c>false</c> and the name of the operator to the type name.
90    /// </summary>
[2845]91    protected Operator()
92      : base() {
[2653]93      canceled = false;
94      breakpoint = false;
[2]95    }
[2845]96    protected Operator(string name)
97      : base(name) {
98      canceled = false;
99      breakpoint = false;
100    }
101    protected Operator(string name, ParameterCollection parameters)
102      : base(name, parameters) {
103      canceled = false;
104      breakpoint = false;
105    }
106    protected Operator(string name, string description)
107      : base(name, description) {
108      canceled = false;
109      breakpoint = false;
110    }
111    protected Operator(string name, string description, ParameterCollection parameters)
112      : base(name, description, parameters) {
113      canceled = false;
114      breakpoint = false;
115    }
[3317]116    [StorableConstructor]
117    protected Operator(bool deserializing) : base(deserializing) { }
[2]118
[776]119    /// <summary>
120    /// Clones the current instance (deep clone).
121    /// </summary>
122    /// <remarks>Clones also sub operators, variables and variable infos.</remarks>
123    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
124    /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
[2653]125    public override IDeepCloneable Clone(Cloner cloner) {
[2664]126      Operator clone = (Operator)base.Clone(cloner);
[2653]127      clone.canceled = canceled;
128      clone.breakpoint = breakpoint;
[2834]129      clone.executionContext = (IExecutionContext)cloner.Clone(executionContext);
[2]130      return clone;
131    }
132
[776]133    /// <inheritdoc/>
[2834]134    public virtual IOperation Execute(IExecutionContext context) {
[2740]135      try {
[2757]136        Canceled = false;
137        ExecutionContext = context;
[2740]138        foreach (IParameter param in Parameters)
139          param.ExecutionContext = context;
[2834]140        IOperation next = Apply();
[2740]141        OnExecuted();
142        return next;
143      }
144      finally {
145        foreach (IParameter param in Parameters)
146          param.ExecutionContext = null;
[2757]147        ExecutionContext = null;
[2740]148      }
[2]149    }
[776]150    /// <inheritdoc/>
151    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
[2757]152    public void Abort() {
153      Canceled = true;
[2]154    }
[776]155    /// <summary>
156    /// Performs the current operator on the specified <paramref name="scope"/>.
157    /// </summary>
158    /// <param name="scope">The scope where to execute the operator</param>
159    /// <returns><c>null</c>.</returns>
[2834]160    public abstract IOperation Apply();
[2653]161
[2757]162    protected virtual void OnExecutionContextChanged() { }
163    protected virtual void OnCanceledChanged() { }
[776]164    /// <inheritdoc/>
[2]165    public event EventHandler BreakpointChanged;
[776]166    /// <summary>
167    /// Fires a new <c>BreakpointChanged</c> event.
168    /// </summary>
[2]169    protected virtual void OnBreakpointChanged() {
170      if (BreakpointChanged != null) {
[2793]171        BreakpointChanged(this, EventArgs.Empty);
[2]172      }
173    }
[776]174    /// <inheritdoc/>
[2]175    public event EventHandler Executed;
[776]176    /// <summary>
177    /// Fires a new <c>Executed</c> event.
178    /// </summary>
[2]179    protected virtual void OnExecuted() {
180      if (Executed != null) {
[2793]181        Executed(this, EventArgs.Empty);
[2]182      }
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.