#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers { [Item("InitializableOperator", "An operator that can be initialized on it's first execution.")] [StorableClass] public class InitializableOperator : SingleSuccessorOperator { [Storable] private Guid guid; public ILookupParameter Initialized { get { return (ILookupParameter)Parameters[guid.ToString()]; } } [StorableConstructor] protected InitializableOperator(bool deserializing) : base(deserializing) { } protected InitializableOperator(InitializableOperator original, Cloner cloner) : base(original, cloner) { guid = original.guid; } public InitializableOperator() : base() { guid = Guid.NewGuid(); Parameters.Add(new LookupParameter(guid.ToString(), "Indicates if the operator is initialized.")); } public override IDeepCloneable Clone(Cloner cloner) { return new InitializableOperator(this, cloner); } protected void Initialize() { if (Initialized.ActualValue == null) { AddVariableToGlobalScope(guid.ToString(), "Indicates if the operator is initialized.", new BoolValue(true)); InitializeAction(); } else if (!Initialized.ActualValue.Value) { Initialized.ActualValue.Value = true; InitializeAction(); } } private void AddVariableToGlobalScope(string name, string description, BoolValue value) { var globalScope = ExecutionContext; while (globalScope.Parent != null) { globalScope = globalScope.Parent; } globalScope.Scope.Variables.Add(new Variable(name, description, value)); } protected virtual void InitializeAction() { } } }