1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Operators {
|
---|
29 | /// <summary>
|
---|
30 | /// Removes one specified or - if nothing specified - all operators from the given scope.
|
---|
31 | /// </summary>
|
---|
32 | public class SubScopesRemover : OperatorBase {
|
---|
33 | /// <inheritdoc select="summary"/>
|
---|
34 | public override string Description {
|
---|
35 | get {
|
---|
36 | return @"This operator either removes the subscope specified in the variable SubScopeIndex or if this variable is absent, removes all subscopes.";
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// Initializes a new instance of <see cref="SubScopesRemover"/> with one variable info
|
---|
42 | /// (<c>SubScopeIndex</c>).
|
---|
43 | /// </summary>
|
---|
44 | public SubScopesRemover()
|
---|
45 | : base() {
|
---|
46 | AddVariableInfo(new VariableInfo("SubScopeIndex", "(Optional) the index of the subscope to remove", typeof(IntData), VariableKind.In));
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Removes one sub scope with a specified index from the given <paramref name="scope"/>, or if no
|
---|
51 | /// index has been specified, all sub scopes are removed.
|
---|
52 | /// </summary>
|
---|
53 | /// <exception cref="InvalidOperationException">Thrown when no sub scope with the specified index
|
---|
54 | /// exists.</exception>
|
---|
55 | /// <param name="scope">The scope whose sub scope(s) to remove.</param>
|
---|
56 | /// <returns><c>null</c>.</returns>
|
---|
57 | public override IOperation Apply(IScope scope) {
|
---|
58 | IntData index = GetVariableValue<IntData>("SubScopeIndex", scope, true, false);
|
---|
59 | if (index == null) { // remove all scopes
|
---|
60 | while (scope.SubScopes.Count > 0) {
|
---|
61 | scope.RemoveSubScope(scope.SubScopes[0]);
|
---|
62 | }
|
---|
63 | } else {
|
---|
64 | if (index.Data < 0 && index.Data >= scope.SubScopes.Count) throw new InvalidOperationException("ERROR: no scope with index " + index.Data + " exists");
|
---|
65 | scope.RemoveSubScope(scope.SubScopes[index.Data]);
|
---|
66 | }
|
---|
67 | return null;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|