[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17209] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
| 22 | using System;
|
---|
[2653] | 23 | using System.Drawing;
|
---|
[9195] | 24 | using System.Linq;
|
---|
[5193] | 25 | using System.Threading;
|
---|
[3376] | 26 | using HeuristicLab.Common;
|
---|
[2805] | 27 | using HeuristicLab.Core;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[2] | 29 |
|
---|
[2805] | 30 | namespace HeuristicLab.Operators {
|
---|
[776] | 31 | /// <summary>
|
---|
[5193] | 32 | /// Base class for operators.
|
---|
[776] | 33 | /// </summary>
|
---|
[2664] | 34 | [Item("Operator", "Base class for operators.")]
|
---|
[16565] | 35 | [StorableType("6AC01841-FF13-41B5-87E4-181F10D08835")]
|
---|
[10149] | 36 | public abstract partial class Operator : ParameterizedNamedItem, IOperator, IStatefulItem {
|
---|
[7201] | 37 | public static new Image StaticItemImage {
|
---|
| 38 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; }
|
---|
| 39 | }
|
---|
[2653] | 40 | public override Image ItemImage {
|
---|
[3306] | 41 | get {
|
---|
[5287] | 42 | if (Breakpoint) return HeuristicLab.Common.Resources.VSImageLibrary.BreakpointActive;
|
---|
[7201] | 43 | else return base.ItemImage;
|
---|
[3306] | 44 | }
|
---|
[2653] | 45 | }
|
---|
[2684] | 46 | public override bool CanChangeDescription {
|
---|
| 47 | get { return false; }
|
---|
| 48 | }
|
---|
[1667] | 49 |
|
---|
[5193] | 50 | private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
|
---|
[2834] | 51 | protected IExecutionContext ExecutionContext {
|
---|
[5193] | 52 | get { return executionContexts.Value.Value; }
|
---|
[2757] | 53 | private set {
|
---|
[5193] | 54 | if (value != executionContexts.Value.Value) {
|
---|
| 55 | executionContexts.Value.Value = value;
|
---|
[2757] | 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
[5193] | 59 | private CancellationToken cancellationToken;
|
---|
| 60 | protected CancellationToken CancellationToken {
|
---|
| 61 | get { return cancellationToken; }
|
---|
[2] | 62 | }
|
---|
[1667] | 63 |
|
---|
| 64 | [Storable]
|
---|
[2653] | 65 | private bool breakpoint;
|
---|
[2] | 66 | public bool Breakpoint {
|
---|
[2653] | 67 | get { return breakpoint; }
|
---|
[2] | 68 | set {
|
---|
[2653] | 69 | if (value != breakpoint) {
|
---|
| 70 | breakpoint = value;
|
---|
[2] | 71 | OnBreakpointChanged();
|
---|
[3306] | 72 | OnItemImageChanged();
|
---|
[2] | 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[4722] | 77 | [StorableConstructor]
|
---|
[16565] | 78 | protected Operator(StorableConstructorFlag _) : base(_) {
|
---|
[6114] | 79 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[5193] | 80 | }
|
---|
[4722] | 81 | protected Operator(Operator original, Cloner cloner)
|
---|
| 82 | : base(original, cloner) {
|
---|
[6114] | 83 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[4722] | 84 | this.breakpoint = original.breakpoint;
|
---|
| 85 | }
|
---|
[2845] | 86 | protected Operator()
|
---|
| 87 | : base() {
|
---|
[6114] | 88 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2653] | 89 | breakpoint = false;
|
---|
[2] | 90 | }
|
---|
[2845] | 91 | protected Operator(string name)
|
---|
| 92 | : base(name) {
|
---|
[6114] | 93 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2845] | 94 | breakpoint = false;
|
---|
| 95 | }
|
---|
| 96 | protected Operator(string name, ParameterCollection parameters)
|
---|
| 97 | : base(name, parameters) {
|
---|
[6114] | 98 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2845] | 99 | breakpoint = false;
|
---|
| 100 | }
|
---|
| 101 | protected Operator(string name, string description)
|
---|
| 102 | : base(name, description) {
|
---|
[6114] | 103 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2845] | 104 | breakpoint = false;
|
---|
| 105 | }
|
---|
| 106 | protected Operator(string name, string description, ParameterCollection parameters)
|
---|
| 107 | : base(name, description, parameters) {
|
---|
[6114] | 108 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2845] | 109 | breakpoint = false;
|
---|
| 110 | }
|
---|
[2] | 111 |
|
---|
[6114] | 112 | public virtual void InitializeState() { }
|
---|
| 113 | public virtual void ClearState() {
|
---|
[6103] | 114 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[5193] | 117 | public virtual IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
|
---|
[2740] | 118 | try {
|
---|
[2757] | 119 | ExecutionContext = context;
|
---|
[5193] | 120 | this.cancellationToken = cancellationToken;
|
---|
[9195] | 121 | foreach (ILookupParameter param in Parameters.OfType<ILookupParameter>())
|
---|
[2740] | 122 | param.ExecutionContext = context;
|
---|
[2834] | 123 | IOperation next = Apply();
|
---|
[2740] | 124 | OnExecuted();
|
---|
| 125 | return next;
|
---|
[6114] | 126 | }
|
---|
| 127 | finally {
|
---|
[9195] | 128 | foreach (ILookupParameter param in Parameters.OfType<ILookupParameter>())
|
---|
[2740] | 129 | param.ExecutionContext = null;
|
---|
[2757] | 130 | ExecutionContext = null;
|
---|
[2740] | 131 | }
|
---|
[2] | 132 | }
|
---|
[2834] | 133 | public abstract IOperation Apply();
|
---|
[2653] | 134 |
|
---|
[2] | 135 | public event EventHandler BreakpointChanged;
|
---|
| 136 | protected virtual void OnBreakpointChanged() {
|
---|
| 137 | if (BreakpointChanged != null) {
|
---|
[2793] | 138 | BreakpointChanged(this, EventArgs.Empty);
|
---|
[2] | 139 | }
|
---|
| 140 | }
|
---|
| 141 | public event EventHandler Executed;
|
---|
| 142 | protected virtual void OnExecuted() {
|
---|
| 143 | if (Executed != null) {
|
---|
[2793] | 144 | Executed(this, EventArgs.Empty);
|
---|
[2] | 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|