Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Operators/3.3/Operator.cs @ 12648

Last change on this file since 12648 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 static new Image StaticItemImage {
37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; }
38    }
39    public override Image ItemImage {
40      get {
41        if (Breakpoint) return HeuristicLab.Common.Resources.VSImageLibrary.BreakpointActive;
42        else return base.ItemImage;
43      }
44    }
45    public override bool CanChangeDescription {
46      get { return false; }
47    }
48
49    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
50    protected IExecutionContext ExecutionContext {
51      get { return executionContexts.Value.Value; }
52      private set {
53        if (value != executionContexts.Value.Value) {
54          executionContexts.Value.Value = value;
55        }
56      }
57    }
58    private CancellationToken cancellationToken;
59    protected CancellationToken CancellationToken {
60      get { return cancellationToken; }
61    }
62
63    [Storable]
64    private bool breakpoint;
65    public bool Breakpoint {
66      get { return breakpoint; }
67      set {
68        if (value != breakpoint) {
69          breakpoint = value;
70          OnBreakpointChanged();
71          OnItemImageChanged();
72        }
73      }
74    }
75
76    [StorableConstructor]
77    protected Operator(bool deserializing)
78      : base(deserializing) {
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 (IParameter param in Parameters)
122          param.ExecutionContext = context;
123        IOperation next = Apply();
124        OnExecuted();
125        return next;
126      }
127      finally {
128        foreach (IParameter param in Parameters)
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.