Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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