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, 14 years ago

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

File size: 3.8 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Parameters {
28  /// <summary>
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.
30  /// </summary>
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.")]
32  [StorableClass]
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) { }
38
39    protected override IItem GetActualValue() {
40      string name = LookupParameter<ItemArray<ItemArray<T>>>.TranslateName(Name, ExecutionContext);
41      IScope scope = ExecutionContext.Scope;
42      ItemArray<ItemArray<T>> values = new ItemArray<ItemArray<T>>(scope.SubScopes.Count);
43      IVariable var;
44      T value;
45
46      for (int i = 0; i < values.Length; i++) {
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          }
60        }
61      }
62      return values;
63    }
64    protected override void SetActualValue(IItem value) {
65      ItemArray<ItemArray<T>> values = value as ItemArray<ItemArray<T>>;
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
72      string name = LookupParameter<ItemArray<ItemArray<T>>>.TranslateName(Name, ExecutionContext);
73      IScope scope = ExecutionContext.Scope;
74      IVariable var;
75
76      for (int i = 0; i < values.Length; i++) {
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        }
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.