namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc { using System; using System.Collections.Generic; using System.Linq; using Boolean; using Char; using Common; using Core; using Data; using Float; using FloatVector; using Integer; using IntegerVector; using Parameters; using Persistence.Default.CompositeSerializers.Storable; using String; using StringVector; [StorableClass] public class ErcOptions : ParameterizedNamedItem, IReadOnlyErcOptions { public const string ProbabilityParameterName = "Probability"; public const string IntegerParameterName = "Integer"; public const string FloatParameterName = "Float"; public const string CharParameterName = "Char"; public const string BooleanParameterName = "Boolean"; public const string StringParameterName = "String"; public const string NameParameterName = "Name"; public const string IntegerVectorParameterName = "IntegerVector"; public const string FloatVectorParameterName = "FloatVector"; public const string StringVectorParameterName = "StringVector"; [Storable] private readonly ReadOnlyCheckedItemCollection ercOptions; [Storable] private readonly NamedItemCollection ercOptionParameters; private IDictionary> ercOptionEnabledChangedHandler; public ErcOptions() { Name = "ERC Options"; Parameters.Add(new FixedValueParameter(ProbabilityParameterName)); ercOptionParameters = new NamedItemCollection { new ValueParameter(IntegerParameterName), new ValueParameter(FloatParameterName), new ValueParameter(BooleanParameterName), new ValueParameter(StringParameterName), new ValueParameter(CharParameterName), new ValueParameter(NameParameterName), new ValueParameter(IntegerVectorParameterName), new ValueParameter(FloatVectorParameterName), new ValueParameter(StringVectorParameterName) }; InitEnabledChangedHandlerDict(ercOptionParameters); var optionsCollection = new CheckedItemCollection(ercOptionParameters); ercOptions = new ReadOnlyCheckedItemCollection(optionsCollection); Parameters.Add(new FixedValueParameter>("Options", ercOptions)); ercOptions.CheckedItemsChanged += ErcOptionsCheckedItemsChanged; DisableAllOptions(); } [StorableConstructor] protected ErcOptions(bool deserializing) : base(deserializing) { } public ErcOptions(ErcOptions origin, Cloner cloner) : base(origin, cloner) { ercOptions = cloner.Clone(origin.ercOptions); ercOptionParameters = cloner.Clone(origin.ercOptionParameters); InitEnabledChangedHandlerDict(ercOptionParameters); } [StorableHook(HookType.AfterDeserialization)] // ReSharper disable once UnusedMember.Local private void AfterDeserialization() { InitEnabledChangedHandlerDict(ercOptionParameters); } private void InitEnabledChangedHandlerDict(NamedItemCollection parameters) { ercOptionEnabledChangedHandler = parameters .ToDictionary>( param => param, param => (sender, state) => ercOptions.SetItemCheckedState(param, state)); } private void ErcOptionsCheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var item in e.Items) { var ercItem = (IErcItem)item.Value; ercItem.IsEnabled = ercOptions.ItemChecked(item); } } public IValueParameter ErcProbabilityParameter { get { return (IValueParameter)Parameters[ProbabilityParameterName]; } } public double ErcProbability { get { return ErcProbabilityParameter.Value.Value; } set { ErcProbabilityParameter.Value.Value = value; } } IErcValueItem IReadOnlyErcOptions.IntegerErcOptions { get { return IntegerErcOptions; } } IErcValueItem IReadOnlyErcOptions.FloatErcOptions { get { return FloatErcOptions; } } IErcValueItem IReadOnlyErcOptions.BooleanErcOptions { get { return BooleanErcOptions; } } IErcValueItem IReadOnlyErcOptions.CharErcOptions { get { return CharErcOptions; } } IErcValueItem IReadOnlyErcOptions.StringErcOptions { get { return StringErcOptions; } } IErcValueItem IReadOnlyErcOptions.NameErcOptions { get { return NameErcOptions; } } IErcValueItem> IReadOnlyErcOptions.IntegerVectorErcOptions { get { return IntegerVectorErcOptions; } } IErcValueItem> IReadOnlyErcOptions.FloatVectorErcOptions { get { return FloatVectorErcOptions; } } IErcValueItem> IReadOnlyErcOptions.StringVectorErcOptions { get { return StringVectorErcOptions; } } public IValueParameter IntegerErcOptionsParameter { get { return (IValueParameter)ercOptionParameters[IntegerParameterName]; } } public IntegerErcOptions IntegerErcOptions { get { return IntegerErcOptionsParameter.Value; } set { SetErcOptions(IntegerErcOptionsParameter, value); } } public IValueParameter FloatErcOptionsParameter { get { return (IValueParameter)ercOptionParameters[FloatParameterName]; } } public FloatErcOptions FloatErcOptions { get { return FloatErcOptionsParameter.Value; } set { SetErcOptions(FloatErcOptionsParameter, value); } } /// /// Integer represents ASCII value /// public IValueParameter CharErcOptionsParameter { get { return (IValueParameter)ercOptionParameters[CharParameterName]; } } public CharErcOptions CharErcOptions { get { return CharErcOptionsParameter.Value; } set { SetErcOptions(CharErcOptionsParameter, value); } } public IValueParameter BooleanErcOptionsParameter { get { return (IValueParameter)ercOptionParameters[BooleanParameterName]; } } public BooleanErcOptions BooleanErcOptions { get { return BooleanErcOptionsParameter.Value; } set { SetErcOptions(BooleanErcOptionsParameter, value); } } public IValueParameter StringErcOptionsParameter { get { return (IValueParameter)ercOptionParameters[StringParameterName]; } } public StringErcOptions StringErcOptions { get { return StringErcOptionsParameter.Value; } set { SetErcOptions(StringErcOptionsParameter, value); } } public IValueParameter NameParameter { get { return (IValueParameter)ercOptionParameters[NameParameterName]; } } public StringErcOptions NameErcOptions { get { return NameParameter.Value; } set { SetErcOptions(NameParameter, value); } } public IValueParameter IntegerVectorErcOptionsParameter { get { return (IValueParameter)ercOptionParameters[IntegerVectorParameterName]; } } public IntegerVectorErcOptions IntegerVectorErcOptions { get { return this.IntegerVectorErcOptionsParameter.Value; } set { SetErcOptions(this.IntegerVectorErcOptionsParameter, value); } } public IValueParameter StringVectorErcOptionsValueParameter { get { return (IValueParameter)ercOptionParameters[StringVectorParameterName]; } } public StringVectorErcOptions StringVectorErcOptions { get { return this.StringVectorErcOptionsValueParameter.Value; } set { SetErcOptions(this.StringVectorErcOptionsValueParameter, value); } } public IValueParameter FloatVectorErcOptionsParameter { get { return (IValueParameter)ercOptionParameters[FloatVectorParameterName]; } } public FloatVectorErcOptions FloatVectorErcOptions { get { return this.FloatVectorErcOptionsParameter.Value; } set { SetErcOptions(this.FloatVectorErcOptionsParameter, value); } } public override IDeepCloneable Clone(Cloner cloner) { return new ErcOptions(this, cloner); } public void DisableAllOptions() { SetOptionsState(false); } public void EnableAllOptions() { SetOptionsState(true); } public void SetOptionsState(bool state) { foreach (var param in this.ercOptions) { this.ercOptions.SetItemCheckedState(param, state); } } private void SetErcOptions(IValueParameter param, IErcItem value) { if (ercOptionEnabledChangedHandler[param] != null && param.Value != null) ((IErcItem)param.Value).EnabledChanged -= ercOptionEnabledChangedHandler[param]; param.Value = value; ercOptions.SetItemCheckedState(param, value != null ? value.IsEnabled : false); if (value != null) value.EnabledChanged += ercOptionEnabledChangedHandler[param]; } } }