- Timestamp:
- 09/17/19 14:05:47 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ContextLookupParameter.cs
r17254 r17257 1 1 using System; 2 using System.Threading;3 2 using HEAL.Attic; 4 3 using HeuristicLab.Common; … … 8 7 [Item("ContextLookupParameter", "A parameter that looks up contexts by type.")] 9 8 [StorableType("4ac189c8-6cf3-48fd-bf79-392d35a872db")] 10 public class ContextLookupParameter<T> : Parameter, IContextLookupParameter<T>, IStatefulItem9 public class ContextLookupParameter<T> : ContextParameter, IContextLookupParameter<T> 11 10 where T : class, IParameterizedItem { 12 11 … … 15 14 } 16 15 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 16 [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); 17 protected ContextLookupParameter(StorableConstructorFlag _) : base(_) { } 18 protected ContextLookupParameter(ContextLookupParameter<T> original, Cloner cloner) : base(original, cloner) { } 19 public ContextLookupParameter() : base("ContextLookup." + typeof(T).Name, string.Empty, typeof(T)) { 20 Hidden = true; 49 21 } 50 22 … … 53 25 } 54 26 55 protected override IItem GetActualValue() { 56 if (CachedActualValue != null) return CachedActualValue; 57 27 protected override IItem GetActualValueFromContext() { 58 28 IItem item = null; 59 29 var context = ExecutionContext; … … 65 35 context = context.Parent; 66 36 } 67 CachedActualValue = item;68 37 return item; 69 38 } … … 72 41 throw new NotSupportedException("The context lookup parameter may not be used to set an item."); 73 42 } 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 43 } 88 44 } -
branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/HeuristicLab.Parameters-3.3.csproj
r17254 r17257 123 123 <Compile Include="ConstrainedValueParameter.cs" /> 124 124 <Compile Include="ContextLookupParameter.cs" /> 125 <Compile Include="ContextParameter.cs" /> 125 126 <Compile Include="FixedValueParameter.cs" /> 126 127 <Compile Include="OptionalConstrainedValueParameter.cs" /> -
branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/LookupParameter.cs
r17254 r17257 21 21 22 22 using System; 23 using System.Threading;24 23 using HEAL.Attic; 25 24 using HeuristicLab.Common; … … 32 31 [Item("LookupParameter", "A parameter whose value is retrieved from or written to a scope.")] 33 32 [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 { 35 34 [Storable] 36 35 private string actualName; … … 60 59 } 61 60 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 79 61 [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(_) { } 84 63 protected LookupParameter(LookupParameter<T> original, Cloner cloner) 85 64 : base(original, cloner) { 86 65 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);90 66 } 91 67 public LookupParameter() 92 68 : base("Anonymous", typeof(T)) { 93 69 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);97 70 } 98 71 public LookupParameter(string name) 99 72 : base(name, typeof(T)) { 100 73 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);104 74 } 105 75 public LookupParameter(string name, string description) 106 76 : base(name, description, typeof(T)) { 107 77 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);111 78 } 112 79 public LookupParameter(string name, string description, string actualName) 113 80 : base(name, description, typeof(T)) { 114 81 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);118 82 } 119 83 … … 137 101 IParameter param = null; 138 102 while (currentExecutionContext != null && (!currentExecutionContext.Parameters.TryGetValue(translatedName, out param) 139 || param is IContext LookupParameter))103 || param is IContextParameter)) 140 104 currentExecutionContext = currentExecutionContext.Parent; 141 105 if (currentExecutionContext == null) break; … … 167 131 } 168 132 169 protected override IItem GetActualValue() { 170 if (CachedActualValue != null) return CachedActualValue; 171 133 protected override IItem GetActualValueFromContext() { 172 134 string translatedName = Name; 173 135 var value = GetValue(ExecutionContext, ref translatedName); … … 178 140 typeof(T).GetPrettyName()) 179 141 ); 180 CachedActualValue = value;181 142 return value; 182 143 } … … 223 184 } 224 185 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 238 186 public event EventHandler ActualNameChanged; 239 187 protected virtual void OnActualNameChanged() { -
branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ScopeParameter.cs
r17226 r17257 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HEAL.Attic;26 26 27 27 namespace HeuristicLab.Parameters { … … 61 61 } 62 62 63 protected override IItem GetActualValue () {63 protected override IItem GetActualValueFromContext() { 64 64 return ExecutionContext.Scope; 65 65 } -
branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ScopeTreeLookupParameter.cs
r17226 r17257 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 using HEAL.Attic;28 28 29 29 namespace HeuristicLab.Parameters { … … 86 86 } 87 87 88 protected override IItem GetActualValue () {88 protected override IItem GetActualValueFromContext() { 89 89 IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope }; 90 90 for (int i = 0; i < depth; i++)
Note: See TracChangeset
for help on using the changeset viewer.