public void WriteDistributionWhenAvailable_StochastConfigurationSet_WriterCalledWithExpectedParameters()
        {
            // Setup
            const string name          = "some name";
            var          configuration = new StochastConfiguration();

            var mocks     = new MockRepository();
            var xmlWriter = mocks.StrictMock <XmlWriter>();

            xmlWriter.Expect(w => w.WriteDistribution(name, configuration));
            mocks.ReplayAll();

            // Call
            ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable(
                xmlWriter,
                name,
                configuration);

            // Assert
            mocks.VerifyAll();
        }
예제 #2
0
        /// <summary>
        /// Writes a single <see cref="StochastConfiguration"/> as a stochast element in file.
        /// </summary>
        /// <param name="writer">The writer to use to write the distribution.</param>
        /// <param name="name">The name of the distribution to write.</param>
        /// <param name="distribution">The distribution to write.</param>
        /// <exception cref="ArgumentNullException">Thrown when any of the input parameters is <c>null</c>.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the <paramref name="writer"/>
        /// is closed.</exception>
        public static void WriteDistribution(this XmlWriter writer, string name, StochastConfiguration distribution)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (distribution == null)
            {
                throw new ArgumentNullException(nameof(distribution));
            }

            writer.WriteStartElement(ConfigurationSchemaIdentifiers.StochastElement);
            writer.WriteAttributeString(ConfigurationSchemaIdentifiers.NameAttribute, name);

            if (distribution.Mean.HasValue)
            {
                writer.WriteElementString(ConfigurationSchemaIdentifiers.MeanElement,
                                          XmlConvert.ToString(distribution.Mean.Value));
            }

            if (distribution.StandardDeviation.HasValue)
            {
                writer.WriteElementString(ConfigurationSchemaIdentifiers.StandardDeviationElement,
                                          XmlConvert.ToString(distribution.StandardDeviation.Value));
            }

            if (distribution.VariationCoefficient.HasValue)
            {
                writer.WriteElementString(ConfigurationSchemaIdentifiers.VariationCoefficientElement,
                                          XmlConvert.ToString(distribution.VariationCoefficient.Value));
            }

            writer.WriteEndElement();
        }
예제 #3
0
        public void ToStochastConfiguration_WithDistribution_InstanceWithExpectedParametersSet()
        {
            // Setup
            var mocks        = new MockRepository();
            var distribution = mocks.Stub <IDistribution>();

            mocks.ReplayAll();

            var           random            = new Random(21);
            RoundedDouble mean              = random.NextRoundedDouble();
            RoundedDouble standardDeviation = random.NextRoundedDouble();

            distribution.Mean = mean;
            distribution.StandardDeviation = standardDeviation;

            // Call
            StochastConfiguration configuration = distribution.ToStochastConfiguration();

            // Assert
            Assert.AreEqual(mean, configuration.Mean);
            Assert.AreEqual(standardDeviation, configuration.StandardDeviation);
        }
예제 #4
0
        public void ToStochastConfiguration_WithVariationCoefficientDistribution_InstanceWithExpectedParametersSet()
        {
            // Setup
            var mocks        = new MockRepository();
            var distribution = mocks.Stub <IVariationCoefficientDistribution>();

            mocks.ReplayAll();

            var           random = new Random(21);
            RoundedDouble mean   = random.NextRoundedDouble();
            RoundedDouble variationCoefficient = random.NextRoundedDouble();

            distribution.Mean = mean;
            distribution.CoefficientOfVariation = variationCoefficient;

            // Call
            StochastConfiguration configuration = distribution.ToStochastConfiguration();

            // Assert
            Assert.AreEqual(mean, configuration.Mean);
            Assert.AreEqual(variationCoefficient, configuration.VariationCoefficient);
        }
예제 #5
0
        /// <summary>
        /// Sets the stochast parameters.
        /// </summary>
        /// <typeparam name="TDistribution">The type of the distribution to read.</typeparam>
        /// <typeparam name="TCalculationInput">The type of the calculation input.</typeparam>
        /// <param name="stochastName">The stochast's name.</param>
        /// <param name="calculationName">The name of the calculation to configure.</param>
        /// <param name="input">The input for which to assign the read stochast.</param>
        /// <param name="stochastConfiguration">The configuration of the stochast.</param>
        /// <param name="getStochast">The function for obtaining the stochast to read.</param>
        /// <param name="setStochast">The function to set the stochast with the read parameters.</param>
        /// <param name="log">Log used to write out errors.</param>
        /// <returns><c>true</c> if reading all required stochast parameters was successful,
        /// <c>false</c> otherwise.</returns>
        public static bool TrySetStandardDeviationStochast <TDistribution, TCalculationInput>(
            string stochastName,
            string calculationName,
            TCalculationInput input,
            StochastConfiguration stochastConfiguration,
            Func <TCalculationInput, TDistribution> getStochast,
            Action <TCalculationInput, TDistribution> 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);
        }
