Skip to content

Commit

Permalink
first batch of addressing the comments
Browse files Browse the repository at this point in the history
Signed-off-by: Laurynas Jagutis <[email protected]>
  • Loading branch information
Laurynas-Jagutis committed Jan 27, 2025
1 parent cea7583 commit 906af12
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct VnfNode {
};

using VnfGrid = pgm::Container<VnfNode>;
template <typename Identifier> using IdentifierLookup = std::map<Identifier, ID>;
template <typename Identifier> using IdentifierLookup = std::unordered_map<Identifier, ID>;
using VisionGUIDLookup = IdentifierLookup<VisionGUID>;

class PgmVnfParser {
Expand Down Expand Up @@ -59,6 +59,7 @@ inline std::string_view PgmVnfParser::find_node_block() const {
std::match_results<std::string_view::const_iterator> nodes_match;

std::regex_search(this->vnf_data_.begin(), this->vnf_data_.end(), nodes_match, nodes_regex);
assert(nodes_match.size() > 1);
auto const& nodes = nodes_match[1]; // the first group is the nodes data
std::string_view const nodes_block{nodes.first, nodes.second};

Expand All @@ -78,6 +79,7 @@ inline void PgmVnfParser::parse_node_input() {
for (svregex_iterator it{nodes_block.begin(), nodes_block.end(), node_regex}, end; it != end; ++it) {
svmatch const& match = *it;

assert(match.size() > 2);
std::string const guid = match[1].str();
double const unom = std::stod(match[2].str());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
#ifndef POWER_GRID_MODEL_IO_NATIVE_C_PGM_VNF_CONVERTER_HPP
#define POWER_GRID_MODEL_IO_NATIVE_C_PGM_VNF_CONVERTER_HPP

#include "converter_parser.hpp"

#include <power_grid_model_io_native/common/common.hpp>
#include <power_grid_model_io_native/common/enum.hpp>
#include <power_grid_model_io_native/common/exception.hpp>
#include <power_grid_model_io_native/pgm_vnf_converter/converter_parser.hpp>

#include <power_grid_model/auxiliary/dataset.hpp>
#include <power_grid_model/auxiliary/meta_data_gen.hpp>
Expand Down Expand Up @@ -113,19 +114,17 @@ inline PgmInput PgmVnfConverter::get_deserialized_dataset() const { return this-
inline std::string const& PgmVnfConverter::get_serialized_data() const { return this->serialized_data_; }

inline void PgmVnfConverter::convert_node_input() {
std::vector<pgm::NodeInput> nodes;

for (auto const& node : this->parsed_vnf_data_.iter<VnfNode>()) {
this->nodes_.reserve(this->parsed_vnf_data_.template size<VnfNode>());
for (auto const& node : this->parsed_vnf_data_.template iter<VnfNode>()) {
// Lookup PGM node id and get vnf node u_nom value
auto node_id = id_lookup_[node.guid];
auto vnfnode_unom = node.u_nom;

// add u_nom multiplier when known
this->deserialized_data_.emplace<pgm::NodeInput>(node_id, node_id, vnfnode_unom);

nodes.emplace_back(pgm::NodeInput{node_id, vnfnode_unom});
this->nodes_.emplace_back(pgm::NodeInput{node_id, vnfnode_unom});
}
this->nodes_ = nodes;
}

inline void parse_vnf_file_wrapper(PgmVnfConverter* obj) { obj->parse_vnf_file(); }
Expand Down
83 changes: 41 additions & 42 deletions tests/c_api_tests/test_c_api_pgm_vnf_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,6 @@

namespace power_grid_model_io_native {

using enum PGM_IO_ExperimentalFeatures;

TEST_CASE("Test PGM_IO_create_vnf_converter") {
PGM_IO_ExperimentalFeatures experimental_feature_flag = PGM_IO_experimental_features_disabled;

SUBCASE("Test PGM_IO_create_vnf_converter without experimental feature flag") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
auto* converter = PGM_IO_create_pgm_vnf_converter(handle, "", experimental_feature_flag);
CHECK(PGM_IO_error_code(handle) == PGM_IO_regular_error);
PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}

SUBCASE("Test PGM_IO_create_vnf_converter with experimental feature flag") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
experimental_feature_flag = PGM_IO_experimental_features_enabled;
auto* converter = PGM_IO_create_pgm_vnf_converter(handle, "", experimental_feature_flag);
CHECK(converter != nullptr);
PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}
}

TEST_CASE("Test PGM_IO_get_vnf_input_data") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
PGM_IO_ExperimentalFeatures const experimental_feature_flag = PGM_IO_experimental_features_enabled;

auto* converter = PGM_IO_create_pgm_vnf_converter(handle, "", experimental_feature_flag);
CHECK(converter != nullptr);

auto const* const json_result = PGM_IO_pgm_vnf_converter_get_input_data(handle, converter);
std::string_view const json_string =
R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})";
CHECK(json_string == json_result);

PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}

namespace {
auto const* const basic_vision_9_7_vnf_file = R"vnf(V9.7
NETWORK
Expand Down Expand Up @@ -197,8 +158,48 @@ NETWORK
[]
)vnf";

std::string_view const json_result_string =
R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{"node":[{"id":0,"u_rated":11},{"id":1,"u_rated":11},{"id":2,"u_rated":0.4},{"id":3,"u_rated":11},{"id":4,"u_rated":0.4}]}})";
} // namespace

