[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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;
|
---|
[1823] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[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.")]
|
---|
[3017] | 35 | [StorableClass]
|
---|
[6103] | 36 | public abstract 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]
|
---|
[5193] | 78 | protected Operator(bool deserializing)
|
---|
| 79 | : base(deserializing) {
|
---|
[6114] | 80 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[5193] | 81 | }
|
---|
[4722] | 82 | protected Operator(Operator original, Cloner cloner)
|
---|
| 83 | : base(original, cloner) {
|
---|
[6114] | 84 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[4722] | 85 | this.breakpoint = original.breakpoint;
|
---|
| 86 | }
|
---|
[2845] | 87 | protected Operator()
|
---|
| 88 | : base() {
|
---|
[6114] | 89 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2653] | 90 | breakpoint = false;
|
---|
[2] | 91 | }
|
---|
[2845] | 92 | protected Operator(string name)
|
---|
| 93 | : base(name) {
|
---|
[6114] | 94 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2845] | 95 | breakpoint = false;
|
---|
| 96 | }
|
---|
| 97 | protected Operator(string name, ParameterCollection parameters)
|
---|
| 98 | : base(name, parameters) {
|
---|
[6114] | 99 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2845] | 100 | breakpoint = false;
|
---|
| 101 | }
|
---|
| 102 | protected Operator(string name, string description)
|
---|
| 103 | : base(name, description) {
|
---|
[6114] | 104 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2845] | 105 | breakpoint = false;
|
---|
| 106 | }
|
---|
| 107 | protected Operator(string name, string description, ParameterCollection parameters)
|
---|
| 108 | : base(name, description, parameters) {
|
---|
[6114] | 109 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
[2845] | 110 | breakpoint = false;
|
---|
| 111 | }
|
---|
[2] | 112 |
|
---|
[6114] | 113 | public virtual void InitializeState() { }
|
---|
| 114 | public virtual void ClearState() {
|
---|
[6103] | 115 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[5193] | 118 | public virtual IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
|
---|
[2740] | 119 | try {
|
---|
[2757] | 120 | ExecutionContext = context;
|
---|
[5193] | 121 | this.cancellationToken = cancellationToken;
|
---|
[9195] | 122 | foreach (ILookupParameter param in Parameters.OfType<ILookupParameter>())
|
---|
[2740] | 123 | param.ExecutionContext = context;
|
---|
[2834] | 124 | IOperation next = Apply();
|
---|
[2740] | 125 | OnExecuted();
|
---|
| 126 | return next;
|
---|
[6114] | 127 | }
|
---|
| 128 | finally {
|
---|
[9195] | 129 | foreach (ILookupParameter param in Parameters.OfType<ILookupParameter>())
|
---|
[2740] | 130 | param.ExecutionContext = null;
|
---|
[2757] | 131 | ExecutionContext = null;
|
---|
[2740] | 132 | }
|
---|
[2] | 133 | }
|
---|
[2834] | 134 | public abstract IOperation Apply();
|
---|
[2653] | 135 |
|
---|
[2] | 136 | public event EventHandler BreakpointChanged;
|
---|
| 137 | protected virtual void OnBreakpointChanged() {
|
---|
| 138 | if (BreakpointChanged != null) {
|
---|
[2793] | 139 | BreakpointChanged(this, EventArgs.Empty);
|
---|
[2] | 140 | }
|
---|
| 141 | }
|
---|
| 142 | public event EventHandler Executed;
|
---|
| 143 | protected virtual void OnExecuted() {
|
---|
| 144 | if (Executed != null) {
|
---|
[2793] | 145 | Executed(this, EventArgs.Empty);
|
---|
[2] | 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | }
|
---|