From 4d4820f05aaeea8945b6f31f5e6371a0d5073782 Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Wed, 11 Dec 2024 16:04:55 +0100 Subject: [PATCH] rename modelConverter --- src/solver/CMakeLists.txt | 1 - src/solver/modelConverter/CMakeLists.txt | 29 -- .../modelConverter/convertorVisitor.cpp | 297 ------------------ .../solver/modelConverter/convertorVisitor.h | 32 -- .../solver/modelConverter/modelConverter.h | 54 ---- src/solver/modelConverter/modelConverter.cpp | 217 ------------- 6 files changed, 630 deletions(-) delete mode 100644 src/solver/modelConverter/CMakeLists.txt delete mode 100644 src/solver/modelConverter/convertorVisitor.cpp delete mode 100644 src/solver/modelConverter/include/antares/solver/modelConverter/convertorVisitor.h delete mode 100644 src/solver/modelConverter/include/antares/solver/modelConverter/modelConverter.h delete mode 100644 src/solver/modelConverter/modelConverter.cpp diff --git a/src/solver/CMakeLists.txt b/src/solver/CMakeLists.txt index acc17b3443..b37cb7f1cf 100644 --- a/src/solver/CMakeLists.txt +++ b/src/solver/CMakeLists.txt @@ -16,7 +16,6 @@ add_subdirectory(lps) add_subdirectory(misc) add_subdirectory(modelConverter) add_subdirectory(modelParser) -add_subdirectory(systemConverter) add_subdirectory(systemParser) add_subdirectory(modeler) add_subdirectory(optimisation) diff --git a/src/solver/modelConverter/CMakeLists.txt b/src/solver/modelConverter/CMakeLists.txt deleted file mode 100644 index 005a47e55d..0000000000 --- a/src/solver/modelConverter/CMakeLists.txt +++ /dev/null @@ -1,29 +0,0 @@ -set(SOURCES - modelConverter.cpp - convertorVisitor.cpp - include/antares/solver/modelConverter/modelConverter.h - include/antares/solver/modelConverter/convertorVisitor.h -) - -# Create the library -add_library(modelConverter STATIC ${SOURCES}) -add_library(Antares::modelConverter ALIAS modelConverter) - -# Specify include directories -target_include_directories(modelConverter - PUBLIC - $ -) - -# Link dependencies (if any) -target_link_libraries(modelConverter - PRIVATE - Antares::antares-study-system-model - Antares::modelParser - Antares::antlr-interface - Antares::solver-expressions -) - -install(DIRECTORY include/antares - DESTINATION "include" -) diff --git a/src/solver/modelConverter/convertorVisitor.cpp b/src/solver/modelConverter/convertorVisitor.cpp deleted file mode 100644 index 6a9267f142..0000000000 --- a/src/solver/modelConverter/convertorVisitor.cpp +++ /dev/null @@ -1,297 +0,0 @@ -/* - * 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 -#include - -#include "ExprLexer.h" -#include "ExprParser.h" -#include "antlr4-runtime.h" - -namespace Antares::Solver::ModelConverter -{ - -using namespace Antares::Solver::Nodes; - -/// Visitor to convert ANTLR expressions to Antares::Solver::Nodes -class ConvertorVisitor: public ExprVisitor -{ -public: - ConvertorVisitor(Registry& registry, const ModelParser::Model& model); - - std::any visit(antlr4::tree::ParseTree* tree) override; - - std::any visitIdentifier(ExprParser::IdentifierContext* context) override; - std::any visitMuldiv(ExprParser::MuldivContext* context) override; - std::any visitFullexpr(ExprParser::FullexprContext* context) override; - std::any visitShift(ExprParser::ShiftContext* context) override; - std::any visitNegation(ExprParser::NegationContext* context) override; - std::any visitExpression(ExprParser::ExpressionContext* context) override; - std::any visitComparison(ExprParser::ComparisonContext* context) override; - std::any visitAddsub(ExprParser::AddsubContext* context) override; - std::any visitPortField(ExprParser::PortFieldContext* context) override; - std::any visitNumber(ExprParser::NumberContext* context) override; - std::any visitTimeIndex(ExprParser::TimeIndexContext* context) override; - std::any visitTimeShift(ExprParser::TimeShiftContext* context) override; - std::any visitFunction(ExprParser::FunctionContext* context) override; - - std::any visitTimeSum(ExprParser::TimeSumContext* context) override; - std::any visitAllTimeSum(ExprParser::AllTimeSumContext* context) override; - std::any visitSignedAtom(ExprParser::SignedAtomContext* context) override; - std::any visitUnsignedAtom(ExprParser::UnsignedAtomContext* context) override; - std::any visitRightAtom(ExprParser::RightAtomContext* context) override; - std::any visitSignedExpression(ExprParser::SignedExpressionContext* context) override; - std::any visitShiftAddsub(ExprParser::ShiftAddsubContext* context) override; - std::any visitShiftMuldiv(ExprParser::ShiftMuldivContext* context) override; - std::any visitRightMuldiv(ExprParser::RightMuldivContext* context) override; - std::any visitRightExpression(ExprParser::RightExpressionContext* context) override; - -private: - Registry& registry_; - const ModelParser::Model& model_; -}; - -NodeRegistry convertExpressionToNode(const std::string& exprStr, const ModelParser::Model& model) -{ - if (exprStr.empty()) - { - return {}; - } - antlr4::ANTLRInputStream input(exprStr); - ExprLexer lexer(&input); - antlr4::CommonTokenStream tokens(&lexer); - ExprParser parser(&tokens); - - ExprParser::ExprContext* tree = parser.expr(); - Antares::Solver::Registry registry; - ConvertorVisitor visitor(registry, model); - Node* root = std::any_cast(visitor.visit(tree)); - return NodeRegistry(root, std::move(registry)); -} - -ConvertorVisitor::ConvertorVisitor(Antares::Solver::Registry& registry, - const ModelParser::Model& model): - registry_(registry), - model_(model) -{ -} - -std::any ConvertorVisitor::visit(antlr4::tree::ParseTree* tree) -{ - return tree->accept(this); -} - -class NoParameterOrVariableWithThisName: public std::runtime_error -{ -public: - explicit NoParameterOrVariableWithThisName(const std::string& name): - runtime_error("No parameter or variable found for this identifier: " + name) - { - } -}; - -std::any ConvertorVisitor::visitIdentifier(ExprParser::IdentifierContext* context) -{ - for (const auto& param: model_.parameters) - { - if (param.id == context->IDENTIFIER()->getText()) - { - return static_cast(registry_.create(param.id)); - } - } - - for (const auto& var: model_.variables) - { - if (var.id == context->getText()) - { - return static_cast(registry_.create(var.id)); - } - } - - throw NoParameterOrVariableWithThisName(context->getText()); -} - -std::any ConvertorVisitor::visitMuldiv(ExprParser::MuldivContext* context) -{ - auto* left = std::any_cast(visit(context->expr(0))); - auto* right = std::any_cast(visit(context->expr(1))); - - std::string op = context->op->getText(); - return (op == "*") ? static_cast(registry_.create(left, right)) - : static_cast(registry_.create(left, right)); -} - -std::any ConvertorVisitor::visitFullexpr(ExprParser::FullexprContext* context) -{ - return context->expr()->accept(this); -} - -std::any ConvertorVisitor::visitNegation(ExprParser::NegationContext* context) -{ - Node* n = std::any_cast(context->expr()->accept(this)); - return static_cast(registry_.create(n)); -} - -std::any ConvertorVisitor::visitExpression(ExprParser::ExpressionContext* context) -{ - return context->expr()->accept(this); -} - -std::any ConvertorVisitor::visitComparison(ExprParser::ComparisonContext* context) -{ - Node* left = std::any_cast(visit(context->expr(0))); - Node* right = std::any_cast(visit(context->expr(1))); - - std::string op = context->COMPARISON()->getText(); - if (op == "=") - { - return static_cast(registry_.create(left, right)); - } - else if (op == "<=") - { - return static_cast(registry_.create(left, right)); - } - else - { - return static_cast(registry_.create(left, right)); - } -} - -std::any ConvertorVisitor::visitAddsub(ExprParser::AddsubContext* context) -{ - Node* left = std::any_cast(visit(context->expr(0))); - Node* right = std::any_cast(visit(context->expr(1))); - - std::string op = context->op->getText(); - return (op == "+") ? static_cast(registry_.create(left, right)) - : static_cast(registry_.create(left, right)); -} - -class NotImplemented: public std::runtime_error -{ -public: - using std::runtime_error::runtime_error; -}; - -// TODO implement this -std::any ConvertorVisitor::visitPortField([[maybe_unused]] ExprParser::PortFieldContext* context) -{ - throw NotImplemented("Node portfield not implemented yet"); -} - -std::any ConvertorVisitor::visitNumber(ExprParser::NumberContext* context) -{ - double d = stod(context->getText()); - return static_cast(registry_.create(d)); -} - -// TODO implement this -std::any ConvertorVisitor::visitTimeIndex([[maybe_unused]] ExprParser::TimeIndexContext* context) -{ - throw NotImplemented("Node time index not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitTimeShift([[maybe_unused]] ExprParser::TimeShiftContext* context) -{ - throw NotImplemented("Node time shift not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitFunction([[maybe_unused]] ExprParser::FunctionContext* context) -{ - throw NotImplemented("Node function not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitTimeSum([[maybe_unused]] ExprParser::TimeSumContext* context) -{ - throw NotImplemented("Node time sum not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitAllTimeSum([[maybe_unused]] ExprParser::AllTimeSumContext* context) -{ - throw NotImplemented("Node all time sum not implemented yet"); -} - -// shift related, not tested -std::any ConvertorVisitor::visitSignedAtom(ExprParser::SignedAtomContext* context) -{ - auto a = context->atom()->accept(this); - if (context->op->getText() == "-") - { - return static_cast(registry_.create(std::any_cast(a))); - } - return a; -} - -std::any ConvertorVisitor::visitUnsignedAtom(ExprParser::UnsignedAtomContext* context) -{ - return context->atom()->accept(this); -} - -// TODO implement this -std::any ConvertorVisitor::visitRightAtom([[maybe_unused]] ExprParser::RightAtomContext* context) -{ - throw NotImplemented("Node right atom not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitShift([[maybe_unused]] ExprParser::ShiftContext* context) -{ - throw NotImplemented("Node shift not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitShiftAddsub( - [[maybe_unused]] ExprParser::ShiftAddsubContext* context) -{ - throw NotImplemented("Node shift add sub not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitShiftMuldiv( - [[maybe_unused]] ExprParser::ShiftMuldivContext* context) -{ - throw NotImplemented("Node shift mul div not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitRightMuldiv( - [[maybe_unused]] ExprParser::RightMuldivContext* context) -{ - throw NotImplemented("Node right mul div not implemented yet"); -} - -// TODO implement this -std::any ConvertorVisitor::visitSignedExpression( - [[maybe_unused]] ExprParser::SignedExpressionContext* context) -{ - throw NotImplemented("Node signed expression not implemented yet"); -} - -std::any ConvertorVisitor::visitRightExpression(ExprParser::RightExpressionContext* context) -{ - return context->expr()->accept(this); -} - -} // namespace Antares::Solver::ModelConverter diff --git a/src/solver/modelConverter/include/antares/solver/modelConverter/convertorVisitor.h b/src/solver/modelConverter/include/antares/solver/modelConverter/convertorVisitor.h deleted file mode 100644 index 1b8dd34643..0000000000 --- a/src/solver/modelConverter/include/antares/solver/modelConverter/convertorVisitor.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 "antares/solver/modelParser/Library.h" - -namespace Antares::Solver::ModelConverter -{ -NodeRegistry convertExpressionToNode(const std::string& exprStr, const ModelParser::Model& model); -} // namespace Antares::Solver::ModelConverter diff --git a/src/solver/modelConverter/include/antares/solver/modelConverter/modelConverter.h b/src/solver/modelConverter/include/antares/solver/modelConverter/modelConverter.h deleted file mode 100644 index ffa0b66a71..0000000000 --- a/src/solver/modelConverter/include/antares/solver/modelConverter/modelConverter.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 "antares/solver/modelParser/Library.h" - -namespace Antares -{ -namespace Study::SystemModel -{ -class Library; -} - -namespace Solver::ModelParser -{ -class Library; -} -} // namespace Antares - -namespace Antares::Solver::Nodes -{ -class Node; -} - -namespace Antares::Solver::ModelConverter -{ -class UnknownTypeException: public std::runtime_error -{ -public: - explicit UnknownTypeException(ModelParser::ValueType type); -}; - -Antares::Study::SystemModel::Library convert(const Antares::Solver::ModelParser::Library& library); -} // namespace Antares::Solver::ModelConverter diff --git a/src/solver/modelConverter/modelConverter.cpp b/src/solver/modelConverter/modelConverter.cpp deleted file mode 100644 index c8b3d856c9..0000000000 --- a/src/solver/modelConverter/modelConverter.cpp +++ /dev/null @@ -1,217 +0,0 @@ -/* - * 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/modelConverter/modelConverter.h" - -#include "antares/solver/modelConverter/convertorVisitor.h" -#include "antares/study/system-model/constraint.h" -#include "antares/study/system-model/expression.h" -#include "antares/study/system-model/library.h" -#include "antares/study/system-model/model.h" -#include "antares/study/system-model/parameter.h" -#include "antares/study/system-model/port.h" -#include "antares/study/system-model/portType.h" -#include "antares/study/system-model/variable.h" - -namespace Antares::Solver::ModelConverter -{ -UnknownTypeException::UnknownTypeException(ModelParser::ValueType type): - std::runtime_error("Unknown type: " + ModelParser::toString(type)) -{ -} - -/** - * \brief Converts parameters from ModelParser::Model to SystemModel::Parameter. - * - * \param model The ModelParser::Model object containing parameters. - * \return A vector of SystemModel::Parameter objects. - */ -std::vector convertTypes( - const Antares::Solver::ModelParser::Library& library) -{ - // Convert portTypes to Antares::Study::SystemModel::PortType - std::vector out; - for (const auto& portType: library.port_types) - { - std::vector 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)); - out.emplace_back(std::move(portTypeModel)); - } - return out; -} - -/** - * \brief Converts a ModelParser::ValueType to an SystemModel::ValueType. - * - * \param type The ModelParser::ValueType to convert. - * \return The corresponding SystemModel::ValueType. - * \throws UnknownType if the type is unknown. - */ -std::vector convertParameters( - const Antares::Solver::ModelParser::Model& model) -{ - std::vector parameters; - for (const auto& parameter: model.parameters) - { - parameters.emplace_back( - parameter.id, - static_cast( - parameter.time_dependent), - static_cast( - parameter.scenario_dependent)); - } - return parameters; -} - -/** - * \brief Converts variables from ModelParser::Model to SystemModel::Variable. - * - * \param model The ModelParser::Model object containing variables. - * \return A vector of SystemModel::Variable objects. - */ -Antares::Study::SystemModel::ValueType convertType(Antares::Solver::ModelParser::ValueType type) -{ - using namespace std::string_literals; - switch (type) - { - case Antares::Solver::ModelParser::ValueType::CONTINUOUS: - return Antares::Study::SystemModel::ValueType::FLOAT; - case Antares::Solver::ModelParser::ValueType::INTEGER: - return Antares::Study::SystemModel::ValueType::INTEGER; - case Antares::Solver::ModelParser::ValueType::BOOL: - return Antares::Study::SystemModel::ValueType::BOOL; - default: - throw UnknownTypeException(type); - } -} - -/** - * \brief Converts ports from ModelParser::Model to SystemModel::Port. - * - * \param model The ModelParser::Model object containing ports. - * \return A vector of SystemModel::Port objects. - */ -std::vector convertVariables(const ModelParser::Model& model) -{ - std::vector variables; - for (const auto& variable: model.variables) - { - Antares::Study::SystemModel::Expression lb(variable.lower_bound, - convertExpressionToNode(variable.lower_bound, - model)); - Antares::Study::SystemModel::Expression ub(variable.upper_bound, - convertExpressionToNode(variable.upper_bound, - model)); - variables.emplace_back(variable.id, - std::move(lb), - std::move(ub), - convertType(variable.variable_type)); - } - - return variables; -} - -/** - * \brief Converts constraints from ModelParser::Model to SystemModel::Constraint. - * - * \param model The ModelParser::Model object containing constraints. - * \return A vector of SystemModel::Constraint objects. - */ -std::vector convertPorts( - [[maybe_unused]] const Antares::Solver::ModelParser::Model& model) -{ - return {}; -} - -std::vector convertConstraints( - const Antares::Solver::ModelParser::Model& model) -{ - std::vector constraints; - for (const auto& constraint: model.constraints) - { - auto expr = convertExpressionToNode(constraint.expression, model); - constraints.emplace_back(constraint.id, - Antares::Study::SystemModel::Expression{constraint.expression, - std::move(expr)}); - } - return constraints; -} - -/** - * \brief Converts models from ModelParser::Library to SystemModel::Model. - * - * \param library The ModelParser::Library object containing models. - * \return A vector of SystemModel::Model objects. - */ -std::vector convertModels( - const Antares::Solver::ModelParser::Library& library) -{ - std::vector models; - for (const auto& model: library.models) - { - Antares::Study::SystemModel::ModelBuilder modelBuilder; - std::vector parameters = convertParameters(model); - std::vector variables = convertVariables(model); - std::vector ports = convertPorts(model); - std::vector constraints = convertConstraints( - model); - - auto nodeObjective = convertExpressionToNode(model.objective, model); - - auto modelObj = modelBuilder.withId(model.id) - .withObjective( - Antares::Study::SystemModel::Expression{model.objective, - std::move(nodeObjective)}) - .withParameters(std::move(parameters)) - .withVariables(std::move(variables)) - .withPorts(std::move(ports)) - .withConstraints(std::move(constraints)) - .build(); - models.emplace_back(std::move(modelObj)); - } - return models; -} - -/** - * \brief Converts a ModelParser::Library object to an SystemModel::Library object. - * - * \param library The ModelParser::Library object to convert. - * \return The corresponding SystemModel::Library object. - */ -Antares::Study::SystemModel::Library convert(const Antares::Solver::ModelParser::Library& library) -{ - Antares::Study::SystemModel::LibraryBuilder builder; - std::vector portTypes = convertTypes(library); - std::vector models = convertModels(library); - Antares::Study::SystemModel::Library lib = builder.withId(library.id) - .withDescription(library.description) - .withPortTypes(std::move(portTypes)) - .withModels(std::move(models)) - .build(); - return lib; -} -} // namespace Antares::Solver::ModelConverter