Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2693 was 2684, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • continued work on adapting and refactoring HeuristicLab.Data according to the changes in HeuristicLab.Core
  • started work on adapting and refactoring HeuristicLab.Operators according to changes in HeuristicLab.Core
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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    /// <summary>
64    /// Flag whether the current instance has been canceled.
65    /// </summary>
66    private bool canceled;
67    /// <inheritdoc/>
68    protected bool Canceled {
69      get { return canceled; }
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        }
83      }
84    }
85
86    /// <summary>
87    /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
88    /// the canceled flag to <c>false</c> and the name of the operator to the type name.
89    /// </summary>
90    protected Operator() {
91      name = ItemName;
92      Parameters = new ParameterCollection();
93      readOnlyParameters = null;
94      canceled = false;
95      breakpoint = false;
96    }
97
98    /// <summary>
99    /// Clones the current instance (deep clone).
100    /// </summary>
101    /// <remarks>Clones also sub operators, variables and variable infos.</remarks>
102    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
103    /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
104    public override IDeepCloneable Clone(Cloner cloner) {
105      Operator clone = (Operator)base.Clone(cloner);
106      clone.Parameters = (ParameterCollection)cloner.Clone(parameters);
107      clone.canceled = canceled;
108      clone.breakpoint = breakpoint;
109      return clone;
110    }
111
112    /// <inheritdoc/>
113    public virtual ExecutionContextCollection Execute(ExecutionContext context) {
114      canceled = false;
115      ExecutionContextCollection next = Apply(context);
116      OnExecuted();
117      return next;
118    }
119    /// <inheritdoc/>
120    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
121    public virtual void Abort() {
122      canceled = true;
123    }
124    /// <summary>
125    /// Performs the current operator on the specified <paramref name="scope"/>.
126    /// </summary>
127    /// <param name="scope">The scope where to execute the operator</param>
128    /// <returns><c>null</c>.</returns>
129    public virtual ExecutionContextCollection Apply(ExecutionContext context) {
130      return new ExecutionContextCollection();
131    }
132
133    /// <inheritdoc/>
134    public event EventHandler BreakpointChanged;
135    /// <summary>
136    /// Fires a new <c>BreakpointChanged</c> event.
137    /// </summary>
138    protected virtual void OnBreakpointChanged() {
139      if (BreakpointChanged != null) {
140        BreakpointChanged(this, new EventArgs());
141      }
142      OnChanged();
143    }
144    /// <inheritdoc/>
145    public event EventHandler Executed;
146    /// <summary>
147    /// Fires a new <c>Executed</c> event.
148    /// </summary>
149    protected virtual void OnExecuted() {
150      if (Executed != null) {
151        Executed(this, new EventArgs());
152      }
153    }
154
155    private void Parameters_Changed(object sender, ChangedEventArgs e) {
156      OnChanged(e);
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.