Changeset 2773 for trunk/sources/HeuristicLab.Parameters
- Timestamp:
- 02/10/10 03:39:02 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Parameters/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Parameters/3.3/LookupParameter.cs
r2757 r2773 74 74 } 75 75 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 } 76 107 private IVariable LookupVariable(string name) { 77 108 IScope scope = ExecutionContext.Scope; … … 81 112 } 82 113 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 } 94 130 } 95 131 return null; … … 102 138 typeof(T).GetPrettyName()) 103 139 ); 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 } 108 149 } 109 150 -
trunk/sources/HeuristicLab.Parameters/3.3/ValueLookupParameter.cs
r2757 r2773 33 33 /// </summary> 34 34 [Item("ValueLookupParameter<T>", "A parameter whose value is either defined it the parameter itself or is retrieved from or written to a scope.")] 35 public class ValueLookupParameter<T> : Parameter, IValueLookupParameter<T> where T : class, IItem { 36 [Storable] 37 private string actualName; 38 public string ActualName { 39 get { return actualName; } 40 set { 41 if (value == null) throw new ArgumentNullException(); 42 if (!actualName.Equals(value)) { 43 actualName = value; 44 OnActualNameChanged(); 45 } 46 } 47 } 48 35 public class ValueLookupParameter<T> : LookupParameter<T>, IValueLookupParameter<T> where T : class, IItem { 49 36 private T value; 50 37 [Storable] … … 61 48 } 62 49 63 public new T ActualValue {64 get { return (T)GetActualValue(); }65 set { SetActualValue(value); }66 }67 68 50 public ValueLookupParameter() 69 : base("Anonymous", typeof(T)) { 70 actualName = Name; 51 : base() { 71 52 } 72 53 public ValueLookupParameter(string name) 73 : base(name, typeof(T)) { 74 actualName = Name; 54 : base(name) { 75 55 } 76 56 public ValueLookupParameter(string name, T value) 77 : base(name, typeof(T)) { 78 actualName = Name; 57 : base(name) { 79 58 Value = value; 80 59 } 81 60 public ValueLookupParameter(string name, string description) 82 : base(name, description, typeof(T)) { 83 actualName = Name; 61 : base(name, description) { 84 62 } 85 63 public ValueLookupParameter(string name, string description, T value) 86 : base(name, description, typeof(T)) { 87 actualName = Name; 64 : base(name, description) { 88 65 Value = value; 89 66 } … … 91 68 public override IDeepCloneable Clone(Cloner cloner) { 92 69 ValueLookupParameter<T> clone = (ValueLookupParameter<T>)base.Clone(cloner); 93 clone.actualName = actualName;94 70 clone.Value = (T)cloner.Clone(value); 95 71 return clone; … … 100 76 } 101 77 102 private IValueParameter<T> GetParameter(out string name) {103 IValueParameter<T> valueParam = this;104 ILookupParameter<T> lookupParam = this;105 ExecutionContext current = ExecutionContext;106 107 name = Name;108 while ((valueParam != null) && (lookupParam != null)) {109 if ((valueParam != null) && (valueParam.Value != null)) return valueParam;110 if (lookupParam != null) name = lookupParam.ActualName;111 112 current = current.Parent;113 while ((current != null) && !current.Operator.Parameters.ContainsKey(name))114 current = current.Parent;115 116 if (current != null) {117 valueParam = current.Operator.Parameters[name] as IValueParameter<T>;118 lookupParam = current.Operator.Parameters[name] as ILookupParameter<T>;119 if ((valueParam == null) && (lookupParam == null))120 throw new InvalidOperationException(121 string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".",122 name,123 typeof(IValueParameter<T>).GetPrettyName(),124 typeof(ILookupParameter<T>).GetPrettyName())125 );126 } else {127 valueParam = null;128 lookupParam = null;129 }130 }131 return null;132 }133 private IVariable LookupVariable(string name) {134 IScope scope = ExecutionContext.Scope;135 while ((scope != null) && !scope.Variables.ContainsKey(name))136 scope = scope.Parent;137 return scope != null ? scope.Variables[actualName] : null;138 }139 protected override IItem GetActualValue() {140 string name;141 // try to get local value from context stack142 IValueParameter<T> param = GetParameter(out name);143 if (param != null) return param.Value;144 else { // try to get variable from scope145 IVariable var = LookupVariable(name);146 if (var != null) {147 T value = var.Value as T;148 if (value == null)149 throw new InvalidOperationException(150 string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",151 name,152 typeof(T).GetPrettyName())153 );154 return value;155 }156 }157 return null;158 }159 protected override void SetActualValue(IItem value) {160 T val = value as T;161 if (val == null)162 throw new InvalidOperationException(163 string.Format("Type mismatch. Value is not a \"{0}\".",164 typeof(T).GetPrettyName())165 );166 // try to get local value from context stack167 string name;168 IValueParameter<T> param = GetParameter(out name);169 if (param != null) param.Value = val;170 else { // try to get variable from scope171 IVariable var = LookupVariable(name);172 if (var != null) var.Value = val;173 else ExecutionContext.Scope.Variables.Add(new Variable(name, value));174 }175 }176 177 public event EventHandler ActualNameChanged;178 private void OnActualNameChanged() {179 if (ActualNameChanged != null)180 ActualNameChanged(this, new EventArgs());181 OnChanged();182 }183 78 public event EventHandler ValueChanged; 184 79 private void OnValueChanged() {
Note: See TracChangeset
for help on using the changeset viewer.