Index: Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilModel.cs =================================================================== diff -u -r11f0867b39150ae5fac83dc178a89fee46d27611 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilModel.cs (.../StochasticSoilModel.cs) (revision 11f0867b39150ae5fac83dc178a89fee46d27611) +++ Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilModel.cs (.../StochasticSoilModel.cs) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -19,8 +19,11 @@ // 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.Base.Geometry; +using Ringtoets.Piping.Primitives; namespace Ringtoets.Piping.Data { @@ -70,6 +73,50 @@ /// public List StochasticSoilProfiles { get; private set; } + /// + /// Updates the with the properties + /// from . + /// + /// The to + /// obtain the property values from. + /// Thrown when + /// is null. + /// Thrown when + /// contains multiple with the same + /// , and also contains a + /// with the same name. + /// + public void Update(StochasticSoilModel fromModel) + { + if (fromModel == null) + { + throw new ArgumentNullException(nameof(fromModel)); + } + + Name = fromModel.Name; + SegmentName = fromModel.SegmentName; + Geometry.Clear(); + foreach (var point in fromModel.Geometry) + { + Geometry.Add(point); + } + List newNames = new List(); + foreach (var fromProfile in fromModel.StochasticSoilProfiles) + { + var sameProfile = StochasticSoilProfiles.SingleOrDefault(sp => sp.SoilProfile.Name.Equals(fromProfile.SoilProfile.Name)); + if (sameProfile != null) + { + sameProfile.Update(fromProfile); + } + else + { + StochasticSoilProfiles.Add(fromProfile); + } + newNames.Add(fromProfile.SoilProfile.Name); + } + StochasticSoilProfiles.RemoveAll(sp => !newNames.Contains(sp.SoilProfile.Name)); + } + public override string ToString() { return Name; Index: Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilProfile.cs =================================================================== diff -u -r11f0867b39150ae5fac83dc178a89fee46d27611 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilProfile.cs (.../StochasticSoilProfile.cs) (revision 11f0867b39150ae5fac83dc178a89fee46d27611) +++ Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilProfile.cs (.../StochasticSoilProfile.cs) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using Ringtoets.Piping.Primitives; namespace Ringtoets.Piping.Data @@ -61,6 +62,24 @@ /// public PipingSoilProfile SoilProfile { get; set; } + /// + /// Updates the with the properties + /// from . + /// + /// The to + /// obtain the property values from. + /// Thrown when + /// is null. + public void Update(StochasticSoilProfile fromProfile) + { + if (fromProfile == null) + { + throw new ArgumentNullException(nameof(fromProfile)); + } + SoilProfile = fromProfile.SoilProfile; + Probability = fromProfile.Probability; + } + public override string ToString() { return SoilProfile == null ? string.Empty : SoilProfile.ToString(); Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/IStochasticSoilModelUpdateStrategy.cs =================================================================== diff -u -rbbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/IStochasticSoilModelUpdateStrategy.cs (.../IStochasticSoilModelUpdateStrategy.cs) (revision bbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/IStochasticSoilModelUpdateStrategy.cs (.../IStochasticSoilModelUpdateStrategy.cs) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -40,7 +40,7 @@ /// is added. /// An action to be used to notify progress changes. void UpdateModelWithImportedData( - ICollection readStochasticSoilModels, + IEnumerable readStochasticSoilModels, string sourceFilePath, StochasticSoilModelCollection targetCollection, Action notifyProgress); Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelImporter.cs =================================================================== diff -u -rbbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelImporter.cs (.../StochasticSoilModelImporter.cs) (revision bbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelImporter.cs (.../StochasticSoilModelImporter.cs) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -32,6 +32,7 @@ using Ringtoets.Piping.IO.SoilProfile; using Ringtoets.Piping.Plugin.Properties; using Ringtoets.Piping.Primitives; +using RingtoestCommonIOResources = Ringtoets.Common.IO.Properties.Resources; namespace Ringtoets.Piping.Plugin.FileImporter { @@ -83,10 +84,67 @@ return false; } - modelUpdateStrategy.UpdateModelWithImportedData(importStochasticSoilModelResult.ImportedItems, FilePath, ImportTarget, NotifyProgress); + modelUpdateStrategy.UpdateModelWithImportedData( + GetValidStochasticSoilModels(importStochasticSoilModelResult), + FilePath, + ImportTarget, + NotifyProgress); + return true; } + private IEnumerable GetValidStochasticSoilModels(ReadResult importStochasticSoilModelResult) + { + var currentStep = 1; + var importedModels = importStochasticSoilModelResult.ImportedItems.ToArray(); + foreach (var importedModel in importedModels) + { + NotifyProgress(RingtoestCommonIOResources.Importer_ProgressText_Adding_imported_data_to_DataModel, currentStep, importedModels.Length); + if (ValidateStochasticSoilModel(importedModel)) + { + yield return importedModel; + } + currentStep++; + } + } + + /// + /// Validate the definition of a . + /// + /// The to validate. + /// false when the stochastic soil model does not contain any stochastic soil profiles + /// or when a stochastic soil profile does not have a definition for a soil profile; true + /// otherwise. + protected bool ValidateStochasticSoilModel(StochasticSoilModel stochasticSoilModel) + { + if (!stochasticSoilModel.StochasticSoilProfiles.Any()) + { + log.WarnFormat(Resources.PipingSoilProfilesImporter_ValidateStochasticSoilModel_No_profiles_found_in_stochastic_soil_model_0, + stochasticSoilModel.Name); + return false; + } + if (stochasticSoilModel.StochasticSoilProfiles.Any(ssp => ssp.SoilProfile == null)) + { + log.WarnFormat(Resources.PipingSoilProfilesImporter_ValidateStochasticSoilModel_SoilModel_0_with_stochastic_soil_profile_without_profile, + stochasticSoilModel.Name); + return false; + } + if (!IsSumOfAllProbabilitiesEqualToOne(stochasticSoilModel)) + { + log.WarnFormat(Resources.PipingSoilProfilesImporter_ValidateStochasticSoilModel_Sum_of_probabilities_of_stochastic_soil_model_0_is_not_correct, + stochasticSoilModel.Name); + } + return true; + } + + private static bool IsSumOfAllProbabilitiesEqualToOne(StochasticSoilModel stochasticSoilModel) + { + double sumOfAllScenarioProbabilities = stochasticSoilModel.StochasticSoilProfiles + .Where(s => s.SoilProfile != null) + .Sum(s => s.Probability); + return Math.Abs(sumOfAllScenarioProbabilities - 1.0) < 1e-6; + } + protected override void LogImportCanceledMessage() { log.Info(Resources.PipingSoilProfilesImporter_Import_Import_canceled); @@ -123,7 +181,7 @@ private void HandleException(Exception e) { - var message = string.Format(Resources.PipingSoilProfilesImporter_CriticalErrorMessage_0_File_Skipped, + var message = String.Format(Resources.PipingSoilProfilesImporter_CriticalErrorMessage_0_File_Skipped, e.Message); log.Error(message); } @@ -166,7 +224,7 @@ } catch (StochasticSoilProfileReadException e) { - var message = string.Format(Resources.PipingSoilProfilesImporter_GetStochasticSoilModelReadResult_Error_0_stochastic_soil_model_skipped, e.Message); + var message = String.Format(Resources.PipingSoilProfilesImporter_GetStochasticSoilModelReadResult_Error_0_stochastic_soil_model_skipped, e.Message); log.Error(message); } } Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelReplaceData.cs =================================================================== diff -u -rbbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelReplaceData.cs (.../StochasticSoilModelReplaceData.cs) (revision bbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelReplaceData.cs (.../StochasticSoilModelReplaceData.cs) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -23,16 +23,17 @@ using System.Collections.Generic; using System.Linq; using log4net; -using Ringtoets.Common.IO.Properties; using Ringtoets.Piping.Data; namespace Ringtoets.Piping.Plugin.FileImporter { /// /// Strategy for replacing the stochastic soil models with the imported stochastic soil models. /// - public class StochasticSoilModelReplaceData : StochasticSoilModelUpdateStrategyBase + public class StochasticSoilModelReplaceData : IStochasticSoilModelUpdateStrategy { + private readonly ILog log = LogManager.GetLogger(typeof(StochasticSoilModelReplaceData)); + /// /// Adds the to the . /// @@ -41,24 +42,15 @@ /// were imported. /// The current collection of . /// An action to be used to notify progress changes. - public override void UpdateModelWithImportedData( - ICollection readStochasticSoilModels, + public void UpdateModelWithImportedData( + IEnumerable readStochasticSoilModels, string sourceFilePath, StochasticSoilModelCollection targetCollection, Action notifyProgress) { - var log = LogManager.GetLogger(typeof(StochasticSoilModelImporter)); - - var stochasticSoilModelCount = readStochasticSoilModels.Count; - var currentIndex = 1; - var modelsToAdd = new List(readStochasticSoilModels.Count); + var modelsToAdd = new List(); foreach (StochasticSoilModel readStochasticSoilModel in readStochasticSoilModels) { - notifyProgress(Resources.Importer_ProgressText_Adding_imported_data_to_DataModel, currentIndex++, stochasticSoilModelCount); - if (!ValidateStochasticSoilModel(readStochasticSoilModel)) - { - continue; - } var stochasticSoilModel = targetCollection.FirstOrDefault(ssm => ssm.Id == readStochasticSoilModel.Id); if (stochasticSoilModel != null) { Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelUpdateData.cs =================================================================== diff -u -rbbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelUpdateData.cs (.../StochasticSoilModelUpdateData.cs) (revision bbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelUpdateData.cs (.../StochasticSoilModelUpdateData.cs) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -31,8 +31,10 @@ /// /// Strategy for updating the current stochastic soil models with the imported stochastic soil models. /// - public class StochasticSoilModelUpdateData : StochasticSoilModelUpdateStrategyBase + public class StochasticSoilModelUpdateData : IStochasticSoilModelUpdateStrategy { + private readonly ILog log = LogManager.GetLogger(typeof(StochasticSoilModelUpdateData)); + /// /// Updates the . /// Updates stochastic soil models in that are part of @@ -47,32 +49,34 @@ /// were imported. /// The current collection of . /// An action to be used to notify progress changes. - public override void UpdateModelWithImportedData( - ICollection readStochasticSoilModels, + /// Thrown when + /// contains multiple with the same , + /// and also contains a with + /// the same name. + /// + public void UpdateModelWithImportedData( + IEnumerable readStochasticSoilModels, string sourceFilePath, StochasticSoilModelCollection targetCollection, Action notifyProgress) { - var log = LogManager.GetLogger(typeof(StochasticSoilModelImporter)); + var updatedModels = new List(); - var stochasticSoilModelCount = readStochasticSoilModels.Count; - var currentIndex = 1; - var modelsToAdd = new List(readStochasticSoilModels.Count); - foreach (StochasticSoilModel readStochasticSoilModel in readStochasticSoilModels) + foreach (var readModel in readStochasticSoilModels) { - notifyProgress(Resources.Importer_ProgressText_Adding_imported_data_to_DataModel, currentIndex++, stochasticSoilModelCount); - if (!ValidateStochasticSoilModel(readStochasticSoilModel)) + var existingModel = targetCollection.SingleOrDefault(existing => existing.Name.Equals(readModel.Name)); + if (existingModel != null) { - continue; + existingModel.Update(readModel); + updatedModels.Add(existingModel); } - var stochasticSoilModel = targetCollection.FirstOrDefault(ssm => ssm.Id == readStochasticSoilModel.Id); - if (stochasticSoilModel != null) + else { - log.WarnFormat(Properties.Resources.PipingSoilProfilesImporter_AddImportedDataToModel_Stochastisch_soil_model_0_already_exists, stochasticSoilModel.Name); + updatedModels.Add(readModel); } - modelsToAdd.Add(readStochasticSoilModel); } - targetCollection.AddRange(modelsToAdd, sourceFilePath); + targetCollection.Clear(); + targetCollection.AddRange(updatedModels, sourceFilePath); } } } \ No newline at end of file Fisheye: Tag 5e70f173b3839314912e086b6c1c784b975ee646 refers to a dead (removed) revision in file `Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelUpdateStrategyBase.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/Ringtoets.Piping.Plugin.csproj =================================================================== diff -u -rbbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/Ringtoets.Piping.Plugin.csproj (.../Ringtoets.Piping.Plugin.csproj) (revision bbca25e660e1ebf3d4dce4e9ce6bbd07bf7a12e1) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/Ringtoets.Piping.Plugin.csproj (.../Ringtoets.Piping.Plugin.csproj) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -65,7 +65,6 @@ - True Index: Ringtoets/Piping/test/Ringtoets.Piping.Data.Test/StochasticSoilModelTest.cs =================================================================== diff -u -rcda9bb0707f49cfb8e685d3ec04da01240c73f26 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/test/Ringtoets.Piping.Data.Test/StochasticSoilModelTest.cs (.../StochasticSoilModelTest.cs) (revision cda9bb0707f49cfb8e685d3ec04da01240c73f26) +++ Ringtoets/Piping/test/Ringtoets.Piping.Data.Test/StochasticSoilModelTest.cs (.../StochasticSoilModelTest.cs) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -19,9 +19,12 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using Core.Common.Base.Geometry; using NUnit.Framework; using Rhino.Mocks; +using Ringtoets.Piping.KernelWrapper.TestUtil; +using Ringtoets.Piping.Primitives; namespace Ringtoets.Piping.Data.Test { @@ -92,6 +95,75 @@ } [Test] + public void Update_WithNullModel_ThrowsArgumentNullException() + { + // Setup + var model = new StochasticSoilModel(1234, "name", "segment"); + + // Call + TestDelegate test = () => model.Update(null); + + // Assert + string paramName = Assert.Throws(test).ParamName; + Assert.AreEqual("fromModel", paramName); + } + + [Test] + public void Update_WithOtherModel_PropertiesUpdated() + { + // Setup + var equalProfileName = "nameA"; + var model = new StochasticSoilModel(1234, "name", "segment"); + model.Geometry.Add(new Point2D(6, 2)); + model.Geometry.Add(new Point2D(4, 3)); + + var stochasticProfileA = new StochasticSoilProfile(0.5, SoilProfileType.SoilProfile1D, -11) + { + SoilProfile = new TestPipingSoilProfile(equalProfileName) + }; + var stochasticProfileB = new StochasticSoilProfile(0.5, SoilProfileType.SoilProfile1D, -52) + { + SoilProfile = new TestPipingSoilProfile("nameB") + }; + model.StochasticSoilProfiles.Add(stochasticProfileA); + model.StochasticSoilProfiles.Add(stochasticProfileB); + + var otherName = "other name"; + var otherSegmentName = "other segment"; + var otherModel = new StochasticSoilModel(41, otherName, otherSegmentName); + + var otherPointA = new Point2D(2,0); + var otherPointB = new Point2D(3,0); + otherModel.Geometry.Add(otherPointA); + otherModel.Geometry.Add(otherPointB); + + var otherStochasticProfileA = new StochasticSoilProfile(0.7, SoilProfileType.SoilProfile1D, -1) + { + SoilProfile = new TestPipingSoilProfile(equalProfileName) + }; + var otherStochasticProfileB = new StochasticSoilProfile(0.3, SoilProfileType.SoilProfile1D, -2) + { + SoilProfile = new TestPipingSoilProfile("other profile name") + }; + otherModel.StochasticSoilProfiles.Add(otherStochasticProfileA); + otherModel.StochasticSoilProfiles.Add(otherStochasticProfileB); + + // Call + model.Update(otherModel); + + // Assert + Assert.AreEqual(otherName, model.Name); + Assert.AreEqual(otherSegmentName, model.SegmentName); + Assert.AreSame(otherPointA, model.Geometry[0]); + Assert.AreSame(otherPointB, model.Geometry[1]); + Assert.AreEqual(2, model.StochasticSoilProfiles.Count); + Assert.AreSame(stochasticProfileA, model.StochasticSoilProfiles[0]); + Assert.AreSame(otherStochasticProfileA.SoilProfile, model.StochasticSoilProfiles[0].SoilProfile); + Assert.AreNotSame(stochasticProfileB, model.StochasticSoilProfiles[1]); + Assert.AreSame(otherStochasticProfileB.SoilProfile, model.StochasticSoilProfiles[1].SoilProfile); + } + + [Test] [TestCase(null)] [TestCase("")] [TestCase("some name")] Index: Ringtoets/Piping/test/Ringtoets.Piping.Data.Test/StochasticSoilProfileTest.cs =================================================================== diff -u -r8832cfacbfb0a999d9dd5ddcb93fd81bdb2fb09f -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/test/Ringtoets.Piping.Data.Test/StochasticSoilProfileTest.cs (.../StochasticSoilProfileTest.cs) (revision 8832cfacbfb0a999d9dd5ddcb93fd81bdb2fb09f) +++ Ringtoets/Piping/test/Ringtoets.Piping.Data.Test/StochasticSoilProfileTest.cs (.../StochasticSoilProfileTest.cs) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -19,7 +19,9 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using NUnit.Framework; +using Ringtoets.Piping.KernelWrapper.TestUtil; using Ringtoets.Piping.Primitives; namespace Ringtoets.Piping.Data.Test @@ -43,6 +45,41 @@ } [Test] + public void Update_WithNullProfile_ThrowsArgumentNullException() + { + // Setup + var stochasticProfile = new StochasticSoilProfile(0.0, SoilProfileType.SoilProfile1D, 0); + + // Call + TestDelegate test = () => stochasticProfile.Update(null); + + // Assert + string paramName = Assert.Throws(test).ParamName; + Assert.AreEqual("fromProfile", paramName); + } + + [Test] + public void Update_WithValidProfile_UpdatesProperties() + { + // Setup + var newProbability = 1.0; + var newProfile = new TestPipingSoilProfile(); + var otherStochasticProfile = new StochasticSoilProfile(newProbability, SoilProfileType.SoilProfile1D, 0) + { + SoilProfile = newProfile + }; + + var stochasticProfile = new StochasticSoilProfile(0.0, SoilProfileType.SoilProfile1D, 0); + + // Call + stochasticProfile.Update(otherStochasticProfile); + + // Assert + Assert.AreEqual(newProbability, stochasticProfile.Probability); + Assert.AreSame(newProfile, stochasticProfile.SoilProfile); + } + + [Test] [TestCase(null)] [TestCase("")] [TestCase("some name")] Index: Ringtoets/Piping/test/Ringtoets.Piping.Data.TestUtil.Test/Ringtoets.Piping.Data.TestUtil.Test.csproj =================================================================== diff -u -r026cc14e6eb321e7e3e1c16d2e278cab74602339 -r5e70f173b3839314912e086b6c1c784b975ee646 --- Ringtoets/Piping/test/Ringtoets.Piping.Data.TestUtil.Test/Ringtoets.Piping.Data.TestUtil.Test.csproj (.../Ringtoets.Piping.Data.TestUtil.Test.csproj) (revision 026cc14e6eb321e7e3e1c16d2e278cab74602339) +++ Ringtoets/Piping/test/Ringtoets.Piping.Data.TestUtil.Test/Ringtoets.Piping.Data.TestUtil.Test.csproj (.../Ringtoets.Piping.Data.TestUtil.Test.csproj) (revision 5e70f173b3839314912e086b6c1c784b975ee646) @@ -53,6 +53,7 @@ + @@ -74,6 +75,10 @@ {ce994cc9-6f6a-48ac-b4be-02c30a21f4db} Ringtoets.Piping.Data + + {14c6f716-64e2-4bc4-a1ef-05865fcefa4c} + Ringtoets.Piping.Primitives + {10B8D63D-87E8-46DF-ACA9-A8CF22EE8FB5} Ringtoets.Piping.Service @@ -82,6 +87,10 @@ {955E574D-67CE-4347-AA6B-7DF8A04ED754} Ringtoets.Piping.Data.TestUtil + + {27E0A5C9-3ABF-426A-A3DA-7D0B83A218C8} + Ringtoets.Piping.KernelWrapper.TestUtil +