Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Operators/3.3/Operator.cs @ 11164

Last change on this file since 11164 was 10507, checked in by mkommend, 11 years ago

#2119: Merged r10149, r10231, r10261, r10291, r10292, r10295 and r10298 into stable.

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