Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/10/10 03:39:02 (15 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on parameters and operators
File:
1 edited

Legend:

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

    r2757 r2773  
    7474    }
    7575
     76    private IValueParameter<T> GetParameter(out string name) {
     77      IValueParameter<T> valueParam = this as IValueParameter<T>;
     78      ILookupParameter<T> lookupParam = this as ILookupParameter<T>;
     79      ExecutionContext current = ExecutionContext;
     80
     81      name = Name;
     82      while ((valueParam != null) && (lookupParam != null)) {
     83        if ((valueParam != null) && (valueParam.Value != null)) return valueParam;
     84        if (lookupParam != null) name = lookupParam.ActualName;
     85
     86        current = current.Parent;
     87        while ((current != null) && !current.Operator.Parameters.ContainsKey(name))
     88          current = current.Parent;
     89
     90        if (current != null) {
     91          valueParam = current.Operator.Parameters[name] as IValueParameter<T>;
     92          lookupParam = current.Operator.Parameters[name] as ILookupParameter<T>;
     93          if ((valueParam == null) && (lookupParam == null))
     94            throw new InvalidOperationException(
     95              string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".",
     96                            name,
     97                            typeof(IValueParameter<T>).GetPrettyName(),
     98                            typeof(ILookupParameter<T>).GetPrettyName())
     99            );
     100        } else {
     101          valueParam = null;
     102          lookupParam = null;
     103        }
     104      }
     105      return null;
     106    }
    76107    private IVariable LookupVariable(string name) {
    77108      IScope scope = ExecutionContext.Scope;
     
    81112    }
    82113    protected override IItem GetActualValue() {
    83       string name = TranslateName(Name, ExecutionContext);
    84       IVariable var = LookupVariable(name);
    85       if (var != null) {
    86         T value = var.Value as T;
    87         if (value == null)
    88           throw new InvalidOperationException(
    89             string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
    90                           name,
    91                           typeof(T).GetPrettyName())
    92           );
    93         return value;
     114      string name;
     115      // try to get local value from context stack
     116      IValueParameter<T> param = GetParameter(out name);
     117      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        }
    94130      }
    95131      return null;
     
    102138                        typeof(T).GetPrettyName())
    103139        );
    104       string name = TranslateName(Name, ExecutionContext);
    105       IVariable var = LookupVariable(name);
    106       if (var != null) var.Value = val;
    107       else ExecutionContext.Scope.Variables.Add(new Variable(name, val));
     140      // try to get local value from context stack
     141      string name;
     142      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      }
    108149    }
    109150
Note: See TracChangeset for help on using the changeset viewer.