// 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.Collections.Specialized;
using System.Globalization;
using System.Linq;
using Ringtoets.HydraRing.Data;
namespace Ringtoets.HydraRing.Calculation
{
///
/// Container for all configurations that are necessary for performing a Hydra-Ring calculation.
///
public class HydraRingConfiguration
{
private readonly IEnumerable configurationSettings;
///
/// Creates a new instance of the class.
///
public HydraRingConfiguration()
{
configurationSettings = new[]
{
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.AssessmentLevel,
CalculationTypeId = 1,
VariableId = 26,
SubMechanismId = 1,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.WaveHeight,
CalculationTypeId = 1,
VariableId = 28,
SubMechanismId = 11,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.WavePeakPeriod,
CalculationTypeId = 1,
VariableId = 29,
SubMechanismId = 14,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.WaveSpectralPeriod,
CalculationTypeId = 1,
VariableId = 29,
SubMechanismId = 16,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.QVariant,
CalculationTypeId = 6,
VariableId = 114,
SubMechanismId = 3,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.QVariant,
CalculationTypeId = 6,
VariableId = 114,
SubMechanismId = 4,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.QVariant,
CalculationTypeId = 6,
VariableId = 114,
SubMechanismId = 5,
CalculationTechniqueId = 4
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.DikesOvertopping,
CalculationTypeId = 1,
VariableId = 1,
SubMechanismId = 102,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.DikesOvertopping,
CalculationTypeId = 1,
VariableId = 1,
SubMechanismId = 103,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.DikesPiping,
CalculationTypeId = 1,
VariableId = 44,
SubMechanismId = 311,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.DikesPiping,
CalculationTypeId = 1,
VariableId = 44,
SubMechanismId = 313,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.DikesPiping,
CalculationTypeId = 1,
VariableId = 44,
SubMechanismId = 314,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresOvertopping,
CalculationTypeId = 1,
VariableId = 60,
SubMechanismId = 421,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresOvertopping,
CalculationTypeId = 1,
VariableId = 60,
SubMechanismId = 422,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresOvertopping,
CalculationTypeId = 1,
VariableId = 60,
SubMechanismId = 423,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresClosure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 422,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresClosure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 424,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresClosure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 425,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresClosure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 426,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresClosure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 427,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 422,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 424,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 425,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 430,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 431,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 432,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 433,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 434,
CalculationTechniqueId = 1
},
new HydraRingConfigurationSettings
{
HydraRingFailureMechanismType = HydraRingFailureMechanismType.StructuresStructuralFailure,
CalculationTypeId = 1,
VariableId = 65,
SubMechanismId = 435,
CalculationTechniqueId = 1
}
};
}
///
/// Gets or sets the .
///
public HydraRingTimeIntegrationSchemeType? TimeIntegrationSchemeType { get; set; }
///
/// Gets or sets the .
///
public HydraRingUncertaintiesType? UncertaintiesType { get; set; }
///
/// Gets or sets the .
///
public HydraulicBoundaryLocation HydraulicBoundaryLocation { get; set; }
///
/// Gets or sets the .
///
public HydraRingFailureMechanismType? FailureMechanismType { get; set; }
///
/// Generates a database creation script that can be used to perform a Hydra-Ring calculation.
///
/// The database creation script.
/// Thrown when one of the relevant input properties is not set.
public string GenerateDataBaseCreationScript()
{
var configurationDictionary = new Dictionary>();
ValidateDataBaseCreationScriptInput();
InitializeHydraulicModelsConfiguration(configurationDictionary);
InitializeSectionsConfiguration(configurationDictionary);
InitializeDesignTablesConfiguration(configurationDictionary);
InitializeNumericsConfiguration(configurationDictionary);
InitializeAreasConfiguration(configurationDictionary);
InitializeProjectsConfiguration(configurationDictionary);
return GenerateDataBaseCreationScript(configurationDictionary);
}
private void ValidateDataBaseCreationScriptInput()
{
var formattedExceptionMessage = "Cannot generate database creation script: {0} unspecified.";
if (TimeIntegrationSchemeType == null)
{
throw new InvalidOperationException(string.Format(formattedExceptionMessage, "TimeIntegrationSchemeType"));
}
if (UncertaintiesType == null)
{
throw new InvalidOperationException(string.Format(formattedExceptionMessage, "UncertaintiesType"));
}
if (HydraulicBoundaryLocation == null)
{
throw new InvalidOperationException(string.Format(formattedExceptionMessage, "HydraulicBoundaryLocation"));
}
if (FailureMechanismType == null)
{
throw new InvalidOperationException(string.Format(formattedExceptionMessage, "FailureMechanismType"));
}
}
private void InitializeHydraulicModelsConfiguration(Dictionary> configurationDictionary)
{
configurationDictionary["HydraulicModels"] = new List
{
new OrderedDictionary
{
{
"TimeIntegrationSchemeID", (int?) TimeIntegrationSchemeType
},
{
"UncertaintiesID", (int?) UncertaintiesType
},
{
"DataSetName", "WTI 2017" // Fixed: use the WTI 2017 set of station locations
}
}
};
}
private void InitializeSectionsConfiguration(Dictionary> configurationDictionary)
{
configurationDictionary["Sections"] = new List
{
new OrderedDictionary
{
{
"SectionId", 999 // TODO: Dike section integration
},
{
"PresentationId", 1 // Fixed: no support for combination of multiple dike sections
},
{
"MainMechanismId", 1 // Fixed: no support for combination of multiple dike sections
},
{
"Name", "HydraRingLocation" // TODO: Dike section integration
},
{
"Description", "HydraRingLocation" // TODO: Dike section integration
},
{
"RingCoordinateBegin", null // TODO: Dike section integration
},
{
"RingCoordinateEnd", null // TODO: Dike section integration
},
{
"XCoordinate", null // TODO: Dike cross section integration
},
{
"YCoordinate", null // TODO: Dike cross section integration
},
{
"StationId1", HydraulicBoundaryLocation.Id
},
{
"StationId2", HydraulicBoundaryLocation.Id // Same as "StationId1": no support for coupling two stations
},
{
"Relative", 100.0 // Fixed: no support for coupling two stations
},
{
"Normal", null // TODO: Dike cross section integration
},
{
"Length", null // TODO: Dike section integration
}
}
};
}
private void InitializeDesignTablesConfiguration(Dictionary> configurationDictionary)
{
var configurationSettingsForFailureMechanism = configurationSettings.First(cs => cs.HydraRingFailureMechanismType == FailureMechanismType);
configurationDictionary["DesignTables"] = new List
{
new OrderedDictionary
{
{
"SectionId", 999 // TODO: Dike section integration
},
{
"MechanismId", (int?) FailureMechanismType
},
{
"LayerId", null // Fixed: no support for revetments
},
{
"AlternativeId", null // Fixed: no support for piping
},
{
"Method", configurationSettingsForFailureMechanism.CalculationTypeId
},
{
"VariableId", configurationSettingsForFailureMechanism.VariableId
},
{
"LoadVariableId", null // Fixed: not relevant
},
{
"TableMin", null // Fixed: no support for type 3 computations (see "Method")
},
{
"TableMax", null // Fixed: no support for type 3 computations (see "Method")
},
{
"TableStepSize", null // Fixed: no support for type 3 computations (see "Method")
},
{
"ValueMin", null // Fixed: no support for type 2 computations (see "Method")
},
{
"ValueMax", null // Fixed: no support for type 2 computations (see "Method")
},
{
"Beta", null // Fixed: no support for type 2 computations (see "Method")
}
}
};
}
private void InitializeNumericsConfiguration(Dictionary> configurationDictionary)
{
var configurationSettingsForFailureMechanism = configurationSettings.First(cs => cs.HydraRingFailureMechanismType == FailureMechanismType);
configurationDictionary["Numerics"] = new List
{
new OrderedDictionary
{
{
"SectionId", 999 // TODO: Dike section integration
},
{
"MechanismId", (int?) FailureMechanismType
},
{
"LayerId", null // Fixed: no support for revetments
},
{
"AlternativeId", null // Fixed: no support for piping
},
{
"SubMechanismId", null // TODO: fix as part of WTI-324
},
{
"Method", null // TODO: fix as part of WTI-324
}
}
};
}
private void InitializeAreasConfiguration(Dictionary> configurationDictionary)
{
configurationDictionary["Areas"] = new List
{
new OrderedDictionary
{
{
"aDefault", 1 // Fixed: Not relevant
},
{
"bDefault", "1" // Fixed: Not relevant
},
{
"cDefault", "Nederland" // Fixed: Not relevant
}
}
};
}
private void InitializeProjectsConfiguration(Dictionary> configurationDictionary)
{
configurationDictionary["Projects"] = new List
{
new OrderedDictionary
{
{
"aDefault", 1 // Fixed: Not relevant
},
{
"bDefault", "Sprint" // Fixed: Not relevant
},
{
"cDefault", "Hydra-Ring Sprint" // Fixed: Not relevant
}
}
};
}
private static string GenerateDataBaseCreationScript(Dictionary> configurationDictionary)
{
var lines = new List();
foreach (var tableName in configurationDictionary.Keys)
{
lines.Add("DELETE FROM [" + tableName + "];");
if (configurationDictionary[tableName].Count <= 0)
{
continue;
}
foreach (var orderedDictionary in configurationDictionary[tableName])
{
var valueStrings = new List();
foreach (var val in orderedDictionary.Values)
{
if (val == null)
{
valueStrings.Add("NULL");
continue;
}
if (val is string)
{
valueStrings.Add("'" + val + "'");
continue;
}
if (val is double)
{
valueStrings.Add(((double) val).ToString(CultureInfo.InvariantCulture));
continue;
}
valueStrings.Add(val.ToString());
}
var valuesString = string.Join(", ", valueStrings);
lines.Add("INSERT INTO [" + tableName + "] VALUES (" + valuesString + ");");
}
lines.Add("");
}
return string.Join(Environment.NewLine, lines);
}
}
}