// Copyright (C) Stichting Deltares 2017. 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.Base.Data;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Common.IO.Configurations.Helpers;
using Ringtoets.Common.IO.Configurations.Import;
using Ringtoets.MacroStabilityInwards.Data;
using Ringtoets.MacroStabilityInwards.Data.SoilProfile;
using Ringtoets.MacroStabilityInwards.IO.Configurations.Helpers;
using Ringtoets.MacroStabilityInwards.IO.Properties;
using Ringtoets.MacroStabilityInwards.Primitives;
namespace Ringtoets.MacroStabilityInwards.IO.Configurations
{
///
/// Imports a macro stability inwards calculation configuration from an XML file and stores it on a
/// .
///
public class MacroStabilityInwardsCalculationConfigurationImporter
: CalculationConfigurationImporter<
MacroStabilityInwardsCalculationConfigurationReader,
MacroStabilityInwardsCalculationConfiguration>
{
private readonly IEnumerable availableHydraulicBoundaryLocations;
private readonly MacroStabilityInwardsFailureMechanism failureMechanism;
///
/// Creates a new instance of .
///
/// The path to the XML file to import from.
/// The calculation group to update.
/// The hydraulic boundary locations
/// used to check if the imported objects contain the right location.
/// The failure mechanism used to check
/// if the imported objects contain the right data.
/// Thrown when any parameter is
/// null.
public MacroStabilityInwardsCalculationConfigurationImporter(string xmlFilePath,
CalculationGroup importTarget,
IEnumerable availableHydraulicBoundaryLocations,
MacroStabilityInwardsFailureMechanism failureMechanism)
: base(xmlFilePath, importTarget)
{
if (availableHydraulicBoundaryLocations == null)
{
throw new ArgumentNullException(nameof(availableHydraulicBoundaryLocations));
}
if (failureMechanism == null)
{
throw new ArgumentNullException(nameof(failureMechanism));
}
this.availableHydraulicBoundaryLocations = availableHydraulicBoundaryLocations;
this.failureMechanism = failureMechanism;
}
protected override MacroStabilityInwardsCalculationConfigurationReader CreateCalculationConfigurationReader(string xmlFilePath)
{
return new MacroStabilityInwardsCalculationConfigurationReader(xmlFilePath);
}
protected override ICalculation ParseReadCalculation(MacroStabilityInwardsCalculationConfiguration calculationConfiguration)
{
var calculation = new MacroStabilityInwardsCalculationScenario
{
Name = calculationConfiguration.Name
};
if (TrySetHydraulicBoundaryData(calculationConfiguration, calculation)
&& TrySetSurfaceLine(calculationConfiguration, calculation)
&& TrySetStochasticSoilModel(calculationConfiguration, calculation)
&& TrySetStochasticSoilProfile(calculationConfiguration, calculation)
&& TrySetScenarioParameters(calculationConfiguration.Scenario, calculation))
{
SetDikeSoilScenario(calculationConfiguration, calculation);
SetGridDeterminationType(calculationConfiguration, calculation);
SetTangentLineDeterminationType(calculationConfiguration, calculation);
return calculation;
}
return null;
}
///
/// Assigns the hydraulic boundary location or the assessment level that is set manually.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the has a
/// set which is not available in , true otherwise.
private bool TrySetHydraulicBoundaryData(MacroStabilityInwardsCalculationConfiguration calculationConfiguration,
MacroStabilityInwardsCalculationScenario macroStabilityInwardsCalculation)
{
HydraulicBoundaryLocation location;
bool locationRead = TryReadHydraulicBoundaryLocation(calculationConfiguration.HydraulicBoundaryLocationName,
calculationConfiguration.Name,
availableHydraulicBoundaryLocations,
out location);
if (!locationRead)
{
return false;
}
if (location != null)
{
macroStabilityInwardsCalculation.InputParameters.HydraulicBoundaryLocation = location;
}
else if (calculationConfiguration.AssessmentLevel.HasValue)
{
macroStabilityInwardsCalculation.InputParameters.UseAssessmentLevelManualInput = true;
macroStabilityInwardsCalculation.InputParameters.AssessmentLevel = (RoundedDouble) calculationConfiguration.AssessmentLevel.Value;
}
return true;
}
///
/// Assigns the surface line.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the has a
/// set which is not available in , true otherwise.
private bool TrySetSurfaceLine(MacroStabilityInwardsCalculationConfiguration calculationConfiguration,
MacroStabilityInwardsCalculationScenario macroStabilityInwardsCalculation)
{
if (calculationConfiguration.SurfaceLineName != null)
{
MacroStabilityInwardsSurfaceLine surfaceLine = failureMechanism.SurfaceLines
.FirstOrDefault(sl => sl.Name == calculationConfiguration.SurfaceLineName);
if (surfaceLine == null)
{
Log.LogCalculationConversionError(string.Format(
Resources.MacroStabilityInwardsCalculationConfigurationImporter_ReadSurfaceLine_SurfaceLine_0_does_not_exist,
calculationConfiguration.SurfaceLineName),
macroStabilityInwardsCalculation.Name);
return false;
}
macroStabilityInwardsCalculation.InputParameters.SurfaceLine = surfaceLine;
}
return true;
}
///
/// Assigns the stochastic soil model.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when
///
/// - the has a set
/// which is not available in the failure mechanism.
/// - The does not intersect with the
/// when this is set.
///
/// true otherwise.
private bool TrySetStochasticSoilModel(MacroStabilityInwardsCalculationConfiguration calculationConfiguration,
MacroStabilityInwardsCalculationScenario macroStabilityInwardsCalculation)
{
if (calculationConfiguration.StochasticSoilModelName != null)
{
MacroStabilityInwardsStochasticSoilModel soilModel = failureMechanism.StochasticSoilModels
.FirstOrDefault(ssm => ssm.Name == calculationConfiguration.StochasticSoilModelName);
if (soilModel == null)
{
Log.LogCalculationConversionError(string.Format(
Resources.MacroStabilityInwardsCalculationConfigurationImporter_ReadStochasticSoilModel_Stochastische_soil_model_0_does_not_exist,
calculationConfiguration.StochasticSoilModelName),
macroStabilityInwardsCalculation.Name);
return false;
}
if (macroStabilityInwardsCalculation.InputParameters.SurfaceLine != null
&& !soilModel.IntersectsWithSurfaceLineGeometry(macroStabilityInwardsCalculation.InputParameters.SurfaceLine))
{
Log.LogCalculationConversionError(string.Format(
Resources.MacroStabilityInwardsCalculationConfigurationImporter_ReadStochasticSoilModel_Stochastische_soil_model_0_does_not_intersect_with_surfaceLine_1,
calculationConfiguration.StochasticSoilModelName,
calculationConfiguration.SurfaceLineName),
macroStabilityInwardsCalculation.Name);
return false;
}
macroStabilityInwardsCalculation.InputParameters.StochasticSoilModel = soilModel;
}
return true;
}
///
/// Assigns the stochastic soil profile.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the has:
///
/// - a set but no is specified;
/// - a set which is not available in the .
///
/// true otherwise.
private bool TrySetStochasticSoilProfile(MacroStabilityInwardsCalculationConfiguration calculationConfiguration,
MacroStabilityInwardsCalculationScenario macroStabilityInwardsCalculation)
{
if (calculationConfiguration.StochasticSoilProfileName != null)
{
if (macroStabilityInwardsCalculation.InputParameters.StochasticSoilModel == null)
{
Log.LogCalculationConversionError(string.Format(
Resources.MacroStabilityInwardsCalculationConfigurationImporter_ReadStochasticSoilProfile_No_soil_model_provided_for_soil_profile_with_name_0,
calculationConfiguration.StochasticSoilProfileName),
macroStabilityInwardsCalculation.Name);
return false;
}
MacroStabilityInwardsStochasticSoilProfile soilProfile = macroStabilityInwardsCalculation.InputParameters
.StochasticSoilModel
.StochasticSoilProfiles
.FirstOrDefault(ssp => ssp.SoilProfile.Name == calculationConfiguration.StochasticSoilProfileName);
if (soilProfile == null)
{
Log.LogCalculationConversionError(string.Format(
Resources.MacroStabilityInwardsCalculationConfigurationImporter_ReadStochasticSoilProfile_Stochastic_soil_profile_0_does_not_exist_within_soil_model_1,
calculationConfiguration.StochasticSoilProfileName,
calculationConfiguration.StochasticSoilModelName),
macroStabilityInwardsCalculation.Name);
return false;
}
macroStabilityInwardsCalculation.InputParameters.StochasticSoilProfile = soilProfile;
}
return true;
}
///
/// Assigns the dike soil scenario.
///
/// The read calculation read.
/// The calculation to configure.
private static void SetDikeSoilScenario(MacroStabilityInwardsCalculationConfiguration configuration,
MacroStabilityInwardsCalculationScenario calculation)
{
if (!configuration.DikeSoilScenario.HasValue)
{
return;
}
var dikeSoilScenario = (MacroStabilityInwardsDikeSoilScenario?) new ConfigurationDikeSoilScenarioTypeConverter()
.ConvertTo(configuration.DikeSoilScenario.Value, typeof(MacroStabilityInwardsDikeSoilScenario));
if (dikeSoilScenario.HasValue)
{
calculation.InputParameters.DikeSoilScenario = dikeSoilScenario.Value;
}
}
///
/// Assigns the grid determination type.
///
/// The read calculation read.
/// The calculation to configure.
private static void SetGridDeterminationType(MacroStabilityInwardsCalculationConfiguration configuration,
MacroStabilityInwardsCalculationScenario calculation)
{
if (!configuration.GridDeterminationType.HasValue)
{
return;
}
var gridDeterminationType = (MacroStabilityInwardsGridDeterminationType?) new ConfigurationGridDeterminationTypeConverter()
.ConvertTo(configuration.GridDeterminationType.Value, typeof(MacroStabilityInwardsGridDeterminationType));
if (gridDeterminationType.HasValue)
{
calculation.InputParameters.GridDeterminationType = gridDeterminationType.Value;
}
}
///
/// Assigns the tangent line determination type.
///
/// The read calculation read.
/// The calculation to configure.
private static void SetTangentLineDeterminationType(MacroStabilityInwardsCalculationConfiguration configuration,
MacroStabilityInwardsCalculationScenario calculation)
{
if (!configuration.TangentLineDeterminationType.HasValue)
{
return;
}
var tangentLineDeterminationType = (MacroStabilityInwardsTangentLineDeterminationType?) new ConfigurationTangentLineDeterminationTypeConverter()
.ConvertTo(configuration.TangentLineDeterminationType.Value, typeof(MacroStabilityInwardsTangentLineDeterminationType));
if (tangentLineDeterminationType.HasValue)
{
calculation.InputParameters.TangentLineDeterminationType = tangentLineDeterminationType.Value;
}
}
}
}