Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Operators/3.3/Operator.cs @ 6049

Last change on this file since 6049 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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