1 | using System;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
8 | [StorableClass]
|
---|
9 | public class Range<T> : Item, IRange<T> where T : class, IStringConvertibleValue, IDeepCloneable {
|
---|
10 | [Storable]
|
---|
11 | private T lowerBound;
|
---|
12 | public T LowerBound {
|
---|
13 | get { return lowerBound; }
|
---|
14 | set {
|
---|
15 | if (lowerBound != value) {
|
---|
16 | if (lowerBound != null) {
|
---|
17 | lowerBound.ValueChanged -= new EventHandler(lowerBound_ToStringChanged);
|
---|
18 | }
|
---|
19 | lowerBound = value;
|
---|
20 | if (lowerBound != null) {
|
---|
21 | lowerBound.ValueChanged += new EventHandler(lowerBound_ToStringChanged);
|
---|
22 | }
|
---|
23 | OnLowerBoundChanged();
|
---|
24 | }
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | [Storable]
|
---|
29 | private T upperBound;
|
---|
30 | public T UpperBound {
|
---|
31 | get { return upperBound; }
|
---|
32 | set {
|
---|
33 | if (upperBound != value) {
|
---|
34 | if (upperBound != null) {
|
---|
35 | upperBound.ValueChanged -= new EventHandler(upperBound_ToStringChanged);
|
---|
36 | }
|
---|
37 | upperBound = value;
|
---|
38 | if (upperBound != null) {
|
---|
39 | upperBound.ValueChanged += new EventHandler(upperBound_ToStringChanged);
|
---|
40 | }
|
---|
41 | OnUpperBoundChanged();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [Storable]
|
---|
47 | private T stepSize;
|
---|
48 | public T StepSize {
|
---|
49 | get { return stepSize; }
|
---|
50 | set {
|
---|
51 | if (stepSize != value) {
|
---|
52 | if (stepSize != null) {
|
---|
53 | stepSize.ValueChanged -= new EventHandler(upperBound_ToStringChanged);
|
---|
54 | }
|
---|
55 | stepSize = value;
|
---|
56 | if (stepSize != null) {
|
---|
57 | stepSize.ValueChanged += new EventHandler(stepSize_ToStringChanged);
|
---|
58 | }
|
---|
59 | OnStepSizeChanged();
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | #region IRange Members
|
---|
65 | object IRange.LowerBound {
|
---|
66 | get { return lowerBound; }
|
---|
67 | set { lowerBound = (T)value; }
|
---|
68 | }
|
---|
69 | object IRange.UpperBound {
|
---|
70 | get { return upperBound; }
|
---|
71 | set { upperBound = (T)value; }
|
---|
72 | }
|
---|
73 | object IRange.StepSize {
|
---|
74 | get { return stepSize; }
|
---|
75 | set { stepSize = (T)value; }
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | #region Constructors and Cloning
|
---|
80 | public Range(T lowerBound, T upperBound, T stepSize) {
|
---|
81 | this.LowerBound = lowerBound;
|
---|
82 | this.UpperBound = upperBound;
|
---|
83 | this.StepSize = stepSize;
|
---|
84 | }
|
---|
85 |
|
---|
86 | [StorableConstructor]
|
---|
87 | protected Range(bool deserializing) : base(deserializing) { }
|
---|
88 | protected Range(Range<T> original, Cloner cloner)
|
---|
89 | : base(original, cloner) {
|
---|
90 | this.LowerBound = cloner.Clone(original.LowerBound);
|
---|
91 | this.UpperBound = cloner.Clone(original.UpperBound);
|
---|
92 | this.StepSize = cloner.Clone(original.StepSize);
|
---|
93 | }
|
---|
94 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
95 | return new Range<T>(this, cloner);
|
---|
96 | }
|
---|
97 | #endregion
|
---|
98 |
|
---|
99 | [StorableHook(HookType.AfterDeserialization)]
|
---|
100 | private void AfterDeserialization() {
|
---|
101 | if (lowerBound != null) {
|
---|
102 | lowerBound.ValueChanged += new EventHandler(lowerBound_ToStringChanged);
|
---|
103 | }
|
---|
104 | if (upperBound != null) {
|
---|
105 | upperBound.ValueChanged += new EventHandler(upperBound_ToStringChanged);
|
---|
106 | }
|
---|
107 | if (stepSize != null) {
|
---|
108 | stepSize.ValueChanged += new EventHandler(stepSize_ToStringChanged);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | #region Events
|
---|
113 | private void lowerBound_ToStringChanged(object sender, EventArgs e) {
|
---|
114 | OnToStringChanged();
|
---|
115 | }
|
---|
116 | private void upperBound_ToStringChanged(object sender, EventArgs e) {
|
---|
117 | OnToStringChanged();
|
---|
118 | }
|
---|
119 | private void stepSize_ToStringChanged(object sender, EventArgs e) {
|
---|
120 | OnToStringChanged();
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | #region Eventhandler
|
---|
125 | public event EventHandler LowerBoundChanged;
|
---|
126 | private void OnLowerBoundChanged() {
|
---|
127 | var handler = LowerBoundChanged;
|
---|
128 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
129 | }
|
---|
130 | public event EventHandler UpperBoundChanged;
|
---|
131 | private void OnUpperBoundChanged() {
|
---|
132 | var handler = UpperBoundChanged;
|
---|
133 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
134 | }
|
---|
135 | public event EventHandler StepSizeChanged;
|
---|
136 | private void OnStepSizeChanged() {
|
---|
137 | var handler = StepSizeChanged;
|
---|
138 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
139 | }
|
---|
140 | public event EventHandler ValueChanged;
|
---|
141 | private void OnValueChanged() {
|
---|
142 | var handler = ValueChanged;
|
---|
143 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
144 | }
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | public override string ToString() {
|
---|
148 | return string.Format("{0},{1}:{2}", LowerBound.ToString(), UpperBound.ToString(), StepSize.ToString());
|
---|
149 | }
|
---|
150 |
|
---|
151 | #region IStringConvertibleValue Members
|
---|
152 |
|
---|
153 | public string GetValue() {
|
---|
154 | return this.ToString();
|
---|
155 | }
|
---|
156 | public bool ReadOnly {
|
---|
157 | get { return false; }
|
---|
158 | }
|
---|
159 | public bool SetValue(string value) {
|
---|
160 | var parts1 = value.Split(':');
|
---|
161 | if (parts1.Length != 2) return false;
|
---|
162 | var parts2 = parts1[0].Split(',');
|
---|
163 | if (parts2.Length != 2) return false;
|
---|
164 |
|
---|
165 | return
|
---|
166 | lowerBound.SetValue(parts2[0]) &&
|
---|
167 | upperBound.SetValue(parts2[1]) &&
|
---|
168 | stepSize.SetValue(parts1[1]);
|
---|
169 | }
|
---|
170 | public bool Validate(string value, out string errorMessage) {
|
---|
171 | // TODO: check that upper is larger than lower
|
---|
172 | T lower = (T)lowerBound.Clone();
|
---|
173 | T upper = (T)upperBound.Clone();
|
---|
174 | T step = (T)stepSize.Clone();
|
---|
175 |
|
---|
176 | var parts1 = value.Split(':');
|
---|
177 | if (parts1.Length != 2) {
|
---|
178 | errorMessage = "Could not parse range."; return false;
|
---|
179 | }
|
---|
180 | var parts2 = parts1[0].Split(',');
|
---|
181 | if (parts2.Length != 2) {
|
---|
182 | errorMessage = "Could not parse range."; return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (lowerBound.SetValue(parts2[0]) &&
|
---|
186 | upperBound.SetValue(parts2[1]) &&
|
---|
187 | stepSize.SetValue(parts1[1])) {
|
---|
188 | errorMessage = string.Empty; return true;
|
---|
189 | } else {
|
---|
190 | errorMessage = "Could not parse range."; return false;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | #endregion
|
---|
195 | }
|
---|
196 | }
|
---|