Skip to content

Commit

Permalink
Merge pull request #490 from potassco/simplify-output
Browse files Browse the repository at this point in the history
Simplify clasp text output customization.
  • Loading branch information
rkaminsk authored Mar 20, 2024
2 parents d84c400 + 7b69624 commit 3d2f2b6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 72 deletions.
2 changes: 1 addition & 1 deletion libclingo/clingo/clingo_app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected:

ProblemType getProblemType() override;
void run(Clasp::ClaspFacade& clasp) override;
ClaspOutput* createOutput(ProblemType f) override;
ClaspOutput* createTextOutput(const Clasp::Cli::ClaspAppBase::TextOptions& options) override;
void printHelp(const Potassco::ProgramOptions::OptionContext& root) override;
void printVersion() override;

Expand Down
97 changes: 27 additions & 70 deletions libclingo/src/clingo_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,80 +111,37 @@ Clasp::ProblemType ClingoApp::getProblemType() {
return Clasp::ClaspFacade::detectProblemType(getStream());
}

// TODO: the code below is annoying. There is too much copy and paste. The
// easiest way would be if the textoutput would already provide something to
// customize the output.
namespace {

class CustomTextOutput : public Clasp::Cli::TextOutput {
public:
CustomTextOutput(std::unique_ptr<ClingoControl> &ctl, IClingoApp &app, Clasp::uint32 verbosity, Format f, const char* catAtom = 0, char ifs = ' ')
: TextOutput(verbosity, f, catAtom, ifs), ctl_(ctl), app_(app) { }

protected:
void printModel(const Clasp::OutputTable& out, const Clasp::Model& m, PrintLevel x) override {
if (ctl_) {
if (x == modelQ()) {
comment(1, "%s: %" PRIu64"\n", !m.up ? "Answer" : "Update", m.num);
ClingoModel cm(*ctl_, &m);
std::lock_guard<decltype(ctl_->propLock_)> lock(ctl_->propLock_);
app_.print_model(&cm, [&]() { printValues(out, m); });
}
if (x == optQ()) {
printMeta(out, m);
}
fflush(stdout);
}
else { Clasp::Cli::TextOutput::printModel(out, m, x); }
}
private:
std::unique_ptr<ClingoControl> &ctl_;
IClingoApp &app_;
};

} // namespace

ClingoApp::ClaspOutput* ClingoApp::createOutput(ProblemType f) {
if (mode_ == mode_gringo) return 0;
using namespace Clasp;
using namespace Clasp::Cli;
SingleOwnerPtr<ClaspOutput> out;
if (claspAppOpts_.outf == ClaspAppOptions::out_none) {
return 0;
ClingoApp::ClaspOutput* ClingoApp::createTextOutput(const Clasp::Cli::ClaspAppBase::TextOptions &options) {
if (mode_ == mode_gringo) {
return nullptr;
}
if (claspAppOpts_.outf != ClaspAppOptions::out_json || claspAppOpts_.onlyPre) {
TextOutput::Format outFormat = TextOutput::format_asp;
if (f == Problem_t::Sat){ outFormat = TextOutput::format_sat09; }
else if (f == Problem_t::Pb) { outFormat = TextOutput::format_pb09; }
else if (f == Problem_t::Asp && claspAppOpts_.outf == ClaspAppOptions::out_comp) {
outFormat = TextOutput::format_aspcomp;
}
if (app_->has_printer()) {
out.reset(new CustomTextOutput(grd, *app_, verbose(), outFormat, claspAppOpts_.outAtom.c_str(), claspAppOpts_.ifs));
}
else {
out.reset(new TextOutput(verbose(), outFormat, claspAppOpts_.outAtom.c_str(), claspAppOpts_.ifs));
}
if (claspConfig_.parse.isEnabled(ParserOptions::parse_maxsat) && f == Problem_t::Sat) {
static_cast<TextOutput*>(out.get())->result[TextOutput::res_sat] = "UNKNOWN";
}
else if (!app_->has_printer()) {
return Clasp::Cli::ClaspAppBase::createTextOutput(options);
}
else {
out.reset(new JsonOutput(verbose()));
}
if (claspAppOpts_.quiet[0] != static_cast<uint8>(UCHAR_MAX)) {
out->setModelQuiet((ClaspOutput::PrintLevel)std::min(uint8(ClaspOutput::print_no), claspAppOpts_.quiet[0]));
}
if (claspAppOpts_.quiet[1] != static_cast<uint8>(UCHAR_MAX)) {
out->setOptQuiet((ClaspOutput::PrintLevel)std::min(uint8(ClaspOutput::print_no), claspAppOpts_.quiet[1]));
}
if (claspAppOpts_.quiet[2] != static_cast<uint8>(UCHAR_MAX)) {
out->setCallQuiet((ClaspOutput::PrintLevel)std::min(uint8(ClaspOutput::print_no), claspAppOpts_.quiet[2]));
}
if (claspAppOpts_.hideAux && clasp_.get()) {
clasp_->ctx.output.setFilter('_');
class CustomTextOutput : public Clasp::Cli::TextOutput {
public:
using BaseType = Clasp::Cli::TextOutput;
CustomTextOutput(std::unique_ptr<ClingoControl> &ctl, IClingoApp &app, const Clasp::Cli::ClaspAppBase::TextOptions &opts)
: TextOutput(opts.verbosity, opts.format, opts.catAtom, opts.ifs), ctl_(ctl), app_(app) { }

protected:
void printModelValues(const Clasp::OutputTable &out, const Clasp::Model &m) override {
if (ctl_) {
ClingoModel cm(*ctl_, &m);
std::lock_guard<decltype(ctl_->propLock_)> lock(ctl_->propLock_);
app_.print_model(&cm, [&]() { BaseType::printModelValues(out, m); });
}
else {
BaseType::printModelValues(out, m);
}
}
private:
std::unique_ptr<ClingoControl> &ctl_;
IClingoApp &app_;
};
return new CustomTextOutput(grd, *app_, options);
}
return out.release();
}

void ClingoApp::printHelp(const Potassco::ProgramOptions::OptionContext& root) {
Expand Down

0 comments on commit 3d2f2b6

Please sign in to comment.