using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { [StorableClass] public class ConstrainedValue : Item { [Storable] protected Type valueDataType; public Type ValueDataType { get { return this.valueDataType; } protected set { this.valueDataType = value; } } [Storable] protected IItem value; public IItem Value { get { return value; } set { if (this.value != value) { if(this.value != null) DeregisterEvents(); this.value = value; if(this.value != null) RegisterEvents(); OnToStringChanged(); } } } [Storable] protected IItemSet validValues; public IItemSet ValidValues { get { return validValues; } protected set { if (this.validValues != value) { this.validValues = value; } } } [Storable] protected bool isNullable; public bool IsNullable { get { return isNullable; } protected set { if (this.isNullable != value) { this.isNullable = value; } } } private void RegisterEvents() { this.value.ToStringChanged += new EventHandler(value_ToStringChanged); if (this.value is Symbol) { ((Symbol)this.value).Changed += new EventHandler(ConstrainedValue_Changed); } } private void DeregisterEvents() { this.value.ToStringChanged -= new EventHandler(value_ToStringChanged); if (this.value is Symbol) { ((Symbol)this.value).Changed -= new EventHandler(ConstrainedValue_Changed); } } #region constructors and cloning public ConstrainedValue() { } [StorableConstructor] protected ConstrainedValue(bool deserializing) : base(deserializing) { } public ConstrainedValue(IItem value, Type valueDataType, IItemSet validValues, bool isNullable) { this.Value = value; this.ValueDataType = valueDataType; this.ValidValues = validValues; this.isNullable = isNullable; } protected ConstrainedValue(ConstrainedValue original, Cloner cloner) : base(original, cloner) { this.valueDataType = original.valueDataType; this.Value = cloner.Clone(original.value); if(original.ValidValues != null) this.ValidValues = cloner.Clone(original.ValidValues); this.isNullable = original.isNullable; } public override IDeepCloneable Clone(Cloner cloner) { return new ConstrainedValue(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (this.value != null) RegisterEvents(); } #endregion private void value_ToStringChanged(object sender, EventArgs e) { OnToStringChanged(); } private void ConstrainedValue_Changed(object sender, EventArgs e) { OnToStringChanged(); } public override string ToString() { if (this.value is Symbol) { return string.Format("{0}: {1}", this.value, ((Symbol)this.value).InitialFrequency); } return value != null ? value.ToString() : base.ToString(); } } }