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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Drawing;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Operators {
28  /// <summary>
29  /// The base class for all operators.
30  /// </summary>
31  [Item("Operator", "Base class for operators.")]
32  [StorableClass]
33  public abstract class Operator : ParameterizedNamedItem, IOperator {
34    public override Image ItemImage {
35      get {
36        if (Breakpoint) return HeuristicLab.Common.Resources.VS2008ImageLibrary.BreakpointActive;
37        else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method;
38      }
39    }
40    public override bool CanChangeDescription {
41      get { return false; }
42    }
43
44    [Storable]
45    private IExecutionContext executionContext;
46    protected IExecutionContext ExecutionContext {
47      get { return executionContext; }
48      private set {
49        if (value != executionContext) {
50          executionContext = value;
51          OnExecutionContextChanged();
52        }
53      }
54    }
55
56    /// <summary>
57    /// Flag whether the current instance has been canceled.
58    /// </summary>
59    private bool canceled;
60    /// <inheritdoc/>
61    protected bool Canceled {
62      get { return canceled; }
63      private set {
64        if (value != canceled) {
65          canceled = value;
66          OnCanceledChanged();
67        }
68      }
69    }
70
71    [Storable]
72    private bool breakpoint;
73    /// <inheritdoc/>
74    /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
75    public bool Breakpoint {
76      get { return breakpoint; }
77      set {
78        if (value != breakpoint) {
79          breakpoint = value;
80          OnBreakpointChanged();
81          OnItemImageChanged();
82        }
83      }
84    }
85
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>
90    protected Operator()
91      : base() {
92      canceled = false;
93      breakpoint = false;
94    }
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    }
115
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>
122    public override IDeepCloneable Clone(Cloner cloner) {
123      Operator clone = (Operator)base.Clone(cloner);
124      clone.canceled = canceled;
125      clone.breakpoint = breakpoint;
126      clone.executionContext = (IExecutionContext)cloner.Clone(executionContext);
127      return clone;
128    }
129
130    /// <inheritdoc/>
131    public virtual IOperation Execute(IExecutionContext context) {
132      try {
133        Canceled = false;
134        ExecutionContext = context;
135        foreach (IParameter param in Parameters)
136          param.ExecutionContext = context;
137        IOperation next = Apply();
138        OnExecuted();
139        return next;
140      }
141      finally {
142        foreach (IParameter param in Parameters)
143          param.ExecutionContext = null;
144        ExecutionContext = null;
145      }
146    }
147    /// <inheritdoc/>
148    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
149    public void Abort() {
150      Canceled = true;
151    }
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>
157    public abstract IOperation Apply();
158
159    protected virtual void OnExecutionContextChanged() { }
160    protected virtual void OnCanceledChanged() { }
161    /// <inheritdoc/>
162    public event EventHandler BreakpointChanged;
163    /// <summary>
164    /// Fires a new <c>BreakpointChanged</c> event.
165    /// </summary>
166    protected virtual void OnBreakpointChanged() {
167      if (BreakpointChanged != null) {
168        BreakpointChanged(this, EventArgs.Empty);
169      }
170    }
171    /// <inheritdoc/>
172    public event EventHandler Executed;
173    /// <summary>
174    /// Fires a new <c>Executed</c> event.
175    /// </summary>
176    protected virtual void OnExecuted() {
177      if (Executed != null) {
178        Executed(this, EventArgs.Empty);
179      }
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.