Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Operators/3.3/Operator.cs @ 6714

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

Code formatting and minor changes (#1424)

File size: 5.6 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, IStatefulItem {
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 void InitializeState() { }
110    public virtual void ClearState() {
111      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
112    }
113
114    public virtual IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
115      try {
116        ExecutionContext = context;
117        this.cancellationToken = cancellationToken;
118        foreach (IParameter param in Parameters)
119          param.ExecutionContext = context;
120        IOperation next = Apply();
121        OnExecuted();
122        return next;
123      }
124      finally {
125        foreach (IParameter param in Parameters)
126          param.ExecutionContext = null;
127        ExecutionContext = null;
128      }
129    }
130    public abstract IOperation Apply();
131
132    public event EventHandler BreakpointChanged;
133    protected virtual void OnBreakpointChanged() {
134      if (BreakpointChanged != null) {
135        BreakpointChanged(this, EventArgs.Empty);
136      }
137    }
138    public event EventHandler Executed;
139    protected virtual void OnExecuted() {
140      if (Executed != null) {
141        Executed(this, EventArgs.Empty);
142      }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.