// 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 Core.Common.Base.Data;
using log4net;
using Ringtoets.Common.Data.Probabilistics;
using Ringtoets.Common.IO.Properties;
namespace Ringtoets.Common.IO.Configurations.Helpers
{
///
/// Extension methods for converting to .
///
public static class DistributionConversionExtensions
{
private static readonly ILog log = LogManager.GetLogger(typeof(DistributionConversionExtensions));
///
/// Configure a new with
/// and
/// taken from
/// .
///
/// The distribution to take the values from.
/// A new with
/// and
/// set.
/// Thrown when is null.
public static StochastConfiguration ToStochastConfiguration(this IDistribution distribution)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
return new StochastConfiguration
{
Mean = distribution.Mean,
StandardDeviation = distribution.StandardDeviation
};
}
///
/// Configure a new with
/// taken from
/// .
///
/// The distribution to take the values from.
/// A new with
/// set.
/// Thrown when is null.
public static StochastConfiguration ToStochastConfigurationWithMean(this IDistribution distribution)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
return new StochastConfiguration
{
Mean = distribution.Mean
};
}
///
/// Configure a new with
/// taken from
/// .
///
/// The distribution to take the values from.
/// A new with
/// set.
/// Thrown when is null.
public static StochastConfiguration ToStochastConfigurationWithStandardDeviation(this IDistribution distribution)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
return new StochastConfiguration
{
StandardDeviation = distribution.StandardDeviation
};
}
///
/// Configure a new with
/// and
/// taken from
/// .
///
/// The distribution to take the values from.
/// A new with
/// and
/// set.
/// Thrown when is null.
public static StochastConfiguration ToStochastConfiguration(this IVariationCoefficientDistribution distribution)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
return new StochastConfiguration
{
Mean = distribution.Mean,
VariationCoefficient = distribution.CoefficientOfVariation
};
}
///
/// Configure a new with
/// taken from
/// .
///
/// The distribution to take the values from.
/// A new with
/// set.
/// Thrown when is null.
public static StochastConfiguration ToStochastConfigurationWithMean(this IVariationCoefficientDistribution distribution)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
return new StochastConfiguration
{
Mean = distribution.Mean
};
}
///
/// Configure a new with
/// taken from
/// .
///
/// The distribution to take the values from.
/// A new with
/// set.
/// Thrown when is null.
public static StochastConfiguration ToStochastConfigurationWithVariationCoefficient(this IVariationCoefficientDistribution distribution)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
return new StochastConfiguration
{
VariationCoefficient = distribution.CoefficientOfVariation
};
}
///
/// Attempts to set the parameters of an .
///
/// The to be updated.
/// The new value for .
/// The new value for .
/// The descriptive name of .
/// The name of the calculation to which
/// is associated.
/// true if setting all properties was successful, false otherwise.
/// Thrown when
/// is null.
public static bool TrySetDistributionProperties(this IDistribution distribution,
double? mean, double? standardDeviation,
string stochastName, string calculationName)
{
return distribution.TrySetMean(mean, stochastName, calculationName)
&& distribution.TrySetStandardDeviation(standardDeviation, stochastName, calculationName);
}
///
/// Attempts to set the parameters of an .
///
/// The to be updated.
/// The configuration containing the new values for
/// and .
/// The descriptive name of .
/// The name of the calculation to which
/// is associated.
/// true if setting all properties was successful, false otherwise.
/// Thrown when
/// is null.
public static bool TrySetDistributionProperties(this IDistribution distribution,
StochastConfiguration configuration,
string stochastName, string calculationName)
{
return distribution.TrySetMean(configuration.Mean, stochastName, calculationName)
&& distribution.TrySetStandardDeviation(configuration.StandardDeviation, stochastName, calculationName);
}
///
/// Attempts to set .
///
/// The to be updated.
/// The new value for .
/// The descriptive name of .
/// The name of the calculation to which
/// is associated.
/// true if setting was successful,
/// false otherwise.
/// Thrown when
/// is null.
public static bool TrySetMean(this IDistribution distribution, double? mean,
string stochastName, string calculationName)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
if (mean.HasValue)
{
try
{
distribution.Mean = (RoundedDouble) mean.Value;
}
catch (ArgumentOutOfRangeException e)
{
string errorMessage = string.Format(
Resources.IDistributionExtensions_TrySetMean_Mean_0_is_invalid_for_Stochast_1_,
mean, stochastName);
LogOutOfRangeException(errorMessage,
calculationName,
e);
return false;
}
}
return true;
}
///
/// Attempts to set .
///
/// The to be updated.
/// The new value for .
/// The descriptive name of .
/// The name of the calculation to which
/// is associated.
/// true if setting
/// was successful, false otherwise.
/// Thrown when
/// is null.
public static bool TrySetStandardDeviation(this IDistribution distribution, double? standardDeviation,
string stochastName, string calculationName)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
if (standardDeviation.HasValue)
{
try
{
distribution.StandardDeviation = (RoundedDouble) standardDeviation.Value;
}
catch (ArgumentOutOfRangeException e)
{
string errorMessage = string.Format(
Resources.IDistributionExtensions_TrySetStandardDeviation_StandardDeviation_0_is_invalid_for_Stochast_1_,
standardDeviation, stochastName);
LogOutOfRangeException(errorMessage,
calculationName,
e);
return false;
}
}
return true;
}
///
/// Attempts to set the parameters of an .
///
/// The to be updated.
/// The new value for .
/// The new value for .
/// The descriptive name of .
/// The name of the calculation to which
/// is associated.
/// true if setting all properties was successful, false otherwise.
/// Thrown when
/// is null.
public static bool TrySetDistributionProperties(this IVariationCoefficientDistribution distribution,
double? mean, double? variationCoefficient,
string stochastName, string calculationName)
{
return distribution.TrySetMean(mean, stochastName, calculationName)
&& distribution.TrySetVariationCoefficient(variationCoefficient, stochastName, calculationName);
}
///
/// Attempts to set the parameters of an .
///
/// The to be updated.
/// The configuration containing the new values for
/// and .
/// The descriptive name of .
/// The name of the calculation to which
/// is associated.
/// true if setting all properties was successful, false otherwise.
/// Thrown when
/// is null.
public static bool TrySetDistributionProperties(this IVariationCoefficientDistribution distribution,
StochastConfiguration configuration,
string stochastName, string calculationName)
{
return distribution.TrySetMean(configuration.Mean, stochastName, calculationName)
&& distribution.TrySetVariationCoefficient(configuration.VariationCoefficient, stochastName, calculationName);
}
///
/// Attempts to set .
///
/// The to be updated.
/// The new value for .
/// The descriptive name of .
/// The name of the calculation to which
/// is associated.
/// true if setting was successful,
/// false otherwise.
/// Thrown when
/// is null.
public static bool TrySetMean(this IVariationCoefficientDistribution distribution, double? mean,
string stochastName, string calculationName)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
if (mean.HasValue)
{
try
{
distribution.Mean = (RoundedDouble) mean.Value;
}
catch (ArgumentOutOfRangeException e)
{
string errorMessage = string.Format(
Resources.IVariationCoefficientDistributionExtensions_TrySetMean_Mean_0_is_invalid_for_Stochast_1_,
mean, stochastName);
LogOutOfRangeException(errorMessage,
calculationName,
e);
return false;
}
}
return true;
}
///
/// Attempts to set .
///
/// The to be updated.
/// The new value for .
/// The descriptive name of .
/// The name of the calculation to which
/// is associated.
/// true if setting
/// was successful, false otherwise.
/// Thrown when
/// is null.
public static bool TrySetVariationCoefficient(this IVariationCoefficientDistribution distribution, double? variationCoefficient,
string stochastName, string calculationName)
{
if (distribution == null)
{
throw new ArgumentNullException(nameof(distribution));
}
if (variationCoefficient.HasValue)
{
try
{
distribution.CoefficientOfVariation = (RoundedDouble) variationCoefficient.Value;
}
catch (ArgumentOutOfRangeException e)
{
string errorMessage = string.Format(
Resources.IVariationCoefficientDistributionExtensions_TrySetVariationCoefficient_VariationCoefficient_0_is_invalid_for_Stochast_1_,
variationCoefficient, stochastName);
LogOutOfRangeException(errorMessage,
calculationName,
e);
return false;
}
}
return true;
}
private static void LogOutOfRangeException(string errorMessage, string calculationName, ArgumentOutOfRangeException e)
{
log.ErrorFormat(Resources.ILogExtensions_LogCalculationConversionError_ErrorMessage_0_Calculation_1_skipped,
$"{errorMessage} {e.Message}", calculationName);
}
}
}