Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14056


Ignore:
Timestamp:
07/13/16 09:47:55 (8 years ago)
Author:
abeham
Message:

#2281:

  • Moved check if context contains a parameter of the requested translated name to the front of the loop
    • Reason is that ResultsParameter queries for a parameter "Results" which doesn't exist in its own context
  • Added a flag to suppress the type check in LookupParameter's GetValue
    • Default value of that flag is true, so existing code is not affected
    • Reason is that ResultsParameter queries for ResultCollection with a generic type e.g. IntValue which don't match
  • Replaced some ContainsKey/Access combinations with TryGetValue
File:
1 edited

Legend:

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

    r14037 r14056  
    135135
    136136      while (currentExecutionContext != null) {
    137         valueParam = currentExecutionContext.Parameters[translatedName] as IValueParameter;
    138         lookupParam = currentExecutionContext.Parameters[translatedName] as ILookupParameter;
     137        IParameter param = null;
     138        while (currentExecutionContext != null && !currentExecutionContext.Parameters.TryGetValue(translatedName, out param))
     139          currentExecutionContext = currentExecutionContext.Parent;
     140        if (currentExecutionContext == null) break;
     141
     142        valueParam = param as IValueParameter;
     143        lookupParam = param as ILookupParameter;
    139144
    140145        if ((valueParam == null) && (lookupParam == null))
     
    148153          else if (lookupParam == null) return valueParam;
    149154        }
    150         if (lookupParam != null) translatedName = lookupParam.ActualName;
     155        translatedName = lookupParam.ActualName;
    151156
    152157        currentExecutionContext = currentExecutionContext.Parent;
    153         while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey(translatedName))
    154           currentExecutionContext = currentExecutionContext.Parent;
    155158      }
    156159      return null;
    157160    }
    158161    protected static IVariable LookupVariable(IScope scope, string name) {
    159       while ((scope != null) && !scope.Variables.ContainsKey(name))
     162      IVariable variable = null;
     163      while (scope != null && !scope.Variables.TryGetValue(name, out variable))
    160164        scope = scope.Parent;
    161       return scope != null ? scope.Variables[name] : null;
     165      return scope != null ? variable : null;
    162166    }
    163167
     
    171175    }
    172176
    173     protected static IItem GetValue(IExecutionContext executionContext, ref string name) {
     177    protected static IItem GetValue(IExecutionContext executionContext, ref string name, bool verifyType = true) {
    174178      // try to get value from context stack
    175179      IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name);
     
    179183      IVariable var = LookupVariable(executionContext.Scope, name);
    180184      if (var != null) {
    181         if (!(var.Value is T))
     185        if (verifyType && !(var.Value is T))
    182186          throw new InvalidOperationException(
    183187            string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
Note: See TracChangeset for help on using the changeset viewer.