Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Operators/3.3/InstrumentedOperator.cs @ 12858

Last change on this file since 12858 was 10149, checked in by mkommend, 11 years ago

#2119: Implemented instrumentation for operators.

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