예제 #6
0
        public void GetStochastConfiguration_StochastWithParameters_ReturnsNewStochastWithParametersSet()
        {
            // Setup
            const string stochastName      = "stochastA";
            const double mean              = 1.2;
            const double standardDeviation = 3.5;

            var stochastElement = new XElement("stochast");

            stochastElement.SetAttributeValue("naam", stochastName);
            stochastElement.Add(new XElement("verwachtingswaarde", mean));
            stochastElement.Add(new XElement("standaardafwijking", standardDeviation));
            stochastElement.Add(new XElement("variatiecoefficient", standardDeviation));
            var xElement = new XElement("root", new XElement("stochasten", stochastElement));

            // Call
            StochastConfiguration stochast = xElement.GetStochastConfiguration(stochastName);

            // Assert
            Assert.AreEqual(mean, stochast.Mean);
            Assert.AreEqual(standardDeviation, stochast.StandardDeviation);
            Assert.AreEqual(standardDeviation, stochast.VariationCoefficient);
        }
        public void TrySetStandardDeviationStochast_StochastConfigurationWithStandardDeviation_LogsErrorReturnsFalse()
        {
            // Setup
            const string stochastName    = "some stochast name";
            const string calculationName = "some calculation name";

            const string expectedFormat = "{0} Berekening '{1}' is overgeslagen.";
            string       expectedError  = $"Indien voor parameter '{stochastName}' de spreiding wordt opgegeven, moet dit door middel van een standaardafwijking. " +
                                          $"Voor berekening '{calculationName}' is een variatiecoëfficiënt gevonden.";

            var mocks = new MockRepository();
            var log   = mocks.StrictMock <ILog>();

            log.Expect(l => l.ErrorFormat(expectedFormat, expectedError, calculationName));
            mocks.ReplayAll();

            var configuration = new StochastConfiguration
            {
                VariationCoefficient = new Random(21).NextDouble()
            };

            var input = new TestInputWithStochasts();

            // Call
            bool valid = ConfigurationImportHelper.TrySetStandardDeviationStochast(
                stochastName,
                calculationName,
                input, configuration,
                i => i.Distribution,
                (i, s) => i.Distribution = s,
                log);

            // Assert
            Assert.IsFalse(valid);

            mocks.VerifyAll();
        }
 public static void PublicWriteDistributionWhenAvailable(XmlWriter writer, string distributionName, StochastConfiguration configuration)
 {
     WriteDistributionWhenAvailable(writer, distributionName, configuration);
 }
예제 #9
0
        public void WriteDistribution_WithDifferentSetParameters_WritesStochastWithSetParameters(StochastConfiguration distribution, string fileName)
        {
            // Setup
            string       filePath = TestHelper.GetScratchPadPath(nameof(WriteDistribution_WithDifferentSetParameters_WritesStochastWithSetParameters));
            const string name     = "distribution";

            try
            {
                using (XmlWriter xmlWriter = CreateXmlWriter(filePath))
                {
                    // Call
                    xmlWriter.WriteDistribution(name, distribution);
                }

                // Assert
                string actualXml   = File.ReadAllText(filePath);
                string expectedXml = GetTestFileContent(fileName);
                Assert.AreEqual(expectedXml, actualXml);
            }
            finally
            {
                File.Delete(filePath);
            }
        }
