Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ContextLookupParameter.cs @ 17254

Last change on this file since 17254 was 17254, checked in by abeham, 5 years ago

#2521: Added context lookup parameter

  • Refactored tests
  • Fixed duplicate GUID
File size: 3.8 KB
Line 
1using System;
2using System.Threading;
3using HEAL.Attic;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6
7namespace HeuristicLab.Parameters {
8  [Item("ContextLookupParameter", "A parameter that looks up contexts by type.")]
9  [StorableType("4ac189c8-6cf3-48fd-bf79-392d35a872db")]
10  public class ContextLookupParameter<T> : Parameter, IContextLookupParameter<T>, IStatefulItem
11      where T : class, IParameterizedItem {
12
13    public new T ActualValue {
14      get { return (T)base.ActualValue; }
15    }
16
17    private Lazy<ThreadLocal<IItem>> cachedActualValues;
18    protected IItem CachedActualValue {
19      get { return cachedActualValues.Value.Value; }
20      set { cachedActualValues.Value.Value = value; }
21    }
22
23    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
24    public IExecutionContext ExecutionContext {
25      get { return executionContexts.Value.Value; }
26      set {
27        if (value != executionContexts.Value.Value) {
28          executionContexts.Value.Value = value;
29          cachedActualValues.Value.Value = null;
30        }
31      }
32    }
33
34    [StorableConstructor]
35    protected ContextLookupParameter(StorableConstructorFlag _) : base(_) {
36      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
37      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
38    }
39    protected ContextLookupParameter(ContextLookupParameter<T> original, Cloner cloner)
40      : base(original, cloner) {
41      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
42      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
43    }
44    public ContextLookupParameter() : this("Anonymous") { }
45    public ContextLookupParameter(string name, string description = null)
46      : base(name, description, typeof(T)) {
47      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
48      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new ContextLookupParameter<T>(this, cloner);
53    }
54
55    protected override IItem GetActualValue() {
56      if (CachedActualValue != null) return CachedActualValue;
57
58      IItem item = null;
59      var context = ExecutionContext;
60      while (context != null) {
61        if (context.Item != null && typeof(T).IsAssignableFrom(context.Item.GetType())) {
62          item = context.Item;
63          break;
64        }
65        context = context.Parent;
66      }
67      CachedActualValue = item;
68      return item;
69    }
70
71    protected override void SetActualValue(IItem value) {
72      throw new NotSupportedException("The context lookup parameter may not be used to set an item.");
73    }
74
75    public virtual void InitializeState() {
76    }
77    public virtual void ClearState() {
78      if (cachedActualValues.IsValueCreated) {
79        cachedActualValues.Value.Dispose();
80        cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
81      }
82      if (executionContexts.IsValueCreated) {
83        executionContexts.Value.Dispose();
84        executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
85      }
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.