source: trunk/sources/HeuristicLab.Parameters/3.3/OptionalConstrainedValueParameter.cs @ 2924

Last change on this file since 2924 was 2924, checked in by swagner, 12 years ago

Operator architecture refactoring (#95)

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