#region License Information
/* HeuristicLab
* Copyright (C) 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 System.Collections.Generic;
using System.Linq;
using HEAL.Attic;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
namespace HeuristicLab.Parameters {
///
/// A parameter whose value has to be chosen from a set of valid values.
///
[Item("ConstrainedValueParameter", "A parameter whose value has to be chosen from a set of valid values.")]
[StorableType("A9B43259-2687-4AE8-9803-B58CE01B81EE")]
public class ConstrainedValueParameter : OptionalConstrainedValueParameter where T : class, IItem {
public override T Value {
get { return base.Value; }
set {
if ((value == null) && (ValidValues.Count > 0)) throw new ArgumentNullException();
base.Value = value;
}
}
[StorableConstructor]
protected ConstrainedValueParameter(StorableConstructorFlag _) : base(_) { }
protected ConstrainedValueParameter(ConstrainedValueParameter original, Cloner cloner) : base(original, cloner) { }
public ConstrainedValueParameter() : base() { }
public ConstrainedValueParameter(string name) : base(name) { }
public ConstrainedValueParameter(string name, ItemSet validValues) : base(name, validValues) { }
public ConstrainedValueParameter(string name, ItemSet validValues, T value) : base(name, validValues, value) { }
public ConstrainedValueParameter(string name, string description) : base(name, description) { }
public ConstrainedValueParameter(string name, string description, ItemSet validValues) : base(name, description, validValues) { }
public ConstrainedValueParameter(string name, string description, ItemSet validValues, T value) : base(name, description, validValues, value) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new ConstrainedValueParameter(this, cloner);
}
public override void Repopulate(IEnumerable items) {
var itemsOfT = items.OfType().ToList();
T oldItem = Value;
ValidValues.Clear();
T defaultItem = itemsOfT.FirstOrDefault();
foreach (T i in itemsOfT.OrderBy(x => x is INamedItem ? ((INamedItem)x).Name : x.ItemName))
ValidValues.Add(i);
if (oldItem != null) {
T item = ValidValues.FirstOrDefault(x => x.GetType() == oldItem.GetType());
if (item != null) Value = item;
else oldItem = null;
}
if (oldItem == null && defaultItem != null)
Value = defaultItem;
}
protected override void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) {
if (Value == null) Value = ValidValues.First();
}
protected override void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) {
if ((Value != null) && !ValidValues.Contains(Value)) Value = ValidValues.FirstOrDefault();
}
protected override void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
if ((Value != null) && !ValidValues.Contains(Value)) Value = ValidValues.FirstOrDefault();
}
}
}