Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2520 Update plugin dependencies and references for HL.MetaOptimization for new persistence

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