Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/10 06:50:14 (14 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on parameters, TSP and selection
File:
1 edited

Legend:

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

    r2793 r2805  
    111111      return scope != null ? scope.Variables[actualName] : null;
    112112    }
     113    private IValueParameter<T> GetProblemParameter(string name) {
     114      IValueParameter<T> param = null;
     115      if (ExecutionContext.Problem.Parameters.ContainsKey(name)) {
     116        param = ExecutionContext.Problem.Parameters[name] as IValueParameter<T>;
     117        if (param == null)
     118          throw new InvalidOperationException(
     119            string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\".",
     120                          name,
     121                          typeof(IValueParameter<T>).GetPrettyName())
     122          );
     123      }
     124      return param;
     125    }
    113126    protected override IItem GetActualValue() {
    114127      string name;
    115       // try to get local value from context stack
     128      // try to get value from context stack
    116129      IValueParameter<T> param = GetParameter(out name);
    117130      if (param != null) return param.Value;
    118       else {  // try to get variable from scope
    119         IVariable var = LookupVariable(name);
    120         if (var != null) {
    121           T value = var.Value as T;
    122           if (value == null)
    123             throw new InvalidOperationException(
    124               string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
    125                             name,
    126                             typeof(T).GetPrettyName())
    127             );
    128           return value;
    129         }
    130       }
     131
     132      // try to get variable from scope
     133      IVariable var = LookupVariable(name);
     134      if (var != null) {
     135        T value = var.Value as T;
     136        if (value == null)
     137          throw new InvalidOperationException(
     138            string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
     139                          name,
     140                          typeof(T).GetPrettyName())
     141          );
     142        return value;
     143      }
     144
     145      // try to get value from problem
     146      IValueParameter<T> problemParam = GetProblemParameter(name);
     147      if (problemParam != null) return problemParam.Value;
     148
    131149      return null;
    132150    }
     
    138156                        typeof(T).GetPrettyName())
    139157        );
    140       // try to get local value from context stack
     158      // try to set value in context stack
    141159      string name;
    142160      IValueParameter<T> param = GetParameter(out name);
    143       if (param != null) param.Value = val;
    144       else {  // try to get variable from scope
    145         IVariable var = LookupVariable(name);
    146         if (var != null) var.Value = val;
    147         else ExecutionContext.Scope.Variables.Add(new Variable(name, value));
    148       }
     161      if (param != null) {
     162        param.Value = val;
     163        return;
     164      }
     165
     166      // try to set value in scope
     167      IVariable var = LookupVariable(name);
     168      if (var != null) {
     169        var.Value = val;
     170        return;
     171      }
     172
     173      // try to set value in problem
     174      IValueParameter<T> problemParam = GetProblemParameter(name);
     175      if (problemParam != null) {
     176        problemParam.Value = val;
     177        return;
     178      }
     179
     180      // create new variable
     181      ExecutionContext.Scope.Variables.Add(new Variable(name, value));
    149182    }
    150183
Note: See TracChangeset for help on using the changeset viewer.