1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 |
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | |
---|
9 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
10 | [StorableClass]
|
---|
11 | public abstract class ErcOptionConvertible<TValue, TValueItem> : CheckedItemCollection<WeightedErcItem<TValueItem>>, IErcItem<TValue> {
|
---|
12 | [Storable]
|
---|
13 | protected bool isEnabled;
|
---|
14 |
|
---|
15 | public event EventHandler<bool> EnabledChanged;
|
---|
16 |
|
---|
17 | protected ErcOptionConvertible() : this(false, new WeightedErcItem<TValueItem>[0]) { }
|
---|
18 |
|
---|
19 | protected ErcOptionConvertible(bool isEnabled, params WeightedErcItem<TValueItem>[] values) : base(values) {
|
---|
20 | this.isEnabled = isEnabled;
|
---|
21 | SetEnabledChangedHandler(values);
|
---|
22 |
|
---|
23 | foreach (var value in values)
|
---|
24 | SetItemCheckedState(value, value.IsEnabled);
|
---|
25 | }
|
---|
26 |
|
---|
27 | [StorableConstructor]
|
---|
28 | protected ErcOptionConvertible(bool deserializing)
|
---|
29 | : base(deserializing) {
|
---|
30 | }
|
---|
31 |
|
---|
32 | [StorableHook(HookType.AfterDeserialization)]
|
---|
33 | // ReSharper disable once UnusedMember.Local
|
---|
34 | private void AfterDeserialization() {
|
---|
35 | SetEnabledChangedHandler(list);
|
---|
36 | }
|
---|
37 |
|
---|
38 | protected ErcOptionConvertible(ErcOptionConvertible<TValue, TValueItem> origin, Cloner cloner)
|
---|
39 | : base(origin, cloner) {
|
---|
40 | IsEnabled = origin.IsEnabled;
|
---|
41 | }
|
---|
42 |
|
---|
43 | private void SetEnabledChangedHandler(IEnumerable<WeightedErcItem<TValueItem>> values) {
|
---|
44 | foreach (var item in values)
|
---|
45 | item.EnabledChanged += ErcValueItemEnabledChanged;
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected override void OnItemsAdded(IEnumerable<WeightedErcItem<TValueItem>> items) {
|
---|
49 | base.OnItemsAdded(items);
|
---|
50 | SetEnabledChangedHandler(items);
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void OnItemsRemoved(IEnumerable<WeightedErcItem<TValueItem>> items) {
|
---|
54 | base.OnItemsRemoved(items);
|
---|
55 |
|
---|
56 | foreach (var item in items)
|
---|
57 | item.EnabledChanged -= ErcValueItemEnabledChanged;
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void ErcValueItemEnabledChanged(object sender, bool state) {
|
---|
61 | SetItemCheckedState((WeightedErcItem<TValueItem>)sender, state);
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected override void OnCheckedItemsChanged(IEnumerable<WeightedErcItem<TValueItem>> items) {
|
---|
65 | base.OnCheckedItemsChanged(items);
|
---|
66 |
|
---|
67 | foreach (var item in items) {
|
---|
68 | item.IsEnabled = ItemChecked(item);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public bool IsEnabled {
|
---|
73 | get { return isEnabled; }
|
---|
74 | set {
|
---|
75 | if (value == isEnabled)
|
---|
76 | return;
|
---|
77 |
|
---|
78 | isEnabled = value;
|
---|
79 |
|
---|
80 | if (EnabledChanged != null)
|
---|
81 | EnabledChanged(this, value);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public abstract TValue GetErcValue(IRandom random);
|
---|
86 |
|
---|
87 | protected TValue GetErcValue(IRandom random, Converter<TValueItem, TValue> converter) {
|
---|
88 | if (Count == 0 || !IsEnabled) return default(TValue);
|
---|
89 |
|
---|
90 | var ercValueItem = list
|
---|
91 | .Where(item => item.IsEnabled)
|
---|
92 | .RandomWeightedOrDefault(random, x => x.Weight);
|
---|
93 |
|
---|
94 | return ercValueItem == null
|
---|
95 | ? default(TValue)
|
---|
96 | : converter(ercValueItem.GetErcValue(random));
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|