Changeset 14037
- Timestamp:
- 07/12/16 15:47:45 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Parameters/3.3/LookupParameter.cs
r12012 r14037 50 50 public string TranslatedName { 51 51 get { 52 string translatedName ;53 GetValueParameterAndTranslateName( outtranslatedName);52 string translatedName = Name; 53 GetValueParameterAndTranslateName(ExecutionContext, ref translatedName); 54 54 return translatedName; 55 55 } … … 61 61 62 62 private Lazy<ThreadLocal<IItem>> cachedActualValues; 63 pr ivateIItem CachedActualValue {63 protected IItem CachedActualValue { 64 64 get { return cachedActualValues.Value.Value; } 65 set { cachedActualValues.Value.Value = value; } 65 66 } 66 67 … … 128 129 } 129 130 130 pr ivate IValueParameter GetValueParameterAndTranslateName(out string actualName) {131 protected static IValueParameter GetValueParameterAndTranslateName(IExecutionContext executionContext, ref string translatedName) { 131 132 IValueParameter valueParam; 132 133 ILookupParameter lookupParam; 133 IExecutionContext currentExecutionContext = ExecutionContext; 134 135 actualName = Name; 134 IExecutionContext currentExecutionContext = executionContext; 135 136 136 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; 139 139 140 140 if ((valueParam == null) && (lookupParam == null)) 141 141 throw new InvalidOperationException( 142 142 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()) 144 144 ); 145 145 … … 148 148 else if (lookupParam == null) return valueParam; 149 149 } 150 if (lookupParam != null) actualName = lookupParam.ActualName;150 if (lookupParam != null) translatedName = lookupParam.ActualName; 151 151 152 152 currentExecutionContext = currentExecutionContext.Parent; 153 while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey( actualName))153 while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey(translatedName)) 154 154 currentExecutionContext = currentExecutionContext.Parent; 155 155 } 156 156 return null; 157 157 } 158 private IVariable LookupVariable(string name) { 159 IScope scope = ExecutionContext.Scope; 158 protected static IVariable LookupVariable(IScope scope, string name) { 160 159 while ((scope != null) && !scope.Variables.ContainsKey(name)) 161 160 scope = scope.Parent; 162 161 return scope != null ? scope.Variables[name] : null; 163 162 } 163 164 164 protected override IItem GetActualValue() { 165 165 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) { 167 174 // try to get value from context stack 168 IValueParameter param = GetValueParameterAndTranslateName( outname);175 IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name); 169 176 if (param != null) return param.Value; 170 177 171 178 // try to get variable from scope 172 IVariable var = LookupVariable( name);179 IVariable var = LookupVariable(executionContext.Scope, name); 173 180 if (var != null) { 174 181 if (!(var.Value is T)) … … 178 185 typeof(T).GetPrettyName()) 179 186 ); 180 cachedActualValues.Value.Value = var.Value;181 187 return var.Value; 182 188 } 183 189 return null; 184 190 } 191 185 192 protected override void SetActualValue(IItem value) { 186 193 if (!(value is T)) … … 189 196 typeof(T).GetPrettyName()) 190 197 ); 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) { 193 205 // try to set value in context stack 194 string name; 195 IValueParameter param = GetValueParameterAndTranslateName(out name); 206 IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name); 196 207 if (param != null) { 197 208 param.Value = value; … … 200 211 201 212 // try to set value in scope 202 IVariable var = LookupVariable( name);213 IVariable var = LookupVariable(executionContext.Scope, name); 203 214 if (var != null) { 204 215 var.Value = value; … … 207 218 208 219 // create new variable 209 ExecutionContext.Scope.Variables.Add(new Variable(name, value));220 executionContext.Scope.Variables.Add(new Variable(name, value)); 210 221 } 211 222
Note: See TracChangeset
for help on using the changeset viewer.