Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8679 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 5.7 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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]
[6103]35  public abstract class Operator : ParameterizedNamedItem, IOperator, IStatefulItem {
[7201]36    public static new Image StaticItemImage {
37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; }
38    }
[2653]39    public override Image ItemImage {
[3306]40      get {
[5287]41        if (Breakpoint) return HeuristicLab.Common.Resources.VSImageLibrary.BreakpointActive;
[7201]42        else return base.ItemImage;
[3306]43      }
[2653]44    }
[2684]45    public override bool CanChangeDescription {
46      get { return false; }
47    }
[1667]48
[5193]49    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
[2834]50    protected IExecutionContext ExecutionContext {
[5193]51      get { return executionContexts.Value.Value; }
[2757]52      private set {
[5193]53        if (value != executionContexts.Value.Value) {
54          executionContexts.Value.Value = value;
[2757]55        }
56      }
57    }
[5193]58    private CancellationToken cancellationToken;
59    protected CancellationToken CancellationToken {
60      get { return cancellationToken; }
[2]61    }
[1667]62
63    [Storable]
[2653]64    private bool breakpoint;
[2]65    public bool Breakpoint {
[2653]66      get { return breakpoint; }
[2]67      set {
[2653]68        if (value != breakpoint) {
69          breakpoint = value;
[2]70          OnBreakpointChanged();
[3306]71          OnItemImageChanged();
[2]72        }
73      }
74    }
75
[4722]76    [StorableConstructor]
[5193]77    protected Operator(bool deserializing)
78      : base(deserializing) {
[6114]79      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[5193]80    }
[4722]81    protected Operator(Operator original, Cloner cloner)
82      : base(original, cloner) {
[6114]83      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[4722]84      this.breakpoint = original.breakpoint;
85    }
[2845]86    protected Operator()
87      : base() {
[6114]88      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2653]89      breakpoint = false;
[2]90    }
[2845]91    protected Operator(string name)
92      : base(name) {
[6114]93      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]94      breakpoint = false;
95    }
96    protected Operator(string name, ParameterCollection parameters)
97      : base(name, parameters) {
[6114]98      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]99      breakpoint = false;
100    }
101    protected Operator(string name, string description)
102      : base(name, description) {
[6114]103      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]104      breakpoint = false;
105    }
106    protected Operator(string name, string description, ParameterCollection parameters)
107      : base(name, description, parameters) {
[6114]108      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]109      breakpoint = false;
110    }
[2]111
[6114]112    public virtual void InitializeState() { }
113    public virtual void ClearState() {
[6103]114      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
115    }
116
[5193]117    public virtual IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
[2740]118      try {
[2757]119        ExecutionContext = context;
[5193]120        this.cancellationToken = cancellationToken;
[2740]121        foreach (IParameter param in Parameters)
122          param.ExecutionContext = context;
[2834]123        IOperation next = Apply();
[2740]124        OnExecuted();
125        return next;
[6114]126      }
127      finally {
[2740]128        foreach (IParameter param in Parameters)
129          param.ExecutionContext = null;
[2757]130        ExecutionContext = null;
[2740]131      }
[2]132    }
[2834]133    public abstract IOperation Apply();
[2653]134
[2]135    public event EventHandler BreakpointChanged;
136    protected virtual void OnBreakpointChanged() {
137      if (BreakpointChanged != null) {
[2793]138        BreakpointChanged(this, EventArgs.Empty);
[2]139      }
140    }
141    public event EventHandler Executed;
142    protected virtual void OnExecuted() {
143      if (Executed != null) {
[2793]144        Executed(this, EventArgs.Empty);
[2]145      }
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.