Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2815 was 2805, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • worked on parameters, TSP and selection
File size: 6.1 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[2653]23using System.Drawing;
[2794]24using HeuristicLab.Collections;
[2805]25using HeuristicLab.Core;
[1823]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]27
[2805]28namespace HeuristicLab.Operators {
[776]29  /// <summary>
30  /// The base class for all operators.
31  /// </summary>
[2664]32  [Item("Operator", "Base class for operators.")]
33  public abstract class Operator : NamedItem, IOperator {
[2653]34    public override Image ItemImage {
35      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
36    }
[2684]37    public override bool CanChangeDescription {
38      get { return false; }
39    }
[1667]40
[2653]41    private ParameterCollection parameters;
[1667]42    [Storable]
[2653]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);
[2]50      }
51    }
[2653]52    private ReadOnlyObservableKeyedCollection<string, IParameter> readOnlyParameters;
53    IObservableKeyedCollection<string, IParameter> IOperator.Parameters {
54      get {
55        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
56        return readOnlyParameters;
57      }
[2]58    }
[2653]59
[2757]60    [Storable]
61    private ExecutionContext executionContext;
62    protected ExecutionContext ExecutionContext {
63      get { return executionContext; }
64      private set {
65        if (value != executionContext) {
66          executionContext = value;
67          OnExecutionContextChanged();
68        }
69      }
70    }
71
[776]72    /// <summary>
73    /// Flag whether the current instance has been canceled.
74    /// </summary>
[2653]75    private bool canceled;
[776]76    /// <inheritdoc/>
[2653]77    protected bool Canceled {
78      get { return canceled; }
[2757]79      private set {
80        if (value != canceled) {
81          canceled = value;
82          OnCanceledChanged();
83        }
84      }
[2]85    }
[1667]86
87    [Storable]
[2653]88    private bool breakpoint;
[776]89    /// <inheritdoc/>
90    /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
[2]91    public bool Breakpoint {
[2653]92      get { return breakpoint; }
[2]93      set {
[2653]94        if (value != breakpoint) {
95          breakpoint = value;
[2]96          OnBreakpointChanged();
97        }
98      }
99    }
100
[776]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>
[2664]105    protected Operator() {
[2790]106      Name = ItemName;
[2653]107      Parameters = new ParameterCollection();
108      readOnlyParameters = null;
109      canceled = false;
110      breakpoint = false;
[2]111    }
112
[776]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>
[2653]119    public override IDeepCloneable Clone(Cloner cloner) {
[2664]120      Operator clone = (Operator)base.Clone(cloner);
[2653]121      clone.Parameters = (ParameterCollection)cloner.Clone(parameters);
122      clone.canceled = canceled;
123      clone.breakpoint = breakpoint;
[2740]124      clone.executionContext = (ExecutionContext)cloner.Clone(executionContext);
[2]125      return clone;
126    }
127
[776]128    /// <inheritdoc/>
[2773]129    public virtual IExecutionSequence Execute(ExecutionContext context) {
[2740]130      try {
[2757]131        Canceled = false;
132        ExecutionContext = context;
[2740]133        foreach (IParameter param in Parameters)
134          param.ExecutionContext = context;
[2773]135        IExecutionSequence next = Apply();
[2740]136        OnExecuted();
137        return next;
138      }
139      finally {
140        foreach (IParameter param in Parameters)
141          param.ExecutionContext = null;
[2757]142        ExecutionContext = null;
[2740]143      }
[2]144    }
[776]145    /// <inheritdoc/>
146    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
[2757]147    public void Abort() {
148      Canceled = true;
[2]149    }
[776]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>
[2773]155    public abstract IExecutionSequence Apply();
[2653]156
[2757]157    protected virtual void OnExecutionContextChanged() { }
158    protected virtual void OnCanceledChanged() { }
[776]159    /// <inheritdoc/>
[2]160    public event EventHandler BreakpointChanged;
[776]161    /// <summary>
162    /// Fires a new <c>BreakpointChanged</c> event.
163    /// </summary>
[2]164    protected virtual void OnBreakpointChanged() {
165      if (BreakpointChanged != null) {
[2793]166        BreakpointChanged(this, EventArgs.Empty);
[2]167      }
[2653]168      OnChanged();
[2]169    }
[776]170    /// <inheritdoc/>
[2]171    public event EventHandler Executed;
[776]172    /// <summary>
173    /// Fires a new <c>Executed</c> event.
174    /// </summary>
[2]175    protected virtual void OnExecuted() {
176      if (Executed != null) {
[2793]177        Executed(this, EventArgs.Empty);
[2]178      }
179    }
[2653]180
181    private void Parameters_Changed(object sender, ChangedEventArgs e) {
182      OnChanged(e);
183    }
[2]184  }
185}
Note: See TracBrowser for help on using the repository browser.