Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/CheckedValueConfigurationCollection.cs @ 16574

Last change on this file since 16574 was 16574, checked in by gkronber, 6 years ago

#2520: changed HeuristicLab.MetaOptimization addon to compile with new HL.Persistence

File size: 3.5 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Collections;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HEAL.Attic;
8
9namespace HeuristicLab.Problems.MetaOptimization {
10  // todo: check that at least 1 elements needs to be selected
11  // todo: control creatable item types
12  [StorableType("D6F285A9-E8F7-4136-A685-B2EBEA53FEA0")]
13  public class CheckedValueConfigurationList : CheckedItemList<IValueConfiguration>, ICheckedValueConfigurationList {
14    [Storable]
15    private int minItemCount = 1;
16    public int MinItemCount {
17      get { return minItemCount; }
18    }
19
20    [Storable]
21    protected IItemSet<IItem> validValues;
22    public IItemSet<IItem> ValidValues {
23      get { return validValues; }
24    }
25
26    public CheckedValueConfigurationList(IItemSet<IItem> validValues) {
27      this.validValues = validValues;
28      RegisterEvents();
29    }
30    public CheckedValueConfigurationList(IEnumerable<IValueConfiguration> collection)
31      : base(collection) {
32      validValues = null;
33      // event wiring not needed
34    }
35    public CheckedValueConfigurationList() {
36      RegisterEvents();
37    }
38    [StorableConstructor]
39    protected CheckedValueConfigurationList(StorableConstructorFlag _) : base(_) {
40      RegisterEvents();
41    }
42    protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner)
43      : base(original, cloner) {
44      this.minItemCount = original.MinItemCount;
45      this.validValues = cloner.Clone(original.validValues);
46      RegisterEvents();
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new CheckedValueConfigurationList(this, cloner);
51    }
52    [StorableHook(HookType.AfterDeserialization)]
53    private void AfterDeserialization() {
54      RegisterEvents();
55    }
56
57    private void RegisterEvents() {
58      this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
59      this.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
60    }
61
62    private void DeregisterEvents() {
63      this.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
64      this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
65    }
66
67    private void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
68      foreach (var item in e.Items) {
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();
72          item.Value.Number = maxNumber + 1;
73        }
74      }
75    }
76
77    private void CheckedValueConfigurationList_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
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    }
88
89  }
90}
Note: See TracBrowser for help on using the repository browser.