Changeset 2924 for trunk/sources/HeuristicLab.Parameters
- Timestamp:
- 03/04/10 04:52:28 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Parameters/3.3
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/sources/HeuristicLab.Parameters/3.3/ConstrainedValueParameter.cs ¶
r2852 r2924 21 21 22 22 using System; 23 using HeuristicLab.Common; 23 using System.Linq; 24 using HeuristicLab.Collections; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 27 28 namespace HeuristicLab.Parameters { 28 29 /// <summary> 29 /// A parameter whose value has to be chosen from a set of valid values which is defined it the parameter itself.30 /// A parameter whose value has to be chosen from a set of valid values. 30 31 /// </summary> 31 [Item("ConstrainedValueParameter<T>", "A parameter whose value has to be chosen from a set of valid values which is defined it the parameter itself.")] 32 public sealed class ConstrainedValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem { 33 private ItemSet<T> validValues; 34 [Storable] 35 public ItemSet<T> ValidValues { 36 get { return validValues; } 37 private set { 38 DeregisterValidValuesEvents(); 39 validValues = value; 40 RegisterValidValuesEvents(); 32 [Item("ConstrainedValueParameter<T>", "A parameter whose value has to be chosen from a set of valid values.")] 33 [EmptyStorableClass] 34 public class ConstrainedValueParameter<T> : OptionalConstrainedValueParameter<T> where T : class, IItem { 35 public override T Value { 36 get { return base.Value; } 37 set { 38 if ((value == null) && (ValidValues.Count > 0)) throw new ArgumentNullException(); 39 base.Value = value; 41 40 } 42 41 } 43 42 44 private T value; 45 [Storable] 46 public T Value { 47 get { return this.value; } 48 set { 49 if (value != this.value) { 50 if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value."); 51 this.value = value; 52 OnValueChanged(); 53 } 54 } 43 public ConstrainedValueParameter() : base() { } 44 public ConstrainedValueParameter(string name) : base(name) { } 45 public ConstrainedValueParameter(string name, ItemSet<T> validValues) : base(name, validValues) { } 46 public ConstrainedValueParameter(string name, ItemSet<T> validValues, T value) : base(name, validValues, value) { } 47 public ConstrainedValueParameter(string name, string description) : base(name, description) { } 48 public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues) : base(name, description, validValues) { } 49 public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value) : base(name, description, validValues, value) { } 50 51 protected override void validValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { 52 if (Value == null) Value = ValidValues.First(); 55 53 } 56 IItem IValueParameter.Value { 57 get { return Value; } 58 set { 59 T val = value as T; 60 if ((value != null) && (val == null)) 61 throw new InvalidOperationException( 62 string.Format("Type mismatch. Value is not a \"{0}\".", 63 typeof(T).GetPrettyName()) 64 ); 65 Value = val; 66 } 54 protected override void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) { 55 if ((Value != null) && !ValidValues.Contains(Value)) Value = ValidValues.FirstOrDefault(); 67 56 } 68 69 public ConstrainedValueParameter() 70 : base("Anonymous", typeof(T)) { 71 ValidValues = new ItemSet<T>(); 72 } 73 public ConstrainedValueParameter(string name) 74 : base(name, typeof(T)) { 75 ValidValues = new ItemSet<T>(); 76 } 77 public ConstrainedValueParameter(string name, ItemSet<T> validValues) 78 : base(name, typeof(T)) { 79 ValidValues = validValues; 80 } 81 public ConstrainedValueParameter(string name, ItemSet<T> validValues, T value) 82 : base(name, typeof(T)) { 83 ValidValues = validValues; 84 Value = value; 85 } 86 public ConstrainedValueParameter(string name, string description) 87 : base(name, description, typeof(T)) { 88 ValidValues = new ItemSet<T>(); 89 } 90 public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues) 91 : base(name, description, typeof(T)) { 92 ValidValues = validValues; 93 } 94 public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value) 95 : base(name, description, typeof(T)) { 96 ValidValues = validValues; 97 Value = value; 98 } 99 100 public override IDeepCloneable Clone(Cloner cloner) { 101 ConstrainedValueParameter<T> clone = (ConstrainedValueParameter<T>)base.Clone(cloner); 102 clone.ValidValues = (ItemSet<T>)cloner.Clone(validValues); 103 clone.Value = (T)cloner.Clone(value); 104 return clone; 105 } 106 107 public override string ToString() { 108 return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : "null", DataType.GetPrettyName()); 109 } 110 111 protected override IItem GetActualValue() { 112 return Value; 113 } 114 protected override void SetActualValue(IItem value) { 115 ((IValueParameter)this).Value = value; 116 } 117 118 public event EventHandler ValueChanged; 119 private void OnValueChanged() { 120 if (ValueChanged != null) 121 ValueChanged(this, EventArgs.Empty); 122 OnChanged(); 123 } 124 125 private void RegisterValidValuesEvents() { 126 if (validValues != null) { 127 validValues.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved); 128 validValues.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset); 129 validValues.Changed += new ChangedEventHandler(ValidValues_Changed); 130 } 131 } 132 private void DeregisterValidValuesEvents() { 133 if (validValues != null) { 134 validValues.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved); 135 validValues.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset); 136 validValues.Changed -= new ChangedEventHandler(ValidValues_Changed); 137 } 138 } 139 140 private void ValidValues_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) { 141 if ((Value != null) && !validValues.Contains(Value)) Value = null; 142 } 143 private void ValidValues_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) { 144 if ((Value != null) && !validValues.Contains(Value)) Value = null; 145 } 146 private void ValidValues_Changed(object sender, ChangedEventArgs e) { 147 OnChanged(e); 57 protected override void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) { 58 if ((Value != null) && !ValidValues.Contains(Value)) Value = ValidValues.FirstOrDefault(); 148 59 } 149 60 } -
TabularUnified trunk/sources/HeuristicLab.Parameters/3.3/HeuristicLab.Parameters-3.3.csproj ¶
r2900 r2924 85 85 <None Include="HeuristicLabParametersPlugin.cs.frame" /> 86 86 <Compile Include="ConstrainedValueParameter.cs" /> 87 <Compile Include="OptionalConstrainedValueParameter.cs" /> 87 88 <Compile Include="ValueParameter.cs" /> 88 89 <Compile Include="LookupParameter.cs" /> -
TabularUnified trunk/sources/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs ¶
r2891 r2924 33 33 private T value; 34 34 [Storable] 35 public T Value {35 public virtual T Value { 36 36 get { return this.value; } 37 37 set { -
TabularUnified trunk/sources/HeuristicLab.Parameters/3.3/ValueParameter.cs ¶
r2891 r2924 30 30 /// </summary> 31 31 [Item("ValueParameter<T>", "A parameter whose value is defined it the parameter itself.")] 32 public sealed class ValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem { 33 private T value; 34 [Storable] 35 public T Value { 36 get { return this.value; } 32 [EmptyStorableClass] 33 public class ValueParameter<T> : OptionalValueParameter<T> where T : class, IItem { 34 public override T Value { 35 get { return base.Value; } 37 36 set { 38 37 if (value == null) throw new ArgumentNullException(); 39 if (value != this.value) { 40 if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed); 41 this.value = value; 42 this.value.Changed += new ChangedEventHandler(Value_Changed); 43 OnValueChanged(); 44 } 45 } 46 } 47 IItem IValueParameter.Value { 48 get { return Value; } 49 set { 50 if (value == null) throw new ArgumentNullException(); 51 T val = value as T; 52 if (val == null) 53 throw new InvalidOperationException( 54 string.Format("Type mismatch. Value is not a \"{0}\".", 55 typeof(T).GetPrettyName()) 56 ); 57 Value = val; 38 base.Value = value; 58 39 } 59 40 } 60 41 61 private ValueParameter() 62 : base("Anonymous", typeof(T)) { 63 } 64 public ValueParameter(string name, T value) 65 : base(name, typeof(T)) { 66 Value = value; 67 } 68 public ValueParameter(string name, string description, T value) 69 : base(name, description, typeof(T)) { 70 Value = value; 71 } 42 private ValueParameter() : base() { } 43 public ValueParameter(string name, T value) : base(name, value) { } 44 public ValueParameter(string name, string description, T value) : base(name, description, value) { } 72 45 73 46 public override IDeepCloneable Clone(Cloner cloner) { 74 ValueParameter<T> clone = new ValueParameter<T>(name, description, value);47 ValueParameter<T> clone = new ValueParameter<T>(name, description, Value); 75 48 cloner.RegisterClonedObject(this, clone); 76 clone.Value = (T)cloner.Clone( value);49 clone.Value = (T)cloner.Clone(Value); 77 50 return clone; 78 }79 80 public override string ToString() {81 return string.Format("{0}: {1} ({2})", Name, Value.ToString(), DataType.GetPrettyName());82 }83 84 protected override IItem GetActualValue() {85 return Value;86 }87 protected override void SetActualValue(IItem value) {88 ((IValueParameter)this).Value = value;89 }90 91 public event EventHandler ValueChanged;92 private void OnValueChanged() {93 if (ValueChanged != null)94 ValueChanged(this, EventArgs.Empty);95 OnChanged();96 }97 private void Value_Changed(object sender, ChangedEventArgs e) {98 OnChanged(e);99 51 } 100 52 }
Note: See TracChangeset
for help on using the changeset viewer.