// Copyright (C) Stichting Deltares 2017. 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 log4net; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.Probabilistics; using Ringtoets.Common.IO.Properties; namespace Ringtoets.Common.IO.Configurations.Helpers { /// /// Methods for helping assign values from configurations to calculations. /// public static class ConfigurationImportHelper { /// /// Sets the stochast parameters. /// /// The type of the distribution to read. /// The type of the calculation input. /// The stochast's name. /// The name of the calculation to configure. /// The input for which to assign the read stochast. /// The configuration of the stochast. /// The function for obtaining the stochast to read. /// The function to set the stochast with the read parameters. /// Log used to write out errors. /// true if reading all required stochast parameters was successful, /// false otherwise. public static bool TrySetStandardDeviationStochast( string stochastName, string calculationName, TCalculationInput input, StochastConfiguration stochastConfiguration, Func getStochast, Action setStochast, ILog log) where TDistribution : IDistribution where TCalculationInput : ICalculationInput { if (stochastConfiguration == null) { return true; } if (stochastConfiguration.VariationCoefficient.HasValue) { log.LogCalculationConversionError(string.Format( Resources.ConfigurationImportHelper_TrySetStandardDeviationStochast_Stochast_0_requires_standard_deviation_but_variation_coefficient_found_for_Calculation_1_, stochastName, calculationName), calculationName); return false; } var distribution = (TDistribution) getStochast(input).Clone(); if (!distribution.TrySetDistributionProperties(stochastConfiguration, stochastName, calculationName)) { return false; } setStochast(input, distribution); return true; } /// /// Sets the stochast parameters. /// /// The type of the distribution to read. /// The type of the calculation input. /// The stochast's name. /// The name of the calculation to configure. /// The input for which to assign the read stochast. /// The configuration of the stochast. /// The function for obtaining the stochast to read. /// The function to set the stochast with the read parameters. /// Log used to write out errors. /// true if reading all required stochast parameters was successful, /// false otherwise. public static bool TrySetVariationCoefficientStochast( string stochastName, string calculationName, TCalculationInput input, StochastConfiguration stochastConfiguration, Func getStochast, Action setStochast, ILog log) where TDistribution : IVariationCoefficientDistribution where TCalculationInput : ICalculationInput { if (stochastConfiguration == null) { return true; } if (stochastConfiguration.StandardDeviation.HasValue) { log.LogCalculationConversionError(string.Format( Resources.ConfigurationImportHelper_TrySetVariationCoefficientStochast_Stochast_0_requires_variation_coefficient_but_standard_deviation_found_for_Calculation_1_, stochastName, calculationName), calculationName); return false; } var distribution = (TDistribution) getStochast(input).Clone(); if (!distribution.TrySetDistributionProperties(stochastConfiguration, stochastName, calculationName)) { return false; } setStochast(input, distribution); return true; } } }