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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.ParameterConfigurationTreeEncoding {
|
---|
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 | public ConstrainedValue() { }
|
---|
90 | [StorableConstructor]
|
---|
91 | protected ConstrainedValue(bool deserializing) : base(deserializing) { }
|
---|
92 | public ConstrainedValue(IItem value, Type valueDataType, IItemSet<IItem> validValues, bool isNullable) {
|
---|
93 | this.Value = value;
|
---|
94 | this.ValueDataType = valueDataType;
|
---|
95 | this.ValidValues = validValues;
|
---|
96 | this.isNullable = isNullable;
|
---|
97 | }
|
---|
98 | protected ConstrainedValue(ConstrainedValue original, Cloner cloner)
|
---|
99 | : base(original, cloner) {
|
---|
100 | this.valueDataType = original.valueDataType;
|
---|
101 | this.Value = cloner.Clone(original.value);
|
---|
102 | if (original.ValidValues != null) this.ValidValues = cloner.Clone(original.ValidValues);
|
---|
103 | this.isNullable = original.isNullable;
|
---|
104 | }
|
---|
105 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
106 | return new ConstrainedValue(this, cloner);
|
---|
107 | }
|
---|
108 | [StorableHook(HookType.AfterDeserialization)]
|
---|
109 | private void AfterDeserialization() {
|
---|
110 | if (this.value != null) RegisterEvents();
|
---|
111 | }
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | private void value_ToStringChanged(object sender, EventArgs e) {
|
---|
115 | OnToStringChanged();
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void ConstrainedValue_Changed(object sender, EventArgs e) {
|
---|
119 | OnToStringChanged();
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override string ToString() {
|
---|
123 | if (this.value is Symbol) {
|
---|
124 | return string.Format("{0}: {1}", this.value, ((Symbol)this.value).InitialFrequency);
|
---|
125 | }
|
---|
126 | return value != null ? value.ToString() : base.ToString();
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|