Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2948 was 2932, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • replaced Changed in IItem by ToStringChanged
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          if (this.value != null) this.value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
53          this.value = value;
54          if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
55          OnValueChanged();
56        }
57      }
58    }
59
60    IItem IValueParameter.Value {
61      get { return Value; }
62      set {
63        T val = value as T;
64        if ((value != null) && (val == null))
65          throw new InvalidOperationException(
66            string.Format("Type mismatch. Value is not a \"{0}\".",
67                          typeof(T).GetPrettyName())
68          );
69        Value = val;
70      }
71    }
72
73    public OptionalConstrainedValueParameter()
74      : base("Anonymous", typeof(T)) {
75      ValidValues = new ItemSet<T>();
76    }
77    public OptionalConstrainedValueParameter(string name)
78      : base(name, typeof(T)) {
79      ValidValues = new ItemSet<T>();
80    }
81    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
82      : base(name, typeof(T)) {
83      ValidValues = validValues;
84    }
85    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
86      : base(name, typeof(T)) {
87      ValidValues = validValues;
88      Value = value;
89    }
90    public OptionalConstrainedValueParameter(string name, string description)
91      : base(name, description, typeof(T)) {
92      ValidValues = new ItemSet<T>();
93    }
94    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
95      : base(name, description, typeof(T)) {
96      ValidValues = validValues;
97    }
98    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
99      : base(name, description, typeof(T)) {
100      ValidValues = validValues;
101      Value = value;
102    }
103
104    public override IDeepCloneable Clone(Cloner cloner) {
105      OptionalConstrainedValueParameter<T> clone = (OptionalConstrainedValueParameter<T>)base.Clone(cloner);
106      clone.ValidValues = (ItemSet<T>)cloner.Clone(validValues);
107      clone.Value = (T)cloner.Clone(value);
108      return clone;
109    }
110
111    public override string ToString() {
112      return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : "null", DataType.GetPrettyName());
113    }
114
115    protected override IItem GetActualValue() {
116      return Value;
117    }
118    protected override void SetActualValue(IItem value) {
119      ((IValueParameter)this).Value = value;
120    }
121
122    public event EventHandler ValueChanged;
123    protected virtual void OnValueChanged() {
124      if (ValueChanged != null)
125        ValueChanged(this, EventArgs.Empty);
126      OnToStringChanged();
127    }
128
129    private void RegisterValidValuesEvents() {
130      if (validValues != null) {
131        validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(validValues_ItemsAdded);
132        validValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
133        validValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
134      }
135    }
136
137    private void DeregisterValidValuesEvents() {
138      if (validValues != null) {
139        validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(validValues_ItemsAdded);
140        validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
141        validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
142      }
143    }
144
145    protected virtual void validValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
146    protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
147      if ((Value != null) && !validValues.Contains(Value)) Value = null;
148    }
149    protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
150      if ((Value != null) && !validValues.Contains(Value)) Value = null;
151    }
152    protected virtual void Value_ToStringChanged(object sender, EventArgs e) {
153      OnToStringChanged();
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.