Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParallelEngine/HeuristicLab.Parameters/3.3/Parameter.cs @ 5178

Last change on this file since 5178 was 5178, checked in by swagner, 14 years ago

Revoked changes of r5177 (#1333)

File size: 5.4 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System.Collections.Generic;
28using System.Threading;
29
30namespace HeuristicLab.Parameters {
31  /// <summary>
32  /// A base class for parameters.
33  /// </summary>
34  [Item("Parameter", "A base class for parameters.")]
35  [StorableClass]
36  public abstract class Parameter : NamedItem, IParameter {
37    public override Image ItemImage {
38      get {
39        if ((dataType != null) && (typeof(IOperator).IsAssignableFrom(dataType)))
40          return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method;
41        else
42          return base.ItemImage;
43      }
44    }
45    public override bool CanChangeName {
46      get { return false; }
47    }
48    public override bool CanChangeDescription {
49      get { return false; }
50    }
51
52    [Storable]
53    private Type dataType;
54    public Type DataType {
55      get { return dataType; }
56    }
57    protected Dictionary<int, IItem> cachedActualValues;
58    public IItem ActualValue {
59      get {
60        int id = Thread.CurrentThread.ManagedThreadId;
61        IItem value = null;
62        lock (cachedActualValues) {
63          cachedActualValues.TryGetValue(id, out value);
64        }
65        if (value == null) {
66          value = GetActualValue();
67          lock (cachedActualValues) {
68            if (cachedActualValues.ContainsKey(id)) cachedActualValues[id] = value;
69            else cachedActualValues.Add(id, value);
70          }
71        }
72        return value;
73      }
74      set {
75        int id = Thread.CurrentThread.ManagedThreadId;
76        lock (cachedActualValues) {
77          if (cachedActualValues.ContainsKey(id)) cachedActualValues[id] = value;
78          else cachedActualValues.Add(id, value);
79        }
80        SetActualValue(value);
81      }
82    }
83    private Dictionary<int, IExecutionContext> executionContexts;
84    public IExecutionContext ExecutionContext {
85      get {
86        int id = Thread.CurrentThread.ManagedThreadId;
87        IExecutionContext context = null;
88        lock (executionContexts) {
89          executionContexts.TryGetValue(id, out context);
90        }
91        return context;
92      }
93      set {
94        IExecutionContext context = null;
95        int id = Thread.CurrentThread.ManagedThreadId;
96        lock (executionContexts) {
97          executionContexts.TryGetValue(id, out context);
98          if (value != context) {
99            if (context == null) executionContexts.Add(id, value);
100            else if (value == null) {
101              executionContexts.Remove(id);
102              lock (cachedActualValues) cachedActualValues.Remove(id);
103            } else {
104              executionContexts[id] = value;
105              lock (cachedActualValues) cachedActualValues.Remove(id);
106            }
107          }
108        }
109      }
110    }
111
112    [StorableConstructor]
113    protected Parameter(bool deserializing) : base(deserializing) {
114      cachedActualValues = new Dictionary<int, IItem>();
115      executionContexts = new Dictionary<int, IExecutionContext>();
116    }
117    protected Parameter(Parameter original, Cloner cloner)
118      : base(original, cloner) {
119      dataType = original.dataType;
120      cachedActualValues = new Dictionary<int, IItem>();
121      executionContexts = new Dictionary<int, IExecutionContext>();
122      foreach (var item in original.executionContexts) {
123        executionContexts.Add(item.Key, cloner.Clone(item.Value));
124      }
125    }
126    protected Parameter()
127      : base("Anonymous") {
128      dataType = typeof(IItem);
129      cachedActualValues = new Dictionary<int, IItem>();
130      executionContexts = new Dictionary<int, IExecutionContext>();
131    }
132    protected Parameter(string name, Type dataType)
133      : base(name) {
134      if (dataType == null) throw new ArgumentNullException();
135      this.dataType = dataType;
136      cachedActualValues = new Dictionary<int, IItem>();
137      executionContexts = new Dictionary<int, IExecutionContext>();
138    }
139    protected Parameter(string name, string description, Type dataType)
140      : base(name, description) {
141      if (dataType == null) throw new ArgumentNullException();
142      this.dataType = dataType;
143      cachedActualValues = new Dictionary<int, IItem>();
144      executionContexts = new Dictionary<int, IExecutionContext>();
145    }
146
147    public override string ToString() {
148      return Name;
149    }
150
151    protected abstract IItem GetActualValue();
152    protected abstract void SetActualValue(IItem value);
153  }
154}
Note: See TracBrowser for help on using the repository browser.