Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3445 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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