[5927] | 1 | using System.Collections.Generic;
|
---|
[5112] | 2 | using System.Linq;
|
---|
[5927] | 3 | using HeuristicLab.Collections;
|
---|
| 4 | using HeuristicLab.Common;
|
---|
[5112] | 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[16574] | 7 | using HEAL.Attic;
|
---|
[5112] | 8 |
|
---|
| 9 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 10 | // todo: check that at least 1 elements needs to be selected
|
---|
| 11 | // todo: control creatable item types
|
---|
[16574] | 12 | [StorableType("D6F285A9-E8F7-4136-A685-B2EBEA53FEA0")]
|
---|
[5277] | 13 | public class CheckedValueConfigurationList : CheckedItemList<IValueConfiguration>, ICheckedValueConfigurationList {
|
---|
[5112] | 14 | [Storable]
|
---|
| 15 | private int minItemCount = 1;
|
---|
[5361] | 16 | public int MinItemCount {
|
---|
| 17 | get { return minItemCount; }
|
---|
[5112] | 18 | }
|
---|
| 19 |
|
---|
| 20 | [Storable]
|
---|
| 21 | protected IItemSet<IItem> validValues;
|
---|
| 22 | public IItemSet<IItem> ValidValues {
|
---|
| 23 | get { return validValues; }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
[5313] | 26 | public CheckedValueConfigurationList(IItemSet<IItem> validValues) {
|
---|
[5231] | 27 | this.validValues = validValues;
|
---|
[5112] | 28 | RegisterEvents();
|
---|
| 29 | }
|
---|
[5927] | 30 | public CheckedValueConfigurationList(IEnumerable<IValueConfiguration> collection)
|
---|
| 31 | : base(collection) {
|
---|
| 32 | validValues = null;
|
---|
| 33 | // event wiring not needed
|
---|
| 34 | }
|
---|
[5277] | 35 | public CheckedValueConfigurationList() {
|
---|
[5112] | 36 | RegisterEvents();
|
---|
| 37 | }
|
---|
| 38 | [StorableConstructor]
|
---|
[16574] | 39 | protected CheckedValueConfigurationList(StorableConstructorFlag _) : base(_) {
|
---|
[5112] | 40 | RegisterEvents();
|
---|
| 41 | }
|
---|
[5361] | 42 | protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner)
|
---|
| 43 | : base(original, cloner) {
|
---|
[5112] | 44 | this.minItemCount = original.MinItemCount;
|
---|
[8133] | 45 | this.validValues = cloner.Clone(original.validValues);
|
---|
[5112] | 46 | RegisterEvents();
|
---|
| 47 | }
|
---|
[5927] | 48 |
|
---|
[5112] | 49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5277] | 50 | return new CheckedValueConfigurationList(this, cloner);
|
---|
[5112] | 51 | }
|
---|
| 52 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 53 | private void AfterDeserialization() {
|
---|
| 54 | RegisterEvents();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | private void RegisterEvents() {
|
---|
[5359] | 58 | this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
[5277] | 59 | this.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
|
---|
[5112] | 60 | }
|
---|
[5277] | 61 |
|
---|
[5112] | 62 | private void DeregisterEvents() {
|
---|
[5277] | 63 | this.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
|
---|
[5359] | 64 | this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
[5112] | 65 | }
|
---|
| 66 |
|
---|
[5927] | 67 | private void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
[5359] | 68 | foreach (var item in e.Items) {
|
---|
[5361] | 69 | var matchingItems = this.Where(x => x != item.Value && x.ActualValue.ValueDataType == item.Value.ActualValue.ValueDataType);
|
---|
| 70 | if (matchingItems.Count() > 0) {
|
---|
| 71 | int maxNumber = matchingItems.Select(x => x.Number).Max();
|
---|
[5359] | 72 | item.Value.Number = maxNumber + 1;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[5277] | 77 | private void CheckedValueConfigurationList_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
[5112] | 78 | // auch collectionreset gehört berücksichtigt
|
---|
| 79 | // funktioniert so nicht ganz, weil die view das hinzufügen nicht mitkriegt
|
---|
| 80 | //if (this.Count < minItemCount) {
|
---|
| 81 | // foreach (var item in e.Items) {
|
---|
| 82 | // if (this.Count >= minItemCount)
|
---|
| 83 | // break;
|
---|
| 84 | // this.Add(item);
|
---|
| 85 | // }
|
---|
| 86 | //}
|
---|
| 87 | }
|
---|
[5277] | 88 |
|
---|
[5112] | 89 | }
|
---|
| 90 | }
|
---|