Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Operators/3.3/Operator.cs @ 17764

Last change on this file since 17764 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
29
30namespace HeuristicLab.Operators {
31  /// <summary>
32  /// Base class for operators.
33  /// </summary>
34  [Item("Operator", "Base class for operators.")]
35  [StorableType("6AC01841-FF13-41B5-87E4-181F10D08835")]
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(StorableConstructorFlag _) : base(_) {
79      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
80    }
81    protected Operator(Operator original, Cloner cloner)
82      : base(original, cloner) {
83      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
84      this.breakpoint = original.breakpoint;
85    }
86    protected Operator()
87      : base() {
88      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
89      breakpoint = false;
90    }
91    protected Operator(string name)
92      : base(name) {
93      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
94      breakpoint = false;
95    }
96    protected Operator(string name, ParameterCollection parameters)
97      : base(name, parameters) {
98      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
99      breakpoint = false;
100    }
101    protected Operator(string name, string description)
102      : base(name, description) {
103      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
104      breakpoint = false;
105    }
106    protected Operator(string name, string description, ParameterCollection parameters)
107      : base(name, description, parameters) {
108      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
109      breakpoint = false;
110    }
111
112    public virtual void InitializeState() { }
113    public virtual void ClearState() {
114      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
115    }
116
117    public virtual IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
118      try {
119        ExecutionContext = context;
120        this.cancellationToken = cancellationToken;
121        foreach (ILookupParameter param in Parameters.OfType<ILookupParameter>())
122          param.ExecutionContext = context;
123        IOperation next = Apply();
124        OnExecuted();
125        return next;
126      }
127      finally {
128        foreach (ILookupParameter param in Parameters.OfType<ILookupParameter>())
129          param.ExecutionContext = null;
130        ExecutionContext = null;
131      }
132    }
133    public abstract IOperation Apply();
134
135    public event EventHandler BreakpointChanged;
136    protected virtual void OnBreakpointChanged() {
137      if (BreakpointChanged != null) {
138        BreakpointChanged(this, EventArgs.Empty);
139      }
140    }
141    public event EventHandler Executed;
142    protected virtual void OnExecuted() {
143      if (Executed != null) {
144        Executed(this, EventArgs.Empty);
145      }
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.