Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15583 was 15583, checked in by swagner, 6 years ago

#2640: Updated year of copyrights in license headers

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