Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3075


Ignore:
Timestamp:
03/16/10 23:50:46 (15 years ago)
Author:
swagner
Message:

Refactored parameter lookup as suggested in a patch of mkommend (#919)

File:
1 edited

Legend:

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

    r3017 r3075  
    7878    }
    7979
    80     private IValueParameter GetParameter(out string name) {
    81       IValueParameter valueParam = this as IValueParameter;
    82       ILookupParameter lookupParam = this as ILookupParameter;
    83       IExecutionContext current = ExecutionContext;
     80    private IValueParameter GetValueParameterAndTranslateName(out string actualName) {
     81      IValueParameter valueParam;
     82      ILookupParameter lookupParam;
     83      IExecutionContext currentExecutionContext = ExecutionContext;
    8484
    85       name = Name;
    86       while ((valueParam != null) || (lookupParam != null)) {
    87         if ((valueParam != null) && (valueParam.Value != null)) return valueParam;
    88         if (lookupParam != null) name = lookupParam.ActualName;
     85      actualName = Name;
     86      while (currentExecutionContext != null) {
     87        valueParam = currentExecutionContext.Parameters[actualName] as IValueParameter;
     88        lookupParam = currentExecutionContext.Parameters[actualName] as ILookupParameter;
    8989
    90         current = current.Parent;
    91         while ((current != null) && !current.Parameters.ContainsKey(name))
    92           current = current.Parent;
     90        if ((valueParam == null) && (lookupParam == null))
     91          throw new InvalidOperationException(
     92            string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".",
     93                          actualName, typeof(IValueParameter).GetPrettyName(), typeof(ILookupParameter).GetPrettyName())
     94          );
    9395
    94         if (current != null) {
    95           valueParam = current.Parameters[name] as IValueParameter;
    96           lookupParam = current.Parameters[name] as ILookupParameter;
    97           if ((valueParam == null) && (lookupParam == null))
    98             throw new InvalidOperationException(
    99               string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".",
    100                             name,
    101                             typeof(IValueParameter).GetPrettyName(),
    102                             typeof(ILookupParameter).GetPrettyName())
    103             );
    104         } else {
    105           valueParam = null;
    106           lookupParam = null;
     96        if (valueParam != null) {
     97          if (valueParam.Value != null) return valueParam;
     98          else if (lookupParam == null) return valueParam;
    10799        }
     100        if (lookupParam != null) actualName = lookupParam.ActualName;
     101
     102        currentExecutionContext = currentExecutionContext.Parent;
     103        while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey(actualName))
     104          currentExecutionContext = currentExecutionContext.Parent;
    108105      }
    109106      return null;
     
    118115      string name;
    119116      // try to get value from context stack
    120       IValueParameter param = GetParameter(out name);
    121       if (param != null) return param.Value;
     117      IValueParameter param = GetValueParameterAndTranslateName(out name);
     118      if (param != null && param.Value != null) return param.Value;
    122119
    123120      // try to get variable from scope
     
    142139      // try to set value in context stack
    143140      string name;
    144       IValueParameter param = GetParameter(out name);
     141      IValueParameter param = GetValueParameterAndTranslateName(out name);
    145142      if (param != null) {
    146143        param.Value = value;
Note: See TracChangeset for help on using the changeset viewer.