Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/OptionalConstrainedValueParameter.cs @ 17317

Last change on this file since 17317 was 17317, checked in by abeham, 5 years ago

#2521: refactored multi-objective problems' maximization

  • Add ForceValue method to IValueParameter to perform changes even when it is read-only
  • Add MaximizationChanged event handler
File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
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  [StorableType("9B2BFAE8-CD6E-499C-83A0-401B6CEE3A08")]
35  public class OptionalConstrainedValueParameter<T> : Parameter, IConstrainedValueParameter<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 IItemSet<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 (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
55        DoSetValue(value);
56      }
57    }
58    public virtual void ForceValue(T value) {
59      DoSetValue(value);
60    }
61    private void DoSetValue(T value) {
62      if (value != this.value) {
63        if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
64        DeregisterValueEvents();
65        this.value = value;
66        RegisterValueEvents();
67        OnValueChanged();
68      }
69    }
70    IItem IValueParameter.Value {
71      get { return Value; }
72      set {
73        T val = value as T;
74        if ((value != null) && (val == null))
75          throw new InvalidOperationException(
76            string.Format("Type mismatch. Value is not a \"{0}\".",
77                          typeof(T).GetPrettyName())
78          );
79        Value = val;
80      }
81    }
82
83    [Storable(DefaultValue = false)]
84    private bool readOnly;
85    public bool ReadOnly {
86      get { return readOnly; }
87      set {
88        if (value == readOnly) return;
89        readOnly = value;
90        OnReadOnlyChanged();
91      }
92    }
93
94    [Storable(DefaultValue = true)]
95    private bool getsCollected;
96    public bool GetsCollected {
97      get { return getsCollected; }
98      set {
99        if (value != getsCollected) {
100          getsCollected = value;
101          OnGetsCollectedChanged();
102        }
103      }
104    }
105
106    #region Constructors
107    [StorableConstructor]
108    protected OptionalConstrainedValueParameter(StorableConstructorFlag _) : base(_) { }
109    protected OptionalConstrainedValueParameter(OptionalConstrainedValueParameter<T> original, Cloner cloner)
110      : base(original, cloner) {
111      validValues = cloner.Clone(original.validValues);
112      value = cloner.Clone(original.value);
113      readOnly = original.readOnly;
114      getsCollected = original.getsCollected;
115      Initialize();
116    }
117    public OptionalConstrainedValueParameter()
118      : base("Anonymous", typeof(T)) {
119      this.validValues = new ItemSet<T>();
120      this.readOnly = false;
121      this.getsCollected = true;
122      Initialize();
123    }
124    public OptionalConstrainedValueParameter(string name)
125      : base(name, typeof(T)) {
126      this.validValues = new ItemSet<T>();
127      this.readOnly = false;
128      this.getsCollected = true;
129      Initialize();
130    }
131    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
132      : base(name, typeof(T)) {
133      this.validValues = validValues;
134      this.readOnly = false;
135      this.getsCollected = true;
136      Initialize();
137    }
138    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
139      : base(name, typeof(T)) {
140      this.validValues = validValues;
141      this.value = value;
142      this.readOnly = false;
143      this.getsCollected = true;
144      Initialize();
145    }
146    public OptionalConstrainedValueParameter(string name, string description)
147      : base(name, description, typeof(T)) {
148      this.validValues = new ItemSet<T>();
149      this.readOnly = false;
150      this.getsCollected = true;
151      Initialize();
152    }
153    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
154      : base(name, description, typeof(T)) {
155      this.validValues = validValues;
156      this.readOnly = false;
157      this.getsCollected = true;
158      Initialize();
159    }
160    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
161      : base(name, description, typeof(T)) {
162      this.validValues = validValues;
163      this.value = value;
164      this.readOnly = false;
165      this.getsCollected = true;
166      Initialize();
167    }
168    #endregion
169
170    [StorableHook(HookType.AfterDeserialization)]
171    private void AfterDeserialization() {
172      Initialize();
173    }
174
175    private void Initialize() {
176      RegisterValidValuesEvents();
177      RegisterValueEvents();
178    }
179
180    public override IDeepCloneable Clone(Cloner cloner) {
181      return new OptionalConstrainedValueParameter<T>(this, cloner);
182    }
183
184    public override string ToString() {
185      return Name + ": " + (Value != null ? Value.ToString() : "null");
186    }
187
188    protected override IItem GetActualValue() {
189      return Value;
190    }
191    protected override void SetActualValue(IItem value) {
192      ((IValueParameter)this).Value = value;
193    }
194
195    public event EventHandler ValueChanged;
196    protected virtual void OnValueChanged() {
197      EventHandler handler = ValueChanged;
198      if (handler != null) handler(this, EventArgs.Empty);
199      OnItemImageChanged();
200      OnToStringChanged();
201    }
202    public event EventHandler ReadOnlyChanged;
203    protected virtual void OnReadOnlyChanged() {
204      EventHandler handler = ReadOnlyChanged;
205      if (handler != null) handler(this, EventArgs.Empty);
206    }
207    public event EventHandler GetsCollectedChanged;
208    protected virtual void OnGetsCollectedChanged() {
209      EventHandler handler = GetsCollectedChanged;
210      if (handler != null) handler(this, EventArgs.Empty);
211    }
212
213    private void RegisterValidValuesEvents() {
214      if (validValues != null) {
215        validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
216        validValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
217        validValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
218      }
219    }
220    private void DeregisterValidValuesEvents() {
221      if (validValues != null) {
222        validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
223        validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
224        validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
225      }
226    }
227    protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
228    protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
229      if ((Value != null) && !validValues.Contains(Value)) Value = null;
230    }
231    protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
232      if ((Value != null) && !validValues.Contains(Value)) Value = null;
233    }
234
235    private void RegisterValueEvents() {
236      if (value != null) {
237        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
238        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
239      }
240    }
241    private void DeregisterValueEvents() {
242      if (value != null) {
243        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
244        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
245      }
246    }
247    private void Value_ItemImageChanged(object sender, EventArgs e) {
248      OnItemImageChanged();
249    }
250    private void Value_ToStringChanged(object sender, EventArgs e) {
251      OnToStringChanged();
252    }
253  }
254}
Note: See TracBrowser for help on using the repository browser.