Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5193 was 5193, checked in by swagner, 13 years ago

Merged ParallelEngine branch back into trunk (#1333)

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