Changeset 14079 for branches/crossvalidation-2434/HeuristicLab.Parameters
- Timestamp:
- 07/15/16 10:20:58 (8 years ago)
- Location:
- branches/crossvalidation-2434
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/crossvalidation-2434
- Property svn:mergeinfo changed
/trunk/sources merged: 14032,14034,14036-14037,14056-14057,14071,14078
- Property svn:mergeinfo changed
-
branches/crossvalidation-2434/HeuristicLab.Parameters/3.3/LookupParameter.cs
r12012 r14079 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 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; 139 144 140 145 if ((valueParam == null) && (lookupParam == null)) 141 146 throw new InvalidOperationException( 142 147 string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".", 143 actualName, typeof(IValueParameter).GetPrettyName(), typeof(ILookupParameter).GetPrettyName())148 translatedName, typeof(IValueParameter).GetPrettyName(), typeof(ILookupParameter).GetPrettyName()) 144 149 ); 145 150 … … 148 153 else if (lookupParam == null) return valueParam; 149 154 } 150 if (lookupParam != null) actualName = lookupParam.ActualName;155 translatedName = lookupParam.ActualName; 151 156 152 157 currentExecutionContext = currentExecutionContext.Parent; 153 while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey(actualName))154 currentExecutionContext = currentExecutionContext.Parent;155 158 } 156 159 return null; 157 160 } 158 pr ivate IVariable LookupVariable(string name) {159 I Scope scope = ExecutionContext.Scope;160 while ( (scope != null) && !scope.Variables.ContainsKey(name))161 protected static IVariable LookupVariable(IScope scope, string name) { 162 IVariable variable = null; 163 while (scope != null && !scope.Variables.TryGetValue(name, out variable)) 161 164 scope = scope.Parent; 162 return scope != null ? scope.Variables[name] : null; 163 } 165 return scope != null ? variable : null; 166 } 167 164 168 protected override IItem GetActualValue() { 165 169 if (CachedActualValue != null) return CachedActualValue; 166 string name; 170 171 string translatedName = Name; 172 var value = GetValue(ExecutionContext, ref translatedName); 173 if (value != null && !(value is T)) 174 throw new InvalidOperationException( 175 string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".", 176 translatedName, 177 typeof(T).GetPrettyName()) 178 ); 179 CachedActualValue = value; 180 return value; 181 } 182 183 protected static IItem GetValue(IExecutionContext executionContext, ref string name) { 167 184 // try to get value from context stack 168 IValueParameter param = GetValueParameterAndTranslateName( outname);185 IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name); 169 186 if (param != null) return param.Value; 170 187 171 188 // try to get variable from scope 172 IVariable var = LookupVariable(name); 173 if (var != null) { 174 if (!(var.Value is T)) 175 throw new InvalidOperationException( 176 string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".", 177 name, 178 typeof(T).GetPrettyName()) 179 ); 180 cachedActualValues.Value.Value = var.Value; 181 return var.Value; 182 } 183 return null; 184 } 189 IVariable var = LookupVariable(executionContext.Scope, name); 190 return var != null ? var.Value : null; 191 } 192 185 193 protected override void SetActualValue(IItem value) { 186 194 if (!(value is T)) … … 189 197 typeof(T).GetPrettyName()) 190 198 ); 191 cachedActualValues.Value.Value = value; 192 199 CachedActualValue = value; 200 201 string translatedName = Name; 202 SetValue(ExecutionContext, ref translatedName, value); 203 } 204 205 protected static void SetValue(IExecutionContext executionContext, ref string name, IItem value) { 193 206 // try to set value in context stack 194 string name; 195 IValueParameter param = GetValueParameterAndTranslateName(out name); 207 IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name); 196 208 if (param != null) { 197 209 param.Value = value; … … 200 212 201 213 // try to set value in scope 202 IVariable var = LookupVariable( name);214 IVariable var = LookupVariable(executionContext.Scope, name); 203 215 if (var != null) { 204 216 var.Value = value; … … 207 219 208 220 // create new variable 209 ExecutionContext.Scope.Variables.Add(new Variable(name, value));221 executionContext.Scope.Variables.Add(new Variable(name, value)); 210 222 } 211 223
Note: See TracChangeset
for help on using the changeset viewer.