Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs @ 4695

Last change on this file since 4695 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: 6.0 KB
RevLine 
[2756]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2756]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;
[3341]23using System.Drawing;
[2757]24using HeuristicLab.Common;
[2756]25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Parameters {
29  /// <summary>
[2947]30  /// A parameter whose value is defined in the parameter itself or is null.
[2756]31  /// </summary>
[3822]32  [Item("OptionalValueParameter", "A parameter whose value is defined in the parameter itself or is null.")]
[3017]33  [StorableClass]
[2890]34  public class OptionalValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
[3341]35    public override Image ItemImage {
36      get {
37        if (value != null) return value.ItemImage;
38        else return base.ItemImage;
39      }
40    }
41
[3317]42    [Storable]
[2756]43    private T value;
[2924]44    public virtual T Value {
[2756]45      get { return this.value; }
46      set {
47        if (value != this.value) {
[3341]48          DeregisterValueEvents();
[2756]49          this.value = value;
[3341]50          RegisterValueEvents();
[2756]51          OnValueChanged();
52        }
53      }
54    }
[2796]55    IItem IValueParameter.Value {
[2757]56      get { return Value; }
[2796]57      set {
58        T val = value as T;
[2852]59        if ((value != null) && (val == null))
[2796]60          throw new InvalidOperationException(
61            string.Format("Type mismatch. Value is not a \"{0}\".",
62                          typeof(T).GetPrettyName())
63          );
64        Value = val;
65      }
[2757]66    }
[2756]67
[4332]68    [Storable(DefaultValue = true)]
69    private bool getsCollected;
70    public bool GetsCollected {
71      get { return getsCollected; }
72      set {
73        if (value != getsCollected) {
74          getsCollected = value;
75          OnGetsCollectedChanged();
76        }
77      }
78    }
79
80    #region Constructors
[2890]81    public OptionalValueParameter()
[2756]82      : base("Anonymous", typeof(T)) {
[4332]83      this.getsCollected = true;
[2756]84    }
[2890]85    public OptionalValueParameter(string name)
[2756]86      : base(name, typeof(T)) {
[4332]87      this.getsCollected = true;
[2756]88    }
[4332]89    public OptionalValueParameter(string name, bool getsCollected)
90      : base(name, typeof(T)) {
91      this.getsCollected = getsCollected;
92    }
[2890]93    public OptionalValueParameter(string name, T value)
[2756]94      : base(name, typeof(T)) {
[3317]95      this.value = value;
[4332]96      this.getsCollected = true;
[3317]97      Initialize();
[2756]98    }
[4332]99    public OptionalValueParameter(string name, T value, bool getsCollected)
100      : base(name, typeof(T)) {
101      this.value = value;
102      this.getsCollected = getsCollected;
103      Initialize();
104    }
[2890]105    public OptionalValueParameter(string name, string description)
[2756]106      : base(name, description, typeof(T)) {
[4332]107      this.getsCollected = true;
[2756]108    }
[4332]109    public OptionalValueParameter(string name, string description, bool getsCollected)
110      : base(name, description, typeof(T)) {
111      this.getsCollected = getsCollected;
112    }
[2890]113    public OptionalValueParameter(string name, string description, T value)
[2756]114      : base(name, description, typeof(T)) {
[3317]115      this.value = value;
[4332]116      this.getsCollected = true;
[3317]117      Initialize();
[2756]118    }
[4332]119    public OptionalValueParameter(string name, string description, T value, bool getsCollected)
120      : base(name, description, typeof(T)) {
121      this.value = value;
122      this.getsCollected = getsCollected;
123      Initialize();
124    }
[3317]125    [StorableConstructor]
126    protected OptionalValueParameter(bool deserializing) : base(deserializing) { }
[4332]127    #endregion
[2756]128
[3317]129    [StorableHook(HookType.AfterDeserialization)]
130    private void Initialize() {
[3341]131      RegisterValueEvents();
[3317]132    }
133
[2756]134    public override IDeepCloneable Clone(Cloner cloner) {
[2890]135      OptionalValueParameter<T> clone = (OptionalValueParameter<T>)base.Clone(cloner);
[3317]136      clone.value = (T)cloner.Clone(value);
[4332]137      clone.getsCollected = getsCollected;
[3317]138      clone.Initialize();
[2756]139      return clone;
140    }
141
142    public override string ToString() {
[3688]143      return Name + ": " + (Value != null ? Value.ToString() : "null");
[2756]144    }
145
[2757]146    protected override IItem GetActualValue() {
147      return Value;
148    }
149    protected override void SetActualValue(IItem value) {
[2796]150      ((IValueParameter)this).Value = value;
[2757]151    }
152
[2756]153    public event EventHandler ValueChanged;
[4332]154    protected virtual void OnValueChanged() {
155      EventHandler handler = ValueChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
[3341]157      OnItemImageChanged();
[2932]158      OnToStringChanged();
[2756]159    }
[4332]160    public event EventHandler GetsCollectedChanged;
161    protected virtual void OnGetsCollectedChanged() {
162      EventHandler handler = GetsCollectedChanged;
163      if (handler != null) handler(this, EventArgs.Empty);
164    }
[3341]165
166    private void RegisterValueEvents() {
167      if (value != null) {
168        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
169        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
170      }
171    }
172    private void DeregisterValueEvents() {
173      if (value != null) {
174        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
175        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
176      }
177    }
178    private void Value_ItemImageChanged(object sender, EventArgs e) {
179      OnItemImageChanged();
180    }
[2932]181    private void Value_ToStringChanged(object sender, EventArgs e) {
182      OnToStringChanged();
[2756]183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.