Changeset 3659 for trunk/sources/HeuristicLab.Parameters
- Timestamp:
- 05/06/10 00:47:32 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Parameters/3.3
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Parameters/3.3/HeuristicLab.Parameters-3.3.csproj
r3634 r3659 86 86 <None Include="HeuristicLabParametersPlugin.cs.frame" /> 87 87 <Compile Include="ConstrainedValueParameter.cs" /> 88 <Compile Include="SubScopesSubScopesLookupParameter.cs" />89 88 <Compile Include="OptionalConstrainedValueParameter.cs" /> 90 89 <Compile Include="ValueParameter.cs" /> -
trunk/sources/HeuristicLab.Parameters/3.3/SubScopesLookupParameter.cs
r3634 r3659 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 23 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Core; … … 27 29 namespace HeuristicLab.Parameters { 28 30 /// <summary> 29 /// A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the current scope.31 /// A generic parameter representing instances of type T which are collected from or written to scope tree. 30 32 /// </summary> 31 [Item("S ubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the current scope.")]33 [Item("ScopeTreeLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to scope tree.")] 32 34 [StorableClass] 33 public class SubScopesLookupParameter<T> : LookupParameter<ItemArray<T>> where T : class, IItem { 34 public SubScopesLookupParameter() : base() { } 35 public SubScopesLookupParameter(string name) : base(name) { } 36 public SubScopesLookupParameter(string name, string description) : base(name, description) { } 37 public SubScopesLookupParameter(string name, string description, string actualName) : base(name, description, actualName) { } 35 public class ScopeTreeLookupParameter<T> : LookupParameter<ItemArray<T>> where T : class, IItem { 36 [Storable] 37 private int depth; 38 public int Depth { 39 get { return depth; } 40 set { 41 if (value < 0) throw new ArgumentException("Depth must be larger than or equal to 0."); 42 if (depth != value) { 43 depth = value; 44 OnDepthChanged(); 45 } 46 } 47 } 48 49 public ScopeTreeLookupParameter() 50 : base() { 51 depth = 1; 52 } 53 public ScopeTreeLookupParameter(string name) 54 : base(name) { 55 depth = 1; 56 } 57 public ScopeTreeLookupParameter(string name, string description) 58 : base(name, description) { 59 depth = 1; 60 } 61 public ScopeTreeLookupParameter(string name, string description, string actualName) 62 : base(name, description, actualName) { 63 depth = 1; 64 } 65 [StorableConstructor] 66 protected ScopeTreeLookupParameter(bool deserializing) : base(deserializing) { } 67 68 public override IDeepCloneable Clone(Cloner cloner) { 69 ScopeTreeLookupParameter<T> clone = (ScopeTreeLookupParameter<T>)base.Clone(cloner); 70 clone.depth = depth; 71 return clone; 72 } 38 73 39 74 protected override IItem GetActualValue() { 40 75 string name = LookupParameter<ItemArray<T>>.TranslateName(Name, ExecutionContext); 41 IScope scope = ExecutionContext.Scope; 42 ItemArray<T> values = new ItemArray<T>(scope.SubScopes.Count); 76 77 IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope }; 78 for (int i = 0; i < depth; i++) 79 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 80 81 List<T> values = new List<T>(); 43 82 IVariable var; 44 83 T value; 45 46 for (int i = 0; i < values.Length; i++) { 47 scope.SubScopes[i].Variables.TryGetValue(name, out var); 84 foreach (IScope scope in scopes) { 85 scope.Variables.TryGetValue(name, out var); 48 86 if (var != null) { 49 87 value = var.Value as T; … … 54 92 typeof(T).GetPrettyName()) 55 93 ); 56 values [i] = value;94 values.Add(value); 57 95 } 58 96 } 59 return values;97 return new ItemArray<T>(values); 60 98 } 61 99 protected override void SetActualValue(IItem value) { … … 68 106 69 107 string name = LookupParameter<ItemArray<T>>.TranslateName(Name, ExecutionContext); 70 IScope scope = ExecutionContext.Scope; 108 109 IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope }; 110 for (int i = 0; i < depth; i++) 111 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 112 113 if (scopes.Count() != values.Length) throw new InvalidOperationException("Number of values is not equal to number of scopes."); 114 115 int j = 0; 71 116 IVariable var; 117 foreach (IScope scope in scopes) { 118 scope.Variables.TryGetValue(name, out var); 119 if (var != null) var.Value = values[j]; 120 else scope.Variables.Add(new Variable(name, values[j])); 121 j++; 122 } 123 } 72 124 73 for (int i = 0; i < values.Length; i++) { 74 scope.SubScopes[i].Variables.TryGetValue(name, out var); 75 if (var != null) var.Value = values[i]; 76 else scope.SubScopes[i].Variables.Add(new Variable(name, values[i])); 77 } 125 public event EventHandler DepthChanged; 126 protected virtual void OnDepthChanged() { 127 EventHandler handler = DepthChanged; 128 if (handler != null) handler(this, EventArgs.Empty); 78 129 } 79 130 }
Note: See TracChangeset
for help on using the changeset viewer.