Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/IO/HydraulicModelSettingsCsvReader.cs =================================================================== diff -u --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/IO/HydraulicModelSettingsCsvReader.cs (revision 0) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/IO/HydraulicModelSettingsCsvReader.cs (revision 1a0ff933f6b5e06d3f7376652c88c068f9610a92) @@ -0,0 +1,154 @@ +// 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 Ringtoets.HydraRing.Calculation.Data; + +namespace Ringtoets.HydraRing.Calculation.IO +{ + /// + /// The reader for the in csv format. + /// + internal class HydraulicModelSettingsCsvReader + { + private const char separator = ';'; + + private readonly string fileContents; + + private readonly IDictionary>> settings = new Dictionary>>(); + + private readonly Dictionary columns = new Dictionary + { + { + ringIdKey, 0 + }, + { + mechanismIdKey, 1 + }, + { + subMechanismIdKey, 2 + }, + { + timeIntegrationKey, 3 + } + }; + + /// + /// Reads the settings from the file. + /// + /// A with the settings. + public HydraulicModelSettingsCsvReader(string file) + { + if (file == null) + { + throw new ArgumentNullException("file", @"A file must be set."); + } + + fileContents = file; + } + + /// + /// Reads the settings from the file. + /// + /// A with the settings. + public IDictionary>> ReadSettings() + { + string[] lines = fileContents.Split('\n'); + + foreach (string line in lines.Skip(1).Where(s => !string.IsNullOrEmpty(s))) + { + CreateSetting(TokenizeString(line)); + } + + return settings; + } + + private void CreateSetting(IList line) + { + // Get failure mechanism + int failureMechanismType = GetFailureMechanismType(line); + + if (!settings.ContainsKey(failureMechanismType)) + { + settings.Add(failureMechanismType, new Dictionary>()); + } + + // Get sub mechanism + int subMechanism = GetSubMechanismType(line); + + if (!settings[failureMechanismType].ContainsKey(subMechanism)) + { + settings[failureMechanismType].Add(subMechanism, new Dictionary()); + } + + // Get TrajectId + string ringId = GetRingId(line); + + if (!settings[failureMechanismType][subMechanism].ContainsKey(ringId)) + { + settings[failureMechanismType][subMechanism].Add(ringId, (HydraRingTimeIntegrationSchemeType) GetIntValueFromElement(line[columns[timeIntegrationKey]])); + } + } + + private int GetFailureMechanismType(IList line) + { + return GetIntValueFromElement(line[columns[mechanismIdKey]]); + } + + private int GetSubMechanismType(IList line) + { + return GetIntValueFromElement(line[columns[subMechanismIdKey]]); + } + + private string GetRingId(IList line) + { + return line[columns[ringIdKey]].Trim().Replace("\"", ""); + } + + private static int GetIntValueFromElement(string element) + { + return int.Parse(element.Trim()); + } + + private string[] TokenizeString(string readText) + { + if (!readText.Contains(separator)) + { + return new string[] + {}; + } + return readText.Split(separator) + .TakeWhile(text => !string.IsNullOrEmpty(text)) + .ToArray(); + } + + #region Csv column names + + private const string ringIdKey = "TrajectID"; + private const string mechanismIdKey = "MechanismID"; + private const string subMechanismIdKey = "SubMechanismID"; + private const string timeIntegrationKey = "TimeIntegration"; + + #endregion + } +} \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Ringtoets.HydraRing.Calculation.csproj =================================================================== diff -u -r557c60f6aadb50bcb07503a0f5a1ea34ca6a3885 -r1a0ff933f6b5e06d3f7376652c88c068f9610a92 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Ringtoets.HydraRing.Calculation.csproj (.../Ringtoets.HydraRing.Calculation.csproj) (revision 557c60f6aadb50bcb07503a0f5a1ea34ca6a3885) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Ringtoets.HydraRing.Calculation.csproj (.../Ringtoets.HydraRing.Calculation.csproj) (revision 1a0ff933f6b5e06d3f7376652c88c068f9610a92) @@ -68,6 +68,7 @@ + Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/HydraulicModelSettingsCsvReaderTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/HydraulicModelSettingsCsvReaderTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/HydraulicModelSettingsCsvReaderTest.cs (revision 1a0ff933f6b5e06d3f7376652c88c068f9610a92) @@ -0,0 +1,140 @@ +// 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.IO; +using Core.Common.TestUtil; +using NUnit.Framework; +using Ringtoets.HydraRing.Calculation.Data; +using Ringtoets.HydraRing.Calculation.IO; + +namespace Ringtoets.HydraRing.Calculation.Test.IO +{ + [TestFixture] + public class HydraulicModelSettingsCsvReaderTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.HydraRing.Calculation, "Settings"); + + [Test] + public void Constructor_PathSet_DoesNotThrowArgumentNullException() + { + // Call + TestDelegate call = () => new HydraulicModelSettingsCsvReader("path.csv"); + + // Assert + Assert.DoesNotThrow(call); + } + + [Test] + public void Constructor_PathNotSet_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => new HydraulicModelSettingsCsvReader(null); + + // Assert + const string expectedMessage = "A file must be set."; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); + } + + [Test] + public void ReadSettings_ValidFile_ReturnsSettings() + { + // Setup + string testFile = Path.Combine(testDataPath, "HydraulicModelSettingsTest.csv"); + + using (StreamReader streamReader = new StreamReader(testFile)) + { + string fileContents = streamReader.ReadToEnd(); + + HydraulicModelSettingsCsvReader reader = new HydraulicModelSettingsCsvReader(fileContents); + IDictionary>> expectedDictionary = GetDictionary(); + + // Call + IDictionary>> settings = reader.ReadSettings(); + + // Assert + Assert.AreEqual(2, settings.Count); + + foreach (KeyValuePair>> expectedMechanism in expectedDictionary) + { + Assert.IsTrue(settings.ContainsKey(expectedMechanism.Key)); + Assert.IsInstanceOf>>(settings[expectedMechanism.Key]); + + foreach (KeyValuePair> expectedSubMechanism in expectedMechanism.Value) + { + Assert.IsTrue(settings[expectedMechanism.Key].ContainsKey(expectedSubMechanism.Key)); + Assert.IsInstanceOf>(settings[expectedMechanism.Key][expectedSubMechanism.Key]); + + foreach (KeyValuePair expectedHydraRingTimeIntegrationSchemeType in expectedSubMechanism.Value) + { + Assert.IsTrue(settings[expectedMechanism.Key][expectedSubMechanism.Key].ContainsKey(expectedHydraRingTimeIntegrationSchemeType.Key)); + Assert.IsInstanceOf(settings[expectedMechanism.Key][expectedSubMechanism.Key][expectedHydraRingTimeIntegrationSchemeType.Key]); + + HydraRingTimeIntegrationSchemeType setting = settings[expectedMechanism.Key][expectedSubMechanism.Key][expectedHydraRingTimeIntegrationSchemeType.Key]; + + Assert.AreEqual(expectedHydraRingTimeIntegrationSchemeType.Value, setting); + } + } + } + } + } + + private IDictionary>> GetDictionary() + { + return new Dictionary>> + { + { + 1, new Dictionary> + { + { + 1, new Dictionary + { + { + "205", HydraRingTimeIntegrationSchemeType.FerryBorgesCastanheta + }, + { + "11-1", HydraRingTimeIntegrationSchemeType.FerryBorgesCastanheta + } + } + } + } + }, + { + 11, new Dictionary> + { + { + 11, new Dictionary + { + { + "205", HydraRingTimeIntegrationSchemeType.FerryBorgesCastanheta + }, + { + "11-1", HydraRingTimeIntegrationSchemeType.NumericalTimeIntegration + } + } + } + } + } + }; + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/NumericSettingsCsvReaderTest.cs =================================================================== diff -u -r67aa825f436900190ff7324c7ad0d30338dda6ff -r1a0ff933f6b5e06d3f7376652c88c068f9610a92 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/NumericSettingsCsvReaderTest.cs (.../NumericSettingsCsvReaderTest.cs) (revision 67aa825f436900190ff7324c7ad0d30338dda6ff) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/NumericSettingsCsvReaderTest.cs (.../NumericSettingsCsvReaderTest.cs) (revision 1a0ff933f6b5e06d3f7376652c88c068f9610a92) @@ -59,20 +59,19 @@ public void ReadSettings_ValidFile_ReturnsSettings() { // Setup - var testFile = Path.Combine(testDataPath, "NumericsSettingsTest.csv"); + string testFile = Path.Combine(testDataPath, "NumericsSettingsTest.csv"); - using (var streamReader = new StreamReader(testFile)) + using (StreamReader streamReader = new StreamReader(testFile)) { - var fileContents = streamReader.ReadToEnd(); + string fileContents = streamReader.ReadToEnd(); - var reader = new NumericsSettingsCsvReader(fileContents); - var expectedDictionary = GetDictionary(); + NumericsSettingsCsvReader reader = new NumericsSettingsCsvReader(fileContents); + IDictionary>> expectedDictionary = GetDictionary(); // Call - var settings = reader.ReadSettings(); + IDictionary>> settings = reader.ReadSettings(); // Assert - Assert.IsInstanceOf>>>(settings); Assert.AreEqual(2, settings.Count); foreach (KeyValuePair>> expectedMechanism in expectedDictionary) Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Ringtoets.HydraRing.Calculation.Test.csproj =================================================================== diff -u -r5c026d00112581020f7547e0b956406abc9c56b8 -r1a0ff933f6b5e06d3f7376652c88c068f9610a92 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Ringtoets.HydraRing.Calculation.Test.csproj (.../Ringtoets.HydraRing.Calculation.Test.csproj) (revision 5c026d00112581020f7547e0b956406abc9c56b8) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Ringtoets.HydraRing.Calculation.Test.csproj (.../Ringtoets.HydraRing.Calculation.Test.csproj) (revision 1a0ff933f6b5e06d3f7376652c88c068f9610a92) @@ -75,6 +75,7 @@ + Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/test-data/Settings/HydraulicModelSettingsTest.csv =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/test-data/Settings/HydraulicModelSettingsTest.csv (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/test-data/Settings/HydraulicModelSettingsTest.csv (revision 1a0ff933f6b5e06d3f7376652c88c068f9610a92) @@ -0,0 +1,5 @@ +TrajectID;MechanismID;SubMechanism;TimeIntegration +205;1;1;1 +205;11;11;1 +11-1;1;1;1 +11-1;11;11;3