Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • replaced Changed in IItem by ToStringChanged
File size: 5.6 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  public abstract class Operator : ParameterizedNamedItem, IOperator {
33    public override Image ItemImage {
34      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
35    }
36    public override bool CanChangeDescription {
37      get { return false; }
38    }
39
40    [Storable]
41    private IExecutionContext executionContext;
42    protected IExecutionContext ExecutionContext {
43      get { return executionContext; }
44      private set {
45        if (value != executionContext) {
46          executionContext = value;
47          OnExecutionContextChanged();
48        }
49      }
50    }
51
52    /// <summary>
53    /// Flag whether the current instance has been canceled.
54    /// </summary>
55    private bool canceled;
56    /// <inheritdoc/>
57    protected bool Canceled {
58      get { return canceled; }
59      private set {
60        if (value != canceled) {
61          canceled = value;
62          OnCanceledChanged();
63        }
64      }
65    }
66
67    [Storable]
68    private bool breakpoint;
69    /// <inheritdoc/>
70    /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
71    public bool Breakpoint {
72      get { return breakpoint; }
73      set {
74        if (value != breakpoint) {
75          breakpoint = value;
76          OnBreakpointChanged();
77        }
78      }
79    }
80
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>
85    protected Operator()
86      : base() {
87      canceled = false;
88      breakpoint = false;
89    }
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    }
110
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>
117    public override IDeepCloneable Clone(Cloner cloner) {
118      Operator clone = (Operator)base.Clone(cloner);
119      clone.canceled = canceled;
120      clone.breakpoint = breakpoint;
121      clone.executionContext = (IExecutionContext)cloner.Clone(executionContext);
122      return clone;
123    }
124
125    /// <inheritdoc/>
126    public virtual IOperation Execute(IExecutionContext context) {
127      try {
128        Canceled = false;
129        ExecutionContext = context;
130        foreach (IParameter param in Parameters)
131          param.ExecutionContext = context;
132        IOperation next = Apply();
133        OnExecuted();
134        return next;
135      }
136      finally {
137        foreach (IParameter param in Parameters)
138          param.ExecutionContext = null;
139        ExecutionContext = null;
140      }
141    }
142    /// <inheritdoc/>
143    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
144    public void Abort() {
145      Canceled = true;
146    }
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>
152    public abstract IOperation Apply();
153
154    protected virtual void OnExecutionContextChanged() { }
155    protected virtual void OnCanceledChanged() { }
156    /// <inheritdoc/>
157    public event EventHandler BreakpointChanged;
158    /// <summary>
159    /// Fires a new <c>BreakpointChanged</c> event.
160    /// </summary>
161    protected virtual void OnBreakpointChanged() {
162      if (BreakpointChanged != null) {
163        BreakpointChanged(this, EventArgs.Empty);
164      }
165    }
166    /// <inheritdoc/>
167    public event EventHandler Executed;
168    /// <summary>
169    /// Fires a new <c>Executed</c> event.
170    /// </summary>
171    protected virtual void OnExecuted() {
172      if (Executed != null) {
173        Executed(this, EventArgs.Empty);
174      }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.