Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/SubScopesSubScopesLookupParameter.cs @ 3634

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

Worked on best solution analysis for the TSP (#999)

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