- Timestamp:
- 02/05/10 05:23:56 (14 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Parameters/3.3/ValueLookupParameter.cs
r2754 r2756 24 24 using System.Text; 25 25 using System.Xml; 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 29 30 namespace HeuristicLab.Parameters { 30 31 /// <summary> 31 /// A generic parameter which represents an instance of type T.32 /// A parameter whose value is either defined it the parameter itself or is retrieved from the scope. 32 33 /// </summary> 33 [Item(" ItemParameter<T>", "A generic parameter which represents an instance of type T.")]34 public class ItemParameter<T> : Parameterwhere T : class, IItem {34 [Item("ValueLookupParameter<T>", "A parameter whose value is either defined it the parameter itself or is retrieved from the scope.")] 35 public class ValueLookupParameter<T> : Parameter, IValueLookupParameter<T> where T : class, IItem { 35 36 [Storable] 36 37 private string actualName; … … 46 47 } 47 48 48 private T localValue;49 private T value; 49 50 [Storable] 50 public T LocalValue {51 get { return this. localValue; }51 public T Value { 52 get { return this.value; } 52 53 set { 53 if (value != this.localValue) { 54 if ((value != null) && (!DataType.IsInstanceOfType(value))) throw new ArgumentException("Static value does not match data type of parameter"); 55 if (this.localValue != null) this.localValue.Changed -= new ChangedEventHandler(LocalValue_Changed); 56 this.localValue = value; 57 if (this.localValue != null) this.localValue.Changed += new ChangedEventHandler(LocalValue_Changed); 58 OnLocalValueChanged(); 54 if (value != this.value) { 55 if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed); 56 this.value = value; 57 if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed); 58 OnValueChanged(); 59 59 } 60 60 } 61 61 } 62 62 63 public T Value {64 get { return Get Value(); }65 set { Set Value(value); }63 public T ActualValue { 64 get { return GetActualValue(); } 65 set { SetActualValue(value); } 66 66 } 67 67 68 public ItemParameter()69 : base("Anonymous", null,typeof(T)) {68 public ValueLookupParameter() 69 : base("Anonymous", typeof(T)) { 70 70 actualName = Name; 71 LocalValue = null;72 71 } 73 public ItemParameter(string name, string description) 72 public ValueLookupParameter(string name) 73 : base(name, typeof(T)) { 74 actualName = Name; 75 } 76 public ValueLookupParameter(string name, T value) 77 : base(name, typeof(T)) { 78 actualName = Name; 79 Value = value; 80 } 81 public ValueLookupParameter(string name, string description) 74 82 : base(name, description, typeof(T)) { 75 83 actualName = Name; 76 LocalValue = null;77 84 } 78 public ItemParameter(string name, string description, T localValue)85 public ValueLookupParameter(string name, string description, T value) 79 86 : base(name, description, typeof(T)) { 80 87 actualName = Name; 81 LocalValue = localValue;88 Value = value; 82 89 } 83 90 84 91 public override IDeepCloneable Clone(Cloner cloner) { 85 ItemParameter<T> clone = (ItemParameter<T>)base.Clone(cloner);92 ValueLookupParameter<T> clone = (ValueLookupParameter<T>)base.Clone(cloner); 86 93 clone.actualName = actualName; 87 clone. LocalValue = (T)cloner.Clone(localValue);94 clone.Value = (T)cloner.Clone(value); 88 95 return clone; 89 96 } 90 97 91 98 public override string ToString() { 92 return string.Format("{0}: {1} ({2})", Name, LocalValue != null ? LocalValue.ToString() : ActualName, DataType.Name);99 return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : ActualName, DataType.Name); 93 100 } 94 101 95 protected ItemParameter<T> GetParameter(out string name) { 96 ItemParameter<T> param = this; 102 private IValueParameter<T> GetParameter(out string name) { 103 IValueParameter<T> valueParam = this; 104 ILookupParameter<T> lookupParam = this; 97 105 ExecutionContext current = ExecutionContext; 98 name = param.Name; 99 while (param != null) { 100 if (param.LocalValue != null) return param; 101 name = param.ActualName; 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 102 112 current = current.Parent; 103 113 while ((current != null) && !current.Operator.Parameters.ContainsKey(name)) 104 114 current = current.Parent; 105 if (current != null) 106 param = (ItemParameter<T>)current.Operator.Parameters[actualName]; 107 else 108 param = null; 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 } 109 130 } 110 131 return null; 111 132 } 112 pr otected IVariable GetVariable(string name) {133 private IVariable LookupVariable(string name) { 113 134 IScope scope = ExecutionContext.Scope; 114 135 while ((scope != null) && !scope.Variables.ContainsKey(name)) … … 116 137 return scope != null ? scope.Variables[actualName] : null; 117 138 } 118 protected virtual T Get Value() {139 protected virtual T GetActualValue() { 119 140 string name; 120 141 // try to get local value from context stack 121 I temParameter<T> param = GetParameter(out name);142 IValueParameter<T> param = GetParameter(out name); 122 143 if (param != null) return param.Value; 123 else { 124 // try to get variable from scope 125 IVariable var = GetVariable(name); 126 if (var != null) return (T)var.Value; 144 else { // try to get variable from scope 145 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 } 127 156 } 128 157 return null; 129 158 } 130 protected virtual void Set Value(T value) {159 protected virtual void SetActualValue(T value) { 131 160 string name; 132 161 // try to get local value from context stack 133 I temParameter<T> param = GetParameter(out name);162 IValueParameter<T> param = GetParameter(out name); 134 163 if (param != null) param.Value = value; 135 else { 136 // try to get variable from scope 137 IVariable var = GetVariable(name); 164 else { // try to get variable from scope 165 IVariable var = LookupVariable(name); 138 166 if (var != null) var.Value = value; 139 167 else ExecutionContext.Scope.Variables.Add(new Variable(name, value)); … … 147 175 OnChanged(); 148 176 } 149 public event EventHandler LocalValueChanged;150 private void On LocalValueChanged() {151 if ( LocalValueChanged != null)152 LocalValueChanged(this, new EventArgs());177 public event EventHandler ValueChanged; 178 private void OnValueChanged() { 179 if (ValueChanged != null) 180 ValueChanged(this, new EventArgs()); 153 181 OnChanged(); 154 182 } 155 private void LocalValue_Changed(object sender, ChangedEventArgs e) {183 private void Value_Changed(object sender, ChangedEventArgs e) { 156 184 OnChanged(e); 157 185 }
Note: See TracChangeset
for help on using the changeset viewer.