Skip to content

Commit

Permalink
Raise exception when converting port types
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Feb 24, 2025
1 parent ecc1714 commit 3043877
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool (*)(const fs::path&)>(&fs::is_regular_file))
| filter(&hasRightExtension);
auto pathFilter = filter(static_cast<bool (*)(const fs::path&)>(&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<TimeSeriesSet>(importFromFile(entry, csvSeparators));
repo.addDataSeries(std::move(timeSeriesSet));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 23 additions & 3 deletions src/io/inputs/model-converter/modelConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -52,14 +62,24 @@ std::vector<Antares::Study::SystemModel::PortType> 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<Antares::Study::SystemModel::PortField> 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;
Expand Down
4 changes: 0 additions & 4 deletions src/tests/src/io/yml-importers/testModelTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
1 change: 0 additions & 1 deletion src/tests/src/io/yml-importers/test_full.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3043877

Please sign in to comment.