From d20b30d34dd430626970df1a88c0bcf001400d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 13 Oct 2017 18:41:20 +0200 Subject: [PATCH] =?UTF-8?q?Implemented=20=E2=80=9Cnormalize=E2=80=9D=20com?= =?UTF-8?q?mand.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plasp-app/commands/CommandNormalize.h | 23 +++ .../plasp-app/commands/CommandNormalize.cpp | 157 ++++++++++++++++++ app/src/plasp-app/main.cpp | 3 + 3 files changed, 183 insertions(+) create mode 100644 app/include/plasp-app/commands/CommandNormalize.h create mode 100644 app/src/plasp-app/commands/CommandNormalize.cpp diff --git a/app/include/plasp-app/commands/CommandNormalize.h b/app/include/plasp-app/commands/CommandNormalize.h new file mode 100644 index 0000000..d7278b2 --- /dev/null +++ b/app/include/plasp-app/commands/CommandNormalize.h @@ -0,0 +1,23 @@ +#ifndef __PLASP_APP__COMMANDS__COMMAND_NORMALIZE_H +#define __PLASP_APP__COMMANDS__COMMAND_NORMALIZE_H + +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Command Normalize +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class CommandNormalize : public Command +{ + public: + CommandNormalize(); + + int run(int argc, char **argv); +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#endif diff --git a/app/src/plasp-app/commands/CommandNormalize.cpp b/app/src/plasp-app/commands/CommandNormalize.cpp new file mode 100644 index 0000000..3f52514 --- /dev/null +++ b/app/src/plasp-app/commands/CommandNormalize.cpp @@ -0,0 +1,157 @@ +#include + +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Command Normalize +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +CommandNormalize::CommandNormalize() +: Command(cxxopts::Options("plasp normalize", "Normalize PDDL to plasp’s custom PDDL format.")) +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +int CommandNormalize::run(int argc, char **argv) +{ + parseOptions(argc, argv); + + const auto &basicOptions = std::get(m_optionGroups); + const auto &outputOptions = std::get(m_optionGroups); + const auto &parserOptions = std::get(m_optionGroups); + + if (basicOptions.help) + { + printHelp(); + return EXIT_SUCCESS; + } + + if (basicOptions.version) + { + printVersion(); + return EXIT_SUCCESS; + } + + colorlog::Logger logger; + logger.setColorPolicy(outputOptions.colorPolicy); + logger.setLogPriority(outputOptions.logPriority); + + if (basicOptions.warningsAsErrors) + logger.setAbortPriority(colorlog::Priority::Warning); + + const auto printCompatibilityInfo = + [&]() + { + if (parserOptions.parsingMode != pddl::Mode::Compatibility) + logger.log(colorlog::Priority::Info, "try using --parsing-mode=compatibility for extended legacy feature support"); + }; + + try + { + tokenize::Tokenizer tokenizer; + + if (!parserOptions.inputFiles.empty()) + std::for_each(parserOptions.inputFiles.cbegin(), parserOptions.inputFiles.cend(), + [&](const auto &inputFile) + { + tokenizer.read(inputFile); + }); + else + { + logger.log(colorlog::Priority::Info, "reading from stdin"); + tokenizer.read("std::cin", std::cin); + } + + const auto detectLanguage = + [&]() + { + if (parserOptions.language == plasp::Language::Type::Automatic) + return plasp::detectLanguage(tokenizer); + + return parserOptions.language; + }; + + switch (detectLanguage()) + { + case plasp::Language::Type::Automatic: + case plasp::Language::Type::Unknown: + { + logger.log(colorlog::Priority::Error, "unknown input language"); + std::cout << std::endl; + printHelp(); + return EXIT_FAILURE; + } + + // TODO: get rid of unknown language type, use exception instead + case plasp::Language::Type::PDDL: + { + const auto logWarning = + [&](const auto &location, const auto &warning) + { + logger.log(colorlog::Priority::Warning, location, warning); + }; + + auto context = pddl::Context(std::move(tokenizer), logWarning); + context.mode = parserOptions.parsingMode; + auto description = pddl::parseDescription(context); + auto normalizedDescription = pddl::normalize(std::move(description)); + logger.outputStream() << normalizedDescription << std::endl; + break; + } + + case plasp::Language::Type::SAS: + { + logger.log(colorlog::Priority::Error, "Normalization is only supported for PDDL specifications"); + return EXIT_FAILURE; + } + } + } + catch (const tokenize::TokenizerException &e) + { + logger.log(colorlog::Priority::Error, e.location(), e.message().c_str()); + + printCompatibilityInfo(); + + return EXIT_FAILURE; + } + catch (const pddl::ParserException &e) + { + if (e.location()) + logger.log(colorlog::Priority::Error, e.location().value(), e.message().c_str()); + else + logger.log(colorlog::Priority::Error, e.message().c_str()); + + printCompatibilityInfo(); + + return EXIT_FAILURE; + } + catch (const std::exception &e) + { + logger.log(colorlog::Priority::Error, e.what()); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/app/src/plasp-app/main.cpp b/app/src/plasp-app/main.cpp index 03187a3..b009804 100644 --- a/app/src/plasp-app/main.cpp +++ b/app/src/plasp-app/main.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -90,6 +91,8 @@ int main(int argc, char **argv) case CommandType::Version: return CommandVersion().run(argc - 1, &argv[1]); return EXIT_SUCCESS; + case CommandType::Normalize: + return CommandNormalize().run(argc - 1, &argv[1]); case CommandType::Translate: return CommandTranslate().run(argc - 1, &argv[1]); default: