Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6103 was 6103, checked in by cneumuel, 13 years ago

#1424

  • created extension method GetObjectGraphObjects for objects
  • created IStatefulItem interface, which means an object has a state which can be initialized and cleared
  • after an algorithm stopped, all objects of the algorithm-objectgraph with the type IStatefulItem are cleared
  • on prepare, all IStatefulItem objects are initialized
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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, IStatefulItem {
36    public override Image ItemImage {
37      get {
38        if ((dataType != null) && (typeof(IOperator).IsAssignableFrom(dataType)))
39          return HeuristicLab.Common.Resources.VSImageLibrary.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    [Storable(DefaultValue = false)]
58    private bool hidden;
59    public bool Hidden {
60      get { return hidden; }
61      set {
62        if (value != hidden) {
63          hidden = value;
64          OnHiddenChanged();
65        }
66      }
67    }
68
69    private Lazy<ThreadLocal<IItem>> cachedActualValues;
70    public IItem ActualValue {
71      get {
72        if (cachedActualValues.Value.Value == null) cachedActualValues.Value.Value = GetActualValue();
73        return cachedActualValues.Value.Value;
74      }
75      set {
76        cachedActualValues.Value.Value = value;
77        SetActualValue(value);
78      }
79    }
80    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
81    public IExecutionContext ExecutionContext {
82      get { return executionContexts.Value.Value; }
83      set {
84        if (value != executionContexts.Value.Value) {
85          executionContexts.Value.Value = value;
86          cachedActualValues.Value.Value = null;
87        }
88      }
89    }
90
91    [StorableConstructor]
92    protected Parameter(bool deserializing)
93      : base(deserializing) {
94      InitializeState();
95    }
96    protected Parameter(Parameter original, Cloner cloner)
97      : base(original, cloner) {
98      dataType = original.dataType;
99      hidden = original.hidden;
100      InitializeState();
101    }
102    protected Parameter()
103      : base("Anonymous") {
104      dataType = typeof(IItem);
105      hidden = false;
106      InitializeState();
107    }
108    protected Parameter(string name, Type dataType)
109      : base(name) {
110      if (dataType == null) throw new ArgumentNullException();
111      this.dataType = dataType;
112      hidden = false;
113      InitializeState();
114    }
115    protected Parameter(string name, string description, Type dataType)
116      : base(name, description) {
117      if (dataType == null) throw new ArgumentNullException();
118      this.dataType = dataType;
119      hidden = false;
120      InitializeState();
121    }
122
123    public virtual void InitializeState() {
124      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
125      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
126    }
127    public virtual void ClearState() {
128      cachedActualValues = null;
129      executionContexts = null;
130    }
131
132    public override string ToString() {
133      return Name;
134    }
135
136    protected abstract IItem GetActualValue();
137    protected abstract void SetActualValue(IItem value);
138
139    public event EventHandler HiddenChanged;
140    protected virtual void OnHiddenChanged() {
141      EventHandler handler = HiddenChanged;
142      if (handler != null) handler(this, EventArgs.Empty);
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.