Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ParameterChangeHandler.cs @ 17614

Last change on this file since 17614 was 17614, checked in by abeham, 4 years ago

#2521: work in progress (removed solution creator parameter from encoding), OrienteeringProblem and test functions are broken

File size: 11.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Core;
24using HeuristicLab.Data;
25
26namespace HeuristicLab.Parameters {
27  public class ParameterChangeHandler<TItem> where TItem : class, IItem {
28    protected Action handler;
29
30    protected ParameterChangeHandler(IValueParameter<TItem> parameter, Action handler) {
31      if (!(parameter is IFixedValueParameter<TItem>))
32        parameter.ValueChanged += ParameterOnValueChanged;
33      this.handler = handler;
34    }
35
36    protected virtual void ParameterOnValueChanged(object sender, EventArgs e) {
37      handler();
38    }
39
40    public static ParameterChangeHandler<TItem> Create(IValueParameter<TItem> parameter, Action handler) {
41      return new ParameterChangeHandler<TItem>(parameter, handler);
42    }
43  }
44
45  public class ValueTypeValueParameterChangeHandler<TItem, TValue> : ParameterChangeHandler<TItem>
46    where TValue : struct
47    where TItem : ValueTypeValue<TValue> {
48    private ValueTypeValue<TValue> last;
49
50    protected ValueTypeValueParameterChangeHandler(IValueParameter<TItem> parameter, Action handler)
51      : base(parameter, handler) {
52      last = parameter.Value;
53      if (last != null && !last.ReadOnly)
54        last.ValueChanged += ParameterValueOnValueChanged;
55    }
56
57    protected override void ParameterOnValueChanged(object sender, EventArgs e) {
58      if (last != null) last.ValueChanged -= ParameterValueOnValueChanged;
59      last = ((IValueParameter<TItem>)sender).Value;
60      if (last != null && !last.ReadOnly)
61        last.ValueChanged += ParameterValueOnValueChanged;
62      base.ParameterOnValueChanged(sender, e);
63    }
64
65    protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
66      handler();
67    }
68
69    public static ValueTypeValueParameterChangeHandler<TItem, TValue> Create(IValueParameter<TItem> parameter, Action handler)
70      => new ValueTypeValueParameterChangeHandler<TItem, TValue>(parameter, handler);
71  }
72  public class ValueTypeArrayParameterChangeHandler<TItem, TValue> : ParameterChangeHandler<TItem>
73    where TValue : struct
74    where TItem : ValueTypeArray<TValue> {
75    private ValueTypeArray<TValue> last;
76
77    protected ValueTypeArrayParameterChangeHandler(IValueParameter<TItem> parameter, Action handler)
78      : base(parameter, handler) {
79      last = parameter.Value;
80      if (last != null && !last.ReadOnly)
81        last.ToStringChanged += ParameterValueOnValueChanged;
82    }
83
84    protected override void ParameterOnValueChanged(object sender, EventArgs e) {
85      if (last != null) last.ToStringChanged -= ParameterValueOnValueChanged;
86      last = ((IValueParameter<TItem>)sender).Value;
87      if (last != null && !last.ReadOnly)
88        last.ToStringChanged += ParameterValueOnValueChanged;
89      base.ParameterOnValueChanged(sender, e);
90    }
91
92    protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
93      handler();
94    }
95
96    public static ValueTypeArrayParameterChangeHandler<TItem, TValue> Create(IValueParameter<TItem> parameter, Action handler)
97      => new ValueTypeArrayParameterChangeHandler<TItem, TValue>(parameter, handler);
98  }
99
100  public class ValueTypeMatrixParameterChangeHandler<TItem, TValue> : ParameterChangeHandler<TItem>
101    where TValue : struct
102    where TItem : ValueTypeMatrix<TValue> {
103    private ValueTypeMatrix<TValue> last;
104
105    protected ValueTypeMatrixParameterChangeHandler(IValueParameter<TItem> parameter, Action handler)
106      : base(parameter, handler) {
107      last = parameter.Value;
108      if (last != null && !last.ReadOnly)
109        last.ToStringChanged += ParameterValueOnValueChanged;
110    }
111
112    protected override void ParameterOnValueChanged(object sender, EventArgs e) {
113      if (last != null) last.ToStringChanged -= ParameterValueOnValueChanged;
114      last = ((IValueParameter<TItem>)sender).Value;
115      if (last != null && !last.ReadOnly)
116        last.ToStringChanged += ParameterValueOnValueChanged;
117      base.ParameterOnValueChanged(sender, e);
118    }
119
120    protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
121      handler();
122    }
123
124    public static ValueTypeMatrixParameterChangeHandler<TItem, TValue> Create(IValueParameter<TItem> parameter, Action handler)
125      => new ValueTypeMatrixParameterChangeHandler<TItem, TValue>(parameter, handler);
126  }
127
128  public class BoolValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<BoolValue, bool> {
129    private BoolValueParameterChangeHandler(IValueParameter<BoolValue> parameter, Action handler) : base(parameter, handler) { }
130    public static new BoolValueParameterChangeHandler Create(IValueParameter<BoolValue> parameter, Action handler)
131      => new BoolValueParameterChangeHandler(parameter, handler);
132  }
133  public class IntValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<IntValue, int> {
134    private IntValueParameterChangeHandler(IValueParameter<IntValue> parameter, Action handler) : base(parameter, handler) { }
135    public static new IntValueParameterChangeHandler Create(IValueParameter<IntValue> parameter, Action handler)
136      => new IntValueParameterChangeHandler(parameter, handler);
137  }
138  public class DoubleValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<DoubleValue, double> {
139    private DoubleValueParameterChangeHandler(IValueParameter<DoubleValue> parameter, Action handler) : base(parameter, handler) { }
140    public static new DoubleValueParameterChangeHandler Create(IValueParameter<DoubleValue> parameter, Action handler)
141      => new DoubleValueParameterChangeHandler(parameter, handler);
142  }
143  public class PercentValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<PercentValue, double> {
144    private PercentValueParameterChangeHandler(IValueParameter<PercentValue> parameter, Action handler) : base(parameter, handler) { }
145    public static new PercentValueParameterChangeHandler Create(IValueParameter<PercentValue> parameter, Action handler)
146      => new PercentValueParameterChangeHandler(parameter, handler);
147  }
148  public class DateTimeValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<DateTimeValue, DateTime> {
149    private DateTimeValueParameterChangeHandler(IValueParameter<DateTimeValue> parameter, Action handler) : base(parameter, handler) { }
150    public static new DateTimeValueParameterChangeHandler Create(IValueParameter<DateTimeValue> parameter, Action handler)
151      => new DateTimeValueParameterChangeHandler(parameter, handler);
152  }
153  public class TimeSpanValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<TimeSpanValue, TimeSpan> {
154    private TimeSpanValueParameterChangeHandler(IValueParameter<TimeSpanValue> parameter, Action handler) : base(parameter, handler) { }
155    public static new TimeSpanValueParameterChangeHandler Create(IValueParameter<TimeSpanValue> parameter, Action handler)
156      => new TimeSpanValueParameterChangeHandler(parameter, handler);
157  }
158  public class EnumValueParameterChangeHandler<TEnum> : ValueTypeValueParameterChangeHandler<EnumValue<TEnum>, TEnum> where TEnum : struct, IComparable {
159    private EnumValueParameterChangeHandler(IValueParameter<EnumValue<TEnum>> parameter, Action handler) : base(parameter, handler) { }
160    public static new EnumValueParameterChangeHandler<TEnum> Create(IValueParameter<EnumValue<TEnum>> parameter, Action handler)
161      => new EnumValueParameterChangeHandler<TEnum>(parameter, handler);
162  }
163  public class BoolArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler<BoolArray, bool> {
164    private BoolArrayParameterChangeHandler(IValueParameter<BoolArray> parameter, Action handler) : base(parameter, handler) { }
165    public static new BoolArrayParameterChangeHandler Create(IValueParameter<BoolArray> parameter, Action handler)
166      => new BoolArrayParameterChangeHandler(parameter, handler);
167  }
168  public class IntArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler<IntArray, int> {
169    private IntArrayParameterChangeHandler(IValueParameter<IntArray> parameter, Action handler) : base(parameter, handler) { }
170    public static new IntArrayParameterChangeHandler Create(IValueParameter<IntArray> parameter, Action handler)
171      => new IntArrayParameterChangeHandler(parameter, handler);
172  }
173  public class DoubleArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler<DoubleArray, double> {
174    private DoubleArrayParameterChangeHandler(IValueParameter<DoubleArray> parameter, Action handler) : base(parameter, handler) { }
175    public static new DoubleArrayParameterChangeHandler Create(IValueParameter<DoubleArray> parameter, Action handler)
176      => new DoubleArrayParameterChangeHandler(parameter, handler);
177  }
178  public class BoolMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler<BoolMatrix, bool> {
179    private BoolMatrixParameterChangeHandler(IValueParameter<BoolMatrix> parameter, Action handler) : base(parameter, handler) { }
180    public static new BoolMatrixParameterChangeHandler Create(IValueParameter<BoolMatrix> parameter, Action handler)
181      => new BoolMatrixParameterChangeHandler(parameter, handler);
182  }
183  public class IntMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler<IntMatrix, int> {
184    private IntMatrixParameterChangeHandler(IValueParameter<IntMatrix> parameter, Action handler) : base(parameter, handler) { }
185    public static new IntMatrixParameterChangeHandler Create(IValueParameter<IntMatrix> parameter, Action handler)
186      => new IntMatrixParameterChangeHandler(parameter, handler);
187  }
188  public class DoubleMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler<DoubleMatrix, double> {
189    private DoubleMatrixParameterChangeHandler(IValueParameter<DoubleMatrix> parameter, Action handler) : base(parameter, handler) { }
190    public static new DoubleMatrixParameterChangeHandler Create(IValueParameter<DoubleMatrix> parameter, Action handler)
191      => new DoubleMatrixParameterChangeHandler(parameter, handler);
192  }
193  public class StringValueParameterChangeHandler : ParameterChangeHandler<StringValue> { // StringValue does not derive from ValueTypeValue
194    private StringValue last;
195
196    private StringValueParameterChangeHandler(IValueParameter<StringValue> parameter, Action handler) : base(parameter, handler) {
197      last = parameter.Value;
198      if (last != null && !last.ReadOnly)
199        last.ValueChanged += ParameterValueOnValueChanged;
200    }
201
202    protected override void ParameterOnValueChanged(object sender, EventArgs e) {
203      if (last != null) last.ValueChanged -= ParameterValueOnValueChanged;
204      last = ((IValueParameter<StringValue>)sender).Value;
205      if (last != null && !last.ReadOnly)
206        last.ValueChanged += ParameterValueOnValueChanged;
207      base.ParameterOnValueChanged(sender, e);
208    }
209
210    private void ParameterValueOnValueChanged(object sender, EventArgs e) {
211      handler();
212    }
213    public static StringValueParameterChangeHandler Create(IValueParameter<StringValue> parameter, Action handler)
214     => new StringValueParameterChangeHandler(parameter, handler);
215  }
216}
Note: See TracBrowser for help on using the repository browser.