Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5404 was 5287, checked in by abeham, 14 years ago

#1337

  • Renamed VS2008ImageLibrary resource to VSImageLibrary
  • Added Filter icon to the VS2010ImageLibrary folder and the resource manager
File size: 5.3 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;
[5193]24using System.Threading;
[3376]25using HeuristicLab.Common;
[2805]26using HeuristicLab.Core;
[1823]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]28
[2805]29namespace HeuristicLab.Operators {
[776]30  /// <summary>
[5193]31  /// Base class for operators.
[776]32  /// </summary>
[2664]33  [Item("Operator", "Base class for operators.")]
[3017]34  [StorableClass]
[2845]35  public abstract class Operator : ParameterizedNamedItem, IOperator {
[2653]36    public override Image ItemImage {
[3306]37      get {
[5287]38        if (Breakpoint) return HeuristicLab.Common.Resources.VSImageLibrary.BreakpointActive;
39        else return HeuristicLab.Common.Resources.VSImageLibrary.Method;
[3306]40      }
[2653]41    }
[2684]42    public override bool CanChangeDescription {
43      get { return false; }
44    }
[1667]45
[5193]46    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
[2834]47    protected IExecutionContext ExecutionContext {
[5193]48      get { return executionContexts.Value.Value; }
[2757]49      private set {
[5193]50        if (value != executionContexts.Value.Value) {
51          executionContexts.Value.Value = value;
[2757]52        }
53      }
54    }
[5193]55    private CancellationToken cancellationToken;
56    protected CancellationToken CancellationToken {
57      get { return cancellationToken; }
[2]58    }
[1667]59
60    [Storable]
[2653]61    private bool breakpoint;
[2]62    public bool Breakpoint {
[2653]63      get { return breakpoint; }
[2]64      set {
[2653]65        if (value != breakpoint) {
66          breakpoint = value;
[2]67          OnBreakpointChanged();
[3306]68          OnItemImageChanged();
[2]69        }
70      }
71    }
72
[4722]73    [StorableConstructor]
[5193]74    protected Operator(bool deserializing)
75      : base(deserializing) {
76      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
77    }
[4722]78    protected Operator(Operator original, Cloner cloner)
79      : base(original, cloner) {
[5193]80      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[4722]81      this.breakpoint = original.breakpoint;
82    }
[2845]83    protected Operator()
84      : base() {
[5193]85      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2653]86      breakpoint = false;
[2]87    }
[2845]88    protected Operator(string name)
89      : base(name) {
[5193]90      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]91      breakpoint = false;
92    }
93    protected Operator(string name, ParameterCollection parameters)
94      : base(name, parameters) {
[5193]95      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]96      breakpoint = false;
97    }
98    protected Operator(string name, string description)
99      : base(name, description) {
[5193]100      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]101      breakpoint = false;
102    }
103    protected Operator(string name, string description, ParameterCollection parameters)
104      : base(name, description, parameters) {
[5193]105      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]106      breakpoint = false;
107    }
[2]108
[5193]109    public virtual IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
[2740]110      try {
[2757]111        ExecutionContext = context;
[5193]112        this.cancellationToken = cancellationToken;
[2740]113        foreach (IParameter param in Parameters)
114          param.ExecutionContext = context;
[2834]115        IOperation next = Apply();
[2740]116        OnExecuted();
117        return next;
[5287]118      } finally {
[2740]119        foreach (IParameter param in Parameters)
120          param.ExecutionContext = null;
[2757]121        ExecutionContext = null;
[2740]122      }
[2]123    }
[2834]124    public abstract IOperation Apply();
[2653]125
[2]126    public event EventHandler BreakpointChanged;
127    protected virtual void OnBreakpointChanged() {
128      if (BreakpointChanged != null) {
[2793]129        BreakpointChanged(this, EventArgs.Empty);
[2]130      }
131    }
132    public event EventHandler Executed;
133    protected virtual void OnExecuted() {
134      if (Executed != null) {
[2793]135        Executed(this, EventArgs.Empty);
[2]136      }
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.