Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/29/13 14:39:02 (12 years ago)
Author:
mkommend
Message:

#1427: Moved actual value caching and execution contexts from Paramater to LookupParameter.

Location:
trunk/sources/HeuristicLab.Parameters/3.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Parameters/3.3/LookupParameter.cs

    r7259 r9195  
    2121
    2222using System;
     23using System.Threading;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    3132  [Item("LookupParameter", "A parameter whose value is retrieved from or written to a scope.")]
    3233  [StorableClass]
    33   public class LookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem {
     34  public class LookupParameter<T> : Parameter, IStatefulItem, ILookupParameter<T> where T : class, IItem {
    3435    [Storable]
    3536    private string actualName;
     
    5960    }
    6061
     62    private Lazy<ThreadLocal<IItem>> cachedActualValues;
     63    private IItem CachedActualValue {
     64      get { return cachedActualValues.Value.Value; }
     65    }
     66
     67    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
     68    public IExecutionContext ExecutionContext {
     69      get { return executionContexts.Value.Value; }
     70      set {
     71        if (value != executionContexts.Value.Value) {
     72          executionContexts.Value.Value = value;
     73          cachedActualValues.Value.Value = null;
     74        }
     75      }
     76    }
     77
    6178    [StorableConstructor]
    62     protected LookupParameter(bool deserializing) : base(deserializing) { }
     79    protected LookupParameter(bool deserializing)
     80      : base(deserializing) {
     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    }
    6384    protected LookupParameter(LookupParameter<T> original, Cloner cloner)
    6485      : base(original, cloner) {
    6586      actualName = original.actualName;
     87      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     88      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    6689    }
    6790    public LookupParameter()
     
    6992      this.actualName = Name;
    7093      this.Hidden = true;
     94      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     95      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    7196    }
    7297    public LookupParameter(string name)
     
    7499      this.actualName = Name;
    75100      this.Hidden = true;
     101      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     102      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    76103    }
    77104    public LookupParameter(string name, string description)
     
    79106      this.actualName = Name;
    80107      this.Hidden = true;
     108      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     109      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    81110    }
    82111    public LookupParameter(string name, string description, string actualName)
     
    84113      this.actualName = string.IsNullOrWhiteSpace(actualName) ? Name : actualName;
    85114      this.Hidden = true;
     115      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     116      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    86117    }
    87118
     
    132163    }
    133164    protected override IItem GetActualValue() {
     165      if (CachedActualValue != null) return CachedActualValue;
    134166      string name;
    135167      // try to get value from context stack
     
    146178                          typeof(T).GetPrettyName())
    147179          );
     180        cachedActualValues.Value.Value = var.Value;
    148181        return var.Value;
    149182      }
     
    156189                        typeof(T).GetPrettyName())
    157190        );
     191      cachedActualValues.Value.Value = value;
     192
    158193      // try to set value in context stack
    159194      string name;
     
    175210    }
    176211
     212    public virtual void InitializeState() {
     213    }
     214    public virtual void ClearState() {
     215      if (cachedActualValues.IsValueCreated) {
     216        cachedActualValues.Value.Dispose();
     217        cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     218      }
     219      if (executionContexts.IsValueCreated) {
     220        executionContexts.Value.Dispose();
     221        executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     222      }
     223    }
     224
    177225    public event EventHandler ActualNameChanged;
    178226    protected virtual void OnActualNameChanged() {
  • trunk/sources/HeuristicLab.Parameters/3.3/Parameter.cs

    r7259 r9195  
    2222using System;
    2323using System.Drawing;
    24 using System.Threading;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Core;
     
    3332  [Item("Parameter", "A base class for parameters.")]
    3433  [StorableClass]
    35   public abstract class Parameter : NamedItem, IParameter, IStatefulItem {
     34  public abstract class Parameter : NamedItem, IParameter {
    3635    public override Image ItemImage {
    3736      get {
     
    6766    }
    6867
    69     private Lazy<ThreadLocal<IItem>> cachedActualValues;
    7068    public IItem ActualValue {
    71       get {
    72         if (cachedActualValues.Value.Value == null) cachedActualValues.Value.Value = GetActualValue();
    73         return cachedActualValues.Value.Value;
    74       }
    75       set {
    76         cachedActualValues.Value.Value = value;
    77         SetActualValue(value);
    78       }
    79     }
    80     private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
    81     public IExecutionContext ExecutionContext {
    82       get { return executionContexts.Value.Value; }
    83       set {
    84         if (value != executionContexts.Value.Value) {
    85           executionContexts.Value.Value = value;
    86           cachedActualValues.Value.Value = null;
    87         }
    88       }
     69      get { return GetActualValue(); }
     70      set { SetActualValue(value); }
    8971    }
    9072
     
    9274    protected Parameter(bool deserializing)
    9375      : base(deserializing) {
    94       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    95       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    9676    }
    9777    protected Parameter(Parameter original, Cloner cloner)
     
    9979      dataType = original.dataType;
    10080      hidden = original.hidden;
    101       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    102       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    10381    }
    10482    protected Parameter()
     
    10684      dataType = typeof(IItem);
    10785      hidden = false;
    108       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    109       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    11086    }
    11187    protected Parameter(string name, Type dataType)
     
    11490      this.dataType = dataType;
    11591      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);
    11892    }
    11993    protected Parameter(string name, string description, Type dataType)
     
    12296      this.dataType = dataType;
    12397      hidden = false;
    124       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    125       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    126     }
    127 
    128     public virtual void InitializeState() { }
    129     public virtual void ClearState() {
    130       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    131       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    13298    }
    13399
  • trunk/sources/HeuristicLab.Parameters/3.3/ScopeParameter.cs

    r7259 r9195  
    2121
    2222using System;
     23using System.Threading;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    3132  [Item("ScopeParameter", "A parameter which represents the current scope.")]
    3233  [StorableClass]
    33   public class ScopeParameter : Parameter {
     34  public class ScopeParameter : LookupParameter<IScope> {
    3435    public new IScope ActualValue {
    3536      get { return ExecutionContext.Scope; }
    3637    }
     38
    3739
    3840    [StorableConstructor]
     
    4042    protected ScopeParameter(ScopeParameter original, Cloner cloner) : base(original, cloner) { }
    4143    public ScopeParameter()
    42       : base("Anonymous", typeof(IScope)) {
     44      : base("Anonymous") {
    4345      this.Hidden = true;
    4446    }
    4547    public ScopeParameter(string name)
    46       : base(name, typeof(IScope)) {
     48      : base(name) {
    4749      this.Hidden = true;
    4850    }
    4951    public ScopeParameter(string name, string description)
    50       : base(name, description, typeof(IScope)) {
     52      : base(name, description) {
    5153      this.Hidden = true;
    5254    }
Note: See TracChangeset for help on using the changeset viewer.