Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.Parameters/3.3/ScopeTreeLookupParameter.cs @ 16079

Last change on this file since 16079 was 15973, checked in by gkronber, 6 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    [StorableConstructor]
50    protected ScopeTreeLookupParameter(bool deserializing) : base(deserializing) { }
51    protected ScopeTreeLookupParameter(ScopeTreeLookupParameter<T> original, Cloner cloner)
52      : base(original, cloner) {
53      depth = original.depth;
54    }
55    public ScopeTreeLookupParameter()
56      : base() {
57      depth = 1;
58    }
59    public ScopeTreeLookupParameter(string name)
60      : base(name) {
61      depth = 1;
62    }
63    public ScopeTreeLookupParameter(string name, int depth)
64      : base(name) {
65      this.depth = depth;
66    }
67    public ScopeTreeLookupParameter(string name, string description)
68      : base(name, description) {
69      depth = 1;
70    }
71    public ScopeTreeLookupParameter(string name, string description, int depth)
72      : base(name, description) {
73      this.depth = depth;
74    }
75    public ScopeTreeLookupParameter(string name, string description, string actualName)
76      : base(name, description, actualName) {
77      depth = 1;
78    }
79    public ScopeTreeLookupParameter(string name, string description, string actualName, int depth)
80      : base(name, description, actualName) {
81      this.depth = depth;
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new ScopeTreeLookupParameter<T>(this, cloner);
86    }
87
88    protected override IItem GetActualValue() {
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
93      string name = TranslatedName;
94      List<T> values = new List<T>();
95      IVariable var;
96      T value;
97      foreach (IScope scope in scopes) {
98        scope.Variables.TryGetValue(name, out var);
99        if (var != null) {
100          value = var.Value as T;
101          if ((var.Value != null) && (value == null))
102            throw new InvalidOperationException(
103              string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
104                            name,
105                            typeof(T).GetPrettyName())
106            );
107          values.Add(value);
108        }
109      }
110      return new ItemArray<T>(values);
111    }
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
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
126      string name = TranslatedName;
127      int j = 0;
128      IVariable var;
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++;
134      }
135    }
136
137    public event EventHandler DepthChanged;
138    protected virtual void OnDepthChanged() {
139      EventHandler handler = DepthChanged;
140      if (handler != null) handler(this, EventArgs.Empty);
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.