Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3659 was 3659, checked in by swagner, 14 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

  • removed SubScopesSubScopesLookupParameter
  • adapted SubScopesLookupParameter and renamed it into ScopeTreeLookupParameter
File size: 4.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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Parameters {
30  /// <summary>
31  /// A generic parameter representing instances of type T which are collected from or written to scope tree.
32  /// </summary>
33  [Item("ScopeTreeLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to scope tree.")]
34  [StorableClass]
35  public class ScopeTreeLookupParameter<T> : LookupParameter<ItemArray<T>> where T : class, IItem {
36    [Storable]
37    private int depth;
38    public int Depth {
39      get { return depth; }
40      set {
41        if (value < 0) throw new ArgumentException("Depth must be larger than or equal to 0.");
42        if (depth != value) {
43          depth = value;
44          OnDepthChanged();
45        }
46      }
47    }
48
49    public ScopeTreeLookupParameter()
50      : base() {
51      depth = 1;
52    }
53    public ScopeTreeLookupParameter(string name)
54      : base(name) {
55      depth = 1;
56    }
57    public ScopeTreeLookupParameter(string name, string description)
58      : base(name, description) {
59      depth = 1;
60    }
61    public ScopeTreeLookupParameter(string name, string description, string actualName)
62      : base(name, description, actualName) {
63      depth = 1;
64    }
65    [StorableConstructor]
66    protected ScopeTreeLookupParameter(bool deserializing) : base(deserializing) { }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      ScopeTreeLookupParameter<T> clone = (ScopeTreeLookupParameter<T>)base.Clone(cloner);
70      clone.depth = depth;
71      return clone;
72    }
73
74    protected override IItem GetActualValue() {
75      string name = LookupParameter<ItemArray<T>>.TranslateName(Name, ExecutionContext);
76
77      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
78      for (int i = 0; i < depth; i++)
79        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
80
81      List<T> values = new List<T>();
82      IVariable var;
83      T value;
84      foreach (IScope scope in scopes) {
85        scope.Variables.TryGetValue(name, out var);
86        if (var != null) {
87          value = var.Value as T;
88          if ((var.Value != null) && (value == null))
89            throw new InvalidOperationException(
90              string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
91                            name,
92                            typeof(T).GetPrettyName())
93            );
94          values.Add(value);
95        }
96      }
97      return new ItemArray<T>(values);
98    }
99    protected override void SetActualValue(IItem value) {
100      ItemArray<T> values = value as ItemArray<T>;
101      if (values == null)
102        throw new InvalidOperationException(
103          string.Format("Type mismatch. Value is not a \"{0}\".",
104                        typeof(ItemArray<T>).GetPrettyName())
105        );
106
107      string name = LookupParameter<ItemArray<T>>.TranslateName(Name, ExecutionContext);
108
109      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
110      for (int i = 0; i < depth; i++)
111        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
112
113      if (scopes.Count() != values.Length) throw new InvalidOperationException("Number of values is not equal to number of scopes.");
114
115      int j = 0;
116      IVariable var;
117      foreach (IScope scope in scopes) {
118        scope.Variables.TryGetValue(name, out var);
119        if (var != null) var.Value = values[j];
120        else scope.Variables.Add(new Variable(name, values[j]));
121        j++;
122      }
123    }
124
125    public event EventHandler DepthChanged;
126    protected virtual void OnDepthChanged() {
127      EventHandler handler = DepthChanged;
128      if (handler != null) handler(this, EventArgs.Empty);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.