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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
|
---|
30 | // TODO: control creatable item types ### EDIT: done?
|
---|
31 | [StorableClass]
|
---|
32 | public class CheckedValueConfigurationList : CheckedItemList<IValueConfiguration>, ICheckedValueConfigurationList {
|
---|
33 | [Storable]
|
---|
34 | private int minItemCount = 1;
|
---|
35 | public int MinItemCount {
|
---|
36 | get { return minItemCount; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | protected IItemSet<IItem> validValues;
|
---|
41 | public IItemSet<IItem> ValidValues {
|
---|
42 | get { return validValues; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | #region Constructors and Cloning
|
---|
46 | [StorableConstructor]
|
---|
47 | protected CheckedValueConfigurationList(bool deserializing)
|
---|
48 | : base(deserializing) {
|
---|
49 | RegisterEvents();
|
---|
50 | }
|
---|
51 | protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | this.minItemCount = original.MinItemCount;
|
---|
54 | this.validValues = cloner.Clone(original.validValues);
|
---|
55 | RegisterEvents();
|
---|
56 | }
|
---|
57 | public CheckedValueConfigurationList(IItemSet<IItem> validValues)
|
---|
58 | : base() {
|
---|
59 | this.validValues = validValues;
|
---|
60 | RegisterEvents();
|
---|
61 | }
|
---|
62 | public CheckedValueConfigurationList(IEnumerable<IValueConfiguration> collection)
|
---|
63 | : base(collection) {
|
---|
64 | validValues = null;
|
---|
65 | // event wiring not needed
|
---|
66 | }
|
---|
67 | public CheckedValueConfigurationList()
|
---|
68 | : base() {
|
---|
69 | RegisterEvents();
|
---|
70 | }
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | return new CheckedValueConfigurationList(this, cloner);
|
---|
73 | }
|
---|
74 | [StorableHook(HookType.AfterDeserialization)]
|
---|
75 | private void AfterDeserialization() {
|
---|
76 | RegisterEvents();
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | private void RegisterEvents() {
|
---|
81 | this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
82 | }
|
---|
83 |
|
---|
84 | private void DeregisterEvents() {
|
---|
85 | this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
86 | }
|
---|
87 |
|
---|
88 | #region Events
|
---|
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.Any()) {
|
---|
93 | int maxNumber = matchingItems.Select(x => x.Number).Max();
|
---|
94 | item.Value.Number = maxNumber + 1;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | #endregion
|
---|
99 | }
|
---|
100 | }
|
---|