Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Parameters/3.3/ScopeTreeLookupParameter.cs @ 17116

Last change on this file since 17116 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 5.2 KB
RevLine 
[2740]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 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;
[3659]23using System.Collections.Generic;
24using System.Linq;
[2756]25using HeuristicLab.Common;
[2740]26using HeuristicLab.Core;
[16565]27using HEAL.Attic;
[2740]28
29namespace HeuristicLab.Parameters {
30  /// <summary>
[3659]31  /// A generic parameter representing instances of type T which are collected from or written to scope tree.
[2740]32  /// </summary>
[3822]33  [Item("ScopeTreeLookupParameter", "A generic parameter representing instances of type T which are collected from or written to scope tree.")]
[16565]34  [StorableType("51F36637-C22C-4CDC-B34B-F49CA7896E35")]
[3663]35  public class ScopeTreeLookupParameter<T> : LookupParameter<ItemArray<T>>, IScopeTreeLookupParameter<T> where T : class, IItem {
[3659]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    }
[2740]48
[4722]49    [StorableConstructor]
[16565]50    protected ScopeTreeLookupParameter(StorableConstructorFlag _) : base(_) { }
[4722]51    protected ScopeTreeLookupParameter(ScopeTreeLookupParameter<T> original, Cloner cloner)
52      : base(original, cloner) {
53      depth = original.depth;
54    }
[3659]55    public ScopeTreeLookupParameter()
56      : base() {
57      depth = 1;
58    }
59    public ScopeTreeLookupParameter(string name)
60      : base(name) {
61      depth = 1;
62    }
[3740]63    public ScopeTreeLookupParameter(string name, int depth)
64      : base(name) {
65      this.depth = depth;
66    }
[3659]67    public ScopeTreeLookupParameter(string name, string description)
68      : base(name, description) {
69      depth = 1;
70    }
[3740]71    public ScopeTreeLookupParameter(string name, string description, int depth)
72      : base(name, description) {
73      this.depth = depth;
74    }
[3659]75    public ScopeTreeLookupParameter(string name, string description, string actualName)
76      : base(name, description, actualName) {
77      depth = 1;
78    }
[3740]79    public ScopeTreeLookupParameter(string name, string description, string actualName, int depth)
80      : base(name, description, actualName) {
81      this.depth = depth;
82    }
[3659]83
84    public override IDeepCloneable Clone(Cloner cloner) {
[4722]85      return new ScopeTreeLookupParameter<T>(this, cloner);
[3659]86    }
87
[2757]88    protected override IItem GetActualValue() {
[3659]89      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
90      for (int i = 0; i < depth; i++)
91        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
92
[3687]93      string name = TranslatedName;
[3659]94      List<T> values = new List<T>();
[2740]95      IVariable var;
[2756]96      T value;
[3659]97      foreach (IScope scope in scopes) {
98        scope.Variables.TryGetValue(name, out var);
[2756]99        if (var != null) {
100          value = var.Value as T;
[2852]101          if ((var.Value != null) && (value == null))
[2756]102            throw new InvalidOperationException(
103              string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
104                            name,
105                            typeof(T).GetPrettyName())
106            );
[3659]107          values.Add(value);
[2756]108        }
[2740]109      }
[3659]110      return new ItemArray<T>(values);
[2740]111    }
[2757]112    protected override void SetActualValue(IItem value) {
113      ItemArray<T> values = value as ItemArray<T>;
114      if (values == null)
115        throw new InvalidOperationException(
116          string.Format("Type mismatch. Value is not a \"{0}\".",
117                        typeof(ItemArray<T>).GetPrettyName())
118        );
119
[3659]120      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
121      for (int i = 0; i < depth; i++)
122        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
123
124      if (scopes.Count() != values.Length) throw new InvalidOperationException("Number of values is not equal to number of scopes.");
125
[3687]126      string name = TranslatedName;
[3659]127      int j = 0;
[2740]128      IVariable var;
[3659]129      foreach (IScope scope in scopes) {
130        scope.Variables.TryGetValue(name, out var);
131        if (var != null) var.Value = values[j];
132        else scope.Variables.Add(new Variable(name, values[j]));
133        j++;
[2740]134      }
135    }
[3659]136
137    public event EventHandler DepthChanged;
138    protected virtual void OnDepthChanged() {
139      EventHandler handler = DepthChanged;
140      if (handler != null) handler(this, EventArgs.Empty);
141    }
[2740]142  }
143}
Note: See TracBrowser for help on using the repository browser.