Index: Ringtoets/Common/src/Ringtoets.Common.IO/Configurations/StructuresCalculationStochastAssigner.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.IO/Configurations/StructuresCalculationStochastAssigner.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Configurations/StructuresCalculationStochastAssigner.cs (revision 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0) @@ -0,0 +1,328 @@ +// 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 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 + { + private 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; + private readonly TrySetStandardDeviationStochast setStandardDeviationStochast; + private readonly TrySetVariationCoefficientStochast setVariationCoefficientStochast; + + /// + /// Delegate for setting standard deviation stochasts. + /// + /// The definition of a standard deviation stochast. + /// true if setting the stochast succeeded, false otherwise. + public delegate bool TrySetStandardDeviationStochast(StandardDeviationDefinition definition); + + /// + /// Delegate for setting variation coefficient stochasts. + /// + /// The definition of a variation coefficient stochast. + /// true if setting the stochast succeeded, false otherwise. + public delegate bool TrySetVariationCoefficientStochast(VariationCoefficientDefinition definition); + + /// + /// Creates a new instance of + /// + /// The configuration that is used for stochast parameter source. + /// The target calculation. + /// The delegate for setting a stochast with standard deviation. + /// The delegate for setting a stochast with variation coefficient. + /// Thrown when any input parameters is null. + protected StructuresCalculationStochastAssigner( + TConfiguration configuration, + StructuresCalculation calculation, + TrySetStandardDeviationStochast setStandardDeviationStochast, + TrySetVariationCoefficientStochast setVariationCoefficientStochast) + { + if (configuration == null) + { + throw new ArgumentNullException(nameof(configuration)); + } + if (calculation == null) + { + throw new ArgumentNullException(nameof(calculation)); + } + if (setStandardDeviationStochast == null) + { + throw new ArgumentNullException(nameof(setStandardDeviationStochast)); + } + if (setVariationCoefficientStochast == null) + { + throw new ArgumentNullException(nameof(setVariationCoefficientStochast)); + } + Configuration = configuration; + this.calculation = calculation; + this.setStandardDeviationStochast = setStandardDeviationStochast; + this.setVariationCoefficientStochast = setVariationCoefficientStochast; + } + + /// + /// Validates the stochasts and their parameters of the configuration. + /// + /// true if all the stochasts are valid, false otherwise. + public bool AreStochastsValid() + { + if (!ValidateBaseStochasts()) + { + return false; + } + if (!ValidateSpecificStochasts()) + { + return false; + } + + if (Configuration.StructureName != null) + { + return true; + } + + foreach (Tuple stochastValidationDefinition in GetStochastsForParameterValidation()) + { + if (!ValidateNoParametersDefined(stochastValidationDefinition.Item2, stochastValidationDefinition.Item1)) + { + return false; + } + } + + return true; + } + + /// + /// 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 SetAllStochasts() + { + foreach (StandardDeviationDefinition stochastDefinition in GetStandardDeviationStochasts()) + { + if (!setStandardDeviationStochast(stochastDefinition)) + { + return false; + } + } + + foreach (VariationCoefficientDefinition stochastDefinition in GetVariationCoefficientStochasts()) + { + if (!setVariationCoefficientStochast(stochastDefinition)) + { + return false; + } + } + return true; + } + + /// + /// 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); + + + /// bas + /// 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); + + private bool ValidateNoParametersDefined(StochastConfiguration stochastConfiguration, string stochastName) + { + 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 + { + public string StochastName; + public StochastConfiguration Configuration; + public Func Getter; + public Action Setter; + + private StandardDeviationDefinition() {} + + /// + /// Creates a new instance of . + /// + /// The configuration of the stochast, which can be null. + /// The name of the stochast. + /// Operation of obtaining the stochast from an input. + /// Operation of assigning the stuchast to an input. + /// The newly created definition. + public static StandardDeviationDefinition Create( + StochastConfiguration configuration, + string stochastName, + 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)); + } + + return new StandardDeviationDefinition + { + 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 + { + public string StochastName; + public StochastConfiguration Configuration; + public Func Getter; + public Action Setter; + + private VariationCoefficientDefinition() { } + + /// + /// Creates a new instance of . + /// + /// The configuration of the stochast, which can be null. + /// The name of the stochast. + /// Operation of obtaining the stochast from an input. + /// Operation of assigning the stuchast to an input. + /// The newly created definition. + public static VariationCoefficientDefinition Create( + StochastConfiguration configuration, + string stochastName, + 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)); + } + + return new VariationCoefficientDefinition + { + StochastName = stochastName, + Configuration = configuration, + Getter = getter, + Setter = setter + }; + } + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj =================================================================== diff -u -r56ac4eb28f5fcc5b20117474e9e4030399d6806a -r1a0da06a973d48c83dff8ccb8b92cdc4810b83a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj (.../Ringtoets.Common.IO.csproj) (revision 56ac4eb28f5fcc5b20117474e9e4030399d6806a) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj (.../Ringtoets.Common.IO.csproj) (revision 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0) @@ -60,6 +60,7 @@ + Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Configurations/StructuresCalculationStochastAssignerTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Configurations/StructuresCalculationStochastAssignerTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Configurations/StructuresCalculationStochastAssignerTest.cs (revision 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0) @@ -0,0 +1,744 @@ +// 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 Core.Common.TestUtil; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Common.Data; +using Ringtoets.Common.Data.Probabilistics; +using Ringtoets.Common.Data.Structures; +using Ringtoets.Common.IO.Configurations; + +namespace Ringtoets.Common.IO.Test.Configurations +{ + [TestFixture] + public class StructuresCalculationStochastAssignerTest + { + private static IEnumerable SetInvalidBaseStochastParameters + { + get + { + yield return new TestCaseData( + new Action(c => c.StormDuration = new StochastConfiguration + { + StandardDeviation = 3.2 + }), + "stormduur") + .SetName("AreStochastsValid_SetStormDurationStandardDeviation"); + yield return new TestCaseData( + new Action(c => c.StormDuration = new StochastConfiguration + { + VariationCoefficient = 3.2 + }), + "stormduur") + .SetName("AreStochastsValid_SetStormDurationVariationCoefficient"); + yield return new TestCaseData( + new Action(c => c.ModelFactorSuperCriticalFlow = new StochastConfiguration + { + StandardDeviation = 3.2 + }), + "modelfactoroverloopdebiet") + .SetName("AreStochastsValid_SetModelFactorSuperCriticalFlowStandardDeviation"); + yield return new TestCaseData( + new Action(c => c.ModelFactorSuperCriticalFlow = new StochastConfiguration + { + VariationCoefficient = 3.2 + }), + "modelfactoroverloopdebiet") + .SetName("AreStochastsValid_SetModelFactorSuperCriticalFlowStandardDeviation"); + } + } + + [Test] + public void Constructor_WithoutConfiguration_ThrowsArgumentNullException() + { + // Setup + var calculation = new StructuresCalculation(); + + // Call + TestDelegate test = () => new SimpleStructuresCalculationStochastAssigner( + null, + calculation, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("configuration", exception.ParamName); + } + + [Test] + public void Constructor_WithoutCalculation_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var configuration = mocks.Stub("name"); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => new SimpleStructuresCalculationStochastAssigner( + configuration, + null, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("calculation", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void Constructor_WithoutSetStandardDeviationStochast_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var configuration = mocks.Stub("name"); + mocks.ReplayAll(); + + var calculation = new StructuresCalculation(); + + // Call + TestDelegate test = () => new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + null, + VariationCoefficientStochastSetter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("setStandardDeviationStochast", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void Constructor_WithoutSetVariationCoefficientStochast_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var configuration = mocks.Stub("name"); + mocks.ReplayAll(); + + var calculation = new StructuresCalculation(); + + // Call + TestDelegate test = () => new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + StandardDeviationStochastSetter, + null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("setVariationCoefficientStochast", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void Constructor_WithParameters_ReturnsNewInstance() + { + // Setup + var mocks = new MockRepository(); + var configuration = mocks.Stub("name"); + mocks.ReplayAll(); + + var calculation = new StructuresCalculation(); + + // Call + var assigner = new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter); + + // Assert + Assert.NotNull(assigner); + mocks.VerifyAll(); + } + + [Test] + public void ValidateSpecificStochasts_Always_ReturnsTrue() + { + // Setup + var mocks = new MockRepository(); + var configuration = mocks.Stub("name"); + mocks.ReplayAll(); + + var calculation = new StructuresCalculation(); + + var assigner = new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter); + + // Call + bool valid = assigner.PublicValidateSpecificStochasts(); + + // Assert + Assert.IsTrue(valid); + mocks.VerifyAll(); + } + + [Test] + [TestCaseSource(nameof(SetInvalidBaseStochastParameters))] + public void AreStochastsValid_SpreadDefinedForBaseStochast_LogsErrorAndReturnsFalse(Action modify, string stochastName) + { + // Setup + const string calculationName = "name"; + var mocks = new MockRepository(); + var configuration = mocks.Stub(calculationName); + mocks.ReplayAll(); + + var calculation = new StructuresCalculation(); + + modify(configuration); + + var assigner = new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter); + + // Call + var valid = true; + Action validate = () => valid = assigner.AreStochastsValid(); + + // Assert + string expectedMessage = $"Er kan geen spreiding voor stochast '{stochastName}' opgegeven worden. Berekening '{calculationName}' is overgeslagen."; + TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + Assert.IsFalse(valid); + mocks.VerifyAll(); + } + + [Test] + public void AreStochastsValid_SpecificInvalid_ReturnsFalse() + { + // Setup + const string calculationName = "name"; + var mocks = new MockRepository(); + var configuration = mocks.Stub(calculationName); + mocks.ReplayAll(); + + var calculation = new StructuresCalculation(); + + var assigner = new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter) + { + SpecificIsValid = false + }; + + // Call + bool valid = assigner.AreStochastsValid(); + + // Assert + Assert.IsFalse(valid); + mocks.VerifyAll(); + } + + [Test] + [TestCaseSource(typeof(StructuresCalculationStochastAssignerTest), nameof(GetSetStochastParameterActions), new object[] + { + "AreStochastValid_WithoutStructures{0}_LogsErrorReturnFalse" + })] + public void AreStochastsValid_WithoutStructureStochastDefinedWithParamter_LogsErrorReturnsFalse( + Action modifyStandardDeviationStochast, + Action modifyVariationCoefficientStochast, + string stochastName) + { + // Setup + const string calculationName = "name"; + var mocks = new MockRepository(); + var configuration = mocks.Stub(calculationName); + mocks.ReplayAll(); + + var calculation = new StructuresCalculation(); + var standardDeviationStochastConfiguration = new StochastConfiguration(); + var variationCoefficientStochastConfiguration = new StochastConfiguration(); + + modifyStandardDeviationStochast(standardDeviationStochastConfiguration); + modifyVariationCoefficientStochast(variationCoefficientStochastConfiguration); + + var assigner = new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter) + { + StandardDeviationStochasts = new[] + { + StructuresCalculationStochastAssigner.StandardDeviationDefinition.Create(standardDeviationStochastConfiguration, + stochastName, + input => null, (input, distribution) => { }) + }, + VariationCoefficientStochasts = new[] + { + StructuresCalculationStochastAssigner.VariationCoefficientDefinition.Create(variationCoefficientStochastConfiguration, + stochastName, + input => null, (input, distribution) => { }) + } + }; + + // Call + var valid = true; + Action validate = () => valid = assigner.AreStochastsValid(); + + // Assert + string expectedMessage = $"Er is geen kunstwerk opgegeven om de stochast '{stochastName}' aan toe te voegen. Berekening '{calculationName}' is overgeslagen."; + TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + Assert.IsFalse(valid); + mocks.VerifyAll(); + } + + [Test] + [TestCaseSource(typeof(StructuresCalculationStochastAssignerTest), nameof(GetSetStochastParameterActions), new object[] + { + "AreStochastValid_WithStructures{0}_ReturnsTrue" + })] + public void AreStochastsValid_WithStructure_ReturnsTrue( + Action modifyStandardDeviationStochast, + Action modifyVariationCoefficientStochast, + string stochastName) + { + // Setup + const string calculationName = "name"; + var mocks = new MockRepository(); + var configuration = mocks.Stub(calculationName); + mocks.ReplayAll(); + + configuration.StructureName = "some name"; + + var calculation = new StructuresCalculation(); + + var standardDeviationStochastConfiguration = new StochastConfiguration(); + var variationCoefficientStochastConfiguration = new StochastConfiguration(); + + modifyStandardDeviationStochast(standardDeviationStochastConfiguration); + modifyVariationCoefficientStochast(variationCoefficientStochastConfiguration); + + var assigner = new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter) + { + StandardDeviationStochasts = new[] + { + StructuresCalculationStochastAssigner.StandardDeviationDefinition.Create(standardDeviationStochastConfiguration, + stochastName, + input => null, (input, distribution) => { }) + }, + VariationCoefficientStochasts = new[] + { + StructuresCalculationStochastAssigner.VariationCoefficientDefinition.Create(variationCoefficientStochastConfiguration, + stochastName, + input => null, (input, distribution) => { }) + } + }; + + // Call + bool valid = assigner.AreStochastsValid(); + + // Assert + Assert.IsTrue(valid); + mocks.VerifyAll(); + } + + [Test] + [TestCase(true, true)] + [TestCase(true, false)] + [TestCase(false, true)] + [TestCase(false, false)] + public void SetAllStochasts_Always_CallsGettersAndSetters( + bool setStandardDeviationStochastSuccessful, + bool setVariationCoefficientStochastSuccessful) + { + // Setup + const string calculationName = "name"; + var mocks = new MockRepository(); + var configuration = mocks.Stub(calculationName); + mocks.ReplayAll(); + + configuration.StructureName = "some name"; + + var calculation = new StructuresCalculation(); + + var standardDeviationStochastConfiguration = new StochastConfiguration(); + var variationCoefficientStochastConfiguration = new StochastConfiguration(); + + StructuresCalculationStochastAssigner.StandardDeviationDefinition definitionA = + StructuresCalculationStochastAssigner.StandardDeviationDefinition.Create(standardDeviationStochastConfiguration, + "stochastA", + input => null, (input, distribution) => { }); + + StructuresCalculationStochastAssigner.VariationCoefficientDefinition definitionB = + StructuresCalculationStochastAssigner.VariationCoefficientDefinition.Create(variationCoefficientStochastConfiguration, + "stochastB", + input => null, (input, distribution) => { }); + + var standardDeviationSetterCalled = false; + var variationCoefficientSetterCalled = false; + + var assigner = new SimpleStructuresCalculationStochastAssigner( + configuration, + calculation, + definition => + { + standardDeviationSetterCalled = true; + Assert.AreSame(definitionA, definition); + return setStandardDeviationStochastSuccessful; + }, + definition => + { + variationCoefficientSetterCalled = true; + Assert.AreSame(definitionB, definition); + return setVariationCoefficientStochastSuccessful; + }) + { + StandardDeviationStochasts = new[] + { + definitionA + }, + VariationCoefficientStochasts = new[] + { + definitionB + } + }; + + // Call + bool valid = assigner.SetAllStochasts(); + + // Assert + Assert.AreEqual(setStandardDeviationStochastSuccessful && setVariationCoefficientStochastSuccessful, valid); + Assert.IsTrue(standardDeviationSetterCalled); + Assert.AreEqual(setStandardDeviationStochastSuccessful, variationCoefficientSetterCalled); + mocks.VerifyAll(); + } + + #region StandardDeviationDefinition + + [Test] + public void StandardDeviationDefinition_StochastNameMissing_ThrowsArgumentNullException() + { + // Setup + var configuration = new StochastConfiguration(); + var getter = new Func(i => null); + var setter = new Action((i, d) => { }); + + // Call + TestDelegate test = () => StructuresCalculationStochastAssigner + .StandardDeviationDefinition.Create(configuration, + null, + getter, setter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("stochastName", exception.ParamName); + } + + [Test] + public void StandardDeviationDefinition_ConfigurationMissing_ThrowsArgumentNullException() + { + // Setup + const string stochastName = ""; + var getter = new Func(i => null); + var setter = new Action((i, d) => { }); + + // Call + TestDelegate test = () => StructuresCalculationStochastAssigner + .StandardDeviationDefinition.Create(null, + stochastName, + getter, setter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("configuration", exception.ParamName); + } + + [Test] + public void StandardDeviationDefinition_GetterMissing_ThrowsArgumentNullException() + { + // Setup + const string stochastName = ""; + var configuration = new StochastConfiguration(); + var setter = new Action((i, d) => { }); + + // Call + TestDelegate test = () => StructuresCalculationStochastAssigner + .StandardDeviationDefinition.Create(configuration, + stochastName, + null, setter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("getter", exception.ParamName); + } + + [Test] + public void StandardDeviationDefinition_SetterMissing_ThrowsArgumentNullException() + { + // Setup + const string stochastName = ""; + var configuration = new StochastConfiguration(); + var getter = new Func(i => null); + + // Call + TestDelegate test = () => StructuresCalculationStochastAssigner + .StandardDeviationDefinition.Create(configuration, + stochastName, + getter, null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("setter", exception.ParamName); + } + + [Test] + public void StandardDeviationDefinition_WithParameters_ReturnsNewInstance() + { + // Setup + const string stochastName = ""; + var configuration = new StochastConfiguration(); + var getter = new Func(i => null); + var setter = new Action((i, d) => { }); + + // Call + StructuresCalculationStochastAssigner.StandardDeviationDefinition definition = + StructuresCalculationStochastAssigner + .StandardDeviationDefinition.Create(configuration, + stochastName, + getter, setter); + + // Assert + Assert.NotNull(definition); + Assert.AreEqual(stochastName, definition.StochastName); + Assert.AreEqual(configuration, definition.Configuration); + Assert.AreEqual(getter, definition.Getter); + Assert.AreEqual(setter, definition.Setter); + } + + #endregion + + #region VariationCoefficientDefinition + + [Test] + public void VariationCoefficientDefinition_StochastNameMissing_ThrowsArgumentNullException() + { + // Setup + var configuration = new StochastConfiguration(); + var getter = new Func(i => null); + var setter = new Action((i, d) => { }); + + // Call + TestDelegate test = () => StructuresCalculationStochastAssigner + .VariationCoefficientDefinition.Create(configuration, + null, + getter, setter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("stochastName", exception.ParamName); + } + + [Test] + public void VariationCoefficientDefinition_ConfigurationMissing_ThrowsArgumentNullException() + { + // Setup + const string stochastName = ""; + var getter = new Func(i => null); + var setter = new Action((i, d) => { }); + + // Call + TestDelegate test = () => + StructuresCalculationStochastAssigner + .VariationCoefficientDefinition.Create(null, + stochastName, + getter, setter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("configuration", exception.ParamName); + } + + [Test] + public void VariationCoefficientDefinition_GetterMissing_ThrowsArgumentNullException() + { + // Setup + const string stochastName = ""; + var configuration = new StochastConfiguration(); + var setter = new Action((i, d) => { }); + + // Call + TestDelegate test = () => + StructuresCalculationStochastAssigner + .VariationCoefficientDefinition.Create(configuration, + stochastName, + null, setter); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("getter", exception.ParamName); + } + + [Test] + public void VariationCoefficientDefinition_SetterMissing_ThrowsArgumentNullException() + { + // Setup + const string stochastName = ""; + var configuration = new StochastConfiguration(); + var getter = new Func(i => null); + + // Call + TestDelegate test = () => + StructuresCalculationStochastAssigner + .VariationCoefficientDefinition.Create(configuration, + stochastName, + getter, null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("setter", exception.ParamName); + } + + [Test] + public void VariationCoefficientDefinition_WithParameters_ReturnsNewInstance() + { + // Setup + const string stochastName = ""; + var configuration = new StochastConfiguration(); + var getter = new Func(i => null); + var setter = new Action((i, d) => { }); + + // Call + StructuresCalculationStochastAssigner.VariationCoefficientDefinition definition = + StructuresCalculationStochastAssigner + .VariationCoefficientDefinition.Create(configuration, + stochastName, + getter, setter); + + // Assert + Assert.NotNull(definition); + Assert.AreEqual(stochastName, definition.StochastName); + Assert.AreEqual(configuration, definition.Configuration); + Assert.AreEqual(getter, definition.Getter); + Assert.AreEqual(setter, definition.Setter); + } + + #endregion + + private static IEnumerable GetSetStochastParameterActions(string testNameFormat) + { + yield return new TestCaseData( + new Action(c => c.Mean = 3.2), + new Action(c => { }), + "stochastA") + .SetName(string.Format(testNameFormat, "WithStructureAndStandardDeviationStochastMeanSet")); + yield return new TestCaseData( + new Action(c => c.StandardDeviation = 3.2), + new Action(c => { }), + "stochastB") + .SetName(string.Format(testNameFormat, "WithStructureAndStandardDeviationStochastStandardDeviationSet")); + yield return new TestCaseData( + new Action(c => c.VariationCoefficient = 3.2), + new Action(c => { }), + "stochastC") + .SetName(string.Format(testNameFormat, "WithStructureAndStandardDeviationStochastVariationCoefficientSet")); + + yield return new TestCaseData( + new Action(c => { }), + new Action(c => c.Mean = 3.2), + "stochastD") + .SetName(string.Format(testNameFormat, "WithStructureAndVariationCoefficientStochastMeanSet")); + yield return new TestCaseData( + new Action(c => { }), + new Action(c => c.StandardDeviation = 3.2), + "stochastE") + .SetName(string.Format(testNameFormat, "WithStructureAndVariationCoefficientStochastStandardDeviationSet")); + yield return new TestCaseData( + new Action(c => { }), + new Action(c => c.VariationCoefficient = 3.2), + "stochastF") + .SetName(string.Format(testNameFormat, "WithStructureAndVariationCoefficientStochastVariationCoefficientSet")); + } + + private static bool StandardDeviationStochastSetter( + StructuresCalculationStochastAssigner.StandardDeviationDefinition definition) + { + return true; + } + + private static bool VariationCoefficientStochastSetter( + StructuresCalculationStochastAssigner.VariationCoefficientDefinition definition) + { + return true; + } + + #region Simple implementations + + private class SimpleStructuresCalculationStochastAssigner + : StructuresCalculationStochastAssigner + { + public SimpleStructuresCalculationStochastAssigner( + StructuresCalculationConfiguration configuration, + StructuresCalculation calculation, + TrySetStandardDeviationStochast setStandardDeviationStochast, + TrySetVariationCoefficientStochast setVariationCoefficientStochast) + : base(configuration, calculation, setStandardDeviationStochast, setVariationCoefficientStochast) {} + + public bool SpecificIsValid { get; set; } = true; + public IEnumerable StandardDeviationStochasts { get; set; } = Enumerable.Empty(); + public IEnumerable VariationCoefficientStochasts { get; set; } = Enumerable.Empty(); + + protected override bool ValidateSpecificStochasts() + { + return SpecificIsValid; + } + + protected override IEnumerable GetStandardDeviationStochasts(bool structureDependent = false) + { + return StandardDeviationStochasts; + } + + protected override IEnumerable GetVariationCoefficientStochasts(bool structureDependent = false) + { + return VariationCoefficientStochasts; + } + + public bool PublicValidateSpecificStochasts() + { + return ValidateSpecificStochasts(); + } + } + + private class SimpleStructuresInput : StructuresInputBase + { + protected override void UpdateStructureParameters() {} + } + + #endregion + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj =================================================================== diff -u -rfe7c27dc926b4087fd6ed9551655d0571c8018c9 -r1a0da06a973d48c83dff8ccb8b92cdc4810b83a0 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj (.../Ringtoets.Common.IO.Test.csproj) (revision fe7c27dc926b4087fd6ed9551655d0571c8018c9) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj (.../Ringtoets.Common.IO.Test.csproj) (revision 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0) @@ -68,6 +68,7 @@ + Index: Ringtoets/HeightStructures/src/Ringtoets.HeightStructures.IO/Configurations/HeightStructuresCalculationStochastAssigner.cs =================================================================== diff -u -ra585e0d396f51ec899944cd1231cba95b8a6625c -r1a0da06a973d48c83dff8ccb8b92cdc4810b83a0 --- Ringtoets/HeightStructures/src/Ringtoets.HeightStructures.IO/Configurations/HeightStructuresCalculationStochastAssigner.cs (.../HeightStructuresCalculationStochastAssigner.cs) (revision a585e0d396f51ec899944cd1231cba95b8a6625c) +++ Ringtoets/HeightStructures/src/Ringtoets.HeightStructures.IO/Configurations/HeightStructuresCalculationStochastAssigner.cs (.../HeightStructuresCalculationStochastAssigner.cs) (revision 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0) @@ -27,72 +27,67 @@ namespace Ringtoets.HeightStructures.IO.Configurations { - public class HeightStructuresCalculationStochastAssigner : StructuresCalculationStochastAssigner + /// + /// Validates and assigns stochast configurations to a height structure calculation input. + /// + public class HeightStructuresCalculationStochastAssigner + : StructuresCalculationStochastAssigner { + /// + /// Creates a new instance of + /// + /// The configuration that is used for stochast parameter source. + /// The target calculation. + /// The delegate for setting a stochast with standard deviation. + /// The delegate for setting a stochast with variation coefficient. public HeightStructuresCalculationStochastAssigner( HeightStructuresCalculationConfiguration configuration, StructuresCalculation calculation, - TrySetStandardDeviationStochast standardDeviationStochastSetter, - TrySetVariationCoefficientStochast variationCoefficientStochastSetter) - : base(configuration, calculation, standardDeviationStochastSetter, variationCoefficientStochastSetter) {} + TrySetStandardDeviationStochast setStandardDeviationStochast, + TrySetVariationCoefficientStochast setVariationCoefficientStochast) + : base(configuration, calculation, setStandardDeviationStochast, setVariationCoefficientStochast) {} protected override IEnumerable GetStandardDeviationStochasts(bool onlyStructureDependent = false) { - yield return StandardDeviationDefinition.Create( + yield return StandardDeviationDefinition.Create(Configuration.LevelCrestStructure, HeightStructuresConfigurationSchemaIdentifiers.LevelCrestStructureStochastName, - configuration.LevelCrestStructure, - i => i.LevelCrestStructure, - (i, d) => i.LevelCrestStructure = d as NormalDistribution); + i => i.LevelCrestStructure, (i, d) => i.LevelCrestStructure = d as NormalDistribution); - yield return StandardDeviationDefinition.Create( + yield return StandardDeviationDefinition.Create(Configuration.AllowedLevelIncreaseStorage, ConfigurationSchemaIdentifiers.AllowedLevelIncreaseStorageStochastName, - configuration.AllowedLevelIncreaseStorage, - i => i.AllowedLevelIncreaseStorage, - (i, d) => i.AllowedLevelIncreaseStorage = d as LogNormalDistribution); + i => i.AllowedLevelIncreaseStorage, (i, d) => i.AllowedLevelIncreaseStorage = d as LogNormalDistribution); - yield return StandardDeviationDefinition.Create( + yield return StandardDeviationDefinition.Create(Configuration.FlowWidthAtBottomProtection, ConfigurationSchemaIdentifiers.FlowWidthAtBottomProtectionStochastName, - configuration.FlowWidthAtBottomProtection, - i => i.FlowWidthAtBottomProtection, - (i, d) => i.FlowWidthAtBottomProtection = d as LogNormalDistribution); + i => i.FlowWidthAtBottomProtection, (i, d) => i.FlowWidthAtBottomProtection = d as LogNormalDistribution); - yield return StandardDeviationDefinition.Create( + yield return StandardDeviationDefinition.Create(Configuration.WidthFlowApertures, ConfigurationSchemaIdentifiers.WidthFlowAperturesStochastName, - configuration.WidthFlowApertures, - i => i.WidthFlowApertures, - (i, d) => i.WidthFlowApertures = d as NormalDistribution); + i => i.WidthFlowApertures, (i, d) => i.WidthFlowApertures = d as NormalDistribution); if (!onlyStructureDependent) { - yield return StandardDeviationDefinition.Create( + yield return StandardDeviationDefinition.Create(Configuration.ModelFactorSuperCriticalFlow, ConfigurationSchemaIdentifiers.ModelFactorSuperCriticalFlowStochastName, - configuration.ModelFactorSuperCriticalFlow, - i => i.ModelFactorSuperCriticalFlow, - (i, d) => i.ModelFactorSuperCriticalFlow = d as NormalDistribution); + i => i.ModelFactorSuperCriticalFlow, (i, d) => i.ModelFactorSuperCriticalFlow = d as NormalDistribution); } } protected override IEnumerable GetVariationCoefficientStochasts(bool onlyStructureDependent = false) { - yield return VariationCoefficientDefinition.Create( + yield return VariationCoefficientDefinition.Create(Configuration.CriticalOvertoppingDischarge, ConfigurationSchemaIdentifiers.CriticalOvertoppingDischargeStochastName, - configuration.CriticalOvertoppingDischarge, - i => i.CriticalOvertoppingDischarge, - (i, d) => i.CriticalOvertoppingDischarge = d as VariationCoefficientLogNormalDistribution); + i => i.CriticalOvertoppingDischarge, (i, d) => i.CriticalOvertoppingDischarge = d as VariationCoefficientLogNormalDistribution); - yield return VariationCoefficientDefinition.Create( + yield return VariationCoefficientDefinition.Create(Configuration.StorageStructureArea, ConfigurationSchemaIdentifiers.StorageStructureAreaStochastName, - configuration.StorageStructureArea, - i => i.StorageStructureArea, - (i, d) => i.StorageStructureArea = d as VariationCoefficientLogNormalDistribution); + i => i.StorageStructureArea, (i, d) => i.StorageStructureArea = d as VariationCoefficientLogNormalDistribution); if (!onlyStructureDependent) { - yield return VariationCoefficientDefinition.Create( + yield return VariationCoefficientDefinition.Create(Configuration.StormDuration, ConfigurationSchemaIdentifiers.StormDurationStochastName, - configuration.StormDuration, - i => i.StormDuration, - (i, d) => i.StormDuration = d as VariationCoefficientLogNormalDistribution); + i => i.StormDuration, (i, d) => i.StormDuration = d as VariationCoefficientLogNormalDistribution); } } } Fisheye: Tag 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0 refers to a dead (removed) revision in file `Ringtoets/HeightStructures/src/Ringtoets.HeightStructures.IO/Configurations/StructuresCalculationStochastAssigner.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/HeightStructures/src/Ringtoets.HeightStructures.IO/Ringtoets.HeightStructures.IO.csproj =================================================================== diff -u -ra585e0d396f51ec899944cd1231cba95b8a6625c -r1a0da06a973d48c83dff8ccb8b92cdc4810b83a0 --- Ringtoets/HeightStructures/src/Ringtoets.HeightStructures.IO/Ringtoets.HeightStructures.IO.csproj (.../Ringtoets.HeightStructures.IO.csproj) (revision a585e0d396f51ec899944cd1231cba95b8a6625c) +++ Ringtoets/HeightStructures/src/Ringtoets.HeightStructures.IO/Ringtoets.HeightStructures.IO.csproj (.../Ringtoets.HeightStructures.IO.csproj) (revision 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0) @@ -52,7 +52,6 @@ - Index: Ringtoets/HeightStructures/test/Ringtoets.HeightStructures.IO.Test/Configurations/HeightStructuresCalculationStochastAssignerTest.cs =================================================================== diff -u --- Ringtoets/HeightStructures/test/Ringtoets.HeightStructures.IO.Test/Configurations/HeightStructuresCalculationStochastAssignerTest.cs (revision 0) +++ Ringtoets/HeightStructures/test/Ringtoets.HeightStructures.IO.Test/Configurations/HeightStructuresCalculationStochastAssignerTest.cs (revision 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0) @@ -0,0 +1,63 @@ +// 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 NUnit.Framework; +using Ringtoets.Common.Data.Structures; +using Ringtoets.HeightStructures.Data; +using Ringtoets.HeightStructures.IO.Configurations; + +namespace Ringtoets.HeightStructures.IO.Test.Configurations +{ + [TestFixture] + public class HeightStructuresCalculationStochastAssignerTest + { + [Test] + public void Constructor_WithParameters_ReturnsNewInstance() + { + // Setup + var configuration = new HeightStructuresCalculationConfiguration("name"); + + var calculation = new StructuresCalculation(); + + // Call + var assigner = new HeightStructuresCalculationStochastAssigner( + configuration, + calculation, + StandardDeviationStochastSetter, + VariationCoefficientStochastSetter); + + // Assert + Assert.NotNull(assigner); + } + + private static bool StandardDeviationStochastSetter( + HeightStructuresCalculationStochastAssigner.StandardDeviationDefinition definition) + { + return true; + } + + private static bool VariationCoefficientStochastSetter( + HeightStructuresCalculationStochastAssigner.VariationCoefficientDefinition definition) + { + return true; + } + } +} \ No newline at end of file Index: Ringtoets/HeightStructures/test/Ringtoets.HeightStructures.IO.Test/Ringtoets.HeightStructures.IO.Test.csproj =================================================================== diff -u -r9a0e768e9ddbc5e4917201fdb864bb2247e5cd90 -r1a0da06a973d48c83dff8ccb8b92cdc4810b83a0 --- Ringtoets/HeightStructures/test/Ringtoets.HeightStructures.IO.Test/Ringtoets.HeightStructures.IO.Test.csproj (.../Ringtoets.HeightStructures.IO.Test.csproj) (revision 9a0e768e9ddbc5e4917201fdb864bb2247e5cd90) +++ Ringtoets/HeightStructures/test/Ringtoets.HeightStructures.IO.Test/Ringtoets.HeightStructures.IO.Test.csproj (.../Ringtoets.HeightStructures.IO.Test.csproj) (revision 1a0da06a973d48c83dff8ccb8b92cdc4810b83a0) @@ -55,6 +55,7 @@ +