Skip to content

Commit

Permalink
Base for system parser
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Dec 10, 2024
1 parent 001e6d5 commit 57c3088
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/solver/systemParser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

# Link dependencies (if any)
target_link_libraries(systemParser
PRIVATE
yaml-cpp
)

install(DIRECTORY include/antares
DESTINATION "include"
)
218 changes: 218 additions & 0 deletions src/solver/systemParser/encoders.hxx
Original file line number Diff line number Diff line change
@@ -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 <https://opensource.org/license/mpl-2-0/>.
*/

#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<std::vector<Antares::Solver::ModelParser::Parameter>>(
node["parameters"]) is equivalent to
node["parameters"].as<std::vector<Antares::Solver::ModelParser::Parameter>>(std::vector<Antares::Solver::ModelParser::Parameter>())
*/
template<typename T>
inline T as_fallback_default(const Node& n)
{
return n.as<T>(T());
}

template<>
struct convert<Antares::Solver::ModelParser::Parameter>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Parameter& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.time_dependent = node["time-dependent"].as<bool>(true);
rhs.scenario_dependent = node["scenario-dependent"].as<bool>(true);
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::ValueType>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::ValueType& rhs)
{
if (!node.IsScalar())
{
return false;
}
const auto value = node.as<std::string>();
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<Antares::Solver::ModelParser::Variable>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Variable& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.lower_bound = node["lower-bound"].as<std::string>("");
rhs.upper_bound = node["upper-bound"].as<std::string>("");
rhs.variable_type = node["variable-type"].as<Antares::Solver::ModelParser::ValueType>(
Antares::Solver::ModelParser::ValueType::CONTINUOUS);
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Port>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Port& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.type = node["type"].as<std::string>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::PortFieldDefinition>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::PortFieldDefinition& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.port = node["port"].as<std::string>();
rhs.field = node["field"].as<std::string>();
rhs.definition = node["definition"].as<std::string>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Constraint>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Constraint& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.expression = node["expression"].as<std::string>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Model>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Model& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.description = node["description"].as<std::string>("");
rhs.parameters = as_fallback_default<std::vector<Antares::Solver::ModelParser::Parameter>>(
node["parameters"]);
rhs.variables = as_fallback_default<std::vector<Antares::Solver::ModelParser::Variable>>(
node["variables"]);
rhs.ports = as_fallback_default<std::vector<Antares::Solver::ModelParser::Port>>(
node["ports"]);
rhs.port_field_definitions = as_fallback_default<
std::vector<Antares::Solver::ModelParser::PortFieldDefinition>>(
node["port-field-definitions"]);
rhs.constraints = as_fallback_default<
std::vector<Antares::Solver::ModelParser::Constraint>>(node["constraints"]);
rhs.objective = node["objective"].as<std::string>("");
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::PortType>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::PortType& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.description = node["description"].as<std::string>("");
for (const auto& field: node["fields"])
{
rhs.fields.push_back(field["id"].as<std::string>());
}
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Library>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Library& rhs)
{
rhs.id = node["id"].as<std::string>();
rhs.description = node["description"].as<std::string>("");
rhs.port_types = as_fallback_default<std::vector<Antares::Solver::ModelParser::PortType>>(
node["port-types"]);
rhs.models = node["models"].as<std::vector<Antares::Solver::ModelParser::Model>>();
return true;
}
};
} // namespace YAML
116 changes: 116 additions & 0 deletions src/solver/systemParser/include/antares/solver/systemParser/Library.h
Original file line number Diff line number Diff line change
@@ -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 <https://opensource.org/license/mpl-2-0/>.
*/

#pragma once
#include <iostream>
#include <string>
#include <vector>

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<Parameter> parameters;
std::vector<Variable> variables;
std::vector<Port> ports;
std::vector<PortFieldDefinition> port_field_definitions;
std::vector<Constraint> 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<std::string> fields;
};

struct Library
{
std::string id;
std::string description;
std::vector<PortType> port_types;
std::vector<Model> models;
};
} // namespace Antares::Solver::ModelParser
Loading

0 comments on commit 57c3088

Please sign in to comment.