Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2793 was 2793, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

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