-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48e1474
commit 8ddf4c8
Showing
12 changed files
with
191 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <antares-xpansion/lpnamer/input_reader/SettingsReader.h> | ||
|
||
#include <regex> | ||
|
||
#include "antares-xpansion/lpnamer/input_reader/Exceptions.h" | ||
#include "antares-xpansion/xpansion_interfaces/StringManip.h" | ||
|
||
SettingsReader::SettingsReader(const std::filesystem::path& file_path, | ||
ProblemGenerationLog::ProblemGenerationLogger* logger) | ||
: logger_(logger) | ||
{ | ||
check_file_exist(file_path); | ||
|
||
parse_file(file_path); | ||
settings_.try_emplace("solver", "CBC"); | ||
solver_name_ = settings_.at("solver"); | ||
|
||
} | ||
void SettingsReader::parse_file(const std::filesystem::path& file_path) { | ||
std::ifstream file(file_path); | ||
std::string line; | ||
std::regex regex_token("="); | ||
|
||
while (std::getline(file, line)) { | ||
if (std::find_if(line.begin(), line.end(), [](auto c) { return !std::isspace(c); }) == line.end()) { | ||
continue; // skip empty lines | ||
} | ||
std::vector<std::string> values; | ||
std::sregex_token_iterator it(line.begin(), line.end(), regex_token, -1); | ||
std::copy(it, std::sregex_token_iterator(), std::back_inserter(values)); | ||
if (values.size() != 2) { | ||
std::ostringstream msg; | ||
msg << "Invalid line in settings file : " << line | ||
<< std::endl; | ||
(*this->logger_)(LogUtils::LOGLEVEL::FATAL) << msg.str(); | ||
throw LogUtils::XpansionError<std::runtime_error>(msg.str(), LOGLOCATION); | ||
} | ||
this->settings_[StringManip::trim(values[0])] = StringManip::trim(values[1]); | ||
} | ||
} | ||
void SettingsReader::check_file_exist( | ||
const std::filesystem::path& file_path) const { | ||
if (file_path.empty() || !std::filesystem::exists(file_path)) { | ||
std::ostringstream msg; | ||
msg << LOGLOCATION | ||
<< "General data file is not found : " << file_path.string() | ||
<< std::endl; | ||
(*this->logger_)(LogUtils::LOGLEVEL::FATAL) << msg.str(); | ||
throw IniFileNotFound(msg.str()); | ||
} | ||
} | ||
std::string SettingsReader::Solver() { | ||
return solver_name_; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/cpp/lpnamer/input_reader/include/antares-xpansion/lpnamer/input_reader/Exceptions.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
class IniFileNotFound : public std::runtime_error { | ||
public: | ||
explicit IniFileNotFound(const std::string& msg) : std::runtime_error(msg) {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/cpp/lpnamer/input_reader/include/antares-xpansion/lpnamer/input_reader/SettingsReader.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include "antares-xpansion/lpnamer/helper/ProblemGenerationLogger.h" | ||
|
||
/** | ||
* A class to read the settings.ini file pertaining to antares-xpansion settings | ||
*/ | ||
class SettingsReader { | ||
public: | ||
/** | ||
* Constructor for the SettingsReader class | ||
* @param file_path The path to the settings.ini file | ||
*/ | ||
explicit SettingsReader(const std::filesystem::path& file_path, | ||
ProblemGenerationLog::ProblemGenerationLogger* logger); | ||
|
||
std::string Solver(); | ||
|
||
private: | ||
ProblemGenerationLog::ProblemGenerationLogger* logger_; | ||
std::string solver_name_; | ||
|
||
std::map<std::string, std::string> settings_; | ||
void check_file_exist(const std::filesystem::path& file_path) const; | ||
void parse_file(const std::filesystem::path& file_path); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <fstream> | ||
|
||
#include "LoggerBuilder.h" | ||
#include "RandomDirGenerator.h" | ||
#include "antares-xpansion/lpnamer/input_reader/SettingsReader.h" | ||
#include "gtest/gtest.h" | ||
|
||
TEST(ReadSettings, ReadSettingsProperly) { | ||
auto tmp_path = CreateRandomSubDir(std::filesystem::temp_directory_path()); | ||
std::ofstream settings_file(tmp_path / "settings.ini"); | ||
settings_file << R"( | ||
uc_type = expansion_fast | ||
master = integer | ||
optimality_gap = 10 | ||
additional-constraints = contraintes.txt | ||
solver = Xpress | ||
)" << std::endl; | ||
auto logger = emptyLogger(); | ||
ASSERT_NO_THROW(SettingsReader reader(tmp_path / "settings.ini", logger.get())); | ||
} | ||
|
||
TEST(ReadSettings, DefaultSolverIsCBC) { | ||
auto tmp_path = CreateRandomSubDir(std::filesystem::temp_directory_path()); | ||
std::ofstream settings_file(tmp_path / "settings.ini"); | ||
settings_file << R"( | ||
uc_type = expansion_fast | ||
master = integer | ||
optimality_gap = 10 | ||
additional-constraints = contraintes.txt | ||
)"<< std::endl;; | ||
auto logger = emptyLogger(); | ||
SettingsReader reader(tmp_path / "settings.ini", logger.get()); | ||
ASSERT_EQ(reader.Solver(), "CBC"); | ||
} | ||
|
||
TEST(ReadSettings, ReadSolverProperly) { | ||
auto tmp_path = CreateRandomSubDir(std::filesystem::temp_directory_path()); | ||
std::ofstream settings_file(tmp_path / "settings.ini"); | ||
settings_file << R"( | ||
uc_type = expansion_fast | ||
master = integer | ||
optimality_gap = 10 | ||
additional-constraints = contraintes.txt | ||
solver = Xpress | ||
)"<< std::endl; | ||
auto logger = emptyLogger(); | ||
SettingsReader reader(tmp_path / "settings.ini", logger.get()); | ||
ASSERT_EQ(reader.Solver(), "Xpress"); | ||
} |