Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10261 was 10261, checked in by mkommend, 10 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
RevLine 
[10149]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
[10231]22using System.Linq;
[10149]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
[10231]35    private IFixedValueParameter<OperatorList> BeforeExecutionOperatorsParameter {
36      get { return (IFixedValueParameter<OperatorList>)Parameters[BeforeExecutionOperatorsParameterName]; }
[10149]37    }
[10231]38    private IFixedValueParameter<OperatorList> AfterExecutionOperatorsParameter {
39      get { return (IFixedValueParameter<OperatorList>)Parameters[AfterExecutionOperatorsParameterName]; }
[10149]40    }
41
[10231]42    public OperatorList BeforeExecutionOperators {
[10149]43      get { return BeforeExecutionOperatorsParameter.Value; }
44    }
[10231]45    public OperatorList AfterExecutionOperators {
[10149]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() {
[10231]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()));
[10149]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)) {
[10231]67        Parameters.Add(new FixedValueParameter<OperatorList>(BeforeExecutionOperatorsParameterName, "Actions that are executed before the execution of the operator", new OperatorList()));
[10149]68        BeforeExecutionOperatorsParameter.Hidden = true;
69      }
70      if (!Parameters.ContainsKey(AfterExecutionOperatorsParameterName)) {
[10231]71        Parameters.Add(new FixedValueParameter<OperatorList>(AfterExecutionOperatorsParameterName, "Actions that are executed after the execution of the operator", new OperatorList()));
[10149]72        AfterExecutionOperatorsParameter.Hidden = true;
73      }
74      #endregion
75    }
76
77    public sealed override IOperation Apply() {
[10231]78      //to speed up the execution call instrumented apply directly if no before operators exists
79      if (!BeforeExecutionOperators.Any())
80        return InstrumentedApply();
[10149]81
82      //build before operations
[10231]83      var opCol = new OperationCollection();
[10149]84      foreach (var beforeAction in BeforeExecutionOperators) {
[10261]85        var beforeActionOperation = ExecutionContext.CreateChildOperation(beforeAction);
[10149]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() {
[10231]94      if (!AfterExecutionOperators.Any()) {
95        if (Successor != null) return ExecutionContext.CreateOperation(Successor);
96        return null;
97      }
98
[10149]99      var opCol = new OperationCollection();
100      foreach (var afterAction in AfterExecutionOperators) {
[10261]101        var afterActionOperation = ExecutionContext.CreateChildOperation(afterAction);
[10149]102        opCol.Add(afterActionOperation);
103      }
[10231]104
[10149]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.