Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: homogenize code surrounding VNF converter #20

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
homogenize
Signed-off-by: Laurynas Jagutis <[email protected]>
Laurynas-Jagutis committed Dec 3, 2024
commit 33d0832c44eabed665d71a3f7992338eeb727575
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
// SPDX-License-Identifier: MPL-2.0

#pragma once
#ifndef POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP
#define POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP
#ifndef POWER_GRID_MODEL_IO_NATIVE_C_PGM_VNF_CONVERTER_HPP
#define POWER_GRID_MODEL_IO_NATIVE_C_PGM_VNF_CONVERTER_HPP

#include <power_grid_model_io_native/common/common.hpp>
#include <power_grid_model_io_native/common/enum.hpp>
@@ -178,4 +178,4 @@ inline std::string const& convert_input_wrapper(PgmVnfConverter* obj) {

} // namespace power_grid_model_io_native

#endif // POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP
#endif // POWER_GRID_MODEL_IO_NATIVE_C_PGM_VNF_CONVERTER_HPP
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
# C API library
add_library(power_grid_model_io_native_c SHARED
"src/handle.cpp"
"src/vnf_pgm_converter.cpp"
"src/pgm_vnf_converter.cpp"
)

target_include_directories(power_grid_model_io_native_c PUBLIC
@@ -16,7 +16,7 @@ target_include_directories(power_grid_model_io_native_c PUBLIC
set(PGM_IO_NATIVE_PUBLIC_HEADERS
"${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c/basics.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c/handle.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c/vnf_pgm_converter.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c/pgm_vnf_converter.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c.h"
)

Original file line number Diff line number Diff line change
@@ -18,6 +18,6 @@

#include "power_grid_model_io_native_c/basics.h"
#include "power_grid_model_io_native_c/handle.h"
#include "power_grid_model_io_native_c/vnf_pgm_converter.h"
#include "power_grid_model_io_native_c/pgm_vnf_converter.h"

#endif // POWER_GRID_MODEL_IO_NATIVE_C_H
Original file line number Diff line number Diff line change
@@ -65,10 +65,10 @@ typedef int32_t PGM_IO_ID;
// typedef struct PGM_IO_VnfConverter PGM_IO_VnfConverter;

/**
* @brief Opaque struct for the VnfConverter class.
* @brief Opaque struct for the PgmVnfConverter class.
*
*/
typedef struct PGM_IO_VnfPgmConverter PGM_IO_VnfPgmConverter;
typedef struct PGM_IO_PgmVnfConverter PGM_IO_PgmVnfConverter;

/**
* @brief Opaque struct for the handle class.
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
// SPDX-License-Identifier: MPL-2.0

#pragma once
#ifndef POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_H
#define POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_H
#ifndef POWER_GRID_MODEL_IO_NATIVE_C_PGM_VNF_CONVERTER_H
#define POWER_GRID_MODEL_IO_NATIVE_C_PGM_VNF_CONVERTER_H

#include "basics.h"

@@ -13,32 +13,32 @@ extern "C" {
#endif

/**
* @brief Create the PGM_IO_VnfPgmConverter
* @brief Create the PGM_IO_PgmVnfConverter
* @param handle
* @param file_buffer A pointer to the null-terminated C string.
* @return The pointer to a PGM_IO_VnfPgmConverter instance. The instance must be freed by
* @return The pointer to a PGM_IO_PgmVnfConverter instance. The instance must be freed by
* PGM_IO_destroy_vnf_converter.
*/
PGM_IO_API PGM_IO_VnfPgmConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char const* file_buffer,
PGM_IO_API PGM_IO_PgmVnfConverter* PGM_IO_create_pgm_vnf_converter(PGM_IO_Handle* handle, char const* file_buffer,
PGM_IO_ExperimentalFeatures experimental_features);

/**
* @brief Retrieve the transformed input data from .vnf format to PGM format
* @param handle
* @param converter_ptr A pointer to a PGM_IO_VnfPgmConverter instace.
* @param converter_ptr A pointer to a PGM_IO_PgmVnfConverter instace.
* @return The pointer to the json string instance that holds data in PGM format.
*/
PGM_IO_API char const* PGM_IO_vnf_pgm_converter_get_input_data(PGM_IO_Handle* handle,
PGM_IO_VnfPgmConverter* converter_ptr);
PGM_IO_API char const* PGM_IO_pgm_vnf_converter_get_input_data(PGM_IO_Handle* handle,
PGM_IO_PgmVnfConverter* converter_ptr);

/**
* @brief Destroy the PGM_IO_VnfPgmConverter and free up the memory that was dedicated to it.
* @param converter_ptr A pointer to a PGM_IO_VnfPgmConverter instance.
* @brief Destroy the PGM_IO_PgmVnfConverter and free up the memory that was dedicated to it.
* @param converter_ptr A pointer to a PGM_IO_PgmVnfConverter instance.
*/
PGM_IO_API void PGM_IO_destroy_vnf_converter(PGM_IO_VnfPgmConverter* converter_ptr);
PGM_IO_API void PGM_IO_destroy_pgm_vnf_converter(PGM_IO_PgmVnfConverter* converter_ptr);

#ifdef __cplusplus
}
#endif

#endif // POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_H
#endif // POWER_GRID_MODEL_IO_NATIVE_C_PGM_VNF_CONVERTER_H
Original file line number Diff line number Diff line change
@@ -5,22 +5,22 @@
#define PGM_IO_DLL_EXPORTS

#include <power_grid_model_io_native/common/enum.hpp>
#include <power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp>
#include <power_grid_model_io_native/pgm_vnf_converter/pgm_vnf_converter.hpp>

#include "handle.hpp"
#include <power_grid_model_io_native_c/basics.h>
#include <power_grid_model_io_native_c/vnf_pgm_converter.h>
#include <power_grid_model_io_native_c/pgm_vnf_converter.h>

#include <power_grid_model/auxiliary/dataset.hpp>
#include <power_grid_model/common/exception.hpp>

namespace pgm_io = power_grid_model_io_native;

struct PGM_IO_VnfPgmConverter : public pgm_io::PgmVnfConverter {
struct PGM_IO_PgmVnfConverter : public pgm_io::PgmVnfConverter {
using PgmVnfConverter::PgmVnfConverter;
};

PGM_IO_VnfPgmConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char const* file_buffer,
PGM_IO_PgmVnfConverter* PGM_IO_create_pgm_vnf_converter(PGM_IO_Handle* handle, char const* file_buffer,
PGM_IO_ExperimentalFeatures experimental_features) {
return call_with_catch(
handle,
@@ -37,16 +37,16 @@ PGM_IO_VnfPgmConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char
default:
throw power_grid_model::MissingCaseForEnumError{"PGM_IO_create_vnf_converter", experimental_features};
}
auto* converter = new PGM_IO_VnfPgmConverter(file_buffer, experimental_feature);
auto* converter = new PGM_IO_PgmVnfConverter(file_buffer, experimental_feature);
parse_vnf_file_wrapper(converter);
return converter;
},
PGM_IO_regular_error);
}

