Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/SelectorBase.cs @ 1462

Last change on this file since 1462 was 817, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Selection namespace (#331)

File size: 3.1 KB
Line 
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 {
30  /// <summary>
31  /// Base class for all selectors.
32  /// </summary>
33  public abstract class SelectorBase : OperatorBase {
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>
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
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>
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
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>
77    protected abstract void Select(IScope source, IScope target, bool copySelected);
78  }
79}
Note: See TracBrowser for help on using the repository browser.