Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/ValueConfigurations/CheckedValueConfigurationCollection.cs @ 8535

Last change on this file since 8535 was 8535, checked in by jkarder, 12 years ago

#1853:

  • enhanced combinations count calculation
  • restructured code
  • minor code improvements
  • added license information
File size: 3.6 KB
Line 
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.ParameterConfigurationEncoding {
30  // TODO: check that at least 1 element 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    #region Constructors and Cloning
47    [StorableConstructor]
48    protected CheckedValueConfigurationList(bool deserializing)
49      : base(deserializing) {
50      RegisterEvents();
51    }
52    protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner)
53      : base(original, cloner) {
54      this.minItemCount = original.MinItemCount;
55      this.validValues = cloner.Clone(original.validValues);
56      RegisterEvents();
57    }
58    public CheckedValueConfigurationList(IItemSet<IItem> validValues)
59      : base() {
60      this.validValues = validValues;
61      RegisterEvents();
62    }
63    public CheckedValueConfigurationList(IEnumerable<IValueConfiguration> collection)
64      : base(collection) {
65      validValues = null;
66      // event wiring not needed
67    }
68    public CheckedValueConfigurationList()
69      : base() {
70      RegisterEvents();
71    }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new CheckedValueConfigurationList(this, cloner);
74    }
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      RegisterEvents();
78    }
79    #endregion
80
81    private void RegisterEvents() {
82      this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
83    }
84
85    private void DeregisterEvents() {
86      this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
87    }
88
89    private void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
90      foreach (var item in e.Items) {
91        var matchingItems = this.Where(x => x != item.Value && x.ActualValue.ValueDataType == item.Value.ActualValue.ValueDataType);
92        if (matchingItems.Count() > 0) {
93          int maxNumber = matchingItems.Select(x => x.Number).Max();
94          item.Value.Number = maxNumber + 1;
95        }
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.