source: trunk/sources/HeuristicLab.Operators/3.3/InstrumentedOperator.cs @ 10261

Last change on this file since 10261 was 10261, checked in by mkommend, 7 years ago

#2119: Chained exeuction contexts of before and after operators in the instrumented operator to enable a parameter lookup.

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Operators {
29  [Item("InstrumentedOperator", "A operator that can execute pre- and post actions.")]
30  [StorableClass]
31  public abstract class InstrumentedOperator : SingleSuccessorOperator {
32    private const string BeforeExecutionOperatorsParameterName = "BeforeExecutionOperators";
33    private const string AfterExecutionOperatorsParameterName = "AfterExecutionOperators";
34
35    private IFixedValueParameter<OperatorList> BeforeExecutionOperatorsParameter {
36      get { return (IFixedValueParameter<OperatorList>)Parameters[BeforeExecutionOperatorsParameterName]; }
37    }
38    private IFixedValueParameter<OperatorList> AfterExecutionOperatorsParameter {
39      get { return (IFixedValueParameter<OperatorList>)Parameters[AfterExecutionOperatorsParameterName]; }
40    }
41
42    public OperatorList BeforeExecutionOperators {
43      get { return BeforeExecutionOperatorsParameter.Value; }
44    }
45    public OperatorList AfterExecutionOperators {
46      get { return AfterExecutionOperatorsParameter.Value; }
47    }
48
49    [StorableConstructor]
50    protected InstrumentedOperator(bool deserializing) : base(deserializing) { }
51    protected InstrumentedOperator(InstrumentedOperator original, Cloner cloner)
52      : base(original, cloner) {
53    }
54    protected InstrumentedOperator()
55      : base() {
56      Parameters.Add(new FixedValueParameter<OperatorList>(BeforeExecutionOperatorsParameterName, "Actions that are executed before the execution of the operator", new OperatorList()));
57      Parameters.Add(new FixedValueParameter<OperatorList>(AfterExecutionOperatorsParameterName, "Actions that are executed after the execution of the operator", new OperatorList()));
58      BeforeExecutionOperatorsParameter.Hidden = true;
59      AfterExecutionOperatorsParameter.Hidden = true;
60    }
61
62    [StorableHook(HookType.AfterDeserialization)]
63    private void AfterDeserialization() {
64      // BackwardsCompatibility3.3
65      #region Backwards compatible code, remove with 3.4
66      if (!Parameters.ContainsKey(BeforeExecutionOperatorsParameterName)) {
67        Parameters.Add(new FixedValueParameter<OperatorList>(BeforeExecutionOperatorsParameterName, "Actions that are executed before the execution of the operator", new OperatorList()));
68        BeforeExecutionOperatorsParameter.Hidden = true;
69      }
70      if (!Parameters.ContainsKey(AfterExecutionOperatorsParameterName)) {
71        Parameters.Add(new FixedValueParameter<OperatorList>(AfterExecutionOperatorsParameterName, "Actions that are executed after the execution of the operator", new OperatorList()));
72        AfterExecutionOperatorsParameter.Hidden = true;
73      }
74      #endregion
75    }
76
77    public sealed override IOperation Apply() {
78      //to speed up the execution call instrumented apply directly if no before operators exists
79      if (!BeforeExecutionOperators.Any())
80        return InstrumentedApply();
81
82      //build before operations
83      var opCol = new OperationCollection();
84      foreach (var beforeAction in BeforeExecutionOperators) {
85        var beforeActionOperation = ExecutionContext.CreateChildOperation(beforeAction);
86        opCol.Add(beforeActionOperation);
87      }
88      //build operation for the instrumented apply
89      opCol.Add(CreateInstrumentedOperation(this));
90      return opCol;
91    }
92
93    public virtual IOperation InstrumentedApply() {
94      if (!AfterExecutionOperators.Any()) {
95        if (Successor != null) return ExecutionContext.CreateOperation(Successor);
96        return null;
97      }
98
99      var opCol = new OperationCollection();
100      foreach (var afterAction in AfterExecutionOperators) {
101        var afterActionOperation = ExecutionContext.CreateChildOperation(afterAction);
102        opCol.Add(afterActionOperation);
103      }
104
105      if (Successor != null)
106        opCol.Add(ExecutionContext.CreateOperation(Successor));
107      return opCol;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.