- Timestamp:
- 02/10/10 03:39:02 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.