#region License Information /* HeuristicLab * Copyright (C) 2002-2014 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 System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.ALPS { [Item("LastSubScopeCloner", "An operator which clones the last sub-scope of the current scope.")] [StorableClass] public sealed class LastSubScopeCloner : SingleSuccessorOperator { [StorableConstructor] private LastSubScopeCloner(bool deserializing) : base(deserializing) { } private LastSubScopeCloner(LastSubScopeCloner original, Cloner cloner) : base(original, cloner) { } public LastSubScopeCloner() : base() { Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes should be cloned.")); Parameters.Add(new ValueParameter("NumberOfCopies", "The number of copies that should be created.", new IntValue(1))); } public override IDeepCloneable Clone(Cloner cloner) { return new LastSubScopeCloner(this, cloner); } public override IOperation Apply() { var scopes = ExecutionContext.Scope.SubScopes; if (scopes.Count < 1) throw new ArgumentException("At least one sub-scope must exist."); var lastSubScope = scopes.Last(); var clone = (IScope)lastSubScope.Clone(new Cloner()); int number; if (int.TryParse(clone.Name, out number)) clone.Name = (number + 1).ToString(); scopes.Add(clone); return base.Apply(); } } }