diff --git a/src/solver/modeler/CMakeLists.txt b/src/solver/modeler/CMakeLists.txt index 74378f6f7f..cb31b11fd4 100644 --- a/src/solver/modeler/CMakeLists.txt +++ b/src/solver/modeler/CMakeLists.txt @@ -1,3 +1,42 @@ add_subdirectory(api) add_subdirectory(ortoolsImpl) +add_subdirectory(loadFiles) add_subdirectory(parameters) + +OMESSAGE(" :: modeler") + +set(exec_name "antares-modeler") + +add_library(modeler-lib INTERFACE + ${SRCS} +) + +add_executable(antares-modeler + main.cpp + ${SRCS} +) + +set_target_properties(antares-modeler PROPERTIES OUTPUT_NAME ${exec_name}) + +target_link_libraries(modeler-lib + INTERFACE + Antares::loadModelerFiles + Antares::modelerParameters +) + +target_link_libraries(antares-modeler + PRIVATE + modeler-lib +) + +import_std_libs(antares-modeler) +executable_strip(antares-modeler) + +copy_dependency(sirius_solver antares-modeler) + +install(TARGETS antares-modeler EXPORT antares-modeler DESTINATION bin) + +INSTALL(EXPORT antares-modeler + FILE antares-modelerConfig.cmake + DESTINATION cmake +) diff --git a/src/solver/modeler/loadFiles/CMakeLists.txt b/src/solver/modeler/loadFiles/CMakeLists.txt new file mode 100644 index 0000000000..678aa9386c --- /dev/null +++ b/src/solver/modeler/loadFiles/CMakeLists.txt @@ -0,0 +1,34 @@ +set(SOURCES + readSystem.cpp + readLibraries.cpp + readParameters.cpp + handleErrors.cpp + + include/antares/solver/modeler/loadFiles/loadFiles.h +) + +# Create the library +add_library(loadModelerFiles STATIC ${SOURCES}) +add_library(Antares::loadModelerFiles ALIAS loadModelerFiles) + +# Specify include directories +target_include_directories(loadModelerFiles + PUBLIC + $ +) + +# Link dependencies (if any) +target_link_libraries(loadModelerFiles + PUBLIC + Antares::antares-study-system-model + PRIVATE + Antares::io + Antares::systemParser + Antares::modelParser + Antares::modelConverter + Antares::modelerParameters +) + +install(DIRECTORY include/antares + DESTINATION "include" +) diff --git a/src/solver/modeler/loadFiles/handleErrors.cpp b/src/solver/modeler/loadFiles/handleErrors.cpp new file mode 100644 index 0000000000..4ee2352970 --- /dev/null +++ b/src/solver/modeler/loadFiles/handleErrors.cpp @@ -0,0 +1,38 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include +#include "antares/solver/modeler/loadFiles/loadFiles.h" + +namespace Antares::Solver::LoadFiles +{ + +void handleYamlError(const YAML::Exception& e, const std::string& context) +{ + logs.error() << "Error while parsing the yaml file: " << context; + if (!e.mark.is_null()) + { + logs.error() << "Line " << e.mark.line << " column " << e.mark.column; + } + logs.error() << e.what(); +} + +} // namespace Antares::Solver::LoadFiles diff --git a/src/solver/modeler/loadFiles/include/antares/solver/modeler/loadFiles/loadFiles.h b/src/solver/modeler/loadFiles/include/antares/solver/modeler/loadFiles/loadFiles.h new file mode 100644 index 0000000000..ff3882e554 --- /dev/null +++ b/src/solver/modeler/loadFiles/include/antares/solver/modeler/loadFiles/loadFiles.h @@ -0,0 +1,54 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once + +#include +#include +#include + +#include +#include +#include + +namespace Antares::Solver::LoadFiles +{ + +ModelerParameters loadParameters(const std::filesystem::path& studyPath); + +std::vector loadLibraries(const std::filesystem::path& studyPath); + +Study::SystemModel::System loadSystem(const std::filesystem::path& studyPath, + const std::vector& libraries); + +void handleYamlError(const YAML::Exception& e, const std::string& context); + +/// Generic error class for all loading errors to catch in the main +class ErrorLoadingYaml: public std::runtime_error +{ +public: + explicit ErrorLoadingYaml(const std::string& s): + runtime_error(s) + { + } +}; + +} // namespace Antares::Solver::LoadFiles diff --git a/src/solver/modeler/loadFiles/readLibraries.cpp b/src/solver/modeler/loadFiles/readLibraries.cpp new file mode 100644 index 0000000000..be6b8e15d8 --- /dev/null +++ b/src/solver/modeler/loadFiles/readLibraries.cpp @@ -0,0 +1,92 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include + +#include +#include +#include +#include +#include "antares/solver/modeler/loadFiles/loadFiles.h" + +namespace fs = std::filesystem; + +namespace Antares::Solver::LoadFiles +{ + +static Study::SystemModel::Library loadSingleLibrary(const fs::path& filePath) +{ + std::string libraryStr; + try + { + libraryStr = IO::readFile(filePath); + } + catch (const std::runtime_error& e) + { + logs.error() << "Error while trying to read this library file: " << filePath; + throw ErrorLoadingYaml(e.what()); + } + + ModelParser::Parser parser; + ModelParser::Library libraryObj; + + try + { + libraryObj = parser.parse(libraryStr); + } + catch (const YAML::Exception& e) + { + handleYamlError(e, filePath.string()); + throw ErrorLoadingYaml(e.what()); + } + + try + { + return ModelConverter::convert(libraryObj); + } + catch (const std::runtime_error& e) + { + logs.error() << "Error while converting this library yaml: " << filePath; + throw ErrorLoadingYaml(e.what()); + } +} + +std::vector loadLibraries(const fs::path& studyPath) +{ + std::vector libraries; + + const fs::path directoryPath = studyPath / "input" / "model-libraries"; + for (const auto& entry: fs::directory_iterator(directoryPath)) + { + if (entry.path().extension() != ".yml") + { + logs.info() << entry.path() + << " ignored, only files having the `.yml` extension are loaded"; + continue; + } + + libraries.push_back(loadSingleLibrary(entry.path())); + logs.info() << "Library loaded: " << libraries.back().Id(); + } + + return libraries; +} +} // namespace Antares::Solver::LoadFiles diff --git a/src/solver/modeler/loadFiles/readParameters.cpp b/src/solver/modeler/loadFiles/readParameters.cpp new file mode 100644 index 0000000000..bab889cb7e --- /dev/null +++ b/src/solver/modeler/loadFiles/readParameters.cpp @@ -0,0 +1,59 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include + +#include +#include +#include "antares/solver/modeler/loadFiles/loadFiles.h" +#include "antares/solver/modeler/parameters/parseModelerParameters.h" + +namespace fs = std::filesystem; + +namespace Antares::Solver::LoadFiles +{ + +ModelerParameters loadParameters(const fs::path& studyPath) +{ + std::string filename = "parameters.yml"; + std::string paramStr; + try + { + paramStr = IO::readFile(studyPath / filename); + } + catch (const std::runtime_error& e) + { + logs.error() << "Error while trying to read file parameters.yml"; + throw ErrorLoadingYaml(e.what()); + } + + try + { + return parseModelerParameters(paramStr); + } + catch (const YAML::Exception& e) + { + handleYamlError(e, filename); + throw ErrorLoadingYaml(e.what()); + } +} + +} // namespace Antares::Solver::LoadFiles diff --git a/src/solver/modeler/loadFiles/readSystem.cpp b/src/solver/modeler/loadFiles/readSystem.cpp new file mode 100644 index 0000000000..dac47889cc --- /dev/null +++ b/src/solver/modeler/loadFiles/readSystem.cpp @@ -0,0 +1,73 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include + +#include +#include +#include +#include +#include "antares/solver/modeler/loadFiles/loadFiles.h" + +namespace fs = std::filesystem; + +namespace Antares::Solver::LoadFiles +{ + +Study::SystemModel::System loadSystem(const fs::path& studyPath, + const std::vector& libraries) +{ + std::string filename = "system.yml"; + std::string systemStr; + try + { + systemStr = IO::readFile(studyPath / "input" / filename); + } + catch (const std::runtime_error& e) + { + logs.error() << "Error while trying to read file system.yml"; + throw ErrorLoadingYaml(e.what()); + } + + SystemParser::Parser parser; + SystemParser::System systemObj; + try + { + systemObj = parser.parse(systemStr); + } + catch (const YAML::Exception& e) + { + handleYamlError(e, filename); + throw ErrorLoadingYaml(e.what()); + } + + try + { + return SystemConverter::convert(systemObj, libraries); + } + catch (const std::runtime_error& e) + { + logs.error() << "Error while converting the system yaml to components"; + throw ErrorLoadingYaml(e.what()); + } +} + +} // namespace Antares::Solver::LoadFiles diff --git a/src/solver/modeler/main.cpp b/src/solver/modeler/main.cpp new file mode 100644 index 0000000000..a4d071660b --- /dev/null +++ b/src/solver/modeler/main.cpp @@ -0,0 +1,68 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include +#include +#include + +using namespace Antares; +using namespace Antares::Solver; + +int main(int argc, const char** argv) +{ + if (argc < 1) + { + logs.error() << "No study path provided, exiting."; + return EXIT_FAILURE; + } + + std::filesystem::path studyPath(argv[1]); + logs.info() << "Study path: " << studyPath; + + if (!std::filesystem::is_directory(studyPath)) + { + logs.error() << "The path provided isn't a valid directory, exiting"; + return EXIT_FAILURE; + } + + try + { + const auto parameters = LoadFiles::loadParameters(studyPath); + logs.info() << "Parameters loaded"; + const auto libraries = LoadFiles::loadLibraries(studyPath); + logs.info() << "Libraries loaded"; + const auto system = LoadFiles::loadSystem(studyPath, libraries); + logs.info() << "System loaded"; + } + catch (const LoadFiles::ErrorLoadingYaml&) + { + logs.error() << "Error while loading files, exiting"; + return EXIT_FAILURE; + } + catch (const std::exception& e) + { + logs.error() << e.what(); + logs.error() << "Error during the execution, exiting"; + return EXIT_FAILURE; + } + + return 0; +} diff --git a/src/solver/modeler/parameters/CMakeLists.txt b/src/solver/modeler/parameters/CMakeLists.txt index 33cfda091e..7ab35d5002 100644 --- a/src/solver/modeler/parameters/CMakeLists.txt +++ b/src/solver/modeler/parameters/CMakeLists.txt @@ -1,14 +1,16 @@ -add_library(modeler-parameters - include/antares/solver/modeler/parameters/modelerParameters.h - include/antares/solver/modeler/parameters/parseModelerParameters.h - parseModelerParameters.cpp - encoder.hxx) +add_library(modelerParameters + include/antares/solver/modeler/parameters/modelerParameters.h + include/antares/solver/modeler/parameters/parseModelerParameters.h + parseModelerParameters.cpp + encoder.hxx) -target_link_libraries(modeler-parameters +add_library(Antares::modelerParameters ALIAS modelerParameters) + +target_link_libraries(modelerParameters PRIVATE yaml-cpp - Antares::io) + Antares::io) -target_include_directories(modeler-parameters +target_include_directories(modelerParameters PUBLIC $) diff --git a/src/solver/modeler/parameters/encoder.hxx b/src/solver/modeler/parameters/encoder.hxx index 32de3e0d83..c43d195ccc 100644 --- a/src/solver/modeler/parameters/encoder.hxx +++ b/src/solver/modeler/parameters/encoder.hxx @@ -1,3 +1,24 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + #include #include "yaml-cpp/yaml.h" diff --git a/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/modelerParameters.h b/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/modelerParameters.h index c6977a748b..6ea6e1072d 100644 --- a/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/modelerParameters.h +++ b/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/modelerParameters.h @@ -1,3 +1,24 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + #pragma once #include diff --git a/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/parseModelerParameters.h b/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/parseModelerParameters.h index 2409fcddf1..f0c9bd7238 100644 --- a/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/parseModelerParameters.h +++ b/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/parseModelerParameters.h @@ -1,11 +1,33 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + #pragma once #include #include -namespace Antares::Solver +namespace Antares::Solver::LoadFiles { -ModelerParameters parseModelerParameters(const std::filesystem::path& path); -} // namespace Antares::Solver +ModelerParameters parseModelerParameters(const std::string& content); + +} // namespace Antares::Solver::LoadFiles diff --git a/src/solver/modeler/parameters/parseModelerParameters.cpp b/src/solver/modeler/parameters/parseModelerParameters.cpp index 39935db38c..470b7a66e4 100644 --- a/src/solver/modeler/parameters/parseModelerParameters.cpp +++ b/src/solver/modeler/parameters/parseModelerParameters.cpp @@ -1,16 +1,38 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + #include -#include #include +#include #include "encoder.hxx" -namespace Antares::Solver +namespace Antares::Solver::LoadFiles { -ModelerParameters parseModelerParameters(const std::filesystem::path& path) + +ModelerParameters parseModelerParameters(const std::string& content) { - const auto contents = Antares::IO::readFile(path); - YAML::Node root = YAML::Load(contents); + YAML::Node root = YAML::Load(content); return root.as(); } -} // namespace Antares::Solver + +} // namespace Antares::Solver::LoadFiles diff --git a/src/tests/src/solver/modeler/CMakeLists.txt b/src/tests/src/solver/modeler/CMakeLists.txt index 9a39b3b19b..d532a0a77d 100644 --- a/src/tests/src/solver/modeler/CMakeLists.txt +++ b/src/tests/src/solver/modeler/CMakeLists.txt @@ -1,2 +1,2 @@ add_subdirectory(api) -add_subdirectory(parameters) +add_subdirectory(loadFiles) diff --git a/src/tests/src/solver/modeler/loadFiles/CMakeLists.txt b/src/tests/src/solver/modeler/loadFiles/CMakeLists.txt new file mode 100644 index 0000000000..896c759e06 --- /dev/null +++ b/src/tests/src/solver/modeler/loadFiles/CMakeLists.txt @@ -0,0 +1,10 @@ +include(${CMAKE_SOURCE_DIR}/tests/macros.cmake) + +add_boost_test(loadFiles + SRC + testLoadModelerFiles.cpp + testParameters.cpp + LIBS + Antares::modelerParameters + Antares::loadModelerFiles + test_utils_unit) diff --git a/src/tests/src/solver/modeler/loadFiles/testLoadModelerFiles.cpp b/src/tests/src/solver/modeler/loadFiles/testLoadModelerFiles.cpp new file mode 100644 index 0000000000..61fe4decf1 --- /dev/null +++ b/src/tests/src/solver/modeler/loadFiles/testLoadModelerFiles.cpp @@ -0,0 +1,220 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ +#define WIN32_LEAN_AND_MEAN + +#include +#define BOOST_TEST_MODULE load modeler files + +#include + +#include + +#include "files-system.h" + +BOOST_AUTO_TEST_SUITE(test_modeler_files_loading) + +namespace fs = std::filesystem; + +struct FixtureLoadFile +{ + fs::path studyPath; + fs::path inputPath; + fs::path libraryDirPath; + + FixtureLoadFile() + { + studyPath = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); + inputPath = createFolder(studyPath.string(), "input"); + libraryDirPath = createFolder(inputPath.string(), "model-libraries"); + } + + ~FixtureLoadFile() + { + fs::remove_all(studyPath); + } +}; + +BOOST_AUTO_TEST_CASE(files_not_existing) +{ + fs::path studyPath = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); + std::vector libraries; + + BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadLibraries(studyPath), std::runtime_error); + BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadSystem(studyPath, libraries), + std::runtime_error); +} + +BOOST_FIXTURE_TEST_CASE(read_one_lib_treile, FixtureLoadFile) +{ + std::ofstream libStream(libraryDirPath / "simple.yml"); + libStream << R"( + library: + id: lib_id + description: lib_description + port-types: [] + models: [] + )"; + libStream.close(); + + auto libraries = Antares::Solver::LoadFiles::loadLibraries(studyPath); + BOOST_CHECK_EQUAL(libraries[0].Id(), "lib_id"); +} + +BOOST_FIXTURE_TEST_CASE(dont_read_bad_extension, FixtureLoadFile) +{ + createFile(libraryDirPath.string(), "abc.txt"); + auto libraries = Antares::Solver::LoadFiles::loadLibraries(studyPath); + BOOST_CHECK(libraries.empty()); +} + +BOOST_FIXTURE_TEST_CASE(incorrect_library, FixtureLoadFile) +{ + std::ofstream libStream(libraryDirPath / "simple.yml"); + libStream << R"( + library: + port-types: [] + models: [] + )"; + libStream.close(); + + BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadLibraries(studyPath), std::runtime_error); +} + +BOOST_FIXTURE_TEST_CASE(incorrect_library2, FixtureLoadFile) +{ + std::ofstream libStream(libraryDirPath / "simple.yml"); + libStream << R"( + library: + id: std + port-types: [] + - id: generator + description: A basic generator model + + )"; + libStream.close(); + BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadLibraries(studyPath), std::runtime_error); +} + +BOOST_FIXTURE_TEST_CASE(read_several_lib_file, FixtureLoadFile) +{ + std::ofstream libStream(libraryDirPath / "simple.yml"); + libStream << R"( + library: + id: lib_id + description: lib_description + port-types: [] + models: [] + )"; + libStream.close(); + + std::ofstream libStream2(libraryDirPath / "2.yml"); + libStream2 << R"( + library: + id: lib_id2 + description: lib_description + port-types: [] + models: [] + )"; + libStream2.close(); + + std::ofstream libStream3(libraryDirPath / "3.yml"); + libStream3 << R"( + library: + id: lib_id3 + description: lib_description + port-types: [] + models: [] + )"; + libStream3.close(); + + auto libraries = Antares::Solver::LoadFiles::loadLibraries(studyPath); + + auto checkLibIdInVector = [&libraries](const std::string& libId) + { + return std::ranges::find_if(libraries, [&libId](const auto& l) { return l.Id() == libId; }) + != libraries.end(); + }; + + BOOST_CHECK(checkLibIdInVector("lib_id")); + BOOST_CHECK(checkLibIdInVector("lib_id2")); + BOOST_CHECK(checkLibIdInVector("lib_id3")); + + BOOST_CHECK(!checkLibIdInVector("id not in vector")); +} + +BOOST_FIXTURE_TEST_CASE(read_system_file, FixtureLoadFile) +{ + std::ofstream libStream(libraryDirPath / "simple.yml"); + libStream << R"( + library: + id: std + description: lib_description + port-types: [] + models: + - id: generator + description: A basic generator model + + )"; + libStream.close(); + + std::ofstream systemStream(inputPath / "system.yml"); + systemStream << R"( + system: + id: base_system + description: two components + components: + - id: N + model: std.generator + scenario-group: group-234 + )"; + systemStream.close(); + + auto libraries = Antares::Solver::LoadFiles::loadLibraries(studyPath); + auto system = Antares::Solver::LoadFiles::loadSystem(studyPath, libraries); +} + +BOOST_FIXTURE_TEST_CASE(read_invalid_system_file, FixtureLoadFile) +{ + std::ofstream libStream(libraryDirPath / "simple.yml"); + libStream << R"( + library: + id: std + description: lib_description + port-types: [] + models: + - id: generator + description: A basic generator model + + )"; + libStream.close(); + + std::ofstream systemStream(inputPath / "system.yml"); + systemStream << R"( + system: + )"; + systemStream.close(); + + auto libraries = Antares::Solver::LoadFiles::loadLibraries(studyPath); + BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadSystem(studyPath, libraries), + std::runtime_error); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/tests/src/solver/modeler/parameters/testParametersParsing.cpp b/src/tests/src/solver/modeler/loadFiles/testParameters.cpp similarity index 50% rename from src/tests/src/solver/modeler/parameters/testParametersParsing.cpp rename to src/tests/src/solver/modeler/loadFiles/testParameters.cpp index aa2b14732b..fb529f3111 100644 --- a/src/tests/src/solver/modeler/parameters/testParametersParsing.cpp +++ b/src/tests/src/solver/modeler/loadFiles/testParameters.cpp @@ -21,54 +21,66 @@ #define WIN32_LEAN_AND_MEAN #include -#define BOOST_TEST_MODULE parse modeler parameters #include -#include +#include #include "files-system.h" -BOOST_AUTO_TEST_SUITE(read_modeler_parameters) - -BOOST_AUTO_TEST_CASE(all_properties_set) +BOOST_AUTO_TEST_CASE(read_parameters) { - const auto working_tmp_dir = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); - const auto fileP = working_tmp_dir / "parameters.yaml"; - { - std::ofstream param(fileP); - param << R"( -solver: sirius -solver-logs: false -solver-parameters: PRESOLVE 1 -no-output: true)"; - } + auto studyPath = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); + std::ofstream paramStream(studyPath / "parameters.yml"); + paramStream << R"( + solver: sirius + solver-logs: false + solver-parameters: PRESOLVE 1 + no-output: true + )"; + paramStream.close(); - auto params = Antares::Solver::parseModelerParameters(fileP); + auto params = Antares::Solver::LoadFiles::loadParameters(studyPath); BOOST_CHECK_EQUAL(params.solver, "sirius"); BOOST_CHECK_EQUAL(params.solverLogs, false); BOOST_CHECK_EQUAL(params.solverParameters, "PRESOLVE 1"); BOOST_CHECK_EQUAL(params.noOutput, true); } -BOOST_AUTO_TEST_CASE(all_properties_set_out_of_order) +BOOST_AUTO_TEST_CASE(read_parameters_out_of_order) { - const auto working_tmp_dir = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); - const auto fileP = working_tmp_dir / "parameters.yaml"; - { - std::ofstream param(fileP); - param << R"( -solver-logs: false -solver: sirius -solver-parameters: PRESOLVE 1 -no-output: true)"; - } + auto studyPath = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); + std::ofstream paramStream(studyPath / "parameters.yml"); + paramStream << R"( + solver-logs: false + solver: sirius + solver-parameters: PRESOLVE 1 + no-output: true + )"; + paramStream.close(); - auto params = Antares::Solver::parseModelerParameters(fileP); + auto params = Antares::Solver::LoadFiles::loadParameters(studyPath); BOOST_CHECK_EQUAL(params.solver, "sirius"); BOOST_CHECK_EQUAL(params.solverLogs, false); BOOST_CHECK_EQUAL(params.solverParameters, "PRESOLVE 1"); BOOST_CHECK_EQUAL(params.noOutput, true); } -BOOST_AUTO_TEST_SUITE_END() +BOOST_AUTO_TEST_CASE(parameters_missing) +{ + auto studyPath = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); + std::ofstream paramStream(studyPath / "parameters.yml"); + paramStream << R"( + solver-logs: false + no-output: true + )"; + paramStream.close(); + + BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadParameters(studyPath), std::runtime_error); +} + +BOOST_AUTO_TEST_CASE(file_missing) +{ + auto studyPath = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); + BOOST_CHECK_THROW(Antares::Solver::LoadFiles::loadParameters(studyPath), std::runtime_error); +} diff --git a/src/tests/src/solver/modeler/parameters/CMakeLists.txt b/src/tests/src/solver/modeler/parameters/CMakeLists.txt deleted file mode 100644 index 85c27e2969..0000000000 --- a/src/tests/src/solver/modeler/parameters/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -include(${CMAKE_SOURCE_DIR}/tests/macros.cmake) - -add_boost_test(parse-parameters - SRC testParametersParsing.cpp - LIBS - modeler-parameters - test_utils_unit) diff --git a/src/tests/src/utils/files-system.cpp b/src/tests/src/utils/files-system.cpp index f6097ee82b..e8d6163ea7 100644 --- a/src/tests/src/utils/files-system.cpp +++ b/src/tests/src/utils/files-system.cpp @@ -10,7 +10,7 @@ fs::path generateAndCreateDirName(const std::string& dirName) return working_dir; } -void createFolder(const std::string& path, const std::string& folder_name) +fs::path createFolder(const std::string& path, const std::string& folder_name) { fs::path folder_path = fs::path(path.c_str()) / folder_name.c_str(); @@ -22,6 +22,8 @@ void createFolder(const std::string& path, const std::string& folder_name) { std::cerr << "Exception creating folder '" + folder_name + "': " + e.what() + "\n"; } + + return folder_path; } void createFile(const std::string& folder_path, const std::string& file_name) diff --git a/src/tests/src/utils/files-system.h b/src/tests/src/utils/files-system.h index 092b498350..9ad4503e54 100644 --- a/src/tests/src/utils/files-system.h +++ b/src/tests/src/utils/files-system.h @@ -31,6 +31,6 @@ std::filesystem::path generateAndCreateDirName(const std::string&); -void createFolder(const std::string& path, const std::string& folder_name); +std::filesystem::path createFolder(const std::string& path, const std::string& folder_name); void createFile(const std::string& folder_path, const std::string& file_name); void removeFolder(std::string& path, std::string& folder_name);