Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented ReadOnlyView property for items (#969).

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