Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17317 was 17317, checked in by abeham, 4 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: 7.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.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Parameters {
29  /// <summary>
30  /// A parameter whose value is defined in the parameter itself or is null.
31  /// </summary>
32  [Item("OptionalValueParameter", "A parameter whose value is defined in the parameter itself or is null.")]
33  [StorableType("1A825EE0-3A72-458C-B621-7CE989EE2F0D")]
34  public class OptionalValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
35    public override Image ItemImage {
36      get {
37        if (value != null) return value.ItemImage;
38        else return base.ItemImage;
39      }
40    }
41
42    [Storable]
43    private T value;
44    public virtual T Value {
45      get { return this.value; }
46      set {
47        if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
48        DoSetValue(value);
49      }
50    }
51    public virtual void ForceValue(T value) {
52      DoSetValue(value);
53    }
54    private void DoSetValue(T value) {
55      if (value != this.value) {
56        DeregisterValueEvents();
57        this.value = value;
58        RegisterValueEvents();
59        OnValueChanged();
60      }
61    }
62    IItem IValueParameter.Value {
63      get { return Value; }
64      set {
65        T val = value as T;
66        if ((value != null) && (val == null))
67          throw new InvalidOperationException(
68            string.Format("Type mismatch. Value is not a \"{0}\".",
69                          typeof(T).GetPrettyName())
70          );
71        Value = val;
72      }
73    }
74
75    [Storable(DefaultValue = false)]
76    private bool readOnly;
77    public bool ReadOnly {
78      get { return readOnly; }
79      set {
80        if (value == readOnly) return;
81        readOnly = value;
82        OnReadOnlyChanged();
83      }
84    }
85
86    [Storable(DefaultValue = true)]
87    private bool getsCollected;
88    public bool GetsCollected {
89      get { return getsCollected; }
90      set {
91        if (value != getsCollected) {
92          getsCollected = value;
93          OnGetsCollectedChanged();
94        }
95      }
96    }
97
98    [Storable(DefaultValue = true)]
99    private bool reactOnValueToStringChangedAndValueItemImageChanged;
100    /// <summary>
101    ///   True if this parameter should react on the ToStringChanged and ItemImageChanged events of its value, otherwise false.
102    /// </summary>
103    /// <remarks>
104    ///   In some rare cases when the value of the parameter is not deeply cloned, this property has to be set to false
105    ///   to avoid a memory leak (cf. ticket #1268). In all other cases this property should always be true.
106    /// </remarks>
107    public bool ReactOnValueToStringChangedAndValueItemImageChanged {
108      get { return reactOnValueToStringChangedAndValueItemImageChanged; }
109      set {
110        if (value != reactOnValueToStringChangedAndValueItemImageChanged) {
111          reactOnValueToStringChangedAndValueItemImageChanged = value;
112          if (reactOnValueToStringChangedAndValueItemImageChanged) {
113            RegisterValueEvents();
114            OnToStringChanged();
115            OnItemImageChanged();
116          } else
117            DeregisterValueEvents();
118        }
119      }
120    }
121
122    #region Constructors
123    [StorableConstructor]
124    protected OptionalValueParameter(StorableConstructorFlag _) : base(_) { }
125    protected OptionalValueParameter(OptionalValueParameter<T> original, Cloner cloner)
126      : base(original, cloner) {
127      value = cloner.Clone(original.value);
128      readOnly = original.readOnly;
129      getsCollected = original.getsCollected;
130      reactOnValueToStringChangedAndValueItemImageChanged = original.reactOnValueToStringChangedAndValueItemImageChanged;
131      RegisterValueEvents();
132    }
133    public OptionalValueParameter()
134      : base("Anonymous", typeof(T)) {
135      this.readOnly = false;
136      this.getsCollected = true;
137      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
138    }
139    public OptionalValueParameter(string name)
140      : base(name, typeof(T)) {
141      this.readOnly = false;
142      this.getsCollected = true;
143      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
144    }
145    public OptionalValueParameter(string name, T value)
146      : base(name, typeof(T)) {
147      this.value = value;
148      this.readOnly = false;
149      this.getsCollected = true;
150      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
151      RegisterValueEvents();
152    }
153    public OptionalValueParameter(string name, string description)
154      : base(name, description, typeof(T)) {
155      this.readOnly = false;
156      this.getsCollected = true;
157      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
158    }
159
160    public OptionalValueParameter(string name, string description, T value)
161      : base(name, description, typeof(T)) {
162      this.value = value;
163      this.readOnly = false;
164      this.getsCollected = true;
165      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
166      RegisterValueEvents();
167    }
168    #endregion
169
170    [StorableHook(HookType.AfterDeserialization)]
171    private void AfterDeserialization() {
172      RegisterValueEvents();
173    }
174
175    public override IDeepCloneable Clone(Cloner cloner) {
176      return new OptionalValueParameter<T>(this, cloner);
177    }
178
179    public override string ToString() {
180      if (reactOnValueToStringChangedAndValueItemImageChanged)
181        return Name + ": " + (Value != null ? Value.ToString() : "null");
182      else
183        return Name;
184    }
185
186    protected override IItem GetActualValue() {
187      return Value;
188    }
189    protected override void SetActualValue(IItem value) {
190      ((IValueParameter)this).Value = value;
191    }
192
193    public event EventHandler ValueChanged;
194    protected virtual void OnValueChanged() {
195      EventHandler handler = ValueChanged;
196      if (handler != null) handler(this, EventArgs.Empty);
197      OnItemImageChanged();
198      OnToStringChanged();
199    }
200
201    public event EventHandler ReadOnlyChanged;
202    protected virtual void OnReadOnlyChanged() {
203      EventHandler handler = ReadOnlyChanged;
204      if (handler != null) handler(this, EventArgs.Empty);
205    }
206    public event EventHandler GetsCollectedChanged;
207    protected virtual void OnGetsCollectedChanged() {
208      EventHandler handler = GetsCollectedChanged;
209      if (handler != null) handler(this, EventArgs.Empty);
210    }
211
212    private void RegisterValueEvents() {
213      if ((value != null) && reactOnValueToStringChangedAndValueItemImageChanged) {
214        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
215        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
216      }
217    }
218    private void DeregisterValueEvents() {
219      if (value != null) {
220        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
221        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
222      }
223    }
224    private void Value_ItemImageChanged(object sender, EventArgs e) {
225      OnItemImageChanged();
226    }
227    private void Value_ToStringChanged(object sender, EventArgs e) {
228      OnToStringChanged();
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.