// 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 System.Collections.Generic;
using System.Globalization;
using Core.Common.Base;
using Core.Common.Base.Data;
using Core.Common.Util.Extensions;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Properties;
namespace Ringtoets.Common.Data.Contribution
{
///
/// This class represents the distribution of all failure mechanism contributions.
///
public class FailureMechanismContribution : Observable
{
private static readonly Range normValidityRange = new Range(1.0 / 1000000, 1.0 / 10);
private readonly List distribution = new List();
private double lowerLimitNorm;
private double signalingNorm;
private NormType normativeNorm;
///
/// Creates a new instance of . Values are taken from the
/// and one item is added with a value of
/// which represents the contribution of any other failure mechanisms.
///
/// The of
/// on which to base the .
/// The collective contribution for other failure mechanisms.
/// The lower limit norm.
/// The signaling norm.
/// Thrown when is null.
/// Thrown when:
///
/// - any of the has a value for
/// not in the interval [0, 100].
/// - the value of is not in the interval [0, 100]
///
///
/// Thrown when:
///
/// - is not in the interval [0.000001, 0.1] or is ;
/// - is not in the interval [0.000001, 0.1] or is ;
/// - The is larger than .
///
///
public FailureMechanismContribution(IEnumerable failureMechanisms,
double otherContribution,
double lowerLimitNorm,
double signalingNorm)
{
ValidateNorms(signalingNorm, lowerLimitNorm);
this.lowerLimitNorm = lowerLimitNorm;
this.signalingNorm = signalingNorm;
NormativeNorm = NormType.LowerLimit;
}
///
/// Gets the signaling norm which has been defined on the assessment section.
///
/// Thrown when
///
/// - The new value is not in the interval [0.000001, 0.1] or is ;
/// - The new value is larger than the .
///
public double SignalingNorm
{
get
{
return signalingNorm;
}
set
{
ValidateNorms(value, lowerLimitNorm);
signalingNorm = value;
SetDistribution();
}
}
///
/// Gets the lower limit norm which has been defined on the assessment section.
///
/// Thrown when
///
/// - The new value is not in the interval [0.000001, 0.1] or is ;
/// - The new value is smaller than the .
///
public double LowerLimitNorm
{
get
{
return lowerLimitNorm;
}
set
{
ValidateNorm(value);
if (value < signalingNorm)
{
throw new ArgumentOutOfRangeException(nameof(value),
value,
Resources.FailureMechanismContribution_LowerLimitNorm_should_be_same_or_greater_than_SignalingNorm);
}
lowerLimitNorm = value;
SetDistribution();
}
}
///
/// Gets the norm which has been defined on the assessment section.
///
/// Thrown when the new value is not in
/// the interval [0.000001, 0.1] or is .
public double Norm
{
get
{
return normativeNorm == NormType.LowerLimit
? LowerLimitNorm
: SignalingNorm;
}
}
///
/// Gets or sets the norm type which has been defined on the assessment section.
///
public NormType NormativeNorm
{
get
{
return normativeNorm;
}
set
{
normativeNorm = value;
SetDistribution();
}
}
private void SetDistribution()
{
distribution.ForEachElementDo(d => d.Norm = Norm);
}
///
/// Validates the norm value;
///
/// The value to validate.
/// Thrown when the new value is not in
/// the interval [0.000001, 0.1] or is .
private static void ValidateNorm(double value)
{
if (!normValidityRange.InRange(value))
{
string message = string.Format(Resources.Norm_should_be_in_Range_0_,
normValidityRange.ToString(FormattableConstants.ShowAtLeastOneDecimal, CultureInfo.CurrentCulture));
throw new ArgumentOutOfRangeException(nameof(value), value, message);
}
}
///
/// Validates the norm values.
///
/// The signaling norm to validate.
/// The lower limit norm to validate against.
/// Thrown when:
///
/// - is not in the interval [0.000001, 0.1] or is ;
/// - is not in the interval [0.000001, 0.1] or is ;
/// - The is larger than .
///
///
private static void ValidateNorms(double signalingNormValue,
double lowerLimitNormValue)
{
ValidateNorm(signalingNormValue);
ValidateNorm(lowerLimitNormValue);
if (signalingNormValue > lowerLimitNormValue)
{
throw new ArgumentOutOfRangeException(nameof(signalingNormValue),
signalingNormValue,
Resources.FailureMechanismContribution_SignalingNorm_should_be_same_or_smaller_than_LowerLimitNorm);
}
}
}
}