using System; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Data; using System.Drawing; namespace HeuristicLab.Problems.MetaOptimization { // TODO: ItemName/Descr, storability public class ValueConfiguration : Item, IValueConfiguration { protected bool optimize; public bool Optimize { get { return optimize; } set { if (optimize != value) { optimize = value; if (optimize) { ClearParameterConfigurations(); PopulateParameterConfigurations(); } else { ClearParameterConfigurations(); } OnOptimizeChanged(); OnToStringChanged(); } } } protected IItemCollection parameterConfigurations = new ItemCollection(); public IItemCollection ParameterConfigurations { get { return new ReadOnlyItemCollection(this.parameterConfigurations); } protected set { this.parameterConfigurations = value; } } protected ConstrainedValue constrainedValue; public ConstrainedValue ConstrainedValue { get { return constrainedValue; } set { if (this.constrainedValue != value) { ClearParameterConfigurations(); this.constrainedValue = value; PopulateParameterConfigurations(); OnValueChanged(); OnToStringChanged(); } } } protected IRange rangeConstraint; public IRange RangeConstraint { get { return rangeConstraint; } } #region Constructors and Cloning public ValueConfiguration(IItem value, Type valueDataType) { this.ParameterConfigurations = new ItemList(); this.ConstrainedValue = new ConstrainedValue(value, valueDataType); if (constrainedValue.ValueDataType == typeof(IntValue)) { rangeConstraint = new Range(new IntValue(0), (IntValue)value, new IntValue(1)); } else if (constrainedValue.ValueDataType == typeof(DoubleValue)) { rangeConstraint = new Range(new DoubleValue(0), (DoubleValue)value, new DoubleValue(0.1)); } else { rangeConstraint = null; } RegisterEvents(); } public ValueConfiguration() { } [StorableConstructor] protected ValueConfiguration(bool deserializing) { } protected ValueConfiguration(ValueConfiguration original, Cloner cloner) : base(original, cloner) { this.ParameterConfigurations = cloner.Clone(original.ParameterConfigurations); this.ConstrainedValue = cloner.Clone(original.ConstrainedValue); this.rangeConstraint = cloner.Clone(original.RangeConstraint); RegisterEvents(); } public override IDeepCloneable Clone(Cloner cloner) { return new ValueConfiguration(this, cloner); } #endregion protected virtual void PopulateParameterConfigurations() { if (this.constrainedValue.Value is IParameterizedNamedItem) { var parameterizedItem = this.constrainedValue.Value as IParameterizedNamedItem; foreach (var childParameter in parameterizedItem.Parameters) { var pc = ParameterConfiguration.Create(parameterizedItem, childParameter); if (pc != null) this.parameterConfigurations.Add(pc); } } } protected virtual void ClearParameterConfigurations() { parameterConfigurations.Clear(); } private void RegisterEvents() { if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged); if (this.ConstrainedValue != null) this.ConstrainedValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged); } private void DeregisterEvents() { if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged); if (this.ConstrainedValue != null) this.ConstrainedValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged); } void ConstrainedValue_ToStringChanged(object sender, EventArgs e) { OnToStringChanged(); } void RangeConstraint_ToStringChanged(object sender, EventArgs e) { OnToStringChanged(); } #region IItem Members public override string ItemDescription { get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemDescription; } } public override Image ItemImage { get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemImage : base.ItemImage; } } public override string ItemName { get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemName; } } #endregion #region Event Handlers public virtual event EventHandler ValueChanged; protected virtual void OnValueChanged() { var handler = ValueChanged; if (handler != null) handler(this, EventArgs.Empty); } public virtual event EventHandler OptimizeChanged; protected virtual void OnOptimizeChanged() { var handler = OptimizeChanged; if (handler != null) handler(this, EventArgs.Empty); } #endregion public override string ToString() { if (ConstrainedValue != null && ConstrainedValue.Value != null) { if(Optimize) { return string.Format("{0}: {1}", ConstrainedValue.Value.ItemName, RangeConstraint); } else { return string.Format("{0}: {1}", ConstrainedValue.Value.ItemName, ConstrainedValue.Value); } } else { return base.ToString(); } } } }