diff --git a/src/solver/systemParser/CMakeLists.txt b/src/solver/systemParser/CMakeLists.txt new file mode 100644 index 0000000000..e48912f901 --- /dev/null +++ b/src/solver/systemParser/CMakeLists.txt @@ -0,0 +1,27 @@ +find_package(yaml-cpp REQUIRED) + +set(SOURCES + parser.cpp + encoders.hxx + include/antares/solver/systemParser/parser.h +) + +# Create the library +add_library(systemParser STATIC ${SOURCES}) +add_library(Antares::systemParser ALIAS systemParser) + +# Specify include directories +target_include_directories(systemParser + PUBLIC + $ +) + +# Link dependencies (if any) +target_link_libraries(systemParser + PRIVATE + yaml-cpp +) + +install(DIRECTORY include/antares + DESTINATION "include" +) diff --git a/src/solver/systemParser/encoders.hxx b/src/solver/systemParser/encoders.hxx new file mode 100644 index 0000000000..09287c06b5 --- /dev/null +++ b/src/solver/systemParser/encoders.hxx @@ -0,0 +1,218 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once + +#include "antares/solver/modelParser/Library.h" + +#include "yaml-cpp/yaml.h" + +// Implement convert specializations +namespace YAML +{ + +/** + * @brief shortend to default construct a value when node is null + * @tparam T Type to convert the node to + * @param n node + * @return Object of type T + * It's just to simplify repertitve and verbose lines + * as_fallback_default>( +node["parameters"]) is equivalent to + node["parameters"].as>(std::vector()) + */ +template +inline T as_fallback_default(const Node& n) +{ + return n.as(T()); +} + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::Parameter& rhs) + { + if (!node.IsMap()) + { + return false; + } + rhs.id = node["id"].as(); + rhs.time_dependent = node["time-dependent"].as(true); + rhs.scenario_dependent = node["scenario-dependent"].as(true); + return true; + } +}; + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::ValueType& rhs) + { + if (!node.IsScalar()) + { + return false; + } + const auto value = node.as(); + if (value == "continuous") + { + rhs = Antares::Solver::ModelParser::ValueType::CONTINUOUS; + } + else if (value == "integer") + { + rhs = Antares::Solver::ModelParser::ValueType::INTEGER; + } + else if (value == "boolean") + { + rhs = Antares::Solver::ModelParser::ValueType::BOOL; + } + else + { + return false; + } + return true; + } +}; + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::Variable& rhs) + { + if (!node.IsMap()) + { + return false; + } + rhs.id = node["id"].as(); + rhs.lower_bound = node["lower-bound"].as(""); + rhs.upper_bound = node["upper-bound"].as(""); + rhs.variable_type = node["variable-type"].as( + Antares::Solver::ModelParser::ValueType::CONTINUOUS); + return true; + } +}; + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::Port& rhs) + { + if (!node.IsMap()) + { + return false; + } + rhs.id = node["id"].as(); + rhs.type = node["type"].as(); + return true; + } +}; + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::PortFieldDefinition& rhs) + { + if (!node.IsMap()) + { + return false; + } + rhs.port = node["port"].as(); + rhs.field = node["field"].as(); + rhs.definition = node["definition"].as(); + return true; + } +}; + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::Constraint& rhs) + { + if (!node.IsMap()) + { + return false; + } + rhs.id = node["id"].as(); + rhs.expression = node["expression"].as(); + return true; + } +}; + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::Model& rhs) + { + if (!node.IsMap()) + { + return false; + } + rhs.id = node["id"].as(); + rhs.description = node["description"].as(""); + rhs.parameters = as_fallback_default>( + node["parameters"]); + rhs.variables = as_fallback_default>( + node["variables"]); + rhs.ports = as_fallback_default>( + node["ports"]); + rhs.port_field_definitions = as_fallback_default< + std::vector>( + node["port-field-definitions"]); + rhs.constraints = as_fallback_default< + std::vector>(node["constraints"]); + rhs.objective = node["objective"].as(""); + return true; + } +}; + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::PortType& rhs) + { + if (!node.IsMap()) + { + return false; + } + rhs.id = node["id"].as(); + rhs.description = node["description"].as(""); + for (const auto& field: node["fields"]) + { + rhs.fields.push_back(field["id"].as()); + } + return true; + } +}; + +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelParser::Library& rhs) + { + rhs.id = node["id"].as(); + rhs.description = node["description"].as(""); + rhs.port_types = as_fallback_default>( + node["port-types"]); + rhs.models = node["models"].as>(); + return true; + } +}; +} // namespace YAML diff --git a/src/solver/systemParser/include/antares/solver/systemParser/Library.h b/src/solver/systemParser/include/antares/solver/systemParser/Library.h new file mode 100644 index 0000000000..c2a881b561 --- /dev/null +++ b/src/solver/systemParser/include/antares/solver/systemParser/Library.h @@ -0,0 +1,116 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once +#include +#include +#include + +namespace Antares::Solver::ModelParser +{ +// Define structures +struct Parameter +{ + std::string id; + bool time_dependent; + bool scenario_dependent; +}; + +enum class ValueType +{ + CONTINUOUS, + INTEGER, + BOOL +}; + +inline std::string toString(const ValueType& value_type) +{ + using namespace std::string_literals; + switch (value_type) + { + case ValueType::CONTINUOUS: + return "CONTINUOUS"s; + case ValueType::INTEGER: + return "INTEGER"s; + case ValueType::BOOL: + return "BOOL"s; + default: + return "UNKNOWN"s; + } +} + +struct Variable +{ + std::string id; + std::string lower_bound; + std::string upper_bound; + ValueType variable_type; +}; + +struct Port +{ + std::string id; + std::string type; +}; + +struct PortFieldDefinition +{ + std::string port; + std::string field; + std::string definition; +}; + +struct Constraint +{ + std::string id; + std::string expression; +}; + +struct Model +{ + std::string id; + std::string description; + std::vector parameters; + std::vector variables; + std::vector ports; + std::vector port_field_definitions; + std::vector constraints; + std::string objective; +}; + +struct PortType +{ + std::string id; + std::string description; + // Small optimization: we only need the name of the fields + // No need for an intermediate struct "field" with just a string "name" member + std::vector fields; +}; + +struct Library +{ + std::string id; + std::string description; + std::vector port_types; + std::vector models; +}; +} // namespace Antares::Solver::ModelParser diff --git a/src/solver/systemParser/include/antares/solver/systemParser/parser.h b/src/solver/systemParser/include/antares/solver/systemParser/parser.h new file mode 100644 index 0000000000..0c042ce027 --- /dev/null +++ b/src/solver/systemParser/include/antares/solver/systemParser/parser.h @@ -0,0 +1,36 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once +#include "antares/solver/modelParser/Library.h" + +namespace Antares::Solver::ModelParser +{ +class Parser +{ +public: + Parser() = default; + ~Parser() = default; + + Library parse(const std::string& content); +}; +} // namespace Antares::Solver::ModelParser diff --git a/src/solver/systemParser/parser.cpp b/src/solver/systemParser/parser.cpp new file mode 100644 index 0000000000..13d1b120eb --- /dev/null +++ b/src/solver/systemParser/parser.cpp @@ -0,0 +1,38 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include "antares/solver/modelParser/parser.h" + +#include "antares/solver/modelParser/Library.h" + +#include "encoders.hxx" + +namespace Antares::Solver::ModelParser +{ +Library Parser::parse(const std::string& content) +{ + YAML::Node root = YAML::Load(content); + + Library library = root["library"].as(); + + return library; +} +} // namespace Antares::Solver::ModelParser