Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/RangeConstraints/ConstrainedValue.cs @ 8614

Last change on this file since 8614 was 8535, checked in by jkarder, 12 years ago

#1853:

  • enhanced combinations count calculation
  • restructured code
  • minor code improvements
  • added license information
File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
29  [StorableClass]
30  public class ConstrainedValue : Item {
31    [Storable]
32    protected Type valueDataType;
33    public Type ValueDataType {
34      get { return this.valueDataType; }
35      protected set { this.valueDataType = value; }
36    }
37
38    [Storable]
39    protected IItem value;
40    public IItem Value {
41      get { return value; }
42      set {
43        if (this.value != value) {
44          if (this.value != null) DeregisterEvents();
45          this.value = value;
46          if (this.value != null) RegisterEvents();
47          OnToStringChanged();
48        }
49      }
50    }
51
52    [Storable]
53    protected IItemSet<IItem> validValues;
54    public IItemSet<IItem> ValidValues {
55      get { return validValues; }
56      protected set {
57        if (this.validValues != value) {
58          this.validValues = value;
59        }
60      }
61    }
62
63    [Storable]
64    protected bool isNullable;
65    public bool IsNullable {
66      get { return isNullable; }
67      protected set {
68        if (this.isNullable != value) {
69          this.isNullable = value;
70        }
71      }
72    }
73
74    private void RegisterEvents() {
75      this.value.ToStringChanged += new EventHandler(value_ToStringChanged);
76      if (this.value is Symbol) {
77        ((Symbol)this.value).Changed += new EventHandler(ConstrainedValue_Changed);
78      }
79    }
80
81    private void DeregisterEvents() {
82      this.value.ToStringChanged -= new EventHandler(value_ToStringChanged);
83      if (this.value is Symbol) {
84        ((Symbol)this.value).Changed -= new EventHandler(ConstrainedValue_Changed);
85      }
86    }
87
88    #region Constructors and Cloning
89    [StorableConstructor]
90    protected ConstrainedValue(bool deserializing) : base(deserializing) { }
91    protected ConstrainedValue(ConstrainedValue original, Cloner cloner)
92      : base(original, cloner) {
93      this.valueDataType = original.valueDataType;
94      this.Value = cloner.Clone(original.value);
95      if (original.ValidValues != null) this.ValidValues = cloner.Clone(original.ValidValues);
96      this.isNullable = original.isNullable;
97    }
98    public ConstrainedValue() : base() { }
99    public ConstrainedValue(IItem value, Type valueDataType, IItemSet<IItem> validValues, bool isNullable)
100      : base() {
101      this.Value = value;
102      this.ValueDataType = valueDataType;
103      this.ValidValues = validValues;
104      this.isNullable = isNullable;
105    }
106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserialization() {
108      if (this.value != null) RegisterEvents();
109    }
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new ConstrainedValue(this, cloner);
112    }
113    #endregion
114
115    private void value_ToStringChanged(object sender, EventArgs e) {
116      OnToStringChanged();
117    }
118
119    private void ConstrainedValue_Changed(object sender, EventArgs e) {
120      OnToStringChanged();
121    }
122
123    public override string ToString() {
124      if (this.value is Symbol) {
125        return string.Format("{0}: {1}", this.value, ((Symbol)this.value).InitialFrequency);
126      }
127      return value != null ? value.ToString() : base.ToString();
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.