Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Operator.cs @ 2796

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

Operator architecture refactoring (#95)

  • worked on operators, parameters and problems
File size: 6.1 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.Collections;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Core {
28  /// <summary>
29  /// The base class for all operators.
30  /// </summary>
31  [Item("Operator", "Base class for operators.")]
32  public abstract class Operator : NamedItem, 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    private ParameterCollection parameters;
41    [Storable]
42    protected ParameterCollection Parameters {
43      get { return parameters;}
44      private set {
45        if (parameters != null) parameters.Changed -= new ChangedEventHandler(Parameters_Changed);
46        parameters = value;
47        readOnlyParameters = null;
48        if (parameters != null) parameters.Changed += new ChangedEventHandler(Parameters_Changed);
49      }
50    }
51    private ReadOnlyObservableKeyedCollection<string, IParameter> readOnlyParameters;
52    IObservableKeyedCollection<string, IParameter> IOperator.Parameters {
53      get {
54        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
55        return readOnlyParameters;
56      }
57    }
58
59    [Storable]
60    private ExecutionContext executionContext;
61    protected ExecutionContext ExecutionContext {
62      get { return executionContext; }
63      private set {
64        if (value != executionContext) {
65          executionContext = value;
66          OnExecutionContextChanged();
67        }
68      }
69    }
70
71    /// <summary>
72    /// Flag whether the current instance has been canceled.
73    /// </summary>
74    private bool canceled;
75    /// <inheritdoc/>
76    protected bool Canceled {
77      get { return canceled; }
78      private set {
79        if (value != canceled) {
80          canceled = value;
81          OnCanceledChanged();
82        }
83      }
84    }
85
86    [Storable]
87    private bool breakpoint;
88    /// <inheritdoc/>
89    /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
90    public bool Breakpoint {
91      get { return breakpoint; }
92      set {
93        if (value != breakpoint) {
94          breakpoint = value;
95          OnBreakpointChanged();
96        }
97      }
98    }
99
100    /// <summary>
101    /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
102    /// the canceled flag to <c>false</c> and the name of the operator to the type name.
103    /// </summary>
104    protected Operator() {
105      Name = ItemName;
106      Parameters = new ParameterCollection();
107      readOnlyParameters = null;
108      canceled = false;
109      breakpoint = false;
110    }
111
112    /// <summary>
113    /// Clones the current instance (deep clone).
114    /// </summary>
115    /// <remarks>Clones also sub operators, variables and variable infos.</remarks>
116    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
117    /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
118    public override IDeepCloneable Clone(Cloner cloner) {
119      Operator clone = (Operator)base.Clone(cloner);
120      clone.Parameters = (ParameterCollection)cloner.Clone(parameters);
121      clone.canceled = canceled;
122      clone.breakpoint = breakpoint;
123      clone.executionContext = (ExecutionContext)cloner.Clone(executionContext);
124      return clone;
125    }
126
127    /// <inheritdoc/>
128    public virtual IExecutionSequence Execute(ExecutionContext context) {
129      try {
130        Canceled = false;
131        ExecutionContext = context;
132        foreach (IParameter param in Parameters)
133          param.ExecutionContext = context;
134        IExecutionSequence next = Apply();
135        OnExecuted();
136        return next;
137      }
138      finally {
139        foreach (IParameter param in Parameters)
140          param.ExecutionContext = null;
141        ExecutionContext = null;
142      }
143    }
144    /// <inheritdoc/>
145    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
146    public void Abort() {
147      Canceled = true;
148    }
149    /// <summary>
150    /// Performs the current operator on the specified <paramref name="scope"/>.
151    /// </summary>
152    /// <param name="scope">The scope where to execute the operator</param>
153    /// <returns><c>null</c>.</returns>
154    public abstract IExecutionSequence Apply();
155
156    protected virtual void OnExecutionContextChanged() { }
157    protected virtual void OnCanceledChanged() { }
158    /// <inheritdoc/>
159    public event EventHandler BreakpointChanged;
160    /// <summary>
161    /// Fires a new <c>BreakpointChanged</c> event.
162    /// </summary>
163    protected virtual void OnBreakpointChanged() {
164      if (BreakpointChanged != null) {
165        BreakpointChanged(this, EventArgs.Empty);
166      }
167      OnChanged();
168    }
169    /// <inheritdoc/>
170    public event EventHandler Executed;
171    /// <summary>
172    /// Fires a new <c>Executed</c> event.
173    /// </summary>
174    protected virtual void OnExecuted() {
175      if (Executed != null) {
176        Executed(this, EventArgs.Empty);
177      }
178    }
179
180    private void Parameters_Changed(object sender, ChangedEventArgs e) {
181      OnChanged(e);
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.