Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/LookupParameter.cs @ 7025

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

Set some parameters to hidden per default (#1377)

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 parameter whose value is retrieved from the scope.
30  /// </summary>
31  [Item("LookupParameter", "A parameter whose value is retrieved from or written to a scope.")]
32  [StorableClass]
33  public class LookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem {
34    [Storable]
35    private string actualName;
36    public string ActualName {
37      get { return actualName; }
38      set {
39        if (value == null) throw new ArgumentNullException();
40        if (string.IsNullOrWhiteSpace(value)) {
41          actualName = Name;
42          OnActualNameChanged();
43        } else if (!actualName.Equals(value)) {
44          actualName = value;
45          OnActualNameChanged();
46        }
47      }
48    }
49    public string TranslatedName {
50      get {
51        string translatedName;
52        GetValueParameterAndTranslateName(out translatedName);
53        return translatedName;
54      }
55    }
56    public new T ActualValue {
57      get { return (T)base.ActualValue; }
58      set { base.ActualValue = value; }
59    }
60
61    [StorableConstructor]
62    protected LookupParameter(bool deserializing) : base(deserializing) { }
63    protected LookupParameter(LookupParameter<T> original, Cloner cloner)
64      : base(original, cloner) {
65      actualName = original.actualName;
66    }
67    public LookupParameter()
68      : base("Anonymous", typeof(T)) {
69      this.actualName = Name;
70      this.Hidden = true;
71    }
72    public LookupParameter(string name)
73      : base(name, typeof(T)) {
74      this.actualName = Name;
75      this.Hidden = true;
76    }
77    public LookupParameter(string name, string description)
78      : base(name, description, typeof(T)) {
79      this.actualName = Name;
80      this.Hidden = true;
81    }
82    public LookupParameter(string name, string description, string actualName)
83      : base(name, description, typeof(T)) {
84      this.actualName = string.IsNullOrWhiteSpace(actualName) ? Name : actualName;
85      this.Hidden = true;
86    }
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new LookupParameter<T>(this, cloner);
90    }
91
92    public override string ToString() {
93      if (Name.Equals(ActualName))
94        return Name;
95      else
96        return Name + ": " + ActualName;
97    }
98
99    private IValueParameter GetValueParameterAndTranslateName(out string actualName) {
100      IValueParameter valueParam;
101      ILookupParameter lookupParam;
102      IExecutionContext currentExecutionContext = ExecutionContext;
103
104      actualName = Name;
105      while (currentExecutionContext != null) {
106        valueParam = currentExecutionContext.Parameters[actualName] as IValueParameter;
107        lookupParam = currentExecutionContext.Parameters[actualName] as ILookupParameter;
108
109        if ((valueParam == null) && (lookupParam == null))
110          throw new InvalidOperationException(
111            string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".",
112                          actualName, typeof(IValueParameter).GetPrettyName(), typeof(ILookupParameter).GetPrettyName())
113          );
114
115        if (valueParam != null) {
116          if (valueParam.Value != null) return valueParam;
117          else if (lookupParam == null) return valueParam;
118        }
119        if (lookupParam != null) actualName = lookupParam.ActualName;
120
121        currentExecutionContext = currentExecutionContext.Parent;
122        while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey(actualName))
123          currentExecutionContext = currentExecutionContext.Parent;
124      }
125      return null;
126    }
127    private IVariable LookupVariable(string name) {
128      IScope scope = ExecutionContext.Scope;
129      while ((scope != null) && !scope.Variables.ContainsKey(name))
130        scope = scope.Parent;
131      return scope != null ? scope.Variables[name] : null;
132    }
133    protected override IItem GetActualValue() {
134      string name;
135      // try to get value from context stack
136      IValueParameter param = GetValueParameterAndTranslateName(out name);
137      if (param != null) return param.Value;
138
139      // try to get variable from scope
140      IVariable var = LookupVariable(name);
141      if (var != null) {
142        if (!(var.Value is T))
143          throw new InvalidOperationException(
144            string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
145                          name,
146                          typeof(T).GetPrettyName())
147          );
148        return var.Value;
149      }
150      return null;
151    }
152    protected override void SetActualValue(IItem value) {
153      if (!(value is T))
154        throw new InvalidOperationException(
155          string.Format("Type mismatch. Value is not a \"{0}\".",
156                        typeof(T).GetPrettyName())
157        );
158      // try to set value in context stack
159      string name;
160      IValueParameter param = GetValueParameterAndTranslateName(out name);
161      if (param != null) {
162        param.Value = value;
163        return;
164      }
165
166      // try to set value in scope
167      IVariable var = LookupVariable(name);
168      if (var != null) {
169        var.Value = value;
170        return;
171      }
172
173      // create new variable
174      ExecutionContext.Scope.Variables.Add(new Variable(name, value));
175    }
176
177    public event EventHandler ActualNameChanged;
178    protected virtual void OnActualNameChanged() {
179      EventHandler handler = ActualNameChanged;
180      if (handler != null) handler(this, EventArgs.Empty);
181      OnToStringChanged();
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.