Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/06/10 00:47:32 (15 years ago)
Author:
swagner
Message:

Worked on refactoring of algorithm analysis and tracing (#999)

  • removed SubScopesSubScopesLookupParameter
  • adapted SubScopesLookupParameter and renamed it into ScopeTreeLookupParameter
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  
    8686    <None Include="HeuristicLabParametersPlugin.cs.frame" />
    8787    <Compile Include="ConstrainedValueParameter.cs" />
    88     <Compile Include="SubScopesSubScopesLookupParameter.cs" />
    8988    <Compile Include="OptionalConstrainedValueParameter.cs" />
    9089    <Compile Include="ValueParameter.cs" />
  • trunk/sources/HeuristicLab.Parameters/3.3/SubScopesLookupParameter.cs

    r3634 r3659  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    2729namespace HeuristicLab.Parameters {
    2830  /// <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.
    3032  /// </summary>
    31   [Item("SubScopesLookupParameter<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.")]
    3234  [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    }
    3873
    3974    protected override IItem GetActualValue() {
    4075      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>();
    4382      IVariable var;
    4483      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);
    4886        if (var != null) {
    4987          value = var.Value as T;
     
    5492                            typeof(T).GetPrettyName())
    5593            );
    56           values[i] = value;
     94          values.Add(value);
    5795        }
    5896      }
    59       return values;
     97      return new ItemArray<T>(values);
    6098    }
    6199    protected override void SetActualValue(IItem value) {
     
    68106
    69107      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;
    71116      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    }
    72124
    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);
    78129    }
    79130  }
Note: See TracChangeset for help on using the changeset viewer.