// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets 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. // // This program 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 this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Linq; using log4net; using Ringtoets.Common.Data; using Ringtoets.Common.Data.Probabilistics; using Ringtoets.Common.Data.Structures; using Ringtoets.Common.IO.Configurations.Helpers; using Ringtoets.Common.IO.Properties; namespace Ringtoets.Common.IO.Configurations { /// /// Validates and assigns stochast configurations to a structure calculation input. /// /// The type of the configuration that is validated and used in assignments. /// The type of the input to which to assign stochasts. /// The type of structure assigned to the input. public abstract class StructuresCalculationStochastAssigner where TConfiguration : StructuresCalculationConfiguration where TInput : StructuresInputBase, new() where TStructure : StructureBase { protected static readonly ILog Log = LogManager.GetLogger(typeof(StructuresCalculationStochastAssigner)); /// /// The configuration that is used for stochast parameter source. /// protected readonly TConfiguration Configuration; private readonly StructuresCalculation calculation; /// /// Creates a new instance of /// /// The configuration that is used for stochast parameter source. /// The target calculation. /// Thrown when any input parameters is null. protected StructuresCalculationStochastAssigner( TConfiguration configuration, StructuresCalculation calculation) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (calculation == null) { throw new ArgumentNullException(nameof(calculation)); } Configuration = configuration; this.calculation = calculation; } /// /// Validates the configuration and sets all the parameters for the stochasts from the configuration /// to the calculation. /// /// true if setting the parameters of all configured stochasts succeeded, false /// otherwise. public bool Assign() { return Validate() && GetStandardDeviationStochasts().All(SetStandardDeviationStochast) && GetVariationCoefficientStochasts().All(SetVariationCoefficientStochast); } /// /// Performs additional validations for structure specific stochasts. /// /// true if no errors were found, false otherwise. protected virtual bool ValidateSpecificStochasts() { return true; } /// /// Gets the definitions for all stochasts with standard deviation that are defined for the calculation input. /// /// Optional. If set to true, only definitions for structure dependent /// stochasts are returned. /// The standard deviation stochasts definitions for the calculation. protected abstract IEnumerable GetStandardDeviationStochasts(bool structureDependent = false); /// /// Gets the definitions for all stochasts with variation coefficient that are defined for the calculation input. /// /// Optional. If set to true, only definitions for structure dependent /// stochasts are returned. /// The variation coefficient stochasts definitions for the calculation. protected abstract IEnumerable GetVariationCoefficientStochasts(bool structureDependent = false); /// /// Validates the stochasts and their parameters of the configuration. /// /// true if all the stochasts are valid, false otherwise. private bool Validate() { return ValidateBaseStochasts() && ValidateSpecificStochasts() && (Configuration.StructureName != null || GetStochastsForParameterValidation().All(ValidateNoParametersDefined)); } private bool SetVariationCoefficientStochast(VariationCoefficientDefinition definition) { return ConfigurationImportHelper.TrySetVariationCoefficientStochast( definition.StochastName, calculation.Name, calculation.InputParameters, definition.Configuration, definition.Getter, definition.Setter, Log); } private bool SetStandardDeviationStochast(StandardDeviationDefinition definition) { return ConfigurationImportHelper.TrySetStandardDeviationStochast( definition.StochastName, calculation.Name, calculation.InputParameters, definition.Configuration, definition.Getter, definition.Setter, Log); } private bool ValidateNoParametersDefined(Tuple stochastValidationDefinition) { string stochastName = stochastValidationDefinition.Item1; StochastConfiguration stochastConfiguration = stochastValidationDefinition.Item2; string calculationName = Configuration.Name; bool parameterDefined = stochastConfiguration != null && (stochastConfiguration.Mean.HasValue || stochastConfiguration.StandardDeviation.HasValue || stochastConfiguration.VariationCoefficient.HasValue); if (parameterDefined) { Log.LogCalculationConversionError($"Er is geen kunstwerk opgegeven om de stochast '{stochastName}' aan toe te voegen.", calculationName); } return !parameterDefined; } private bool ValidateBaseStochasts() { if (Configuration.StormDuration?.StandardDeviation != null || Configuration.StormDuration?.VariationCoefficient != null) { Log.LogCalculationConversionError(Resources.CalculationConfigurationImporter_ValidateStochasts_Cannot_define_spread_for_StormDuration, Configuration.Name); return false; } if (Configuration.ModelFactorSuperCriticalFlow?.StandardDeviation != null || Configuration.ModelFactorSuperCriticalFlow?.VariationCoefficient != null) { Log.LogCalculationConversionError(Resources.CalculationConfigurationImporter_ValidateStochasts_Cannot_define_spread_for_ModelFactorSuperCriticalFlow, Configuration.Name); return false; } return true; } private IEnumerable> GetStochastsForParameterValidation() { foreach (StandardDeviationDefinition stochastDefinition in GetStandardDeviationStochasts(true)) { yield return Tuple.Create(stochastDefinition.StochastName, stochastDefinition.Configuration); } foreach (VariationCoefficientDefinition stochastDefinition in GetVariationCoefficientStochasts(true)) { yield return Tuple.Create(stochastDefinition.StochastName, stochastDefinition.Configuration); } } /// /// A definition for a stochast with operations defining how to get and set the stochast. /// public class StandardDeviationDefinition { /// /// Gets the name of the stochast. /// public string StochastName { get; } /// /// Gets the configuration of the stochast. Can return null /// if no configuration was defined. /// public StochastConfiguration Configuration { get; } /// /// The method for obtaining the distribution matching the stochast /// to be configured, from the input. /// public Func Getter { get; } /// /// The method for assigning the distribution matching the stochast /// to be configured, to the input. /// public Action Setter { get; } /// /// Creates a new instance of . /// /// The name of the stochast. /// The configuration of the stochast, which can be null. /// Operation of obtaining the stochast from an input. /// Operation of assigning the stochast to an input. /// The newly created definition. /// Thrown when the , /// or is null. public StandardDeviationDefinition( string stochastName, StochastConfiguration configuration, Func getter, Action setter) { if (stochastName == null) { throw new ArgumentNullException(nameof(stochastName)); } if (getter == null) { throw new ArgumentNullException(nameof(getter)); } if (setter == null) { throw new ArgumentNullException(nameof(setter)); } StochastName = stochastName; Configuration = configuration; Getter = getter; Setter = setter; } } /// /// A definition for a stochast with operations defining how to get and set the stochast. /// public class VariationCoefficientDefinition { /// /// Gets the name of the stochast. /// public string StochastName { get; } /// /// Gets the configuration of the stochast. Can return null /// if no configuration was defined. /// public StochastConfiguration Configuration { get; } /// /// The method for obtaining the distribution matching the stochast /// to be configured, from the input. /// public Func Getter { get; } /// /// The method for assigning the distribution matching the stochast /// to be configured, to the input. /// public Action Setter { get; } /// /// Creates a new instance of . /// /// The name of the stochast. /// The configuration of the stochast, which can be null. /// Operation of obtaining the stochast from an input. /// Operation of assigning the stochast to an input. /// The newly created definition. /// Thrown when the , /// or is null. public VariationCoefficientDefinition( string stochastName, StochastConfiguration configuration, Func getter, Action setter) { StochastName = stochastName; Configuration = configuration; Getter = getter; Setter = setter; } } } }