Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3042 was 3034, checked in by swagner, 15 years ago

Set name changing capabilities of operators (#893)

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