Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14037


Ignore:
Timestamp:
07/12/16 15:47:45 (8 years ago)
Author:
mkommend
Message:

#2281: Refactored LookupParameter<T> to prepare for the ResultsParamter and make the name translation methods reusable.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Parameters/3.3/LookupParameter.cs

    r12012 r14037  
    5050    public string TranslatedName {
    5151      get {
    52         string translatedName;
    53         GetValueParameterAndTranslateName(out translatedName);
     52        string translatedName = Name;
     53        GetValueParameterAndTranslateName(ExecutionContext, ref translatedName);
    5454        return translatedName;
    5555      }
     
    6161
    6262    private Lazy<ThreadLocal<IItem>> cachedActualValues;
    63     private IItem CachedActualValue {
     63    protected IItem CachedActualValue {
    6464      get { return cachedActualValues.Value.Value; }
     65      set { cachedActualValues.Value.Value = value; }
    6566    }
    6667
     
    128129    }
    129130
    130     private IValueParameter GetValueParameterAndTranslateName(out string actualName) {
     131    protected static IValueParameter GetValueParameterAndTranslateName(IExecutionContext executionContext, ref string translatedName) {
    131132      IValueParameter valueParam;
    132133      ILookupParameter lookupParam;
    133       IExecutionContext currentExecutionContext = ExecutionContext;
    134 
    135       actualName = Name;
     134      IExecutionContext currentExecutionContext = executionContext;
     135
    136136      while (currentExecutionContext != null) {
    137         valueParam = currentExecutionContext.Parameters[actualName] as IValueParameter;
    138         lookupParam = currentExecutionContext.Parameters[actualName] as ILookupParameter;
     137        valueParam = currentExecutionContext.Parameters[translatedName] as IValueParameter;
     138        lookupParam = currentExecutionContext.Parameters[translatedName] as ILookupParameter;
    139139
    140140        if ((valueParam == null) && (lookupParam == null))
    141141          throw new InvalidOperationException(
    142142            string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".",
    143                           actualName, typeof(IValueParameter).GetPrettyName(), typeof(ILookupParameter).GetPrettyName())
     143                          translatedName, typeof(IValueParameter).GetPrettyName(), typeof(ILookupParameter).GetPrettyName())
    144144          );
    145145
     
    148148          else if (lookupParam == null) return valueParam;
    149149        }
    150         if (lookupParam != null) actualName = lookupParam.ActualName;
     150        if (lookupParam != null) translatedName = lookupParam.ActualName;
    151151
    152152        currentExecutionContext = currentExecutionContext.Parent;
    153         while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey(actualName))
     153        while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey(translatedName))
    154154          currentExecutionContext = currentExecutionContext.Parent;
    155155      }
    156156      return null;
    157157    }
    158     private IVariable LookupVariable(string name) {
    159       IScope scope = ExecutionContext.Scope;
     158    protected static IVariable LookupVariable(IScope scope, string name) {
    160159      while ((scope != null) && !scope.Variables.ContainsKey(name))
    161160        scope = scope.Parent;
    162161      return scope != null ? scope.Variables[name] : null;
    163162    }
     163
    164164    protected override IItem GetActualValue() {
    165165      if (CachedActualValue != null) return CachedActualValue;
    166       string name;
     166
     167      string translatedName = Name;
     168      var value = GetValue(ExecutionContext, ref translatedName);
     169      CachedActualValue = value;
     170      return value;
     171    }
     172
     173    protected static IItem GetValue(IExecutionContext executionContext, ref string name) {
    167174      // try to get value from context stack
    168       IValueParameter param = GetValueParameterAndTranslateName(out name);
     175      IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name);
    169176      if (param != null) return param.Value;
    170177
    171178      // try to get variable from scope
    172       IVariable var = LookupVariable(name);
     179      IVariable var = LookupVariable(executionContext.Scope, name);
    173180      if (var != null) {
    174181        if (!(var.Value is T))
     
    178185                          typeof(T).GetPrettyName())
    179186          );
    180         cachedActualValues.Value.Value = var.Value;
    181187        return var.Value;
    182188      }
    183189      return null;
    184190    }
     191
    185192    protected override void SetActualValue(IItem value) {
    186193      if (!(value is T))
     
    189196                        typeof(T).GetPrettyName())
    190197        );
    191       cachedActualValues.Value.Value = value;
    192 
     198      CachedActualValue = value;
     199
     200      string translatedName = Name;
     201      SetValue(ExecutionContext, ref translatedName, value);
     202    }
     203
     204    protected static void SetValue(IExecutionContext executionContext, ref string name, IItem value) {
    193205      // try to set value in context stack
    194       string name;
    195       IValueParameter param = GetValueParameterAndTranslateName(out name);
     206      IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name);
    196207      if (param != null) {
    197208        param.Value = value;
     
    200211
    201212      // try to set value in scope
    202       IVariable var = LookupVariable(name);
     213      IVariable var = LookupVariable(executionContext.Scope, name);
    203214      if (var != null) {
    204215        var.Value = value;
     
    207218
    208219      // create new variable
    209       ExecutionContext.Scope.Variables.Add(new Variable(name, value));
     220      executionContext.Scope.Variables.Add(new Variable(name, value));
    210221    }
    211222
Note: See TracChangeset for help on using the changeset viewer.