Changeset 2756 for trunk/sources/HeuristicLab.Parameters
- Timestamp:
- 02/05/10 05:23:56 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Parameters/3.3
- Files:
-
- 2 added
- 5 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Parameters/3.3/HeuristicLab.Parameters-3.3.csproj
r2754 r2756 50 50 <ItemGroup> 51 51 <None Include="HeuristicLabParametersPlugin.cs.frame" /> 52 <Compile Include="SubScopesItemParameter.cs" /> 52 <Compile Include="LookupParameter.cs" /> 53 <Compile Include="SubScopesLookupParameter.cs" /> 54 <Compile Include="ValueLookupParameter.cs" /> 55 <Compile Include="ValueParameter.cs" /> 53 56 <Compile Include="ScopeParameter.cs" /> 54 57 <Compile Include="HeuristicLabParametersPlugin.cs" /> 55 <Compile Include="ItemParameter.cs" />56 58 <Compile Include="OperatorParameter.cs" /> 57 59 <Compile Include="Parameter.cs" /> … … 66 68 <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project> 67 69 <Name>HeuristicLab.Collections-3.3</Name> 70 </ProjectReference> 71 <ProjectReference Include="..\..\HeuristicLab.Common\3.2\HeuristicLab.Common-3.2.csproj"> 72 <Project>{1FC004FC-59AF-4249-B1B6-FF25873A20E4}</Project> 73 <Name>HeuristicLab.Common-3.2</Name> 68 74 </ProjectReference> 69 75 <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj"> -
trunk/sources/HeuristicLab.Parameters/3.3/HeuristicLabParametersPlugin.cs.frame
r2754 r2756 32 32 [PluginFile("HeuristicLab.Parameters-3.3.dll", PluginFileType.Assembly)] 33 33 [PluginDependency("HeuristicLab.Collections", "3.3")] 34 [PluginDependency("HeuristicLab.Common", "3.2")] 34 35 [PluginDependency("HeuristicLab.Core", "3.3")] 35 36 [PluginDependency("HeuristicLab.Persistence", "3.3")] -
trunk/sources/HeuristicLab.Parameters/3.3/OperatorParameter.cs
r2740 r2756 32 32 /// </summary> 33 33 [Item("OperatorParameter", "A parameter which represents an operator.")] 34 [EmptyStorableClass] 34 35 [Creatable("Test")] 35 public class OperatorParameter : Parameter, IOperatorParameter { 36 private IOperator value; 37 [Storable] 38 public IOperator Value { 39 get { return this.value; } 40 set { 41 if (value != this.value) { 42 if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed); 43 this.value = value; 44 if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed); 45 OnValueChanged(); 46 } 47 } 36 public class OperatorParameter : ValueParameter<IOperator> { 37 public OperatorParameter() 38 : base("Anonymous") { 48 39 } 49 50 public OperatorParameter() 51 : base("Anonymous", null, typeof(IOperator)) { 40 public OperatorParameter(string name) 41 : base(name) { 42 } 43 public OperatorParameter(string name, IOperator value) 44 : base(name, value) { 45 Value = value; 52 46 } 53 47 public OperatorParameter(string name, string description) 54 : base(name, description , typeof(IOperator)) {48 : base(name, description) { 55 49 } 56 50 public OperatorParameter(string name, string description, IOperator value) 57 : base(name, description, typeof(IOperator)) {51 : base(name, description, value) { 58 52 Value = value; 59 }60 61 public override IDeepCloneable Clone(Cloner cloner) {62 OperatorParameter clone = (OperatorParameter)base.Clone(cloner);63 clone.Value = (IOperator)cloner.Clone(value);64 return clone;65 }66 67 public override string ToString() {68 return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : "null", DataType.Name);69 }70 71 public event EventHandler ValueChanged;72 private void OnValueChanged() {73 if (ValueChanged != null)74 ValueChanged(this, new EventArgs());75 OnChanged();76 }77 private void Value_Changed(object sender, ChangedEventArgs e) {78 OnChanged(e);79 53 } 80 54 } -
trunk/sources/HeuristicLab.Parameters/3.3/Parameter.cs
r2740 r2756 56 56 dataType = typeof(IItem); 57 57 } 58 protected Parameter(string name, Type dataType) 59 : base(name) { 60 if (dataType == null) throw new ArgumentNullException(); 61 this.dataType = dataType; 62 } 58 63 protected Parameter(string name, string description, Type dataType) 59 64 : base(name, description) { -
trunk/sources/HeuristicLab.Parameters/3.3/ScopeParameter.cs
r2740 r2756 40 40 41 41 public ScopeParameter() 42 : base("Anonymous", null, typeof(IScope)) { 42 : base("Anonymous", typeof(IScope)) { 43 } 44 public ScopeParameter(string name) 45 : base(name, typeof(IScope)) { 43 46 } 44 47 public ScopeParameter(string name, string description) -
trunk/sources/HeuristicLab.Parameters/3.3/SubScopesLookupParameter.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; … … 31 32 /// A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope. 32 33 /// </summary> 33 [Item("SubScopes ItemParameter<T>", "A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.")]34 public class SubScopes ItemParameter<T> : Parameterwhere T : class, IItem {34 [Item("SubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.")] 35 public class SubScopesLookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem { 35 36 [Storable] 36 37 private string actualName; … … 46 47 } 47 48 48 public T[] Values {49 get { return Get Values(); }50 set { Set Values(value); }49 public T[] ActualValues { 50 get { return GetActualValues(); } 51 set { SetActualValues(value); } 51 52 } 52 53 53 public SubScopes ItemParameter()54 : base("Anonymous", null,typeof(T)) {54 public SubScopesLookupParameter() 55 : base("Anonymous", typeof(T)) { 55 56 actualName = Name; 56 57 } 57 public SubScopesItemParameter(string name, string description) 58 public SubScopesLookupParameter(string name) 59 : base(name, typeof(T)) { 60 actualName = Name; 61 } 62 public SubScopesLookupParameter(string name, string description) 58 63 : base(name, description, typeof(T)) { 59 64 actualName = Name; … … 61 66 62 67 public override IDeepCloneable Clone(Cloner cloner) { 63 SubScopes ItemParameter<T> clone = (SubScopesItemParameter<T>)base.Clone(cloner);68 SubScopesLookupParameter<T> clone = (SubScopesLookupParameter<T>)base.Clone(cloner); 64 69 clone.actualName = actualName; 65 70 return clone; … … 70 75 } 71 76 72 protected string GetActualName() { 73 string name = Name; 74 ExecutionContext current = ExecutionContext; 75 while (current != null) { 76 if (current.Operator.Parameters.ContainsKey(name)) 77 name = ((SubScopesItemParameter<T>)current.Operator.Parameters[name]).ActualName; 78 current = current.Parent; 77 protected virtual T[] GetActualValues() { 78 string name = LookupParameter<T>.TranslateName(Name, ExecutionContext); 79 IScope scope = ExecutionContext.Scope; 80 T[] values = new T[scope.SubScopes.Count]; 81 IVariable var; 82 T value; 83 84 for (int i = 0; i < values.Length; i++) { 85 scope.SubScopes[i].Variables.TryGetValue(name, out var); 86 if (var != null) { 87 value = var.Value as T; 88 if (value == null) 89 throw new InvalidOperationException( 90 string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".", 91 name, 92 typeof(T).GetPrettyName()) 93 ); 94 values[i] = value; 95 } 79 96 } 80 return name;97 return values; 81 98 } 82 protected virtual T[] GetValues() { 83 string name = GetActualName(); 84 IScope scope = ExecutionContext.Scope; 85 T[] value = new T[scope.SubScopes.Count]; 86 IVariable var; 87 88 for (int i = 0; i < value.Length; i++) { 89 scope.SubScopes[i].Variables.TryGetValue(name, out var); 90 if (var != null) value[i] = (T)var.Value; 91 } 92 return value; 93 } 94 protected virtual void SetValues(T[] values) { 95 string name = GetActualName(); 99 protected virtual void SetActualValues(T[] values) { 100 string name = LookupParameter<T>.TranslateName(Name, ExecutionContext); 96 101 IScope scope = ExecutionContext.Scope; 97 102 IVariable var; -
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.