1 | using System;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | namespace 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 | }
|
---|