#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.ParameterConfigurationTreeEncoding { [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(); } } }