[10149] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[14186] | 4 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10149] | 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | using System;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Threading;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.PluginInfrastructure;
|
---|
| 31 | using ExecutionContext = HeuristicLab.Core.ExecutionContext;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Operators {
|
---|
| 34 | public abstract partial class Operator {
|
---|
| 35 | internal static IOperation CreateInstrumentedOperation(InstrumentedOperator instrumentedOperator) {
|
---|
| 36 | var wrapper = new InstrumentedOperatorWrapper(instrumentedOperator);
|
---|
| 37 | return instrumentedOperator.ExecutionContext.CreateOperation(wrapper);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | [StorableClass]
|
---|
| 41 | [NonDiscoverableType]
|
---|
| 42 | private class InstrumentedOperatorWrapper : Operator {
|
---|
| 43 | [Storable]
|
---|
| 44 | private readonly InstrumentedOperator instrumentedOperator;
|
---|
| 45 |
|
---|
| 46 | [StorableConstructor]
|
---|
| 47 | private InstrumentedOperatorWrapper(bool deserializing) : base(deserializing) { }
|
---|
| 48 |
|
---|
| 49 | private InstrumentedOperatorWrapper(InstrumentedOperatorWrapper original, Cloner cloner)
|
---|
| 50 | : base(original, cloner) {
|
---|
| 51 | instrumentedOperator = cloner.Clone(original.instrumentedOperator);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 55 | return new InstrumentedOperatorWrapper(this, cloner);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public InstrumentedOperatorWrapper(InstrumentedOperator instrumentedOperator)
|
---|
| 59 | : base() {
|
---|
| 60 | this.instrumentedOperator = instrumentedOperator;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public override IOperation Apply() {
|
---|
| 64 | throw new NotSupportedException("InstrumentedOperatorWrapper is not executeable.");
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public override IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
|
---|
| 68 | try {
|
---|
| 69 | //create new executionContext for the instrumented operation to account for parameter lookup and name translation
|
---|
| 70 | var executionContext = new ExecutionContext(context.Parent, instrumentedOperator, context.Scope);
|
---|
| 71 | instrumentedOperator.ExecutionContext = executionContext;
|
---|
| 72 | instrumentedOperator.cancellationToken = cancellationToken;
|
---|
| 73 | foreach (ILookupParameter param in instrumentedOperator.Parameters.OfType<ILookupParameter>())
|
---|
| 74 | param.ExecutionContext = executionContext;
|
---|
| 75 | IOperation next = instrumentedOperator.InstrumentedApply();
|
---|
| 76 | return next;
|
---|
| 77 | }
|
---|
| 78 | finally {
|
---|
| 79 | foreach (ILookupParameter param in instrumentedOperator.Parameters.OfType<ILookupParameter>())
|
---|
| 80 | param.ExecutionContext = null;
|
---|
| 81 | instrumentedOperator.ExecutionContext = null;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|