Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4332 was 4332, checked in by swagner, 14 years ago

Enabled users to choose whether a parameter should be collected in each run or not (#1113)

File size: 9.4 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 System.Drawing;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Parameters {
30  /// <summary>
31  /// A parameter whose value has to be chosen from a set of valid values or is null.
32  /// </summary>
33  [Item("OptionalConstrainedValueParameter", "A parameter whose value has to be chosen from a set of valid values or is null.")]
34  [StorableClass]
35  public class OptionalConstrainedValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
36    public override Image ItemImage {
37      get {
38        if (value != null) return value.ItemImage;
39        else return base.ItemImage;
40      }
41    }
42
43    [Storable]
44    private ItemSet<T> validValues;
45    public ItemSet<T> ValidValues {
46      get { return validValues; }
47    }
48
49    [Storable]
50    private T value;
51    public virtual T Value {
52      get { return this.value; }
53      set {
54        if (value != this.value) {
55          if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
56          DeregisterValueEvents();
57          this.value = value;
58          RegisterValueEvents();
59          OnValueChanged();
60        }
61      }
62    }
63    IItem IValueParameter.Value {
64      get { return Value; }
65      set {
66        T val = value as T;
67        if ((value != null) && (val == null))
68          throw new InvalidOperationException(
69            string.Format("Type mismatch. Value is not a \"{0}\".",
70                          typeof(T).GetPrettyName())
71          );
72        Value = val;
73      }
74    }
75
76    [Storable(DefaultValue = true)]
77    private bool getsCollected;
78    public bool GetsCollected {
79      get { return getsCollected; }
80      set {
81        if (value != getsCollected) {
82          getsCollected = value;
83          OnGetsCollectedChanged();
84        }
85      }
86    }
87
88    #region Constructors
89    public OptionalConstrainedValueParameter()
90      : base("Anonymous", typeof(T)) {
91      this.validValues = new ItemSet<T>();
92      this.getsCollected = true;
93      Initialize();
94    }
95    public OptionalConstrainedValueParameter(string name)
96      : base(name, typeof(T)) {
97      this.validValues = new ItemSet<T>();
98      this.getsCollected = true;
99      Initialize();
100    }
101    public OptionalConstrainedValueParameter(string name, bool getsCollected)
102      : base(name, typeof(T)) {
103      this.validValues = new ItemSet<T>();
104      this.getsCollected = getsCollected;
105      Initialize();
106    }
107    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
108      : base(name, typeof(T)) {
109      this.validValues = validValues;
110      this.getsCollected = true;
111      Initialize();
112    }
113    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, bool getsCollected)
114      : base(name, typeof(T)) {
115      this.validValues = validValues;
116      this.getsCollected = getsCollected;
117      Initialize();
118    }
119    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
120      : base(name, typeof(T)) {
121      this.validValues = validValues;
122      this.value = value;
123      this.getsCollected = true;
124      Initialize();
125    }
126    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value, bool getsCollected)
127      : base(name, typeof(T)) {
128      this.validValues = validValues;
129      this.value = value;
130      this.getsCollected = getsCollected;
131      Initialize();
132    }
133    public OptionalConstrainedValueParameter(string name, string description)
134      : base(name, description, typeof(T)) {
135      this.validValues = new ItemSet<T>();
136      this.getsCollected = true;
137      Initialize();
138    }
139    public OptionalConstrainedValueParameter(string name, string description, bool getsCollected)
140      : base(name, description, typeof(T)) {
141      this.validValues = new ItemSet<T>();
142      this.getsCollected = getsCollected;
143      Initialize();
144    }
145    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
146      : base(name, description, typeof(T)) {
147      this.validValues = validValues;
148      this.getsCollected = true;
149      Initialize();
150    }
151    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, bool getsCollected)
152      : base(name, description, typeof(T)) {
153      this.validValues = validValues;
154      this.getsCollected = getsCollected;
155      Initialize();
156    }
157    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
158      : base(name, description, typeof(T)) {
159      this.validValues = validValues;
160      this.value = value;
161      this.getsCollected = true;
162      Initialize();
163    }
164    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value, bool getsCollected)
165      : base(name, description, typeof(T)) {
166      this.validValues = validValues;
167      this.value = value;
168      this.getsCollected = getsCollected;
169      Initialize();
170    }
171    [StorableConstructor]
172    protected OptionalConstrainedValueParameter(bool deserializing) : base(deserializing) { }
173    #endregion
174
175    [StorableHook(HookType.AfterDeserialization)]
176    private void Initialize() {
177      RegisterValidValuesEvents();
178      RegisterValueEvents();
179    }
180
181    public override IDeepCloneable Clone(Cloner cloner) {
182      OptionalConstrainedValueParameter<T> clone = (OptionalConstrainedValueParameter<T>)base.Clone(cloner);
183      clone.validValues = (ItemSet<T>)cloner.Clone(validValues);
184      clone.value = (T)cloner.Clone(value);
185      clone.getsCollected = getsCollected;
186      clone.Initialize();
187      return clone;
188    }
189
190    public override string ToString() {
191      return Name + ": " + (Value != null ? Value.ToString() : "null");
192    }
193
194    protected override IItem GetActualValue() {
195      return Value;
196    }
197    protected override void SetActualValue(IItem value) {
198      ((IValueParameter)this).Value = value;
199    }
200
201    public event EventHandler ValueChanged;
202    protected virtual void OnValueChanged() {
203      EventHandler handler = ValueChanged;
204      if (handler != null) handler(this, EventArgs.Empty);
205      OnItemImageChanged();
206      OnToStringChanged();
207    }
208    public event EventHandler GetsCollectedChanged;
209    protected virtual void OnGetsCollectedChanged() {
210      EventHandler handler = GetsCollectedChanged;
211      if (handler != null) handler(this, EventArgs.Empty);
212    }
213
214    private void RegisterValidValuesEvents() {
215      if (validValues != null) {
216        validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
217        validValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
218        validValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
219      }
220    }
221    private void DeregisterValidValuesEvents() {
222      if (validValues != null) {
223        validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
224        validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
225        validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
226      }
227    }
228    protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
229    protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
230      if ((Value != null) && !validValues.Contains(Value)) Value = null;
231    }
232    protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
233      if ((Value != null) && !validValues.Contains(Value)) Value = null;
234    }
235
236    private void RegisterValueEvents() {
237      if (value != null) {
238        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
239        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
240      }
241    }
242    private void DeregisterValueEvents() {
243      if (value != null) {
244        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
245        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
246      }
247    }
248    private void Value_ItemImageChanged(object sender, EventArgs e) {
249      OnItemImageChanged();
250    }
251    private void Value_ToStringChanged(object sender, EventArgs e) {
252      OnToStringChanged();
253    }
254  }
255}
Note: See TracBrowser for help on using the repository browser.