Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis.Base/Erc/ErcOptions.cs @ 14905

Last change on this file since 14905 was 14905, checked in by pkimmesw, 8 years ago

#2665 Made ErcOptions checkable

File size: 9.5 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc {
2  using System;
3  using System.Collections.Generic;
4  using System.Linq;
5
6  using Boolean;
7  using Char;
8  using Common;
9  using Core;
10  using Data;
11  using Float;
12  using FloatVector;
13  using Integer;
14  using IntegerVector;
15  using Parameters;
16  using Persistence.Default.CompositeSerializers.Storable;
17  using String;
18  using StringVector;
19
20  [StorableClass]
21  public class ErcOptions : ParameterizedNamedItem, IReadOnlyErcOptions {
22    public const string ProbabilityParameterName = "Probability";
23    public const string IntegerParameterName = "Integer";
24    public const string FloatParameterName = "Float";
25    public const string CharParameterName = "Char";
26    public const string BooleanParameterName = "Boolean";
27    public const string StringParameterName = "String";
28    public const string NameParameterName = "Name";
29    public const string IntegerVectorParameterName = "IntegerVector";
30    public const string FloatVectorParameterName = "FloatVector";
31    public const string StringVectorParameterName = "StringVector";
32
33    [Storable]
34    private readonly ReadOnlyCheckedItemCollection<IValueParameter> ercOptions;
35    [Storable]
36    private readonly NamedItemCollection<IValueParameter> ercOptionParameters;
37    private IDictionary<IValueParameter, EventHandler<bool>> ercOptionEnabledChangedHandler;
38
39    public ErcOptions() {
40      Name = "ERC Options";
41
42      Parameters.Add(new FixedValueParameter<PercentValue>(ProbabilityParameterName));
43
44      ercOptionParameters = new NamedItemCollection<IValueParameter> {
45        new ValueParameter<IntegerErcOptions>(IntegerParameterName),
46        new ValueParameter<FloatErcOptions>(FloatParameterName),
47        new ValueParameter<BooleanErcOptions>(BooleanParameterName),
48        new ValueParameter<StringErcOptions>(StringParameterName),
49        new ValueParameter<CharErcOptions>(CharParameterName),
50        new ValueParameter<StringErcOptions>(NameParameterName),
51        new ValueParameter<IntegerVectorErcOptions>(IntegerVectorParameterName),
52        new ValueParameter<FloatVectorErcOptions>(FloatVectorParameterName),
53        new ValueParameter<StringVectorErcOptions>(StringVectorParameterName)
54      };
55
56      InitEnabledChangedHandlerDict(ercOptionParameters);
57
58      var optionsCollection = new CheckedItemCollection<IValueParameter>(ercOptionParameters);
59      ercOptions = new ReadOnlyCheckedItemCollection<IValueParameter>(optionsCollection);
60      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemCollection<IValueParameter>>("Options", ercOptions));
61
62      ercOptions.CheckedItemsChanged += ErcOptionsCheckedItemsChanged;
63
64      DisableAllOptions();
65    }
66
67    [StorableConstructor]
68    protected ErcOptions(bool deserializing) : base(deserializing) { }
69
70    public ErcOptions(ErcOptions origin, Cloner cloner)
71      : base(origin, cloner) {
72      ercOptions = cloner.Clone(origin.ercOptions);
73      ercOptionParameters = cloner.Clone(origin.ercOptionParameters);
74
75      InitEnabledChangedHandlerDict(ercOptionParameters);
76    }
77
78    [StorableHook(HookType.AfterDeserialization)]
79    // ReSharper disable once UnusedMember.Local
80    private void AfterDeserialization() {
81      InitEnabledChangedHandlerDict(ercOptionParameters);
82    }
83
84    private void InitEnabledChangedHandlerDict(NamedItemCollection<IValueParameter> parameters) {
85      ercOptionEnabledChangedHandler = parameters
86        .ToDictionary<IValueParameter, IValueParameter, EventHandler<bool>>(
87          param => param,
88          param => (sender, state) => ercOptions.SetItemCheckedState(param, state));
89    }
90
91    private void ErcOptionsCheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IValueParameter> e) {
92      foreach (var item in e.Items) {
93        var ercItem = (IErcItem)item.Value;
94        ercItem.IsEnabled = ercOptions.ItemChecked(item);
95      }
96    }
97
98    public IValueParameter<PercentValue> ErcProbabilityParameter
99    {
100      get { return (IValueParameter<PercentValue>)Parameters[ProbabilityParameterName]; }
101    }
102
103    public double ErcProbability
104    {
105      get { return ErcProbabilityParameter.Value.Value; }
106      set { ErcProbabilityParameter.Value.Value = value; }
107    }
108
109    IErcValueItem<int> IReadOnlyErcOptions.IntegerErcOptions { get { return IntegerErcOptions; } }
110    IErcValueItem<double> IReadOnlyErcOptions.FloatErcOptions { get { return FloatErcOptions; } }
111    IErcValueItem<bool> IReadOnlyErcOptions.BooleanErcOptions { get { return BooleanErcOptions; } }
112    IErcValueItem<char> IReadOnlyErcOptions.CharErcOptions { get { return CharErcOptions; } }
113    IErcValueItem<string> IReadOnlyErcOptions.StringErcOptions { get { return StringErcOptions; } }
114    IErcValueItem<string> IReadOnlyErcOptions.NameErcOptions { get { return NameErcOptions; } }
115    IErcValueItem<IReadOnlyList<int>> IReadOnlyErcOptions.IntegerVectorErcOptions { get { return IntegerVectorErcOptions; } }
116    IErcValueItem<IReadOnlyList<double>> IReadOnlyErcOptions.FloatVectorErcOptions { get { return FloatVectorErcOptions; } }
117    IErcValueItem<IReadOnlyList<string>> IReadOnlyErcOptions.StringVectorErcOptions { get { return StringVectorErcOptions; } }
118
119    public IValueParameter<IntegerErcOptions> IntegerErcOptionsParameter
120    {
121      get { return (IValueParameter<IntegerErcOptions>)ercOptionParameters[IntegerParameterName]; }
122    }
123
124    public IntegerErcOptions IntegerErcOptions
125    {
126      get { return IntegerErcOptionsParameter.Value; }
127      set { SetErcOptions(IntegerErcOptionsParameter, value); }
128    }
129
130    public IValueParameter<FloatErcOptions> FloatErcOptionsParameter
131    {
132      get { return (IValueParameter<FloatErcOptions>)ercOptionParameters[FloatParameterName]; }
133    }
134
135    public FloatErcOptions FloatErcOptions
136    {
137      get { return FloatErcOptionsParameter.Value; }
138      set { SetErcOptions(FloatErcOptionsParameter, value); }
139    }
140
141    /// <summary>
142    /// Integer represents ASCII value
143    /// </summary>
144    public IValueParameter<CharErcOptions> CharErcOptionsParameter
145    {
146      get { return (IValueParameter<CharErcOptions>)ercOptionParameters[CharParameterName]; }
147    }
148
149    public CharErcOptions CharErcOptions
150    {
151      get { return CharErcOptionsParameter.Value; }
152      set { SetErcOptions(CharErcOptionsParameter, value); }
153    }
154
155    public IValueParameter<BooleanErcOptions> BooleanErcOptionsParameter
156    {
157      get { return (IValueParameter<BooleanErcOptions>)ercOptionParameters[BooleanParameterName]; }
158    }
159
160    public BooleanErcOptions BooleanErcOptions
161    {
162      get { return BooleanErcOptionsParameter.Value; }
163      set { SetErcOptions(BooleanErcOptionsParameter, value); }
164    }
165
166    public IValueParameter<StringErcOptions> StringErcOptionsParameter
167    {
168      get { return (IValueParameter<StringErcOptions>)ercOptionParameters[StringParameterName]; }
169    }
170
171    public StringErcOptions StringErcOptions
172    {
173      get { return StringErcOptionsParameter.Value; }
174      set { SetErcOptions(StringErcOptionsParameter, value); }
175    }
176
177    public IValueParameter<StringErcOptions> NameParameter
178    {
179      get { return (IValueParameter<StringErcOptions>)ercOptionParameters[NameParameterName]; }
180    }
181
182    public StringErcOptions NameErcOptions
183    {
184      get { return NameParameter.Value; }
185      set { SetErcOptions(NameParameter, value); }
186    }
187
188    public IValueParameter<IntegerVectorErcOptions> IntegerVectorErcOptionsParameter
189    {
190      get { return (IValueParameter<IntegerVectorErcOptions>)ercOptionParameters[IntegerVectorParameterName]; }
191    }
192
193    public IntegerVectorErcOptions IntegerVectorErcOptions
194    {
195      get { return this.IntegerVectorErcOptionsParameter.Value; }
196      set { SetErcOptions(this.IntegerVectorErcOptionsParameter, value); }
197    }
198
199    public IValueParameter<StringVectorErcOptions> StringVectorErcOptionsValueParameter
200    {
201      get { return (IValueParameter<StringVectorErcOptions>)ercOptionParameters[StringVectorParameterName]; }
202    }
203
204    public StringVectorErcOptions StringVectorErcOptions
205    {
206      get { return this.StringVectorErcOptionsValueParameter.Value; }
207      set { SetErcOptions(this.StringVectorErcOptionsValueParameter, value); }
208    }
209
210    public IValueParameter<FloatVectorErcOptions> FloatVectorErcOptionsParameter
211    {
212      get { return (IValueParameter<FloatVectorErcOptions>)ercOptionParameters[FloatVectorParameterName]; }
213    }
214
215    public FloatVectorErcOptions FloatVectorErcOptions
216    {
217      get { return this.FloatVectorErcOptionsParameter.Value; }
218      set { SetErcOptions(this.FloatVectorErcOptionsParameter, value); }
219    }
220
221    public override IDeepCloneable Clone(Cloner cloner) {
222      return new ErcOptions(this, cloner);
223    }
224
225    public void DisableAllOptions() {
226      SetOptionsState(false);
227    }
228
229    public void EnableAllOptions() {
230      SetOptionsState(true);
231    }
232
233    public void SetOptionsState(bool state) {
234      foreach (var param in this.ercOptions) {
235        this.ercOptions.SetItemCheckedState(param, state);
236      }
237    }
238
239    private void SetErcOptions(IValueParameter param, IErcItem value) {
240      if (ercOptionEnabledChangedHandler[param] != null && param.Value != null)
241        ((IErcItem)param.Value).EnabledChanged -= ercOptionEnabledChangedHandler[param];
242
243      param.Value = value;
244      ercOptions.SetItemCheckedState(param, value != null ? value.IsEnabled : false);
245
246      if (value != null)
247        value.EnabledChanged += ercOptionEnabledChangedHandler[param];
248    }
249  }
250}
Note: See TracBrowser for help on using the repository browser.