From 3043877d1e56a0ba9c64a68b01e754fcb3edd5b7 Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Mon, 24 Feb 2025 16:06:35 +0100 Subject: [PATCH] Raise exception when converting port types --- .../DataSeriesRepoImporter.cpp | 5 ++-- .../inputs/model-converter/modelConverter.h | 12 +++++++++ .../inputs/model-converter/modelConverter.cpp | 26 ++++++++++++++++--- .../io/yml-importers/testModelTranslator.cpp | 4 --- src/tests/src/io/yml-importers/test_full.cpp | 1 - 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/io/inputs/data-series-csv-importer/DataSeriesRepoImporter.cpp b/src/io/inputs/data-series-csv-importer/DataSeriesRepoImporter.cpp index 9bdcc962ee..680942bff7 100644 --- a/src/io/inputs/data-series-csv-importer/DataSeriesRepoImporter.cpp +++ b/src/io/inputs/data-series-csv-importer/DataSeriesRepoImporter.cpp @@ -145,13 +145,14 @@ DataSeriesRepository DataSeriesRepoImporter::importFromDirectory(const std::file throw std::invalid_argument("Not a directory: " + path.string()); } using std::views::filter; - auto pathFilter = filter(static_cast(&fs::is_regular_file)) - | filter(&hasRightExtension); + auto pathFilter = filter(static_cast(&fs::is_regular_file)); DataSeriesRepository repo{}; for (auto paths = std::filesystem::directory_iterator{path}; const auto& entry: paths | pathFilter) { + if (!hasRightExtension(entry)) + continue; auto timeSeriesSet = std::make_unique(importFromFile(entry, csvSeparators)); repo.addDataSeries(std::move(timeSeriesSet)); } diff --git a/src/io/inputs/model-converter/include/antares/io/inputs/model-converter/modelConverter.h b/src/io/inputs/model-converter/include/antares/io/inputs/model-converter/modelConverter.h index 317a7cc4e3..1fb99a32b3 100644 --- a/src/io/inputs/model-converter/include/antares/io/inputs/model-converter/modelConverter.h +++ b/src/io/inputs/model-converter/include/antares/io/inputs/model-converter/modelConverter.h @@ -50,5 +50,17 @@ class UnknownTypeException: public std::runtime_error explicit UnknownTypeException(YmlModel::ValueType type); }; +class PortTypeAlreadyExists: public std::runtime_error +{ +public: + explicit PortTypeAlreadyExists(const std::string& id); +}; + +class PortTypeDoesntContainsFields: public std::runtime_error +{ +public: + explicit PortTypeDoesntContainsFields(const std::string& id); +}; + Study::SystemModel::Library convert(const YmlModel::Library& library); } // namespace Antares::IO::Inputs::ModelConverter diff --git a/src/io/inputs/model-converter/modelConverter.cpp b/src/io/inputs/model-converter/modelConverter.cpp index 59e4b47f89..c16d160242 100644 --- a/src/io/inputs/model-converter/modelConverter.cpp +++ b/src/io/inputs/model-converter/modelConverter.cpp @@ -38,6 +38,16 @@ UnknownTypeException::UnknownTypeException(YmlModel::ValueType type): { } +PortTypeDoesntContainsFields::PortTypeDoesntContainsFields(const std::string& id): + std::runtime_error("This port type doesn't contains fields: " + id) +{ +} + +PortTypeAlreadyExists::PortTypeAlreadyExists(const std::string& id): + std::runtime_error("Port type already exists: " + id) +{ +} + /** * \brief Converts parameters from YmlModel::Model to SystemModel::Parameter. * @@ -52,14 +62,24 @@ std::vector convertTypes( out.reserve(library.port_types.size()); for (const auto& portType: library.port_types) { + if (portType.fields.empty()) // Can't have a port type without fields + { + throw PortTypeDoesntContainsFields(portType.id); + } std::vector fields; for (const auto& field: portType.fields) { fields.emplace_back(Antares::Study::SystemModel::PortField{field}); } - Antares::Study::SystemModel::PortType portTypeModel(portType.id, - portType.description, - std::move(fields)); + + // Can't have port types with the same ID + if (std::ranges::find_if(out, [&portType](const auto& p) { return p.Id() == portType.id; }) + != out.end()) + { + throw PortTypeAlreadyExists(portType.id); + } + + Antares::Study::SystemModel::PortType portTypeModel(portType.id, std::move(fields)); out.emplace_back(std::move(portTypeModel)); } return out; diff --git a/src/tests/src/io/yml-importers/testModelTranslator.cpp b/src/tests/src/io/yml-importers/testModelTranslator.cpp index a4bf60db20..a71f74bbd7 100644 --- a/src/tests/src/io/yml-importers/testModelTranslator.cpp +++ b/src/tests/src/io/yml-importers/testModelTranslator.cpp @@ -69,17 +69,13 @@ BOOST_FIXTURE_TEST_CASE(port_type_with_empty_fileds_properly_translated, Fixture SystemModel::Library lib = ModelConverter::convert(library); BOOST_REQUIRE_EQUAL(lib.PortTypes().size(), 2); BOOST_CHECK_EQUAL(lib.PortTypes().at("port1").Id(), "port1"); - BOOST_CHECK_EQUAL(lib.PortTypes().at("port1").Description(), "flow port"); BOOST_CHECK(lib.PortTypes().at("port1").Fields().empty()); BOOST_CHECK_EQUAL(lib.PortTypes().at("port2").Id(), "port2"); - BOOST_CHECK_EQUAL(lib.PortTypes().at("port2").Description(), "impedance port"); BOOST_CHECK(lib.PortTypes().at("port2").Fields().empty()); BOOST_REQUIRE_EQUAL(lib.PortTypes().size(), 2); BOOST_CHECK_EQUAL(lib.PortTypes().at("port1").Id(), "port1"); - BOOST_CHECK_EQUAL(lib.PortTypes().at("port1").Description(), "flow port"); BOOST_CHECK(lib.PortTypes().at("port1").Fields().empty()); BOOST_CHECK_EQUAL(lib.PortTypes().at("port2").Id(), "port2"); - BOOST_CHECK_EQUAL(lib.PortTypes().at("port2").Description(), "impedance port"); BOOST_CHECK(lib.PortTypes().at("port2").Fields().empty()); } diff --git a/src/tests/src/io/yml-importers/test_full.cpp b/src/tests/src/io/yml-importers/test_full.cpp index fc6bc3ff59..385b056c5d 100644 --- a/src/tests/src/io/yml-importers/test_full.cpp +++ b/src/tests/src/io/yml-importers/test_full.cpp @@ -271,7 +271,6 @@ BOOST_AUTO_TEST_CASE(test_full) BOOST_REQUIRE_EQUAL(lib.PortTypes().size(), 1); auto& portType = lib.PortTypes().at("flow"); BOOST_CHECK_EQUAL(portType.Id(), "flow"); - BOOST_CHECK_EQUAL(portType.Description(), "A port which transfers power flow"); BOOST_REQUIRE_EQUAL(portType.Fields().size(), 1); auto& portTypeField = portType.Fields().at(0);