Free cookie consent management tool by TermsFeed Policy Generator

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

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

Code formatting and minor changes (#1424)

File size: 5.6 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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 {
[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) {
[6114]76      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[5193]77    }
[4722]78    protected Operator(Operator original, Cloner cloner)
79      : base(original, cloner) {
[6114]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() {
[6114]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) {
[6114]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) {
[6114]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) {
[6114]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) {
[6114]105      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2845]106      breakpoint = false;
107    }
[2]108
[6114]109    public virtual void InitializeState() { }
110    public virtual void ClearState() {
[6103]111      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
112    }
113
[5193]114    public virtual IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
[2740]115      try {
[2757]116        ExecutionContext = context;
[5193]117        this.cancellationToken = cancellationToken;
[2740]118        foreach (IParameter param in Parameters)
119          param.ExecutionContext = context;
[2834]120        IOperation next = Apply();
[2740]121        OnExecuted();
122        return next;
[6114]123      }
124      finally {
[2740]125        foreach (IParameter param in Parameters)
126          param.ExecutionContext = null;
[2757]127        ExecutionContext = null;
[2740]128      }
[2]129    }
[2834]130    public abstract IOperation Apply();
[2653]131
[2]132    public event EventHandler BreakpointChanged;
133    protected virtual void OnBreakpointChanged() {
134      if (BreakpointChanged != null) {
[2793]135        BreakpointChanged(this, EventArgs.Empty);
[2]136      }
137    }
138    public event EventHandler Executed;
139    protected virtual void OnExecuted() {
140      if (Executed != null) {
[2793]141        Executed(this, EventArgs.Empty);
[2]142      }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.