Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Operators/3.3/Operator.InstrumentedOperatorWrapper.cs @ 14928

Last change on this file since 14928 was 14928, checked in by gkronber, 7 years ago

#2520 added unit tests and fixed all test cases for old persistence

File size: 3.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
24using System;
25using System.Linq;
26using System.Threading;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence;
30using HeuristicLab.PluginInfrastructure;
31using ExecutionContext = HeuristicLab.Core.ExecutionContext;
32
33namespace HeuristicLab.Operators {
34  [StorableType("04de2f5a-b727-4044-a2ba-2507c7f34266")]
35  public abstract partial class Operator {
36    internal static IOperation CreateInstrumentedOperation(InstrumentedOperator instrumentedOperator) {
37      var wrapper = new InstrumentedOperatorWrapper(instrumentedOperator);
38      return instrumentedOperator.ExecutionContext.CreateOperation(wrapper);
39    }
40
41    [StorableType("161c4892-a282-41cb-88b1-166b4dc56c08")]
42    [NonDiscoverableType]
43    private class InstrumentedOperatorWrapper : Operator {
44      [Storable]
45      private readonly InstrumentedOperator instrumentedOperator;
46
47      [StorableConstructor]
48      private InstrumentedOperatorWrapper(bool deserializing) : base(deserializing) { }
49
50      private InstrumentedOperatorWrapper(InstrumentedOperatorWrapper original, Cloner cloner)
51        : base(original, cloner) {
52        instrumentedOperator = cloner.Clone(original.instrumentedOperator);
53      }
54
55      public override IDeepCloneable Clone(Cloner cloner) {
56        return new InstrumentedOperatorWrapper(this, cloner);
57      }
58
59      public InstrumentedOperatorWrapper(InstrumentedOperator instrumentedOperator)
60        : base() {
61        this.instrumentedOperator = instrumentedOperator;
62      }
63
64      public override IOperation Apply() {
65        throw new NotSupportedException("InstrumentedOperatorWrapper is not executeable.");
66      }
67
68      public override IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
69        try {
70          //create new executionContext for the instrumented operation to account for parameter lookup and name translation
71          var executionContext = new ExecutionContext(context.Parent, instrumentedOperator, context.Scope);
72          instrumentedOperator.ExecutionContext = executionContext;
73          instrumentedOperator.cancellationToken = cancellationToken;
74          foreach (ILookupParameter param in instrumentedOperator.Parameters.OfType<ILookupParameter>())
75            param.ExecutionContext = executionContext;
76          IOperation next = instrumentedOperator.InstrumentedApply();
77          return next;
78        } finally {
79          foreach (ILookupParameter param in instrumentedOperator.Parameters.OfType<ILookupParameter>())
80            param.ExecutionContext = null;
81          instrumentedOperator.ExecutionContext = null;
82        }
83      }
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.