Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • replaced new EventArgs() by EventArgs.Empty
File size: 6.2 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.Collections.Generic;
24using System.Text;
25using System.Xml;
26using System.Drawing;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Core;
29using HeuristicLab.Collections;
30
31namespace HeuristicLab.Operators {
32  /// <summary>
33  /// The base class for all operators.
34  /// </summary>
35  [Item("Operator", "Base class for operators.")]
36  public abstract class Operator : NamedItem, IOperator {
37    public override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
39    }
40    public override bool CanChangeDescription {
41      get { return false; }
42    }
43
44    private ParameterCollection parameters;
45    [Storable]
46    protected ParameterCollection Parameters {
47      get { return parameters;}
48      private set {
49        if (parameters != null) parameters.Changed -= new ChangedEventHandler(Parameters_Changed);
50        parameters = value;
51        readOnlyParameters = null;
52        if (parameters != null) parameters.Changed += new ChangedEventHandler(Parameters_Changed);
53      }
54    }
55    private ReadOnlyObservableKeyedCollection<string, IParameter> readOnlyParameters;
56    IObservableKeyedCollection<string, IParameter> IOperator.Parameters {
57      get {
58        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
59        return readOnlyParameters;
60      }
61    }
62
63    [Storable]
64    private ExecutionContext executionContext;
65    protected ExecutionContext ExecutionContext {
66      get { return executionContext; }
67      private set {
68        if (value != executionContext) {
69          executionContext = value;
70          OnExecutionContextChanged();
71        }
72      }
73    }
74
75    /// <summary>
76    /// Flag whether the current instance has been canceled.
77    /// </summary>
78    private bool canceled;
79    /// <inheritdoc/>
80    protected bool Canceled {
81      get { return canceled; }
82      private set {
83        if (value != canceled) {
84          canceled = value;
85          OnCanceledChanged();
86        }
87      }
88    }
89
90    [Storable]
91    private bool breakpoint;
92    /// <inheritdoc/>
93    /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
94    public bool Breakpoint {
95      get { return breakpoint; }
96      set {
97        if (value != breakpoint) {
98          breakpoint = value;
99          OnBreakpointChanged();
100        }
101      }
102    }
103
104    /// <summary>
105    /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
106    /// the canceled flag to <c>false</c> and the name of the operator to the type name.
107    /// </summary>
108    protected Operator() {
109      Name = ItemName;
110      Parameters = new ParameterCollection();
111      readOnlyParameters = null;
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.Parameters = (ParameterCollection)cloner.Clone(parameters);
125      clone.canceled = canceled;
126      clone.breakpoint = breakpoint;
127      clone.executionContext = (ExecutionContext)cloner.Clone(executionContext);
128      return clone;
129    }
130
131    /// <inheritdoc/>
132    public virtual IExecutionSequence Execute(ExecutionContext context) {
133      try {
134        Canceled = false;
135        ExecutionContext = context;
136        foreach (IParameter param in Parameters)
137          param.ExecutionContext = context;
138        IExecutionSequence next = Apply();
139        OnExecuted();
140        return next;
141      }
142      finally {
143        foreach (IParameter param in Parameters)
144          param.ExecutionContext = null;
145        ExecutionContext = null;
146      }
147    }
148    /// <inheritdoc/>
149    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
150    public void Abort() {
151      Canceled = true;
152    }
153    /// <summary>
154    /// Performs the current operator on the specified <paramref name="scope"/>.
155    /// </summary>
156    /// <param name="scope">The scope where to execute the operator</param>
157    /// <returns><c>null</c>.</returns>
158    public abstract IExecutionSequence Apply();
159
160    protected virtual void OnExecutionContextChanged() { }
161    protected virtual void OnCanceledChanged() { }
162    /// <inheritdoc/>
163    public event EventHandler BreakpointChanged;
164    /// <summary>
165    /// Fires a new <c>BreakpointChanged</c> event.
166    /// </summary>
167    protected virtual void OnBreakpointChanged() {
168      if (BreakpointChanged != null) {
169        BreakpointChanged(this, EventArgs.Empty);
170      }
171      OnChanged();
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    private void Parameters_Changed(object sender, ChangedEventArgs e) {
185      OnChanged(e);
186    }
187  }
188}
Note: See TracBrowser for help on using the repository browser.