// 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.IO;
using System.Reflection;
using System.Security;
using Ringtoets.HydraRing.Calculation.Data;
using Ringtoets.HydraRing.Calculation.Providers;
namespace Ringtoets.HydraRing.Calculation.Services
{
///
/// Service for:
///
/// -
/// generating an initialization script that is necessary for performing Hydra-Ring calculations;
///
/// -
/// providing the corresponding file paths.
///
///
///
internal class HydraRingInitializationService
{
private const string hydraRingBinariesSubDirectory = "HydraRing";
private const string iniFileExtension = ".ini";
private const string databaseFileExtension = ".sql";
private const string logFileExtension = ".log";
private readonly int mechanismId;
private readonly int sectionId;
private readonly string hlcdFilePath;
private readonly string hydraRingDirectory;
private readonly string configurationDatabaseFilePath;
private readonly string preprocessorDirectory;
///
/// Creates a new instance of the class.
///
/// The failure mechanism type.
/// The section id.
/// The HLCD directory.
/// The working directory.
/// The preprocessor directory.
/// Preprocessing is disabled when
/// matches .
public HydraRingInitializationService(HydraRingFailureMechanismType failureMechanismType,
int sectionId,
string hlcdDirectory,
string temporaryWorkingDirectory,
string preprocessorDirectory)
{
mechanismId = new FailureMechanismDefaultsProvider().GetFailureMechanismDefaults(failureMechanismType).MechanismId;
this.sectionId = sectionId;
TemporaryWorkingDirectory = temporaryWorkingDirectory;
hlcdFilePath = Path.Combine(hlcdDirectory, HydraRingFileConstants.HlcdDatabaseFileName);
hydraRingDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), hydraRingBinariesSubDirectory);
configurationDatabaseFilePath = Path.Combine(hydraRingDirectory, HydraRingFileConstants.ConfigurationDatabaseFileName);
this.preprocessorDirectory = preprocessorDirectory;
}
///
/// Gets the ini file path.
///
public string IniFilePath
{
get
{
return Path.Combine(TemporaryWorkingDirectory, sectionId + iniFileExtension);
}
}
///
/// Gets the database creation script file path.
///
public string DatabaseCreationScriptFilePath
{
get
{
return Path.Combine(TemporaryWorkingDirectory, sectionId + databaseFileExtension);
}
}
///
/// Gets the path of the MechanismComputation.exe file.
///
public string MechanismComputationExeFilePath
{
get
{
return Path.Combine(hydraRingDirectory, HydraRingFileConstants.HydraRingExecutableFileName);
}
}
///
/// Gets the directory in which Hydra-Ring will place temporary input and output files created during a
/// calculation.
///
public string TemporaryWorkingDirectory { get; }
///
/// Generates the initialization script necessary for performing Hydra-Ring calculations.
///
/// The initialization script.
/// Thrown when an I/O error occurred while opening the file.
/// Thrown when the path can't be accessed due to missing permissions.
/// Thrown when the path can't be accessed due to missing permissions.
public void WriteInitializationScript()
{
string initializationFileContent = string.Join(Environment.NewLine,
"section = " + sectionId,
"mechanism = " + mechanismId,
"alternative = 1", // Fixed: no support for piping
"layer = 1", // Fixed: no support for revetments
"logfile = " + sectionId + logFileExtension,
"outputverbosity = basic",
"outputtofile = file",
"projectdbfilename = " + sectionId + databaseFileExtension,
"outputfilename = " + HydraRingFileConstants.DesignTablesFileName,
"configdbfilename = " + configurationDatabaseFilePath,
"hydraulicdbfilename = " + hlcdFilePath,
"designpointOutput = sqlite");
if (!string.IsNullOrEmpty(preprocessorDirectory))
{
initializationFileContent += Environment.NewLine + "preprocessordbdirectory = " + preprocessorDirectory;
}
File.WriteAllText(IniFilePath, initializationFileContent);
}
}
}