char const* PGM_IO_vnf_pgm_converter_get_input_data(PGM_IO_Handle* handle, PGM_IO_VnfPgmConverter* converter_ptr) {
char const* PGM_IO_pgm_vnf_converter_get_input_data(PGM_IO_Handle* handle, PGM_IO_PgmVnfConverter* converter_ptr) {
return call_with_catch(
handle, [converter_ptr] { return convert_input_wrapper(converter_ptr).c_str(); }, PGM_IO_regular_error);
}

void PGM_IO_destroy_vnf_converter(PGM_IO_VnfPgmConverter* converter_ptr) { delete converter_ptr; }
void PGM_IO_destroy_pgm_vnf_converter(PGM_IO_PgmVnfConverter* converter_ptr) { delete converter_ptr; }
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -178,7 +178,7 @@ def generate_build_ext(pkg_dir: Path, pkg_name: str):
libraries: list[str] = []
sources = [
str(pgm_io_c / pgm_io_c / "src" / "handle.cpp"),
str(pgm_io_c / pgm_io_c / "src" / "vnf_pgm_converter.cpp"),
str(pgm_io_c / pgm_io_c / "src" / "pgm_vnf_converter.cpp"),
]
# macro
define_macros = [
Original file line number Diff line number Diff line change
@@ -23,22 +23,22 @@ def __new__(
):
instance = super().__new__(cls)

instance._pgm_vnf_converter = pgmic.create_vnf_converter(string_buffer, experimental_feature)
instance._pgm_vnf_converter = pgmic.create_pgm_vnf_converter(string_buffer, experimental_feature)
assert_no_error()

return instance

def __del__(self):
if hasattr(self, "_pgm_vnf_converter"):
pgmic.destroy_vnf_converter(self._pgm_vnf_converter)
pgmic.destroy_pgm_vnf_converter(self._pgm_vnf_converter)

def get_pgm_input_data(self):
"""A function of the PgmVnfConverter class which will convert and return the data in PGM format

Returns:
str: json data in PGM format
"""
pgm_data = pgmic.vnf_pgm_converter_get_input_data(self._pgm_vnf_converter)
pgm_data = pgmic.pgm_vnf_converter_get_input_data(self._pgm_vnf_converter)
assert_no_error()
self._serialized_data = pgm_data
return self._serialized_data
Original file line number Diff line number Diff line change
@@ -146,15 +146,15 @@ def error_message(self) -> str: # type: ignore[empty-body]
pass # pragma: no cover

@make_c_binding
def create_vnf_converter(self, data: str, experim_feature: int) -> PgmVnfConverterPtr: # type: ignore[empty-body]
def create_pgm_vnf_converter(self, data: str, experim_feature: int) -> PgmVnfConverterPtr: # type: ignore[empty-body]
pass # pragma: no cover

@make_c_binding
def vnf_pgm_converter_get_input_data(self, pgmvnfconverter: PgmVnfConverterPtr) -> str: # type: ignore[empty-body]
def pgm_vnf_converter_get_input_data(self, pgmvnfconverter: PgmVnfConverterPtr) -> str: # type: ignore[empty-body]
pass # pragma: no cover

@make_c_binding
def destroy_vnf_converter(self, pgmvnfconverter: PgmVnfConverterPtr) -> None: # type: ignore[empty-body]
def destroy_pgm_vnf_converter(self, pgmvnfconverter: PgmVnfConverterPtr) -> None: # type: ignore[empty-body]
pass # pragma: no cover


2 changes: 1 addition & 1 deletion tests/c_api_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
set(PROJECT_SOURCES
"test_c_api.cpp"
"test_entry_point.cpp"
"test_c_api_vnf_converter.cpp"
"test_c_api_pgm_vnf_converter.cpp"
)

add_executable(power_grid_model_io_native_c_api_tests ${PROJECT_SOURCES})
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

#include <power_grid_model_io_native_c/basics.h>
#include <power_grid_model_io_native_c/handle.h>
#include <power_grid_model_io_native_c/vnf_pgm_converter.h>
#include <power_grid_model_io_native_c/pgm_vnf_converter.h>

#include <power_grid_model/common/exception.hpp>

@@ -22,18 +22,18 @@ TEST_CASE("Test PGM_IO_create_vnf_converter") {

SUBCASE("Test PGM_IO_create_vnf_converter without experimental feature flag") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
auto converter = PGM_IO_create_vnf_converter(handle, "", experimental_feature_flag);
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_vnf_converter(converter);
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_vnf_converter(handle, "", experimental_feature_flag);
auto converter = PGM_IO_create_pgm_vnf_converter(handle, "", experimental_feature_flag);
CHECK(converter != nullptr);
PGM_IO_destroy_vnf_converter(converter);
PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}
}
@@ -42,14 +42,14 @@ TEST_CASE("Test PGM_IO_get_vnf_input_data") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
PGM_IO_ExperimentalFeatures experimental_feature_flag = PGM_IO_experimental_features_enabled;

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

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

PGM_IO_destroy_vnf_converter(converter);
PGM_IO_destroy_pgm_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}

2 changes: 1 addition & 1 deletion tests/cpp_unit_tests/test_pgm_vnf_converter.cpp
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
// SPDX-License-Identifier: MPL-2.0

#include <power_grid_model_io_native/common/enum.hpp>
#include <power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp>
#include <power_grid_model_io_native/pgm_vnf_converter/pgm_vnf_converter.hpp>

#include <power_grid_model/auxiliary/dataset.hpp>
#include <power_grid_model/auxiliary/meta_data.hpp>
2 changes: 1 addition & 1 deletion tests/unit/test_vnf_converter.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
import pytest

from power_grid_model_io_native._core.error_handling import InvalidArguments, assert_no_error
from power_grid_model_io_native._core.vnf_converter import PgmVnfConverter
from power_grid_model_io_native._core.pgm_vnf_converter import PgmVnfConverter


def test_pgmvnfconverter_constructor_without_experimental_features():