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