[2852] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Parameters {
|
---|
| 28 | /// <summary>
|
---|
| 29 | /// A parameter whose value has to be chosen from a set of valid values which is defined it the parameter itself.
|
---|
| 30 | /// </summary>
|
---|
| 31 | [Item("ConstrainedValueParameter<T>", "A parameter whose value has to be chosen from a set of valid values which is defined it the parameter itself.")]
|
---|
| 32 | public sealed class ConstrainedValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
|
---|
| 33 | private ItemSet<T> validValues;
|
---|
| 34 | [Storable]
|
---|
| 35 | public ItemSet<T> ValidValues {
|
---|
| 36 | get { return validValues; }
|
---|
| 37 | private set {
|
---|
| 38 | DeregisterValidValuesEvents();
|
---|
| 39 | validValues = value;
|
---|
| 40 | RegisterValidValuesEvents();
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | private T value;
|
---|
| 45 | [Storable]
|
---|
| 46 | public T Value {
|
---|
| 47 | get { return this.value; }
|
---|
| 48 | set {
|
---|
| 49 | if (value != this.value) {
|
---|
| 50 | if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
|
---|
| 51 | this.value = value;
|
---|
| 52 | OnValueChanged();
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | IItem IValueParameter.Value {
|
---|
| 57 | get { return Value; }
|
---|
| 58 | set {
|
---|
| 59 | T val = value as T;
|
---|
| 60 | if ((value != null) && (val == null))
|
---|
| 61 | throw new InvalidOperationException(
|
---|
| 62 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
| 63 | typeof(T).GetPrettyName())
|
---|
| 64 | );
|
---|
| 65 | Value = val;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public ConstrainedValueParameter()
|
---|
| 70 | : base("Anonymous", typeof(T)) {
|
---|
| 71 | ValidValues = new ItemSet<T>();
|
---|
| 72 | }
|
---|
| 73 | public ConstrainedValueParameter(string name)
|
---|
| 74 | : base(name, typeof(T)) {
|
---|
| 75 | ValidValues = new ItemSet<T>();
|
---|
| 76 | }
|
---|
| 77 | public ConstrainedValueParameter(string name, ItemSet<T> validValues)
|
---|
| 78 | : base(name, typeof(T)) {
|
---|
| 79 | ValidValues = validValues;
|
---|
| 80 | }
|
---|
| 81 | public ConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
|
---|
| 82 | : base(name, typeof(T)) {
|
---|
| 83 | ValidValues = validValues;
|
---|
| 84 | Value = value;
|
---|
| 85 | }
|
---|
| 86 | public ConstrainedValueParameter(string name, string description)
|
---|
| 87 | : base(name, description, typeof(T)) {
|
---|
| 88 | ValidValues = new ItemSet<T>();
|
---|
| 89 | }
|
---|
| 90 | public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
|
---|
| 91 | : base(name, description, typeof(T)) {
|
---|
| 92 | ValidValues = validValues;
|
---|
| 93 | }
|
---|
| 94 | public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
|
---|
| 95 | : base(name, description, typeof(T)) {
|
---|
| 96 | ValidValues = validValues;
|
---|
| 97 | Value = value;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 101 | ConstrainedValueParameter<T> clone = (ConstrainedValueParameter<T>)base.Clone(cloner);
|
---|
| 102 | clone.ValidValues = (ItemSet<T>)cloner.Clone(validValues);
|
---|
| 103 | clone.Value = (T)cloner.Clone(value);
|
---|
| 104 | return clone;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public override string ToString() {
|
---|
| 108 | return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : "null", DataType.GetPrettyName());
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | protected override IItem GetActualValue() {
|
---|
| 112 | return Value;
|
---|
| 113 | }
|
---|
| 114 | protected override void SetActualValue(IItem value) {
|
---|
| 115 | ((IValueParameter)this).Value = value;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public event EventHandler ValueChanged;
|
---|
| 119 | private void OnValueChanged() {
|
---|
| 120 | if (ValueChanged != null)
|
---|
| 121 | ValueChanged(this, EventArgs.Empty);
|
---|
| 122 | OnChanged();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private void RegisterValidValuesEvents() {
|
---|
| 126 | if (validValues != null) {
|
---|
| 127 | validValues.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
|
---|
| 128 | validValues.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
|
---|
| 129 | validValues.Changed += new ChangedEventHandler(ValidValues_Changed);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | private void DeregisterValidValuesEvents() {
|
---|
| 133 | if (validValues != null) {
|
---|
| 134 | validValues.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
|
---|
| 135 | validValues.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
|
---|
| 136 | validValues.Changed -= new ChangedEventHandler(ValidValues_Changed);
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | private void ValidValues_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) {
|
---|
| 141 | if ((Value != null) && !validValues.Contains(Value)) Value = null;
|
---|
| 142 | }
|
---|
| 143 | private void ValidValues_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) {
|
---|
| 144 | if ((Value != null) && !validValues.Contains(Value)) Value = null;
|
---|
| 145 | }
|
---|
| 146 | private void ValidValues_Changed(object sender, ChangedEventArgs e) {
|
---|
| 147 | OnChanged(e);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|