[5112] | 1 | using System;
|
---|
[6197] | 2 | using HeuristicLab.Common;
|
---|
[5112] | 3 | using HeuristicLab.Core;
|
---|
[6197] | 4 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[5112] | 5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 6 |
|
---|
| 7 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 8 | [StorableClass]
|
---|
| 9 | public class ConstrainedValue : Item {
|
---|
| 10 | [Storable]
|
---|
| 11 | protected Type valueDataType;
|
---|
| 12 | public Type ValueDataType {
|
---|
| 13 | get { return this.valueDataType; }
|
---|
| 14 | protected set { this.valueDataType = value; }
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | [Storable]
|
---|
| 18 | protected IItem value;
|
---|
| 19 | public IItem Value {
|
---|
| 20 | get { return value; }
|
---|
| 21 | set {
|
---|
| 22 | if (this.value != value) {
|
---|
| 23 | if(this.value != null) DeregisterEvents();
|
---|
| 24 | this.value = value;
|
---|
| 25 | if(this.value != null) RegisterEvents();
|
---|
| 26 | OnToStringChanged();
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | [Storable]
|
---|
[5231] | 32 | protected IItemSet<IItem> validValues;
|
---|
| 33 | public IItemSet<IItem> ValidValues {
|
---|
| 34 | get { return validValues; }
|
---|
[5112] | 35 | protected set {
|
---|
[5231] | 36 | if (this.validValues != value) {
|
---|
| 37 | this.validValues = value;
|
---|
[5112] | 38 | }
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | [Storable]
|
---|
| 43 | protected bool isNullable;
|
---|
| 44 | public bool IsNullable {
|
---|
| 45 | get { return isNullable; }
|
---|
| 46 | protected set {
|
---|
| 47 | if (this.isNullable != value) {
|
---|
| 48 | this.isNullable = value;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | private void RegisterEvents() {
|
---|
| 54 | this.value.ToStringChanged += new EventHandler(value_ToStringChanged);
|
---|
[6197] | 55 | if (this.value is Symbol) {
|
---|
| 56 | ((Symbol)this.value).Changed += new EventHandler(ConstrainedValue_Changed);
|
---|
| 57 | }
|
---|
[5112] | 58 | }
|
---|
[6197] | 59 |
|
---|
[5112] | 60 | private void DeregisterEvents() {
|
---|
| 61 | this.value.ToStringChanged -= new EventHandler(value_ToStringChanged);
|
---|
[6197] | 62 | if (this.value is Symbol) {
|
---|
| 63 | ((Symbol)this.value).Changed -= new EventHandler(ConstrainedValue_Changed);
|
---|
| 64 | }
|
---|
[5112] | 65 | }
|
---|
| 66 |
|
---|
| 67 | #region constructors and cloning
|
---|
| 68 | public ConstrainedValue() { }
|
---|
| 69 | [StorableConstructor]
|
---|
| 70 | protected ConstrainedValue(bool deserializing) : base(deserializing) { }
|
---|
[5231] | 71 | public ConstrainedValue(IItem value, Type valueDataType, IItemSet<IItem> validValues, bool isNullable) {
|
---|
[5112] | 72 | this.Value = value;
|
---|
| 73 | this.ValueDataType = valueDataType;
|
---|
[5231] | 74 | this.ValidValues = validValues;
|
---|
[5112] | 75 | this.isNullable = isNullable;
|
---|
| 76 | }
|
---|
| 77 | protected ConstrainedValue(ConstrainedValue original, Cloner cloner) : base(original, cloner) {
|
---|
| 78 | this.valueDataType = original.valueDataType;
|
---|
| 79 | this.Value = cloner.Clone(original.value);
|
---|
[5231] | 80 | if(original.ValidValues != null) this.ValidValues = cloner.Clone(original.ValidValues);
|
---|
[5112] | 81 | this.isNullable = original.isNullable;
|
---|
| 82 | }
|
---|
| 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new ConstrainedValue(this, cloner);
|
---|
| 85 | }
|
---|
| 86 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 87 | private void AfterDeserialization() {
|
---|
| 88 | if (this.value != null) RegisterEvents();
|
---|
| 89 | }
|
---|
| 90 | #endregion
|
---|
| 91 |
|
---|
[5522] | 92 | private void value_ToStringChanged(object sender, EventArgs e) {
|
---|
[5112] | 93 | OnToStringChanged();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[6197] | 96 | private void ConstrainedValue_Changed(object sender, EventArgs e) {
|
---|
| 97 | OnToStringChanged();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[5112] | 100 | public override string ToString() {
|
---|
[6197] | 101 | if (this.value is Symbol) {
|
---|
| 102 | return string.Format("{0}: {1}", this.value, ((Symbol)this.value).InitialFrequency);
|
---|
| 103 | }
|
---|
[5112] | 104 | return value != null ? value.ToString() : base.ToString();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|