Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/17/19 14:05:47 (5 years ago)
Author:
abeham
Message:

#2521: Refactored ContextLookupParameter with suggestions from mkommend

  • Add StorableType attribute to some types
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/LookupParameter.cs

    r17254 r17257  
    2121
    2222using System;
    23 using System.Threading;
    2423using HEAL.Attic;
    2524using HeuristicLab.Common;
     
    3231  [Item("LookupParameter", "A parameter whose value is retrieved from or written to a scope.")]
    3332  [StorableType("84FE5F33-94B8-4E30-B1CB-CD15314FB83B")]
    34   public class LookupParameter<T> : Parameter, IStatefulItem, ILookupParameter<T> where T : class, IItem {
     33  public class LookupParameter<T> : ContextParameter, ILookupParameter<T> where T : class, IItem {
    3534    [Storable]
    3635    private string actualName;
     
    6059    }
    6160
    62     private Lazy<ThreadLocal<IItem>> cachedActualValues;
    63     protected IItem CachedActualValue {
    64       get { return cachedActualValues.Value.Value; }
    65       set { cachedActualValues.Value.Value = value; }
    66     }
    67 
    68     private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
    69     public IExecutionContext ExecutionContext {
    70       get { return executionContexts.Value.Value; }
    71       set {
    72         if (value != executionContexts.Value.Value) {
    73           executionContexts.Value.Value = value;
    74           cachedActualValues.Value.Value = null;
    75         }
    76       }
    77     }
    78 
    7961    [StorableConstructor]
    80     protected LookupParameter(StorableConstructorFlag _) : base(_) {
    81       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    82       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    83     }
     62    protected LookupParameter(StorableConstructorFlag _) : base(_) { }
    8463    protected LookupParameter(LookupParameter<T> original, Cloner cloner)
    8564      : base(original, cloner) {
    8665      actualName = original.actualName;
    87       this.Hidden = original.Hidden;
    88       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    89       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    9066    }
    9167    public LookupParameter()
    9268      : base("Anonymous", typeof(T)) {
    9369      this.actualName = Name;
    94       this.Hidden = false;
    95       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    96       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    9770    }
    9871    public LookupParameter(string name)
    9972      : base(name, typeof(T)) {
    10073      this.actualName = Name;
    101       this.Hidden = false;
    102       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    103       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    10474    }
    10575    public LookupParameter(string name, string description)
    10676      : base(name, description, typeof(T)) {
    10777      this.actualName = Name;
    108       this.Hidden = false;
    109       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    110       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    11178    }
    11279    public LookupParameter(string name, string description, string actualName)
    11380      : base(name, description, typeof(T)) {
    11481      this.actualName = string.IsNullOrWhiteSpace(actualName) ? Name : actualName;
    115       this.Hidden = false;
    116       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    117       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    11882    }
    11983
     
    137101        IParameter param = null;
    138102        while (currentExecutionContext != null && (!currentExecutionContext.Parameters.TryGetValue(translatedName, out param)
    139           || param is IContextLookupParameter))
     103          || param is IContextParameter))
    140104          currentExecutionContext = currentExecutionContext.Parent;
    141105        if (currentExecutionContext == null) break;
     
    167131    }
    168132
    169     protected override IItem GetActualValue() {
    170       if (CachedActualValue != null) return CachedActualValue;
    171 
     133    protected override IItem GetActualValueFromContext() {
    172134      string translatedName = Name;
    173135      var value = GetValue(ExecutionContext, ref translatedName);
     
    178140                        typeof(T).GetPrettyName())
    179141        );
    180       CachedActualValue = value;
    181142      return value;
    182143    }
     
    223184    }
    224185
    225     public virtual void InitializeState() {
    226     }
    227     public virtual void ClearState() {
    228       if (cachedActualValues.IsValueCreated) {
    229         cachedActualValues.Value.Dispose();
    230         cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    231       }
    232       if (executionContexts.IsValueCreated) {
    233         executionContexts.Value.Dispose();
    234         executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    235       }
    236     }
    237 
    238186    public event EventHandler ActualNameChanged;
    239187    protected virtual void OnActualNameChanged() {
Note: See TracChangeset for help on using the changeset viewer.