Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17257


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
Location:
branches/2521_ProblemRefactoring
Files:
1 added
14 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Core/3.3/Interfaces/ILookupParameter.cs

    r17254 r17257  
    2424
    2525namespace HeuristicLab.Core {
     26  [StorableType("285d7903-5c4b-4136-bd69-491dc1766825")]
     27  public interface IContextParameter : IParameter {
     28    IExecutionContext ExecutionContext { get; set; }
     29  }
     30
    2631  [StorableType("78e29fa3-e603-4a2e-a3e0-2dee459891f1")]
    27   public interface ILookupParameter : IParameter {
     32  public interface ILookupParameter : IContextParameter {
    2833    string ActualName { get; set; }
    2934    string TranslatedName { get; }
    30     IExecutionContext ExecutionContext { get; set; }
    3135    event EventHandler ActualNameChanged;
    3236  }
     
    3741  }
    3842
    39   [StorableType("285d7903-5c4b-4136-bd69-491dc1766825")]
    40   public interface IContextLookupParameter : IParameter {
    41     IExecutionContext ExecutionContext { get; set; }
    42   }
    43 
    4443  [StorableType("26db2a08-7537-4e0e-b93c-4d4478698cd1")]
    45   public interface IContextLookupParameter<T> : IContextLookupParameter where T : class, IParameterizedItem {
     44  public interface IContextLookupParameter<T> : IContextParameter where T : class, IParameterizedItem {
    4645    new T ActualValue { get; }
    4746  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Operators/3.3/Operator.cs

    r17254 r17257  
    119119        ExecutionContext = context;
    120120        this.cancellationToken = cancellationToken;
    121         foreach (var param in Parameters.OfType<ILookupParameter>())
    122           param.ExecutionContext = context;
    123         foreach (var param in Parameters.OfType<IContextLookupParameter>())
     121        foreach (var param in Parameters.OfType<IContextParameter>())
    124122          param.ExecutionContext = context;
    125123        IOperation next = Apply();
     
    128126      }
    129127      finally {
    130         foreach (var param in Parameters.OfType<ILookupParameter>())
    131           param.ExecutionContext = null;
    132         foreach (var param in Parameters.OfType<IContextLookupParameter>())
     128        foreach (var param in Parameters.OfType<IContextParameter>())
    133129          param.ExecutionContext = null;
    134130        ExecutionContext = null;
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncodedSolution.cs

    r16751 r17257  
    2222#endregion
    2323
     24using HEAL.Attic;
    2425using HeuristicLab.Core;
    2526
    2627namespace HeuristicLab.Optimization {
     28  [StorableType("c81bebf8-812c-4f8b-a1bc-d310fb844367")]
    2729  public interface IEncodedSolution : IItem { }
    2830}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblem.cs

    r17225 r17257  
    2020#endregion
    2121
    22 using HeuristicLab.Core;
    23 using HeuristicLab.Data;
     22using HEAL.Attic;
    2423
    2524namespace HeuristicLab.Optimization {
    26   public interface IMultiObjectiveProblem : IProblem, IMultiObjectiveHeuristicOptimizationProblem {
     25  [StorableType("251d79f1-a065-47f9-85a3-2e8dbdbf685e")]
     26  public interface IMultiObjectiveProblem : IProblem, IMultiObjectiveHeuristicOptimizationProblem { }
    2727
    28   }
    29 
     28  [StorableType("806fb361-1469-4903-9f54-f8678b0717b9")]
    3029  public interface IMultiObjectiveProblem<TEncoding, TEncodedSolution> : IMultiObjectiveProblem, IProblem<TEncoding, TEncodedSolution>
    3130    where TEncoding : class, IEncoding<TEncodedSolution>
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblemDefinition.cs

    r17226 r17257  
    2020#endregion
    2121
    22 using System.Collections;
    2322using System.Collections.Generic;
    2423using HEAL.Attic;
    2524using HeuristicLab.Core;
    26 using HeuristicLab.Data;
    2725
    2826namespace HeuristicLab.Optimization {
     27  [StorableType("609e74a2-d5e7-4495-b47c-2d365a8d717c")]
    2928  public interface IMultiObjectiveProblemDefinition {
    3029    int Objectives { get; }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblem.cs

    r17225 r17257  
    2121
    2222
     23using HEAL.Attic;
     24
    2325namespace HeuristicLab.Optimization {
    24   public interface ISingleObjectiveProblem : ISingleObjectiveHeuristicOptimizationProblem {
     26  [StorableType("24830fd5-7d97-41a5-9d7e-d84f1b7ab259")]
     27  public interface ISingleObjectiveProblem : ISingleObjectiveHeuristicOptimizationProblem { }
    2528
    26   }
    27 
     29  [StorableType("9cc9422f-0bb5-41e8-9d9e-6e0b66a66449")]
    2830  public interface ISingleObjectiveProblem<TEncoding, TEncodedSolution> : ISingleObjectiveProblem, IProblem<TEncoding, TEncodedSolution>
    2931    where TEncoding : class, IEncoding<TEncodedSolution>
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs

    r17226 r17257  
    2525
    2626namespace HeuristicLab.Optimization {
     27  [StorableType("80849d87-5cc6-4dc0-8c10-966ecb68d582")]
    2728  public interface ISingleObjectiveProblemDefinition {
    2829    bool Maximization { get; }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/IProblem.cs

    r17226 r17257  
    3838  }
    3939
     40  [StorableType("1b4af8b9-bdf5-4ffd-86e6-35b481bfbf45")]
    4041  public interface IProblem<TEncoding, TEncodedSolution> : IHeuristicOptimizationProblem
    4142    where TEncoding : class, IEncoding<TEncodedSolution>
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Results/ResultParameter.cs

    r17226 r17257  
    2222using System;
    2323using System.Drawing;
     24using HEAL.Attic;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Common.Resources;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Parameters;
    28 using HEAL.Attic;
    2929
    3030namespace HeuristicLab.Optimization {
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ContextLookupParameter.cs

    r17254 r17257  
    11using System;
    2 using System.Threading;
    32using HEAL.Attic;
    43using HeuristicLab.Common;
     
    87  [Item("ContextLookupParameter", "A parameter that looks up contexts by type.")]
    98  [StorableType("4ac189c8-6cf3-48fd-bf79-392d35a872db")]
    10   public class ContextLookupParameter<T> : Parameter, IContextLookupParameter<T>, IStatefulItem
     9  public class ContextLookupParameter<T> : ContextParameter, IContextLookupParameter<T>
    1110      where T : class, IParameterizedItem {
    1211
     
    1514    }
    1615
    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 
    3416    [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;
    4921    }
    5022
     
    5325    }
    5426
    55     protected override IItem GetActualValue() {
    56       if (CachedActualValue != null) return CachedActualValue;
    57 
     27    protected override IItem GetActualValueFromContext() {
    5828      IItem item = null;
    5929      var context = ExecutionContext;
     
    6535        context = context.Parent;
    6636      }
    67       CachedActualValue = item;
    6837      return item;
    6938    }
     
    7241      throw new NotSupportedException("The context lookup parameter may not be used to set an item.");
    7342    }
    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     }
    8743  }
    8844}
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/HeuristicLab.Parameters-3.3.csproj

    r17254 r17257  
    123123    <Compile Include="ConstrainedValueParameter.cs" />
    124124    <Compile Include="ContextLookupParameter.cs" />
     125    <Compile Include="ContextParameter.cs" />
    125126    <Compile Include="FixedValueParameter.cs" />
    126127    <Compile Include="OptionalConstrainedValueParameter.cs" />
  • 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() {
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ScopeParameter.cs

    r17226 r17257  
    2121
    2222using System;
     23using HEAL.Attic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HEAL.Attic;
    2626
    2727namespace HeuristicLab.Parameters {
     
    6161    }
    6262
    63     protected override IItem GetActualValue() {
     63    protected override IItem GetActualValueFromContext() {
    6464      return ExecutionContext.Scope;
    6565    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ScopeTreeLookupParameter.cs

    r17226 r17257  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    27 using HEAL.Attic;
    2828
    2929namespace HeuristicLab.Parameters {
     
    8686    }
    8787
    88     protected override IItem GetActualValue() {
     88    protected override IItem GetActualValueFromContext() {
    8989      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
    9090      for (int i = 0; i < depth; i++)
Note: See TracChangeset for help on using the changeset viewer.