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