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 |
|
---|
22 |
|
---|
23 | using System;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
32 | [Item("InitializableOperator", "An operator that can be initialized on it's first execution.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class InitializableOperator : SingleSuccessorOperator {
|
---|
35 |
|
---|
36 | [Storable]
|
---|
37 | private Guid guid;
|
---|
38 |
|
---|
39 | public ILookupParameter<BoolValue> Initialized {
|
---|
40 | get { return (ILookupParameter<BoolValue>)Parameters[guid.ToString()]; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | protected InitializableOperator(bool deserializing) : base(deserializing) { }
|
---|
45 | protected InitializableOperator(InitializableOperator original, Cloner cloner)
|
---|
46 | : base(original, cloner) {
|
---|
47 | guid = original.guid;
|
---|
48 | }
|
---|
49 | public InitializableOperator()
|
---|
50 | : base() {
|
---|
51 | guid = Guid.NewGuid();
|
---|
52 | Parameters.Add(new LookupParameter<BoolValue>(guid.ToString(), "Indicates if the operator is initialized."));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new InitializableOperator(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected void Initialize() {
|
---|
60 | if (Initialized.ActualValue == null) {
|
---|
61 | AddVariableToGlobalScope(guid.ToString(), "Indicates if the operator is initialized.", new BoolValue(true));
|
---|
62 | InitializeAction();
|
---|
63 | } else if (!Initialized.ActualValue.Value) {
|
---|
64 | Initialized.ActualValue.Value = true;
|
---|
65 | InitializeAction();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void AddVariableToGlobalScope(string name, string description, BoolValue value) {
|
---|
70 | var globalScope = ExecutionContext;
|
---|
71 | while (globalScope.Parent != null) {
|
---|
72 | globalScope = globalScope.Parent;
|
---|
73 | }
|
---|
74 |
|
---|
75 | globalScope.Scope.Variables.Add(new Variable(name, description, value));
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected virtual void InitializeAction() { }
|
---|
79 | }
|
---|
80 | }
|
---|