Skip to content

Commit

Permalink
Merge pull request #11 from PowerGridModel/feature/workflows
Browse files Browse the repository at this point in the history
Adjustment to c api
  • Loading branch information
mgovers authored Oct 17, 2024
2 parents bec85e5 + fd4a65b commit 1274760
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 56 deletions.
7 changes: 6 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ cppcoreguidelines-*,
-cppcoreguidelines-avoid-c-arrays,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-member-init,
-cppcoreguidelines-pro-type-reinterpret-cast,
darwin-*,
hiccp-*,
llvm-*,
-llvm-header-guard,
google-*,
-google-build-using-namespace,
-google-explicit-constructor
-google-default-arguments,
-google-explicit-constructor,
-google-readability-todo,
misc-*,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
modernize-*,
-modernize-avoid-c-arrays,
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

[submodule "deps/power-grid-model"]
path = deps/power-grid-model
url = git@github.com:PowerGridModel/power-grid-model.git
url = https://github.com/PowerGridModel/power-grid-model.git
branch = main
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,52 @@
#define POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP

#include <power_grid_model/auxiliary/dataset.hpp>
#include <power_grid_model/auxiliary/meta_data_gen.hpp>
#include <power_grid_model/auxiliary/serialization/serializer.hpp>
#include <power_grid_model/common/exception.hpp>
#include <power_grid_model/component/node.hpp>
#include <power_grid_model/container.hpp>

#include <iostream>

inline power_grid_model::ConstDataset
create_const_dataset_from_container(power_grid_model::Container<power_grid_model::Node> const& /*container*/,
power_grid_model::meta_data::MetaData const& meta_data) {
// for now leave it empty
std::string_view const dataset_name = "empty_dataset";
power_grid_model::ConstDataset const_dataset{false, 1, dataset_name, meta_data};
return const_dataset;
}

inline std::string serialize_data(power_grid_model::ConstDataset const& const_dataset) {
power_grid_model::meta_data::Serializer serializer(const_dataset, power_grid_model::SerializationFormat::json);
std::string serialized_pgm_data = serializer.get_string(false, -1);
return serialized_pgm_data;
}

