Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Introduce class to make parameter change handling easier, fix bug in SingleObjectiveProblem regarding MaximizationChanged

File size: 6.6 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 abstract 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
41  public class ParameterChangeHandler<TItem, TValue> : ParameterChangeHandler<TItem>
42    where TValue : struct
43    where TItem : ValueTypeValue<TValue> {
44    private ValueTypeValue<TValue> last;
45
46    protected ParameterChangeHandler(IValueParameter<TItem> parameter, Action handler)
47      : base(parameter, handler) {
48      last = parameter.Value;
49      if (last != null && !last.ReadOnly)
50        last.ValueChanged += ParameterValueOnValueChanged;
51    }
52
53    protected override void ParameterOnValueChanged(object sender, EventArgs e) {
54      if (last != null) last.ValueChanged -= ParameterValueOnValueChanged;
55      last = ((IValueParameter<TItem>)sender).Value;
56      if (last != null && !last.ReadOnly)
57        last.ValueChanged += ParameterValueOnValueChanged;
58      base.ParameterOnValueChanged(sender, e);
59    }
60
61    protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
62      handler();
63    }
64
65    public static ParameterChangeHandler<TItem, TValue> Create(IValueParameter<TItem> parameter, Action handler)
66      => new ParameterChangeHandler<TItem, TValue>(parameter, handler);
67  }
68
69  public class BoolValueParameterChangeHandler : ParameterChangeHandler<BoolValue, bool> {
70    private BoolValueParameterChangeHandler(IValueParameter<BoolValue> parameter, Action handler) : base(parameter, handler) { }
71    public static new BoolValueParameterChangeHandler Create(IValueParameter<BoolValue> parameter, Action handler)
72      => new BoolValueParameterChangeHandler(parameter, handler);
73  }
74  public class IntValueParameterChangeHandler : ParameterChangeHandler<IntValue, int> {
75    private IntValueParameterChangeHandler(IValueParameter<IntValue> parameter, Action handler) : base(parameter, handler) { }
76    public static new IntValueParameterChangeHandler Create(IValueParameter<IntValue> parameter, Action handler)
77      => new IntValueParameterChangeHandler(parameter, handler);
78  }
79  public class DoubleValueParameterChangeHandler : ParameterChangeHandler<DoubleValue, double> {
80    private DoubleValueParameterChangeHandler(IValueParameter<DoubleValue> parameter, Action handler) : base(parameter, handler) { }
81    public static new DoubleValueParameterChangeHandler Create(IValueParameter<DoubleValue> parameter, Action handler)
82      => new DoubleValueParameterChangeHandler(parameter, handler);
83  }
84  public class PercentValueParameterChangeHandler : ParameterChangeHandler<PercentValue, double> {
85    private PercentValueParameterChangeHandler(IValueParameter<PercentValue> parameter, Action handler) : base(parameter, handler) { }
86    public static new PercentValueParameterChangeHandler Create(IValueParameter<PercentValue> parameter, Action handler)
87      => new PercentValueParameterChangeHandler(parameter, handler);
88  }
89  public class DateTimeValueParameterChangeHandler : ParameterChangeHandler<DateTimeValue, DateTime> {
90    private DateTimeValueParameterChangeHandler(IValueParameter<DateTimeValue> parameter, Action handler) : base(parameter, handler) { }
91    public static new DateTimeValueParameterChangeHandler Create(IValueParameter<DateTimeValue> parameter, Action handler)
92      => new DateTimeValueParameterChangeHandler(parameter, handler);
93  }
94  public class TimeSpanValueParameterChangeHandler : ParameterChangeHandler<TimeSpanValue, TimeSpan> {
95    private TimeSpanValueParameterChangeHandler(IValueParameter<TimeSpanValue> parameter, Action handler) : base(parameter, handler) { }
96    public static new TimeSpanValueParameterChangeHandler Create(IValueParameter<TimeSpanValue> parameter, Action handler)
97      => new TimeSpanValueParameterChangeHandler(parameter, handler);
98  }
99  public class EnumValueParameterChangeHandler<TEnum> : ParameterChangeHandler<EnumValue<TEnum>, TEnum> where TEnum : struct, IComparable {
100    private EnumValueParameterChangeHandler(IValueParameter<EnumValue<TEnum>> parameter, Action handler) : base(parameter, handler) { }
101    public static new EnumValueParameterChangeHandler<TEnum> Create(IValueParameter<EnumValue<TEnum>> parameter, Action handler)
102      => new EnumValueParameterChangeHandler<TEnum>(parameter, handler);
103  }
104  public class StringValueParameterChangeHandler : ParameterChangeHandler<StringValue> { // StringValue does not derive from ValueTypeValue
105    private StringValue last;
106
107    private StringValueParameterChangeHandler(IValueParameter<StringValue> parameter, Action handler) : base(parameter, handler) {
108      last = parameter.Value;
109      if (last != null && !last.ReadOnly)
110        last.ValueChanged += ParameterValueOnValueChanged;
111    }
112
113    protected override void ParameterOnValueChanged(object sender, EventArgs e) {
114      if (last != null) last.ValueChanged -= ParameterValueOnValueChanged;
115      last = ((IValueParameter<StringValue>)sender).Value;
116      if (last != null && !last.ReadOnly)
117        last.ValueChanged += ParameterValueOnValueChanged;
118      base.ParameterOnValueChanged(sender, e);
119    }
120
121    private void ParameterValueOnValueChanged(object sender, EventArgs e) {
122      handler();
123    }
124    public static StringValueParameterChangeHandler Create(IValueParameter<StringValue> parameter, Action handler)
125     => new StringValueParameterChangeHandler(parameter, handler);
126  }
127}
Note: See TracBrowser for help on using the repository browser.