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

Refactoring

parent 5d691dc5
......@@ -30,12 +30,12 @@ void
dump_config(const config_t &conf, unsigned level)
{
SUBool::logs.TLog(level) << "Configuration:" << std::endl;
SUBool::dump_value("\t", level, "file_left", conf.file_left);
SUBool::dump_value("\t", level, "file_right", conf.file_right);
SUBool::dump_value("\t", level, "verbosity", conf.verbosity);
SUBool::dump_value("\t", level, "suppress_first", conf.suppress_first);
SUBool::dump_value("\t", level, "suppress_second", conf.suppress_second);
SUBool::dump_value("\t", level, "suppress_third", conf.suppress_third);
SUBool::logs.DumpValue("\t", level, "file_left", conf.file_left);
SUBool::logs.DumpValue("\t", level, "file_right", conf.file_right);
SUBool::logs.DumpValue("\t", level, "verbosity", conf.verbosity);
SUBool::logs.DumpValue("\t", level, "suppress_first", conf.suppress_first);
SUBool::logs.DumpValue("\t", level, "suppress_second", conf.suppress_second);
SUBool::logs.DumpValue("\t", level, "suppress_third", conf.suppress_third);
}
int
......
......@@ -87,27 +87,28 @@ void
dump_config(const config_t &conf, unsigned level)
{
SUBool::logs.TLog(level) << "Configuration:" << std::endl;
SUBool::dump_value("\t", level, "input_file", conf.input_file);
SUBool::dump_value("\t", level, "output_file", conf.output_file);
SUBool::dump_value("\t", level, "verbosity", conf.verbosity);
SUBool::dump_value("\t", level, "max_input_var", conf.max_input_var);
SUBool::dump_value("\t", level, "elim.use_minimize", conf.elim.use_minimize);
SUBool::dump_value("\t", level, "elim.block_prime",
SUBool::logs.DumpValue("\t", level, "input_file", conf.input_file);
SUBool::logs.DumpValue("\t", level, "output_file", conf.output_file);
SUBool::logs.DumpValue("\t", level, "verbosity", conf.verbosity);
SUBool::logs.DumpValue("\t", level, "max_input_var", conf.max_input_var);
SUBool::logs.DumpValue(
"\t", level, "elim.use_minimize", conf.elim.use_minimize);
SUBool::logs.DumpValue("\t", level, "elim.block_prime",
block_prime_names.at(static_cast<unsigned>(conf.elim.block_prime)));
SUBool::dump_value(
SUBool::logs.DumpValue(
"\t", level, "elim.eliminate", eliminate_names.at(conf.elim.eliminate));
SUBool::dump_value("\t", level, "elim.ignore_policy",
SUBool::logs.DumpValue("\t", level, "elim.ignore_policy",
elim_ignore_policy_names.at(
static_cast<unsigned>(conf.elim.ignore_policy)));
SUBool::dump_value("\t", level, "elim.inprocessing",
SUBool::logs.DumpValue("\t", level, "elim.inprocessing",
elim_inprocessing_names.at(
static_cast<unsigned>(conf.elim.inprocessing)));
SUBool::dump_value("\t", level, "elim.increase_policy",
SUBool::logs.DumpValue("\t", level, "elim.increase_policy",
elim_increase_policy_names.at(
static_cast<unsigned>(conf.elim.increase_policy)));
SUBool::dump_value(
SUBool::logs.DumpValue(
"\t", level, "elim.minimize_size_step", conf.elim.minimize_size_step);
SUBool::dump_value("\t", level, "elim.minimize_size_factor",
SUBool::logs.DumpValue("\t", level, "elim.minimize_size_factor",
conf.elim.minimize_size_factor);
switch (conf.elim.eliminate)
......@@ -123,7 +124,7 @@ dump_config(const config_t &conf, unsigned level)
default:
break;
}
SUBool::dump_value("\t", level, "final_minimize", conf.final_minimize);
SUBool::logs.DumpValue("\t", level, "final_minimize", conf.final_minimize);
SUBool::logs.TLog(level) << "END" << std::endl;
}
......
......@@ -58,20 +58,21 @@ void
dump_config(const config_t &conf, unsigned level)
{
SUBool::logs.TLog(level) << "Configuration:" << std::endl;
SUBool::dump_value("\t", level, "input_file", conf.input_file);
SUBool::dump_value("\t", level, "output_file", conf.output_file);
SUBool::dump_value("\t", level, "verbosity", conf.verbosity);
SUBool::logs.DumpValue("\t", level, "input_file", conf.input_file);
SUBool::logs.DumpValue("\t", level, "output_file", conf.output_file);
SUBool::logs.DumpValue("\t", level, "verbosity", conf.verbosity);
SUBool::dump_enum_value(
"\t", level, "impl_type", conf.impl_type, impl_type_names);
SUBool::dump_enum_value(
"\t", level, "output_type", conf.output_type, output_type_names);
SUBool::dump_value("\t", level, "impl_sep", conf.impl_sep);
SUBool::dump_value(
SUBool::logs.DumpValue("\t", level, "impl_sep", conf.impl_sep);
SUBool::logs.DumpValue(
"\t", level, "keep_body_in_closure", conf.keep_body_in_closure);
SUBool::dump_value(
SUBool::logs.DumpValue(
"\t", level, "omit_empty_closure", conf.omit_empty_closure);
SUBool::dump_value("\t", level, "body_minimal", conf.body_minimal);
SUBool::dump_value("\t", level, "right_irredundant", conf.right_irredundant);
SUBool::logs.DumpValue("\t", level, "body_minimal", conf.body_minimal);
SUBool::logs.DumpValue(
"\t", level, "right_irredundant", conf.right_irredundant);
}
int
......
......@@ -61,7 +61,7 @@ dump_config(const config_t &conf, unsigned level)
SUBool::logs.TLog(level)
<< "\tencoding_type: " << static_cast<unsigned>(conf.encoding_type)
<< std::endl;
SUBool::dump_value("\t", level, "final_minimize", conf.final_minimize);
SUBool::logs.DumpValue("\t", level, "final_minimize", conf.final_minimize);
SUBool::logs.TLog(level)
<< "\tlevel_func: "
<< level_func_names.at(static_cast<unsigned>(conf.level_func))
......@@ -69,10 +69,10 @@ dump_config(const config_t &conf, unsigned level)
SUBool::logs.TLog(level)
<< "\tcheck_smooth: " << SUBool::bool_to_string(conf.check_smooth)
<< std::endl;
SUBool::dump_value("\t", level, "amo_encoding",
SUBool::logs.DumpValue("\t", level, "amo_encoding",
SUBool::AmoEncoder::kEncodingNames.at(
static_cast<unsigned>(conf.amo_encoding)));
SUBool::dump_value("\t", level, "exone_encoding",
SUBool::logs.DumpValue("\t", level, "exone_encoding",
SUBool::ExOneEncoder::kEncodingNames.at(
static_cast<unsigned>(conf.exone_encoding)));
SUBool::logs.TLog(level) << "END" << std::endl;
......
......@@ -94,7 +94,7 @@ dump_config(const config_t &conf, unsigned level)
SUBool::logs.TLog(level)
<< "\tencode_only: " << SUBool::bool_to_string(conf.encode_only)
<< std::endl;
SUBool::dump_value("\t", level, "no_up_backbones", conf.no_up_backbones);
SUBool::logs.DumpValue("\t", level, "no_up_backbones", conf.no_up_backbones);
SUBool::logs.TLog(level)
<< "\tup_empower: " << static_cast<unsigned>(conf.up_empower)
<< std::endl;
......@@ -110,10 +110,10 @@ dump_config(const config_t &conf, unsigned level)
SUBool::dump_enum_value(
"\t", level, "impl_system", conf.impl_system, SUBool::kImplSystemNames);
SUBool::logs.TLog(level) << "\tgoal: " << goal_name(conf) << std::endl;
SUBool::dump_value("\t", level, "amo_encoding",
SUBool::logs.DumpValue("\t", level, "amo_encoding",
SUBool::AmoEncoder::kEncodingNames.at(
static_cast<unsigned>(conf.amo_encoding)));
SUBool::dump_value("\t", level, "exone_encoding",
SUBool::logs.DumpValue("\t", level, "exone_encoding",
SUBool::ExOneEncoder::kEncodingNames.at(
static_cast<unsigned>(conf.exone_encoding)));
SUBool::dump_enum_value(
......
......@@ -143,12 +143,12 @@ dump_config(const config_t &conf, unsigned level)
SUBool::logs.TLog(level)
<< "\tcomp_base_config.preprocess: "
<< static_cast<unsigned>(conf.comp_base_config.preprocess) << std::endl;
SUBool::dump_value("\t", level, "comp_base_config.propagate_backbones",
SUBool::logs.DumpValue("\t", level, "comp_base_config.propagate_backbones",
conf.comp_base_config.propagate_backbones);
SUBool::logs.TLog(level)
<< "\tuc_interleave_pc: "
<< SUBool::bool_to_string(conf.uc_interleave_pc) << std::endl;
SUBool::dump_value("\t", level, "use_empty_initial_hypothesis",
SUBool::logs.DumpValue("\t", level, "use_empty_initial_hypothesis",
conf.use_empty_initial_hypothesis);
SUBool::logs.TLog(level)
<< "\tpccheck_random_rounds: " << conf.pccheck_random_rounds
......@@ -159,7 +159,7 @@ dump_config(const config_t &conf, unsigned level)
SUBool::logs.TLog(level)
<< "\tpccheck_random_budget_inc: " << conf.pccheck_random_budget_inc
<< std::endl;
SUBool::dump_value("\t", level, "initial_negatives",
SUBool::logs.DumpValue("\t", level, "initial_negatives",
SUBool::PCLearnComp::cInitialNegativesNames[static_cast<size_t>(
conf.initial_negatives)]);
SUBool::logs.TLog(level)
......@@ -180,10 +180,10 @@ dump_config(const config_t &conf, unsigned level)
<< "\tgoal: " << (conf.goal == GOAL::PC ? "PC" : "UC") << std::endl;
SUBool::logs.TLog(level) << "\tenc_conf:" << std::endl;
conf.comp_base_config.enc_conf.Dump("\t\t", level);
SUBool::dump_value("\t", level, "amo_encoding",
SUBool::logs.DumpValue("\t", level, "amo_encoding",
SUBool::AmoEncoder::kEncodingNames.at(
static_cast<unsigned>(conf.amo_encoding)));
SUBool::dump_value("\t", level, "exone_encoding",
SUBool::logs.DumpValue("\t", level, "exone_encoding",
SUBool::ExOneEncoder::kEncodingNames.at(
static_cast<unsigned>(conf.exone_encoding)));
SUBool::logs.TLog(level) << "END" << std::endl;
......
......@@ -198,16 +198,17 @@ void
SUBool::AbsEncoder::AbsConfig::Dump(
const std::string &line_prefix, unsigned level) const
{
dump_value(line_prefix, level, "up_one_prove",
logs.DumpValue(line_prefix, level, "up_one_prove",
AbsEncoder::kUPOneProveNames.at(static_cast<unsigned>(up_one_prove))
+ " (" + std::to_string(static_cast<unsigned>(up_one_prove)) + ")");
dump_value(line_prefix, level, "optimize", optimize);
dump_value(line_prefix, level, "up_level_bound", up_level_bound);
dump_value(line_prefix, level, "use_backmap", use_backmap);
logs.DumpValue(line_prefix, level, "optimize", optimize);
logs.DumpValue(line_prefix, level, "up_level_bound", up_level_bound);
logs.DumpValue(line_prefix, level, "use_backmap", use_backmap);
logs.TLog(level) << line_prefix << "log_conf:" << std::endl;
log_conf.Dump(line_prefix + "\t", level);
dump_value(line_prefix, level, "min_lq_log_weights", min_lq_log_weights);
dump_value(line_prefix, level, "min_lq_quad_weights", min_lq_quad_weights);
logs.DumpValue(line_prefix, level, "min_lq_log_weights", min_lq_log_weights);
logs.DumpValue(
line_prefix, level, "min_lq_quad_weights", min_lq_quad_weights);
}
auto
......
/*
* Project SUBool
* Petr Kucera, 2018
*/
/**@file enum.h
* Utility functions and structures related to annotated enums.
*/
#ifndef __ENUM_H
#define __ENUM_H
#include <algorithm>
#include "logstream.h"
#include "sbexception.h"
namespace SUBool
{
template <class T>
T
enum_of_int(unsigned val)
{
if (val > static_cast<unsigned>(T::Last))
{
throw BadParameterException("enum_of_int", "val too high");
}
return static_cast<T>(val);
}
template <class E, size_t S>
E
enum_of_name(const std::string &val, const std::array<std::string, S> &names)
{
auto i = std::find(names.begin(), names.end(), val);
if (i == names.end())
{
throw BadParameterException("enum_of_name", "name is not on the list");
}
return enum_of_int<E>(i - names.begin());
}
struct EnumClassHash
{
template <class T>
size_t
operator()(T t) const
{
return static_cast<size_t>(t);
}
};
template <class Enum, class Names>
void
dump_enum_value(const std::string &line_prefix, unsigned level,
const std::string &name, const Enum &enum_value, const Names &names)
{
logs.TLog(level) << line_prefix << name << ": "
<< names.at(static_cast<unsigned>(enum_value))
<< std::endl;
}
template <class T> class EnumAnnotation
{
public:
struct Value
{
T value;
std::string name;
std::string desc;
};
using ValueList = std::vector<Value>;
using iterator = typename ValueList::iterator;
using const_iterator = typename ValueList::const_iterator;
private:
ValueList mValues;
std::string mName;
typename ValueList::const_iterator FindByName(
const std::string &name) const;
typename ValueList::const_iterator FindByEnumValue(
const T &enum_val) const;
public:
template <class InputIterator>
EnumAnnotation(
std::string enum_name, InputIterator begin, InputIterator end);
EnumAnnotation(std::string enum_name, ValueList va);
EnumAnnotation(std::string enum_name, std::initializer_list<Value> va);
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 T &enum_value) const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
const_iterator cbegin() const;
const_iterator cend() const;
};
} // namespace SUBool
// Template and inline functions implementation
template <class T>
template <class InputIterator>
SUBool::EnumAnnotation<T>::EnumAnnotation(
std::string enum_name, InputIterator begin, InputIterator end)
: mValues(begin, end), mName(std::move(enum_name))
{
}
template <class T>
SUBool::EnumAnnotation<T>::EnumAnnotation(
std::string enum_name, std::vector<Value> va)
: mValues(std::move(va)), mName(std::move(enum_name))
{
}
template <class T>
SUBool::EnumAnnotation<T>::EnumAnnotation(
std::string enum_name, std::initializer_list<Value> va)
: mValues(std::move(va)), mName(std::move(enum_name))
{
}
template <class T>
T
SUBool::EnumAnnotation<T>::Parse(const std::string &name) const
{
auto i_val = FindByName(name);
if (i_val == mValues.end())
{
std::string err = "Failed to find value " + name + " in enum " + mName;
throw ParseException("EnumAnnotation::Parse", err);
}
return i_val->value;
}
template <class T>
const std::string &
SUBool::EnumAnnotation<T>::Name(const T &value) const
{
auto i_name = FindByEnumValue(value);
if (i_name == mValues.end())
{
std::string err = "Failed to find name for a value in enum " + mName;
throw ParseException("EnumAnnotation::Parse", err);
}
return i_name->name;
}
template <class T>
auto
SUBool::EnumAnnotation<T>::FindByName(const std::string &name) const ->
typename std::vector<Value>::const_iterator
{
return std::find_if(mValues.begin(), mValues.end(),
[&name](const auto &val) { return name == val.name; });
}
template <class T>
auto
SUBool::EnumAnnotation<T>::FindByEnumValue(const T &enum_val) const ->
typename std::vector<Value>::const_iterator
{
return std::find_if(mValues.begin(), mValues.end(),
[&enum_val](const auto &val) { return enum_val == val.value; });
}
template <class T>
void
SUBool::EnumAnnotation<T>::DumpValue(const std::string &line_prefix,
unsigned level, const std::string &label, const T &enum_value) const
{
logs.DumpValue(line_prefix, level, label, Name(enum_value));
}
template <class T>
auto
SUBool::EnumAnnotation<T>::begin() -> iterator
{
return mValues.begin();
}
template <class T>
auto
SUBool::EnumAnnotation<T>::end() -> iterator
{
return mValues.end();
}
template <class T>
auto
SUBool::EnumAnnotation<T>::begin() const -> const_iterator
{
return mValues.begin();
}
template <class T>
auto
SUBool::EnumAnnotation<T>::end() const -> const_iterator
{
return mValues.end();
}
template <class T>
auto
SUBool::EnumAnnotation<T>::cbegin() const -> const_iterator
{
return mValues.begin();
}
template <class T>
auto
SUBool::EnumAnnotation<T>::cend() const -> const_iterator
{
return mValues.end();
}
#endif
......@@ -14,6 +14,7 @@
#include <iostream>
#include "nullstream.h"
#include "util.h"
namespace SUBool
{
......@@ -73,10 +74,29 @@ namespace SUBool
{
return level <= mVerbLevel;
}
template <class T>
void
DumpValue(const std::string &line_prefix, unsigned level,
const std::string &name, const T &value)
{
if (CheckLevel(level))
{
TLog(level) << line_prefix << name << ": " << value << std::endl;
}
}
};
extern LogStream logs;
}; // namespace SUBool
template <>
inline void
SUBool::LogStream::DumpValue<bool>(const std::string &line_prefix,
unsigned level, const std::string &name, const bool &value)
{
DumpValue<std::string>(line_prefix, level, name, bool_to_string(value));
}
#endif
......@@ -40,59 +40,6 @@ SUBool::print_version(std::ostream &os, const std::string &prg_name,
}
}
po::options_description
SUBool::prg_common_opts(const std::string &label, unsigned &verbosity)
{
po::options_description desc(label);
desc.add_options()("help,h", "Print help")(
"version,v", "Print version info")("verbosity,b",
po::value<unsigned>(&verbosity)->default_value(verbosity),
"Verbosity level of the log which is written to standard output."
" 0 means no log is written, however, messages of glucose can still "
"appear.");
return desc;
}
bool
SUBool::prg_process_common_opts(std::ostream &os,
const po::options_description &visible, const std::string &positional_help,
const po::variables_map &vm)
{
if (vm.count("help"))
{
prg_help(os, visible, positional_help);
return false;
}
if (vm.count("version"))
{
print_version(os);
return false;
}
return true;
}
void
SUBool::prg_help(std::ostream &os, const po::options_description &visible,
const std::string &positional_help)
{
print_prg_info(os);
os << "\nUsage:\n\t" << SUBool::kPrgName << " [OPTION...] "
<< positional_help << "\n\n";
os << visible << std::endl;
}
std::pair<po::options_description, po::positional_options_description>
SUBool::prg_input_output_opts(std::string &input_file, std::string &output_file)
{
po::options_description opts;
opts.add_options()("input", po::value<std::string>(&input_file))(
"output", po::value<std::string>(&output_file));
po::positional_options_description positional;
positional.add("input", 1);
positional.add("output", 1);
return {opts, positional};
}
void
SUBool::PrgOptions::InitCommon()
{
......@@ -176,28 +123,6 @@ SUBool::PrgOptions::Version() const
bool
SUBool::PrgOptions::Parse(int argc, char *argv[])
{
/*auto visible = SUBool::prg_common_opts("Allowed options",
conf.verbosity); visible.add_options()("process,p",
po::value<std::string>(&process)->default_value(
process_names.at(static_cast<unsigned>(conf.process))),
kProcessOptDesc);
auto [hidden, positional] =
SUBool::prg_input_output_opts(conf.input_file, conf.output_file);
po::options_description desc;
desc.add(visible).add(hidden);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv)
.options(desc)
.positional(positional)
.run(),
vm);
po::notify(vm);
if (!SUBool::prg_process_common_opts(
std::cout, visible, "[input [output]]", vm))
{
return {};
}*/
po::options_description cmdline_opts;
mVisible.add(mCommon).add(mConfig);
cmdline_opts.add(mCommon).add(mConfig).add(mHidden);
......
......@@ -18,6 +18,7 @@
#include <boost/program_options.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include "enum.h"
#include "logstream.h"
#include "sbexception.h"
#include "util.h"
......@@ -109,39 +110,6 @@ namespace SUBool
os.Out() << t << std::endl;
}
template <class T>
T
enum_of_int(unsigned val)
{
if (val > static_cast<unsigned>(T::Last))
{
throw BadParameterException("enum_of_int", "val too high");
}
return static_cast<T>(val);
}
template <class E, size_t S>
E
enum_of_name(const std::string &val, const std::array<std::string, S> &names)
{
auto i = std::find(names.begin(), names.end(), val);
if (i == names.end())
{
throw BadParameterException("enum_of_name", "name is not on the list");
}
return enum_of_int<E>(i - names.begin());
}
struct EnumClassHash
{
template <class T>
size_t
operator()(T t) const
{
return static_cast<size_t>(t);