Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/ScopeTreeLookupParameter.cs @ 4179

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

Implemented reviewers' comments (#863)

  • adapted item names of generic items to reflect the type of their generic parameters
File size: 5.1 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", "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>>, IScopeTreeLookupParameter<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, int depth)
58      : base(name) {
59      this.depth = depth;
60    }
61    public ScopeTreeLookupParameter(string name, string description)
62      : base(name, description) {
63      depth = 1;
64    }
65    public ScopeTreeLookupParameter(string name, string description, int depth)
66      : base(name, description) {
67      this.depth = depth;
68    }
69    public ScopeTreeLookupParameter(string name, string description, string actualName)
70      : base(name, description, actualName) {
71      depth = 1;
72    }
73    public ScopeTreeLookupParameter(string name, string description, string actualName, int depth)
74      : base(name, description, actualName) {
75      this.depth = depth;
76    }
77    [StorableConstructor]
78    protected ScopeTreeLookupParameter(bool deserializing) : base(deserializing) { }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      ScopeTreeLookupParameter<T> clone = (ScopeTreeLookupParameter<T>)base.Clone(cloner);
82      clone.depth = depth;
83      return clone;
84    }
85
86    protected override IItem GetActualValue() {
87      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
88      for (int i = 0; i < depth; i++)
89        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
90
91      string name = TranslatedName;
92      List<T> values = new List<T>();
93      IVariable var;
94      T value;
95      foreach (IScope scope in scopes) {
96        scope.Variables.TryGetValue(name, out var);
97        if (var != null) {
98          value = var.Value as T;
99          if ((var.Value != null) && (value == null))
100            throw new InvalidOperationException(
101              string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
102                            name,
103                            typeof(T).GetPrettyName())
104            );
105          values.Add(value);
106        }
107      }
108      return new ItemArray<T>(values);
109    }
110    protected override void SetActualValue(IItem value) {
111      ItemArray<T> values = value as ItemArray<T>;
112      if (values == null)
113        throw new InvalidOperationException(
114          string.Format("Type mismatch. Value is not a \"{0}\".",
115                        typeof(ItemArray<T>).GetPrettyName())
116        );
117
118      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
119      for (int i = 0; i < depth; i++)
120        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
121
122      if (scopes.Count() != values.Length) throw new InvalidOperationException("Number of values is not equal to number of scopes.");
123
124      string name = TranslatedName;
125      int j = 0;
126      IVariable var;
127      foreach (IScope scope in scopes) {
128        scope.Variables.TryGetValue(name, out var);
129        if (var != null) var.Value = values[j];
130        else scope.Variables.Add(new Variable(name, values[j]));
131        j++;
132      }
133    }
134
135    public event EventHandler DepthChanged;
136    protected virtual void OnDepthChanged() {
137      EventHandler handler = DepthChanged;
138      if (handler != null) handler(this, EventArgs.Empty);
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.