Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/Parameter.cs @ 5193

Last change on this file since 5193 was 5193, checked in by swagner, 13 years ago

Merged ParallelEngine branch back into trunk (#1333)

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Drawing;
24using System.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Parameters {
30  /// <summary>
31  /// A base class for parameters.
32  /// </summary>
33  [Item("Parameter", "A base class for parameters.")]
34  [StorableClass]
35  public abstract class Parameter : NamedItem, IParameter {
36    public override Image ItemImage {
37      get {
38        if ((dataType != null) && (typeof(IOperator).IsAssignableFrom(dataType)))
39          return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method;
40        else
41          return base.ItemImage;
42      }
43    }
44    public override bool CanChangeName {
45      get { return false; }
46    }
47    public override bool CanChangeDescription {
48      get { return false; }
49    }
50
51    [Storable]
52    private Type dataType;
53    public Type DataType {
54      get { return dataType; }
55    }
56
57    private Lazy<ThreadLocal<IItem>> cachedActualValues;
58    public IItem ActualValue {
59      get {
60        if (cachedActualValues.Value.Value == null) cachedActualValues.Value.Value = GetActualValue();
61        return cachedActualValues.Value.Value;
62      }
63      set {
64        cachedActualValues.Value.Value = value;
65        SetActualValue(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    [StorableConstructor]
80    protected Parameter(bool deserializing)
81      : base(deserializing) {
82      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
83      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
84    }
85    protected Parameter(Parameter original, Cloner cloner)
86      : base(original, cloner) {
87      dataType = original.dataType;
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    }
91    protected Parameter()
92      : base("Anonymous") {
93      dataType = typeof(IItem);
94      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
95      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
96    }
97    protected Parameter(string name, Type dataType)
98      : base(name) {
99      if (dataType == null) throw new ArgumentNullException();
100      this.dataType = dataType;
101      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
102      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
103    }
104    protected Parameter(string name, string description, Type dataType)
105      : base(name, description) {
106      if (dataType == null) throw new ArgumentNullException();
107      this.dataType = dataType;
108      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
109      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
110    }
111
112    public override string ToString() {
113      return Name;
114    }
115
116    protected abstract IItem GetActualValue();
117    protected abstract void SetActualValue(IItem value);
118  }
119}
Note: See TracBrowser for help on using the repository browser.