1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Threading;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Parameters {
|
---|
29 | [Item("ContextParameter", "")]
|
---|
30 | [StorableType("b4f6a624-19b8-46aa-b89b-6bba2f2e2491")]
|
---|
31 | public abstract class ContextParameter : Parameter, IContextParameter, IStatefulItem {
|
---|
32 |
|
---|
33 | protected Lazy<ThreadLocal<IItem>> cachedActualValues;
|
---|
34 | protected virtual IItem CachedActualValue {
|
---|
35 | get { return cachedActualValues.Value.Value; }
|
---|
36 | set { cachedActualValues.Value.Value = value; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | protected Lazy<ThreadLocal<IExecutionContext>> executionContexts;
|
---|
40 | public virtual IExecutionContext ExecutionContext {
|
---|
41 | get { return executionContexts.Value.Value; }
|
---|
42 | set {
|
---|
43 | if (value != executionContexts.Value.Value) {
|
---|
44 | executionContexts.Value.Value = value;
|
---|
45 | cachedActualValues.Value.Value = null;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected ContextParameter(StorableConstructorFlag _) : base(_) {
|
---|
52 | cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
53 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
54 | }
|
---|
55 | protected ContextParameter(ContextParameter original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
58 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
59 | }
|
---|
60 | protected ContextParameter() : this("Anonymous", string.Empty, typeof(IItem)) { }
|
---|
61 | protected ContextParameter(string name, Type dataType) : this(name, string.Empty, dataType) { }
|
---|
62 | protected ContextParameter(string name, string description, Type dataType)
|
---|
63 | : base(name, description, dataType) {
|
---|
64 | cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
65 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override IItem GetActualValue() {
|
---|
69 | if (CachedActualValue != null) return CachedActualValue;
|
---|
70 | CachedActualValue = GetActualValueFromContext();
|
---|
71 | return CachedActualValue;
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected abstract IItem GetActualValueFromContext();
|
---|
75 |
|
---|
76 | public virtual void InitializeState() {
|
---|
77 | }
|
---|
78 | public virtual void ClearState() {
|
---|
79 | if (cachedActualValues.IsValueCreated) {
|
---|
80 | cachedActualValues.Value.Dispose();
|
---|
81 | cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
82 | }
|
---|
83 | if (executionContexts.IsValueCreated) {
|
---|
84 | executionContexts.Value.Dispose();
|
---|
85 | executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|