using enum PGM_IO_ExperimentalFeatures;

TEST_CASE("Test PGM_IO_create_vnf_converter") {
PGM_IO_ExperimentalFeatures experimental_feature_flag = PGM_IO_experimental_features_disabled;

SUBCASE("Test PGM_IO_create_vnf_converter without experimental feature flag") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
auto* converter = PGM_IO_create_pgm_vnf_converter(handle, "", experimental_feature_flag);
CHECK(PGM_IO_error_code(handle) == PGM_IO_regular_error);
PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}

SUBCASE("Test PGM_IO_create_vnf_converter with experimental feature flag") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
experimental_feature_flag = PGM_IO_experimental_features_enabled;
auto* converter = PGM_IO_create_pgm_vnf_converter(handle, basic_vision_9_7_vnf_file, experimental_feature_flag);
CHECK(converter != nullptr);
PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}
}

TEST_CASE("Test PGM_IO_get_vnf_input_data") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
PGM_IO_ExperimentalFeatures const experimental_feature_flag = PGM_IO_experimental_features_enabled;

auto* converter = PGM_IO_create_pgm_vnf_converter(handle, basic_vision_9_7_vnf_file, experimental_feature_flag);
CHECK(converter != nullptr);

auto const* const json_result = PGM_IO_pgm_vnf_converter_get_input_data(handle, converter);
CHECK(json_result == json_result_string);

PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}

TEST_CASE("Test PGM_IO_get_example_vnf_input_data") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
PGM_IO_ExperimentalFeatures const experimental_feature_flag = PGM_IO_experimental_features_enabled;
Expand All @@ -209,9 +210,7 @@ TEST_CASE("Test PGM_IO_get_example_vnf_input_data") {

auto const* const json_result = PGM_IO_pgm_vnf_converter_get_input_data(handle, converter);
REQUIRE(PGM_IO_error_code(handle) == PGM_IO_no_error);
std::string_view const json_string =
R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{"node":[{"id":0,"u_rated":11},{"id":1,"u_rated":11},{"id":2,"u_rated":0.4},{"id":3,"u_rated":11},{"id":4,"u_rated":0.4}]}})";
CHECK(json_string == json_result);
CHECK(json_result == json_result_string);

PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
Expand Down
70 changes: 33 additions & 37 deletions tests/cpp_unit_tests/test_pgm_vnf_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@ namespace power_grid_model_io_native {
namespace {
using enum ExperimentalFeatures;

auto const* const vision_9_7_vnf_file_only_nodes = R"vnf(V9.7
NETWORK
[NODE]
#General GUID:'{7FF722ED-33B3-4761-84AC-A164310D3C86}' CreationTime:44875.5806865509 Name:'node1' Unom:11
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:14800 Y:14800 Symbol:1 Size:6 Width:4 UpstringsY:-70 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
#General GUID:'{1ED177A7-1F5D-4D81-8DE7-AB3E58512E0B}' CreationTime:44875.5937016435 Name:'node2' Unom:11
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:14940 Y:14800 Symbol:1 Size:6 Width:4 UpstringsY:-70 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
#General GUID:'{DDE3457B-DB9A-4DA9-9564-6F49E0F296BD}' CreationTime:44875.5965067593 Name:'node3' Unom:0.4
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:15100 Y:14800 Symbol:1 Size:14 Width:4 UpstringsY:-150 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
#General GUID:'{A79AFDE9-4096-4BEB-AB63-2B851D7FC6D1}' CreationTime:44875.5989385185 Name:'node4' Unom:11
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:15100 Y:15100 Symbol:1 Size:8 Width:4 UpstringsY:-90 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
#General GUID:'{7848DBC8-9685-452C-89AF-9AB308224689}' CreationTime:44886.4465440509 Unom:0.4
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:15040 Y:14580 Symbol:1 Size:4 Width:4 UpstringsY:-50 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
[]
)vnf";

std::string_view const only_nodes_json_string =
R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{"node":[{"id":0,"u_rated":11},{"id":1,"u_rated":11},{"id":2,"u_rated":0.4},{"id":3,"u_rated":11},{"id":4,"u_rated":0.4}]}})";

} // namespace

TEST_CASE("Test converter constructor") {
Expand All @@ -32,7 +63,7 @@ TEST_CASE("Test converter constructor") {
}

TEST_CASE("Test parse_vnf_file is callable") {
auto converter = PgmVnfConverter("", experimental_features_enabled);
auto converter = PgmVnfConverter(vision_9_7_vnf_file_only_nodes, experimental_features_enabled);
CHECK_NOTHROW(converter.parse_vnf_file());
}

Expand All @@ -59,45 +90,10 @@ TEST_CASE("Test setter/getter of deserialized_data") {
}

TEST_CASE("Test parse_vnf_file_wrapper") {
auto converter = PgmVnfConverter("", experimental_features_enabled);
auto converter = PgmVnfConverter(vision_9_7_vnf_file_only_nodes, experimental_features_enabled);
CHECK_NOTHROW(parse_vnf_file_wrapper(&converter));
}

namespace {

auto const* const vision_9_7_vnf_file_only_nodes = R"vnf(V9.7
NETWORK
[NODE]
#General GUID:'{7FF722ED-33B3-4761-84AC-A164310D3C86}' CreationTime:44875.5806865509 Name:'node1' Unom:11
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:14800 Y:14800 Symbol:1 Size:6 Width:4 UpstringsY:-70 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
#General GUID:'{1ED177A7-1F5D-4D81-8DE7-AB3E58512E0B}' CreationTime:44875.5937016435 Name:'node2' Unom:11
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:14940 Y:14800 Symbol:1 Size:6 Width:4 UpstringsY:-70 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
#General GUID:'{DDE3457B-DB9A-4DA9-9564-6F49E0F296BD}' CreationTime:44875.5965067593 Name:'node3' Unom:0.4
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:15100 Y:14800 Symbol:1 Size:14 Width:4 UpstringsY:-150 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
#General GUID:'{A79AFDE9-4096-4BEB-AB63-2B851D7FC6D1}' CreationTime:44875.5989385185 Name:'node4' Unom:11
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:15100 Y:15100 Symbol:1 Size:8 Width:4 UpstringsY:-90 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
#General GUID:'{7848DBC8-9685-452C-89AF-9AB308224689}' CreationTime:44886.4465440509 Unom:0.4
#Railtype
#Installation Kb:0.5 Kt:1
#Presentation Sheet:'{AC5FD754-220B-47EF-B98C-367CB49E8C75}' X:15040 Y:14580 Symbol:1 Size:4 Width:4 UpstringsY:-50 FaultStringsX:-20 FaultStringsY:20 NoteX:5 NoteY:5 IconY:50
[]
)vnf";

std::string_view const only_nodes_json_string =
R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{"node":[{"id":0,"u_rated":11},{"id":1,"u_rated":11},{"id":2,"u_rated":0.4},{"id":3,"u_rated":11},{"id":4,"u_rated":0.4}]}})";

} // namespace

TEST_CASE("Test parse_vnf_file_wrapper minimal example") {
auto converter = PgmVnfConverter(vision_9_7_vnf_file_only_nodes, experimental_features_enabled);
parse_vnf_file_wrapper(&converter);
Expand Down

0 comments on commit 906af12

Please sign in to comment.