예제 #10
0
        public void SetAllStochasts_Always_CallsGettersAndSetters(
            bool setStandardDeviationStochastSuccessful,
            bool setVariationCoefficientStochastSuccessful)
        {
            // Setup
            const string calculationName = "name";
            var          mocks           = new MockRepository();
            var          configuration   = mocks.Stub <StructuresCalculationConfiguration>(calculationName);

            mocks.ReplayAll();

            configuration.StructureId = "some ID";

            var    random = new Random(21);
            double allowedLevelIncreaseStorageMean = setStandardDeviationStochastSuccessful ? random.NextDouble() : -1;
            double allowedLevelIncreaseStorageStandardDeviation       = random.NextDouble();
            double criticalOvertoppingDischargeMean                   = setVariationCoefficientStochastSuccessful ? random.NextDouble() : -1;
            double criticalOvertoppingDischargeCoefficientOfVariation = random.NextDouble();

            var calculation = new StructuresCalculation <SimpleStructuresInput>();

            var standardDeviationStochastConfiguration = new StochastConfiguration
            {
                Mean = allowedLevelIncreaseStorageMean,
                StandardDeviation = allowedLevelIncreaseStorageStandardDeviation
            };
            var variationCoefficientStochastConfiguration = new StochastConfiguration
            {
                Mean = criticalOvertoppingDischargeMean,
                VariationCoefficient = criticalOvertoppingDischargeCoefficientOfVariation
            };

            var definitionA =
                new StructuresCalculationStochastAssigner <StructuresCalculationConfiguration, SimpleStructuresInput, StructureBase> .StandardDeviationDefinition("stochastA",
                                                                                                                                                                  standardDeviationStochastConfiguration,
                                                                                                                                                                  input => input.AllowedLevelIncreaseStorage,
                                                                                                                                                                  (input, distribution) =>
            {
                input.AllowedLevelIncreaseStorage = (LogNormalDistribution)distribution;
            });

            var definitionB =
                new StructuresCalculationStochastAssigner <StructuresCalculationConfiguration, SimpleStructuresInput, StructureBase> .VariationCoefficientDefinition("stochastB",
                                                                                                                                                                     variationCoefficientStochastConfiguration,
                                                                                                                                                                     input => input.CriticalOvertoppingDischarge,
                                                                                                                                                                     (input, distribution) =>
            {
                input.CriticalOvertoppingDischarge = (VariationCoefficientLogNormalDistribution)distribution;
            });

            var assigner = new SimpleStructuresCalculationStochastAssigner(
                configuration,
                calculation)
            {
                StandardDeviationStochasts = new[]
                {
                    definitionA
                },
                VariationCoefficientStochasts = new[]
                {
                    definitionB
                }
            };

            // Call
            bool valid = assigner.Assign();

            // Assert
            Assert.AreEqual(setStandardDeviationStochastSuccessful && setVariationCoefficientStochastSuccessful, valid);
            if (valid)
            {
                Assert.AreEqual(allowedLevelIncreaseStorageMean,
                                calculation.InputParameters.AllowedLevelIncreaseStorage.Mean,
                                calculation.InputParameters.AllowedLevelIncreaseStorage.Mean.GetAccuracy());
                Assert.AreEqual(allowedLevelIncreaseStorageStandardDeviation,
                                calculation.InputParameters.AllowedLevelIncreaseStorage.StandardDeviation,
                                calculation.InputParameters.AllowedLevelIncreaseStorage.StandardDeviation.GetAccuracy());
                Assert.AreEqual(criticalOvertoppingDischargeMean,
                                calculation.InputParameters.CriticalOvertoppingDischarge.Mean,
                                calculation.InputParameters.CriticalOvertoppingDischarge.Mean.GetAccuracy());
                Assert.AreEqual(criticalOvertoppingDischargeCoefficientOfVariation,
                                calculation.InputParameters.CriticalOvertoppingDischarge.CoefficientOfVariation,
                                calculation.InputParameters.CriticalOvertoppingDischarge.CoefficientOfVariation.GetAccuracy());
            }

            mocks.VerifyAll();
        }
예제 #11
0
        /// <summary>
        /// Writes a distribution configuration when it has a value.
        /// </summary>
        /// <param name="writer">The writer to use for writing.</param>
        /// <param name="distributionName">The name of the distribution.</param>
        /// <param name="configuration">The configuration for the distribution that can be <c>null</c>.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="writer"/> or <paramref name="distributionName"/>
        /// is <c>null</c>.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the <paramref name="writer"/>
        /// is closed.</exception>
        protected static void WriteDistributionWhenAvailable(XmlWriter writer, string distributionName, StochastConfiguration configuration)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (distributionName == null)
            {
                throw new ArgumentNullException(nameof(distributionName));
            }

            if (configuration != null)
            {
                writer.WriteDistribution(distributionName, configuration);
            }
        }