class PgmVnfConverter {
public:
PgmVnfConverter(char* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr);

// Public member functions
void parse_vnf_file();
power_grid_model::ConstDataset const* convert_input(power_grid_model::ConstDataset const* dataset);
void convert_input();

std::string const& get_serialized_data() const;
void set_file_buffer(char* file_buffer);
void set_deserialized_data(power_grid_model::WritableDataset* deserialized_data);
char* get_file_buffer();
power_grid_model::WritableDataset* get_deserialized_data();

private:
// Private attributes
char* f_file_buffer;
power_grid_model::WritableDataset*
deserialized_data; // this type because it is generated by a deserializer type structure

// Private setters/getters
void set_file_buffer(char* file_buffer);

void set_deserialized_data(power_grid_model::WritableDataset* deserialized_data);

char* get_file_buffer();

power_grid_model::WritableDataset* get_deserialized_data();
std::string serialized_data;

// Private member functions
void convert_node_input();
static std::vector<power_grid_model::Node> convert_node_input();
void convert_line_input();
void convert_sources_input();
void convert_sym_loads_input();
Expand All @@ -58,9 +75,8 @@ inline void PgmVnfConverter::parse_vnf_file() {
// will be implemented later
}

inline power_grid_model::ConstDataset const*
PgmVnfConverter::convert_input(power_grid_model::ConstDataset const* /*dataset*/) {
convert_node_input();
inline void PgmVnfConverter::convert_input() {
std::vector<power_grid_model::Node> const nodes = convert_node_input();
convert_line_input();
convert_sources_input();
convert_sym_loads_input();
Expand All @@ -69,11 +85,23 @@ PgmVnfConverter::convert_input(power_grid_model::ConstDataset const* /*dataset*/
convert_sym_gens_input();
convert_links_input();

// then return the buffer
// return pgm_input_data;
// for now.
power_grid_model::ConstDataset* fake_data = nullptr;
return fake_data;
power_grid_model::Container<power_grid_model::Node> container;

for (const auto& node : nodes) {
container.emplace<power_grid_model::Node>(node.id(), node);
}

constexpr const auto& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data;
power_grid_model::ConstDataset const const_dataset = create_const_dataset_from_container(container, meta_data);

std::string const serialized_pgm_data = serialize_data(const_dataset);

// 1. our vnf importer it directly understands the vnf format
// 2. convert vnf like dataset to internal types (take raw data and convert it to pgm component container)
// 3. another function which 1. makes const dataset view from pgm component container 2. const dataset to
// serializer

this->serialized_data = serialized_pgm_data;
}

inline void PgmVnfConverter::set_file_buffer(char* file_buffer) { this->f_file_buffer = file_buffer; }
Expand All @@ -86,8 +114,18 @@ inline char* PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; }

inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_data() { return this->deserialized_data; }

inline void PgmVnfConverter::convert_node_input() {
// Implementation
inline std::string const& PgmVnfConverter::get_serialized_data() const { return this->serialized_data; }

inline std::vector<power_grid_model::Node> PgmVnfConverter::convert_node_input() {
std::vector<power_grid_model::NodeInput> const node_inputs = {{1, 110.0}};
std::vector<power_grid_model::Node> nodes;

nodes.reserve(node_inputs.size());
for (const auto& node_input : node_inputs) {
nodes.emplace_back(node_input);
}

return nodes;
}

inline void PgmVnfConverter::convert_line_input() {
Expand Down Expand Up @@ -120,9 +158,9 @@ inline void PgmVnfConverter::convert_links_input() {

inline void parse_vnf_file_wrapper(PgmVnfConverter* obj) { obj->parse_vnf_file(); }

inline power_grid_model::ConstDataset const* convert_input_wrapper(PgmVnfConverter* obj,
power_grid_model::ConstDataset const* dataset) {
return obj->convert_input(dataset);
inline std::string const& convert_input_wrapper(PgmVnfConverter* obj) {
obj->convert_input();
return obj->get_serialized_data();
}

#endif // POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ typedef int32_t PGM_IO_ID;
*/
typedef struct PGM_IO_VnfConverter PGM_IO_VnfConverter;

/**
* @brief Opaque struct for the ConstDataset.
*
*/
typedef struct PGM_IO_ConstDataset PGM_IO_ConstDataset;

/**
* @brief Opaque struct for the handle class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@ extern "C" {
#endif

/**
* @brief Create the PGM_VNF_converter
* @brief Create the PGM_IO_VnfConverter
* @param handle
* @param file_buffer A pointer to the null-terminated C string.
* @return The pointer to a PGM_VNF_converter instance. The instance must be freed by
* PGM_VNF_delete_Converter
* @return The pointer to a PGM_IO_VnfConverter instance. The instance must be freed by
* PGM_IO_destroy_vnf_converter.
*/
PGM_IO_API PGM_IO_VnfConverter* PGM_VNF_create_converter(PGM_IO_Handle* handle, char* file_buffer);
PGM_IO_API PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char* file_buffer);

/**
* @brief Retrieve the transformed input data from .vnf format to PGM format
* @param handle
* @param converter_ptr A pointer to a PGM_VNF_converter instace.
* @param converter_ptr A pointer to a PGM_IO_VnfConverter instace.
* @param dataset A pointer to the const dataset supplied by the user.
* @return The pointer to the const dataset instance supplied by the user which has been filled in.
*/
PGM_IO_API PGM_IO_ConstDataset const* PGM_VNF_get_input_data(PGM_IO_Handle* handle, PGM_IO_VnfConverter* converter_ptr,
PGM_IO_ConstDataset const* dataset);
PGM_IO_API char const* PGM_IO_get_vnf_input_data(PGM_IO_Handle* handle, PGM_IO_VnfConverter* converter_ptr);

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

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,19 @@

using power_grid_model::ConstDataset;

// TODO(Laurynas-Jagutis) add call_with_catch for these functions
PGM_IO_VnfConverter* PGM_VNF_create_converter(const PGM_IO_Handle* /*handle*/, char* file_buffer) {
auto* converter = new PgmVnfConverter(file_buffer);
struct PGM_IO_VnfConverter : public PgmVnfConverter {
using PgmVnfConverter::PgmVnfConverter;
};

// TODO(Laurynas-Jagutis) add call_with_catch for these functions.
PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(const PGM_IO_Handle* /*handle*/, char* file_buffer) {
auto* converter = new PGM_IO_VnfConverter(file_buffer);
parse_vnf_file_wrapper(converter);
return reinterpret_cast<PGM_IO_VnfConverter*>(converter);
return converter;
}

PGM_IO_ConstDataset const* PGM_VNF_get_input_data(const PGM_IO_Handle* /*handle*/, PGM_IO_VnfConverter* converter_ptr,
PGM_IO_ConstDataset const* dataset) {
auto* converter = reinterpret_cast<PgmVnfConverter*>(converter_ptr);
auto const* data = reinterpret_cast<ConstDataset const*>(dataset);
convert_input_wrapper(converter, data);
return reinterpret_cast<PGM_IO_ConstDataset const*>(data);
char const* PGM_IO_get_vnf_input_data(const PGM_IO_Handle* /*handle*/, PGM_IO_VnfConverter* converter_ptr) {
return convert_input_wrapper(converter_ptr).c_str();
}

void PGM_VNF_delete_Converter(PGM_IO_VnfConverter* converter_ptr) {
auto* converter = reinterpret_cast<PgmVnfConverter*>(converter_ptr);
delete converter;
}
void PGM_IO_destroy_vnf_converter(PGM_IO_VnfConverter* converter_ptr) { delete converter_ptr; }

0 comments on commit 1274760

Please sign in to comment.