[10149] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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] | 22 | using System.Collections.Generic;
|
---|
[10231] | 23 | using System.Linq;
|
---|
[10149] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace 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;
|
---|
| 63 | AfterExecutionOperatorsParameter.Hidden = true;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 67 | private void AfterDeserialization() {
|
---|
| 68 | // BackwardsCompatibility3.3
|
---|
| 69 | #region Backwards compatible code, remove with 3.4
|
---|
| 70 | if (!Parameters.ContainsKey(BeforeExecutionOperatorsParameterName)) {
|
---|
[10231] | 71 | Parameters.Add(new FixedValueParameter<OperatorList>(BeforeExecutionOperatorsParameterName, "Actions that are executed before the execution of the operator", new OperatorList()));
|
---|
[10149] | 72 | BeforeExecutionOperatorsParameter.Hidden = true;
|
---|
| 73 | }
|
---|
| 74 | if (!Parameters.ContainsKey(AfterExecutionOperatorsParameterName)) {
|
---|
[10231] | 75 | Parameters.Add(new FixedValueParameter<OperatorList>(AfterExecutionOperatorsParameterName, "Actions that are executed after the execution of the operator", new OperatorList()));
|
---|
[10149] | 76 | AfterExecutionOperatorsParameter.Hidden = true;
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public sealed override IOperation Apply() {
|
---|
[10231] | 82 | //to speed up the execution call instrumented apply directly if no before operators exists
|
---|
| 83 | if (!BeforeExecutionOperators.Any())
|
---|
| 84 | return InstrumentedApply();
|
---|
[10149] | 85 |
|
---|
| 86 | //build before operations
|
---|
[10231] | 87 | var opCol = new OperationCollection();
|
---|
[10149] | 88 | foreach (var beforeAction in BeforeExecutionOperators) {
|
---|
[10261] | 89 | var beforeActionOperation = ExecutionContext.CreateChildOperation(beforeAction);
|
---|
[10149] | 90 | opCol.Add(beforeActionOperation);
|
---|
| 91 | }
|
---|
| 92 | //build operation for the instrumented apply
|
---|
| 93 | opCol.Add(CreateInstrumentedOperation(this));
|
---|
| 94 | return opCol;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public virtual IOperation InstrumentedApply() {
|
---|
[10231] | 98 | if (!AfterExecutionOperators.Any()) {
|
---|
| 99 | if (Successor != null) return ExecutionContext.CreateOperation(Successor);
|
---|
| 100 | return null;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[10149] | 103 | var opCol = new OperationCollection();
|
---|
| 104 | foreach (var afterAction in AfterExecutionOperators) {
|
---|
[10261] | 105 | var afterActionOperation = ExecutionContext.CreateChildOperation(afterAction);
|
---|
[10149] | 106 | opCol.Add(afterActionOperation);
|
---|
| 107 | }
|
---|
[10231] | 108 |
|
---|
[10149] | 109 | if (Successor != null)
|
---|
| 110 | opCol.Add(ExecutionContext.CreateOperation(Successor));
|
---|
| 111 | return opCol;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|