Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/SubScopesLookupParameter.cs @ 2803

Last change on this file since 2803 was 2796, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • worked on operators, parameters and problems
File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Xml;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Parameters {
31  /// <summary>
32  /// A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.
33  /// </summary>
34  [Item("SubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the current scope.")]
35  public class SubScopesLookupParameter<T> : LookupParameter<ItemArray<T>> where T : class, IItem {
36    public SubScopesLookupParameter() : base() { }
37    public SubScopesLookupParameter(string name) : base(name) { }
38    public SubScopesLookupParameter(string name, string description) : base(name, description) { }
39
40    protected override IItem GetActualValue() {
41      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
42      IScope scope = ExecutionContext.Scope;
43      ItemArray<T> values = new ItemArray<T>(scope.SubScopes.Count);
44      IVariable var;
45      T value;
46
47      for (int i = 0; i < values.Length; i++) {
48        scope.SubScopes[i].Variables.TryGetValue(name, out var);
49        if (var != null) {
50          value = var.Value as T;
51          if (value == null)
52            throw new InvalidOperationException(
53              string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
54                            name,
55                            typeof(T).GetPrettyName())
56            );
57          values[i] = value;
58        }
59      }
60      return values;
61    }
62    protected override void SetActualValue(IItem value) {
63      ItemArray<T> values = value as ItemArray<T>;
64      if (values == null)
65        throw new InvalidOperationException(
66          string.Format("Type mismatch. Value is not a \"{0}\".",
67                        typeof(ItemArray<T>).GetPrettyName())
68        );
69
70      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
71      IScope scope = ExecutionContext.Scope;
72      IVariable var;
73
74      for (int i = 0; i < values.Length; i++) {
75        scope.SubScopes[i].Variables.TryGetValue(name, out var);
76        if (var != null) var.Value = values[i];
77        else scope.SubScopes[i].Variables.Add(new Variable(name, values[i]));
78      }
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.