Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ReferenceParameter.cs @ 17594

Last change on this file since 17594 was 17594, checked in by mkommend, 4 years ago

#2521: Added first version of new results. The first algorithm that has been adapted for testing purposes is the hill climber.

File size: 7.8 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  [Item("ReferenceParameter", "A base class for reference parameters that forward to other (referenced) parameters.")]
30  [StorableType("39CE4123-E41C-4935-90FE-91F6A629178A")]
31  public abstract class ReferenceParameter : Parameter, IValueParameter {
32    public override Image ItemImage {
33      get {
34        if (Value != null) return Value.ItemImage;
35        else return base.ItemImage;
36      }
37    }
38
39    public IItem Value {
40      get => GetActualValue();
41      set => SetActualValue(value);
42    }
43
44    [Storable]
45    public IValueParameter ReferencedParameter { get; }
46
47    [Storable(DefaultValue = true)]
48    private bool readOnly;
49    public bool ReadOnly {
50      get { return readOnly; }
51      set {
52        if (value != readOnly) {
53          readOnly = value;
54          OnReadOnlyChanged();
55        }
56      }
57    }
58
59
60    [Storable(DefaultValue = true)]
61    private bool getsCollected;
62    public bool GetsCollected {
63      get { return getsCollected; }
64      set {
65        if (value != getsCollected) {
66          getsCollected = value;
67          OnGetsCollectedChanged();
68        }
69      }
70    }
71
72    protected ReferenceParameter(IValueParameter referencedParameter) : this(referencedParameter.Name, referencedParameter) { }
73    protected ReferenceParameter(string name, IValueParameter referencedParameter) : this(name, referencedParameter.Description, referencedParameter) { }
74    protected ReferenceParameter(string name, string description, IValueParameter referencedParameter) : this(name, description, referencedParameter, referencedParameter.DataType) { }
75    protected ReferenceParameter(string name, string description, IValueParameter referencedParameter, Type dataType) : base(name, description, dataType) {
76      ReferencedParameter = referencedParameter ?? throw new ArgumentNullException("referencedParameter");
77      RegisterEvents();
78    }
79
80    [StorableConstructor]
81    protected ReferenceParameter(StorableConstructorFlag _) : base(_) { }
82
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      RegisterEvents();
86    }
87
88    protected ReferenceParameter(ReferenceParameter original, Cloner cloner) : base(original, cloner) {
89      ReferencedParameter = cloner.Clone(original.ReferencedParameter);
90      ReadOnly = original.ReadOnly;
91      GetsCollected = original.GetsCollected;
92
93      RegisterEvents();
94    }
95
96    private void RegisterEvents() {
97      ReferencedParameter.ToStringChanged += (o, e) => OnToStringChanged();
98      ReferencedParameter.ItemImageChanged += (o, e) => OnItemImageChanged();
99    }
100
101    protected override IItem GetActualValue() {
102      return ReferencedParameter.ActualValue;
103    }
104    protected override void SetActualValue(IItem value) {
105      ReferencedParameter.ActualValue = value;
106    }
107
108    public override string ToString() {
109      return Name + ": " + (Value != null ? Value.ToString() : "null");
110    }
111
112
113    #region event handlers
114    // code for forwarding of events adapted from https://stackoverflow.com/questions/1065355/forwarding-events-in-c-sharp
115    private EventHandler valueChanged;
116    public event EventHandler ValueChanged {
117      add { // only subscribe when we have a subscriber ourselves
118        bool firstSubscription = valueChanged == null;
119        valueChanged += value;
120        if (firstSubscription && valueChanged != null) //only subscribe once
121          ReferencedParameter.ValueChanged += OnReferencedParameterValueChanged;
122      }
123      remove { // unsubscribe if we have no more subscribers
124        valueChanged -= value;
125        if (valueChanged == null) ReferencedParameter.ValueChanged -= OnReferencedParameterValueChanged;
126      }
127    }
128    private void OnReferencedParameterValueChanged(object sender, EventArgs args) {
129      valueChanged?.Invoke(this, args); // note "this", not "sender" as sender would be the referenced parameter
130    }
131
132    public event EventHandler ReadOnlyChanged;
133    private void OnReadOnlyChanged() {
134      ReadOnlyChanged?.Invoke(this, EventArgs.Empty);
135    }
136
137    public event EventHandler GetsCollectedChanged;
138    private void OnGetsCollectedChanged() {
139      GetsCollectedChanged?.Invoke(this, EventArgs.Empty);
140    }
141    #endregion
142  }
143
144
145  [Item("ReferenceParameter", "ValueParameter<T> that forwards to another (referenced) ValueParameter<T>).")]
146  [StorableType("6DD59BE5-C618-4AD4-90FE-0FAAF15650C3")]
147  public sealed class ReferenceParameter<T> : ReferenceParameter, IValueParameter<T>
148    where T : class, IItem {
149
150    public new T Value {
151      get => ReferencedParameter.Value;
152      set => ReferencedParameter.Value = value;
153    }
154
155    public new IValueParameter<T> ReferencedParameter { get => (IValueParameter<T>)base.ReferencedParameter; }
156
157    public ReferenceParameter(IValueParameter<T> referencedParameter) : this(referencedParameter.Name, referencedParameter) { }
158    public ReferenceParameter(string name, IValueParameter<T> referencedParameter) : this(name, referencedParameter.Description, referencedParameter) { }
159    public ReferenceParameter(string name, string description, IValueParameter<T> referencedParameter) : base(name, description, referencedParameter) { }
160
161    [StorableConstructor]
162    private ReferenceParameter(StorableConstructorFlag _) : base(_) { }
163    private ReferenceParameter(ReferenceParameter<T> original, Cloner cloner) : base(original, cloner) { }
164
165    public override IDeepCloneable Clone(Cloner cloner) {
166      return new ReferenceParameter<T>(this, cloner);
167    }
168  }
169
170
171  [Item("ReferenceParameter", "ValueParameter<T> that forwards to another (referenced) ValueParameter<U>).")]
172  [StorableType("83FEA704-6AED-4D76-B25A-B469E0E9187A")]
173  public sealed class ReferenceParameter<T, U> : ReferenceParameter, IValueParameter<T>
174    where T : class, U
175    where U : class, IItem {
176
177    public new T Value {
178      get => (T)ReferencedParameter.Value;
179      set => ReferencedParameter.Value = value;
180    }
181
182    public new IValueParameter<U> ReferencedParameter { get => (IValueParameter<U>)base.ReferencedParameter; }
183
184    public ReferenceParameter(IValueParameter<U> referencedParameter) : this(referencedParameter.Name, referencedParameter) { }
185    public ReferenceParameter(string name, IValueParameter<U> referencedParameter) : this(name, referencedParameter.Description, referencedParameter) { }
186    public ReferenceParameter(string name, string description, IValueParameter<U> referencedParameter) : base(name, description, referencedParameter, typeof(T)) { }
187
188    [StorableConstructor]
189    private ReferenceParameter(StorableConstructorFlag _) : base(_) { }
190    private ReferenceParameter(ReferenceParameter<T, U> original, Cloner cloner) : base(original, cloner) { }
191
192    public override IDeepCloneable Clone(Cloner cloner) {
193      return new ReferenceParameter<T, U>(this, cloner);
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.