Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Operators/3.3/Operator.cs @ 13777

Last change on this file since 13777 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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