Commit f091c5fb authored by Kučera Petr RNDr. Ph.D.'s avatar Kučera Petr RNDr. Ph.D.
Browse files

Refactoring

parent 795a88f2
......@@ -15,6 +15,7 @@
#include "canon.h"
#include "cnfutil.h"
#include "cpuutil.h"
#include "enum_opt.h"
#include "logstream.h"
#include "normalform.h"
#include "prgutil.h"
......@@ -31,6 +32,10 @@ enum class PROCESS
Last = PRIME
};
const SUBool::EnumAnnotation<PROCESS> kProcessAnnotation("PROCESS",
{{PROCESS::NONE, "none", "no inprocessing."},
{PROCESS::PRIME, "prime", "use SAT to find a prime subimplicate."}});
const std::array<std::string, static_cast<size_t>(PROCESS::Last) + 1>
process_names{"none", "prime"};
const char *kProcessOptDesc = "Clause inprocessing and preprocessing level.\n"
......@@ -49,27 +54,16 @@ std::optional<config_t>
parse_arguments(int argc, char *argv[])
{
config_t conf;
try
{
std::string process;
SUBool::PrgOptions opts;
opts.InputOutput(conf.input_file, conf.output_file);
po::options_description desc("Configuration options");
desc.add_options()("process,p",
po::value<std::string>(&process)->default_value(
process_names.at(static_cast<unsigned>(conf.process))),
kProcessOptDesc);
opts.Add(desc);
if (!opts.Parse(argc, argv))
{
return {};
}
conf.process = SUBool::enum_of_name<PROCESS>(process, process_names);
conf.verbosity = opts.Verbosity();
}
catch (const std::exception &e)
auto opt_process = SUBool::make_enum_option(kProcessAnnotation, "process,p",
"Clause inprocessing and preprocessing level.", conf.process);
std::string process;
SUBool::PrgOptions opts(conf.verbosity);
opts.InputOutput(conf.input_file, conf.output_file);
po::options_description desc("Configuration options");
desc.add(opt_process);
opts.Add(desc);
if (!opts.Parse(argc, argv) || !opt_process.GetValue(conf.process))
{
std::cerr << "Error parsing options: " << e.what() << std::endl;
return {};
}
return conf;
......
......@@ -75,42 +75,22 @@ std::optional<config_t>
parse_arguments(int argc, char *argv[])
{
config_t conf;
std::string relation;
std::string compare_kind;
auto opt_relation = SUBool::make_enum_option(
kRelationAnnotation, "relation,r", "Relation to check.", conf.relation);
auto opt_compare_kind = SUBool::make_enum_option(kCompareKindAnnotation,
"compare-kind,k", "How the CNFs should be compared.", conf.compare_kind);
try
{
// options.positional_help("[input-left [input-right]]");
SUBool::PrgOptions opts;
opts.InputPair(
{&conf.file_left, "input-left"}, {&conf.file_right, "input-right"});
po::options_description desc("Configuration options");
desc.add(opt_relation);
desc.add(opt_compare_kind);
opts.Add(desc);
if (!opts.Parse(argc, argv))
{
return {};
}
}
catch (const std::exception &e)
{
std::cerr << "Error parsing options: " << e.what() << std::endl;
return {};
}
try
// options.positional_help("[input-left [input-right]]");
SUBool::PrgOptions opts(conf.verbosity);
opts.InputPair(
{&conf.file_left, "input-left"}, {&conf.file_right, "input-right"});
po::options_description desc("Configuration options");
desc.add(opt_relation);
desc.add(opt_compare_kind);
opts.Add(desc);
if (!opts.Parse(argc, argv) || !opt_relation.GetValue(conf.relation)
|| !opt_compare_kind.GetValue(conf.compare_kind))
{
conf.relation = opt_relation.Value();
conf.compare_kind = opt_compare_kind.Value();
}
catch (SUBool::BadParameterException &e)
{
SUBool::logs.TLog(0) << "Error parsing the parameters: " << e.what()
<< std::endl;
return {};
}
return conf;
......
......@@ -56,7 +56,7 @@ namespace SUBool
using enum_t = T;
struct Value
{
enum_t value;
T value;
std::string name;
std::string desc;
};
......@@ -80,10 +80,10 @@ namespace SUBool
EnumAnnotation(std::string enum_name, ValueList va);
EnumAnnotation(std::string enum_name, std::initializer_list<Value> va);
enum_t Parse(const std::string &name) const;
const std::string &Name(const enum_t &value) const;
T Parse(const std::string &name) const;
const std::string &Name(const T &value) const;
void DumpValue(const std::string &line_prefix, unsigned level,
const std::string &label, const enum_t &enum_value) const;
const std::string &label, const T &enum_value) const;
iterator begin();
iterator end();
......@@ -121,7 +121,7 @@ SUBool::EnumAnnotation<T>::EnumAnnotation(
template <class T>
auto
SUBool::EnumAnnotation<T>::Parse(const std::string &name) const -> enum_t
SUBool::EnumAnnotation<T>::Parse(const std::string &name) const -> T
{
auto i_val = FindByName(name);
if (i_val == mValues.end())
......@@ -156,7 +156,7 @@ SUBool::EnumAnnotation<T>::FindByName(const std::string &name) const ->
template <class T>
auto
SUBool::EnumAnnotation<T>::FindByEnumValue(const enum_t &enum_val) const ->
SUBool::EnumAnnotation<T>::FindByEnumValue(const T &enum_val) const ->
typename std::vector<Value>::const_iterator
{
return std::find_if(mValues.begin(), mValues.end(),
......@@ -166,7 +166,7 @@ SUBool::EnumAnnotation<T>::FindByEnumValue(const enum_t &enum_val) const ->
template <class T>
void
SUBool::EnumAnnotation<T>::DumpValue(const std::string &line_prefix,
unsigned level, const std::string &label, const enum_t &enum_value) const
unsigned level, const std::string &label, const T &enum_value) const
{
logs.DumpValue(line_prefix, level, label, Name(enum_value));
}
......
......@@ -22,6 +22,9 @@ namespace SUBool
template <class T> class EnumOption
{
public:
using enum_t = T;
private:
const EnumAnnotation<T> &mEnumAnnotation;
std::string mOptionName;
......@@ -40,6 +43,7 @@ namespace SUBool
operator boost::shared_ptr<po::option_description>();
T Value() const;
bool GetValue(T &val) const;
};
template <class T, typename... Args>
......@@ -91,6 +95,23 @@ SUBool::EnumOption<T>::Value() const
return mEnumAnnotation.Parse(mOptionValue);
}
template <class T>
bool
SUBool::EnumOption<T>::GetValue(T &val) const
{
try
{
val = mEnumAnnotation.Parse(mOptionValue);
}
catch (const ParseException &e)
{
logs.Log(0) << "Unknown value \"" << mOptionValue << "\" of option \""
<< mOptionName << '"' << std::endl;
return false;
}
return true;
}
template <class T, typename... Args>
auto
SUBool::make_enum_option(const EnumAnnotation<T> &ann, Args &&...args)
......
......@@ -126,12 +126,22 @@ SUBool::PrgOptions::Parse(int argc, char *argv[])
po::options_description cmdline_opts;
mVisible.add(mCommon).add(mConfig);
cmdline_opts.add(mCommon).add(mConfig).add(mHidden);
po::store(po::command_line_parser(argc, argv)
.options(cmdline_opts)
.positional(mPositional)
.run(),
mVM);
po::notify(mVM);
try
{
po::store(po::command_line_parser(argc, argv)
.options(cmdline_opts)
.positional(mPositional)
.run(),
mVM);
po::notify(mVM);
}
catch (const std::exception &e)
{
std::cerr << "Error parsing command line options: " << e.what()
<< std::endl;
return false;
}
if (!ProcessCommon())
{
return false;
......@@ -155,8 +165,16 @@ SUBool::PrgOptions::Parse(int argc, char *argv[])
}
po::options_description config_file_options;
config_file_options.add(mConfigOnly).add(mConfig).add(mHidden);
po::store(po::parse_config_file(ifs, config_file_options), mVM);
po::notify(mVM);
try
{
po::store(po::parse_config_file(ifs, config_file_options), mVM);
po::notify(mVM);
}
catch (const std::exception &e)
{
std::cerr << "Error parsing config file: " << e.what() << std::endl;
return false;
}
return true;
// TODO: parse config file
}
......
......@@ -138,7 +138,7 @@ namespace SUBool
std::string mPrgHelpDesc{};
std::string mPositionalHelp{};
bool mUseConfigFile{false};
unsigned mVerbosity{3};
unsigned &mVerbosity;
std::string mConfigFile{};
po::options_description mCommon{};
......@@ -162,11 +162,10 @@ namespace SUBool
inline static const std::string kCfgSuffix = ".cfg";
static const unsigned kDefaultVerbosity = 3;
PrgOptions(bool use_config_file = false,
const std::string &common_opts_label = kCommonOptsLabel,
unsigned default_verbosity = kDefaultVerbosity)
PrgOptions(unsigned &verbosity, bool use_config_file = false,
const std::string &common_opts_label = kCommonOptsLabel)
: mPrgName(kPrgName), mPrgDesc(kPrgDesc),
mUseConfigFile(use_config_file), mVerbosity(default_verbosity),
mUseConfigFile(use_config_file), mVerbosity(verbosity),
mCommon(common_opts_label)
{
InitCommon();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment