Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/ValueConfigurations/CheckedValueConfigurationCollection.cs @ 10216

Last change on this file since 10216 was 7840, checked in by spimming, 13 years ago

#1853:

  • included files from metaopt branch
  • ignored mutation and crossover
  • updated plugin frame
File size: 4.3 KB
RevLine 
[7840]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.ParameterConfigurationTreeEncoding {
30  // todo: check that at least 1 elements needs to be selected
31  // todo: control creatable item types
32  [StorableClass]
33  public class CheckedValueConfigurationList : CheckedItemList<IValueConfiguration>, ICheckedValueConfigurationList {
34    [Storable]
35    private int minItemCount = 1;
36    public int MinItemCount {
37      get { return minItemCount; }
38    }
39
40    [Storable]
41    protected IItemSet<IItem> validValues;
42    public IItemSet<IItem> ValidValues {
43      get { return validValues; }
44    }
45
46    public CheckedValueConfigurationList(IItemSet<IItem> validValues) {
47      this.validValues = validValues;
48      RegisterEvents();
49    }
50    public CheckedValueConfigurationList(IEnumerable<IValueConfiguration> collection)
51      : base(collection) {
52      validValues = null;
53      // event wiring not needed
54    }
55    public CheckedValueConfigurationList() {
56      RegisterEvents();
57    }
58    [StorableConstructor]
59    protected CheckedValueConfigurationList(bool deserializing)
60      : base(deserializing) {
61      RegisterEvents();
62    }
63    protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner)
64      : base(original, cloner) {
65      this.minItemCount = original.MinItemCount;
66      this.validValues = original.validValues;
67      RegisterEvents();
68    }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new CheckedValueConfigurationList(this, cloner);
72    }
73    [StorableHook(HookType.AfterDeserialization)]
74    private void AfterDeserialization() {
75      RegisterEvents();
76    }
77
78    private void RegisterEvents() {
79      this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
80      this.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
81    }
82
83    private void DeregisterEvents() {
84      this.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
85      this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
86    }
87
88    private void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
89      foreach (var item in e.Items) {
90        var matchingItems = this.Where(x => x != item.Value && x.ActualValue.ValueDataType == item.Value.ActualValue.ValueDataType);
91        if (matchingItems.Count() > 0) {
92          int maxNumber = matchingItems.Select(x => x.Number).Max();
93          item.Value.Number = maxNumber + 1;
94        }
95      }
96    }
97
98    private void CheckedValueConfigurationList_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
99      // auch collectionreset gehört berücksichtigt
100      // funktioniert so nicht ganz, weil die view das hinzufügen nicht mitkriegt
101      //if (this.Count < minItemCount) {
102      //  foreach (var item in e.Items) {
103      //    if (this.Count >= minItemCount)
104      //      break;
105      //    this.Add(item);
106      //  }
107      //}
108    }
109
110  }
111}
Note: See TracBrowser for help on using the repository browser.