Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.2/SelectorBase.cs @ 1838

Last change on this file since 1838 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 3.1 KB
RevLine 
[2]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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28
29namespace HeuristicLab.Selection {
[817]30  /// <summary>
31  /// Base class for all selectors.
32  /// </summary>
[2]33  public abstract class SelectorBase : OperatorBase {
[817]34    /// <summary>
35    /// Initializes a new instance of <see cref="SelectorBase"/> with one variable infos
36    /// (<c>CopySelected</c>), which is a local one.
37    /// </summary>
[2]38    public SelectorBase()
39      : base() {
40      AddVariableInfo(new VariableInfo("CopySelected", "Copy or move selected sub-scopes", typeof(BoolData), VariableKind.In));
41      GetVariableInfo("CopySelected").Local = true;
42      AddVariable(new Variable("CopySelected", new BoolData(false)));
43    }
44
[817]45    /// <summary>
46    /// Inserts a new level of sub scopes in the given <paramref name="scope"/> with a scope containing the
47    /// remaining sub scopes and another with the selected ones.
48    /// </summary>
49    /// <remarks>Calls <see cref="Select"/>.</remarks>
50    /// <param name="scope">The scope where to select the sub scopes.</param>
51    /// <returns><c>null</c>.</returns>
[2]52    public override IOperation Apply(IScope scope) {
53      BoolData copySelected = GetVariableValue<BoolData>("CopySelected", scope, true);
54
55      IScope source = new Scope("Remaining");
56      while (scope.SubScopes.Count > 0) {
57        IScope s = scope.SubScopes[0];
58        scope.RemoveSubScope(s);
59        source.AddSubScope(s);
60      }
61      scope.AddSubScope(source);
62      IScope target = new Scope("Selected");
63      scope.AddSubScope(target);
64
65      Select(source, target, copySelected.Data);
66
67      return null;
68    }
69
[817]70    /// <summary>
71    /// Selects sub scopes from the specified <paramref name="source"/> and moves or copies it to the
72    /// specified <paramref name="target"/>.
73    /// </summary>
74    /// <param name="source">The source scope where to select the sub scopes.</param>
75    /// <param name="target">The target where to add the sub scopes.</param>
76    /// <param name="copySelected">Boolean flag whether to copy or move the selected sub scopes.</param>
[2]77    protected abstract void Select(IScope source, IScope target, bool copySelected);
78  }
79}
Note: See TracBrowser for help on using the repository browser.