Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/ConstrainedValue.cs @ 9827

Last change on this file since 9827 was 6197, checked in by cneumuel, 14 years ago

#1215

  • some fixes
File size: 3.3 KB
Line 
1using System;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6
7namespace HeuristicLab.Problems.MetaOptimization {
8  [StorableClass]
9  public class ConstrainedValue : Item {
10    [Storable]
11    protected Type valueDataType;
12    public Type ValueDataType {
13      get { return this.valueDataType; }
14      protected set { this.valueDataType = value; }
15    }
16
17    [Storable]
18    protected IItem value;
19    public IItem Value {
20      get { return value; }
21      set {
22        if (this.value != value) {
23          if(this.value != null) DeregisterEvents();
24          this.value = value;
25          if(this.value != null) RegisterEvents();
26          OnToStringChanged();
27        }
28      }
29    }
30
31    [Storable]
32    protected IItemSet<IItem> validValues;
33    public IItemSet<IItem> ValidValues {
34      get { return validValues; }
35      protected set {
36        if (this.validValues != value) {
37          this.validValues = value;
38        }
39      }
40    }
41
42    [Storable]
43    protected bool isNullable;
44    public bool IsNullable {
45      get { return isNullable; }
46      protected set {
47        if (this.isNullable != value) {
48          this.isNullable = value;
49        }
50      }
51    }
52
53    private void RegisterEvents() {
54      this.value.ToStringChanged += new EventHandler(value_ToStringChanged);
55      if (this.value is Symbol) {
56        ((Symbol)this.value).Changed += new EventHandler(ConstrainedValue_Changed);
57      }
58    }
59
60    private void DeregisterEvents() {
61      this.value.ToStringChanged -= new EventHandler(value_ToStringChanged);
62      if (this.value is Symbol) {
63        ((Symbol)this.value).Changed -= new EventHandler(ConstrainedValue_Changed);
64      }
65    }
66
67    #region constructors and cloning
68    public ConstrainedValue() { }
69    [StorableConstructor]
70    protected ConstrainedValue(bool deserializing) : base(deserializing) { }
71    public ConstrainedValue(IItem value, Type valueDataType, IItemSet<IItem> validValues, bool isNullable) {
72      this.Value = value;
73      this.ValueDataType = valueDataType;
74      this.ValidValues = validValues;
75      this.isNullable = isNullable;
76    }
77    protected ConstrainedValue(ConstrainedValue original, Cloner cloner) : base(original, cloner) {
78      this.valueDataType = original.valueDataType;
79      this.Value = cloner.Clone(original.value);
80      if(original.ValidValues != null) this.ValidValues = cloner.Clone(original.ValidValues);
81      this.isNullable = original.isNullable;
82    }
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new ConstrainedValue(this, cloner);
85    }
86    [StorableHook(HookType.AfterDeserialization)]
87    private void AfterDeserialization() {
88      if (this.value != null) RegisterEvents();
89    }
90    #endregion
91
92    private void value_ToStringChanged(object sender, EventArgs e) {
93      OnToStringChanged();
94    }
95
96    private void ConstrainedValue_Changed(object sender, EventArgs e) {
97      OnToStringChanged();
98    }
99
100    public override string ToString() {
101      if (this.value is Symbol) {
102        return string.Format("{0}: {1}", this.value, ((Symbol)this.value).InitialFrequency);
103      }
104      return value != null ? value.ToString() : base.ToString();
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.