Skip to content

Commit

Permalink
Move output and archive deduction in ProblemGenerationExeOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Nov 3, 2023
1 parent eb223c8 commit 36de940
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 55 deletions.
43 changes: 4 additions & 39 deletions src/cpp/lpnamer/main/ProblemGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ std::filesystem::path ProblemGeneration::updateProblems() {
const auto archive_path = options_.ArchivePath();

auto deduced_xpansion_output_dir =
deduceXpansionDirIfEmpty(xpansion_output_dir, archive_path);
options_.deduceXpansionDirIfEmpty(xpansion_output_dir, archive_path);

const auto log_file_path =
xpansion_output_dir / "lp" / "ProblemGenerationLog.txt";
Expand All @@ -63,9 +63,9 @@ std::filesystem::path ProblemGeneration::updateProblems() {
auto unnamed_problems = options_.UnnamedProblems();
std::filesystem::path deduced_archive_path;
try {
deduced_archive_path =
deduceArchivePathIfEmpty(deduced_xpansion_output_dir, archive_path);
} catch (const MismatchedParameters& m) {
deduced_archive_path = options_.deduceArchivePathIfEmpty(
deduced_xpansion_output_dir, archive_path);
} catch (const ProblemGenerationOptions::MismatchedParameters& m) {
(*logger)(LogUtils::LOGLEVEL::ERR) << m.what();
throw;
}
Expand All @@ -75,38 +75,6 @@ std::filesystem::path ProblemGeneration::updateProblems() {
logger, log_file_path, weights_file, unnamed_problems);
return deduced_xpansion_output_dir;
}
std::filesystem::path ProblemGeneration::deduceArchivePathIfEmpty(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& archive_path) {
if (archive_path.empty() && !xpansion_output_dir.empty()) {
if (xpansion_output_dir.string().find("-Xpansion") == std::string::npos) {
auto log_location = LOGLOCATION;
auto msg =
"Archive path is missing and output path does not contains"
" \"-Xpansion\" suffixe. Can't deduce archive file name.";
throw MismatchedParameters(msg, log_location);
}
auto deduced_archive_path = xpansion_output_dir;
auto dir_name = deduced_archive_path.stem().string();
dir_name = dir_name.substr(0, dir_name.find("-Xpansion"));
deduced_archive_path =
deduced_archive_path.replace_filename(dir_name).replace_extension(
".zip");
return deduced_archive_path;
}
return archive_path;
}
std::filesystem::path ProblemGeneration::deduceXpansionDirIfEmpty(
std::filesystem::path xpansion_output_dir,
const std::filesystem::path& archive_path) {
if (xpansion_output_dir.empty() && !archive_path.empty()) {
auto deduced_dir = archive_path;
deduced_dir = deduced_dir.replace_filename(
deduced_dir.stem().replace_extension("").string() + "-Xpansion");
return deduced_dir;
}
return xpansion_output_dir;
}

struct Version {
explicit Version(std::string_view version) {
Expand Down Expand Up @@ -303,6 +271,3 @@ std::shared_ptr<ArchiveReader> InstantiateZipReader(
reader->Open();
return reader;
}
ProblemGeneration::MismatchedParameters::MismatchedParameters(
const std::string& err_message, const std::string& log_location)
: XpansionError(err_message, log_location) {}
38 changes: 38 additions & 0 deletions src/cpp/lpnamer/main/ProblemGenerationExeOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,41 @@ void ProblemGenerationExeOptions::Parse(unsigned int argc,
const char *const *argv) {
OptionsParser::Parse(argc, argv);
}

std::filesystem::path ProblemGenerationExeOptions::deduceArchivePathIfEmpty(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& archive_path) const {
if (archive_path.empty() && !xpansion_output_dir.empty()) {
if (xpansion_output_dir.string().find("-Xpansion") == std::string::npos) {
auto log_location = LOGLOCATION;
auto msg =
"Archive path is missing and output path does not contains"
" \"-Xpansion\" suffixe. Can't deduce archive file name.";
throw MismatchedParameters(msg, log_location);
}
auto deduced_archive_path = xpansion_output_dir;
auto dir_name = deduced_archive_path.stem().string();
dir_name = dir_name.substr(0, dir_name.find("-Xpansion"));
deduced_archive_path =
deduced_archive_path.replace_filename(dir_name).replace_extension(
".zip");
return deduced_archive_path;
}
return archive_path;
}

std::filesystem::path ProblemGenerationExeOptions::deduceXpansionDirIfEmpty(
std::filesystem::path xpansion_output_dir,
const std::filesystem::path& archive_path) const {
if (xpansion_output_dir.empty() && !archive_path.empty()) {
auto deduced_dir = archive_path;
deduced_dir = deduced_dir.replace_filename(
deduced_dir.stem().replace_extension("").string() + "-Xpansion");
return deduced_dir;
}
return xpansion_output_dir;
}

ProblemGenerationExeOptions::MismatchedParameters::MismatchedParameters(
const std::string& err_message, const std::string& log_location)
: XpansionError(err_message, log_location) {}
15 changes: 0 additions & 15 deletions src/cpp/lpnamer/main/include/ProblemGeneration.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ class ProblemGeneration {
const std::filesystem::path& log_file_path,
const std::filesystem::path& weights_file, bool unnamed_problems);

class MismatchedParameters
: public LogUtils::XpansionError<std::runtime_error> {
using LogUtils::XpansionError<std::runtime_error>::XpansionError;

public:
explicit MismatchedParameters(const std::string& err_message,
const std::string& log_location);
};

private:
void ProcessWeights(
const std::filesystem::path& xpansion_output_dir,
Expand All @@ -45,10 +36,4 @@ class ProblemGeneration {
const std::filesystem::path& antares_archive_path,
const std::filesystem::path& xpansion_output_dir,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger);
static std::filesystem::path deduceXpansionDirIfEmpty(
std::filesystem::path xpansion_output_dir,
const std::filesystem::path& archive_path);
static std::filesystem::path deduceArchivePathIfEmpty(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& archive_path);
};
7 changes: 7 additions & 0 deletions src/cpp/lpnamer/main/include/ProblemGenerationExeOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ class ProblemGenerationExeOptions : public OptionsParser,
bool UnnamedProblems() const { return unnamed_problems_; }

void Parse(unsigned int argc, const char *const *argv) override;

[[nodiscard]] std::filesystem::path deduceXpansionDirIfEmpty(
std::filesystem::path xpansion_output_dir,
const std::filesystem::path& archive_path) const override;
[[nodiscard]] std::filesystem::path deduceArchivePathIfEmpty(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& archive_path) const override;
};
#endif // ANTARES_XPANSION_SRC_CPP_LPNAMER_MAIN_INCLUDE_PROBLEMGENERATIONEXEOPTIONS_H
15 changes: 15 additions & 0 deletions src/cpp/lpnamer/main/include/ProblemGenerationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ class ProblemGenerationOptions {
[[nodiscard]] virtual std::filesystem::path WeightsFile() const = 0;
[[nodiscard]] virtual std::vector<int> ActiveYears() const = 0;
[[nodiscard]] virtual bool UnnamedProblems() const = 0;
[[nodiscard]] virtual std::filesystem::path deduceXpansionDirIfEmpty(
std::filesystem::path xpansion_output_dir,
const std::filesystem::path& archive_path) const = 0;
[[nodiscard]] virtual std::filesystem::path deduceArchivePathIfEmpty(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& archive_path) const = 0;

class MismatchedParameters
: public LogUtils::XpansionError<std::runtime_error> {
using LogUtils::XpansionError<std::runtime_error>::XpansionError;

public:
explicit MismatchedParameters(const std::string& err_message,
const std::string& log_location);
};
};
3 changes: 2 additions & 1 deletion tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ TEST_F(

ProblemGenerationSpyAndMock pbg(problem_generation_options_parser_);

EXPECT_THROW(pbg.updateProblems(), ProblemGeneration::MismatchedParameters);
EXPECT_THROW(pbg.updateProblems(),
ProblemGenerationOptions::MismatchedParameters);

EXPECT_TRUE(pbg.archive_path_.empty()); // Can't deduce
EXPECT_TRUE(pbg.xpansion_output_dir_
Expand Down
10 changes: 10 additions & 0 deletions tests/cpp/lp_namer/ProblemGenerationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class InMemoryOption : public ProblemGenerationOptions {
}
std::vector<int> ActiveYears() const override { return std::vector<int>(); }
bool UnnamedProblems() const override { return false; }
std::filesystem::path deduceXpansionDirIfEmpty(
std::filesystem::path xpansion_output_dir,
const std::filesystem::path& archive_path) const override {
return std::filesystem::path();
}
std::filesystem::path deduceArchivePathIfEmpty(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& archive_path) const override {
return std::filesystem::path();
}
};

TEST(InitializationTest, FoldersAreEmpty) {
Expand Down

0 comments on commit 36de940

Please sign in to comment.