-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathmain.cc
More file actions
344 lines (300 loc) · 13.2 KB
/
main.cc
File metadata and controls
344 lines (300 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "control.hh" // IWYU pragma: keep
#include "lib.hh" // IWYU pragma: keep
#include "opts.hh"
#include <clingo/app.h>
#include <clingo/control/solver.hh>
#include <clingo/input/parser.hh>
#include <clingo/output/text.hh>
#include <clasp/cli/clasp_app.h>
#include <utility>
using namespace CppClingo::CAPI;
namespace CppClingo::CAPI {
namespace {
using namespace CppClingo::Input;
class AppOptions {
public:
using OptionParser = std::function<bool(std::string_view)>;
explicit AppOptions(Potassco::ProgramOptions::OptionContext &root) : root_(&root) {}
void add_option(std::string_view group, std::string_view option, std::string_view description, OptionParser parser,
std::optional<std::string_view> argument, bool multi = false) {
using namespace Potassco::ProgramOptions;
auto value = parse(std::move(parser));
if (argument) {
value.arg(*argument);
}
if (multi) {
value.composing();
}
add_option_value_(group, option, std::move(value), description);
}
void add_flag(std::string_view group, std::string_view option, std::string_view description, bool &target) {
using namespace Potassco::ProgramOptions;
auto value{flag(target)};
value.negatable();
add_option_value_(group, option, std::move(value), description);
}
auto set_default_value(std::string_view option, std::string_view value) -> bool {
if (!root_->option(option).assignDefault(value)) {
throw std::invalid_argument(std::string("Invalid value for option '").append(option).append("'"));
}
return true;
}
private:
void add_option_value_(std::string_view group, std::string_view option, Potassco::ProgramOptions::ValueDesc value,
std::string_view description) {
auto init = root_->addOptions(group);
init(option, std::move(value), description);
}
Potassco::ProgramOptions::OptionContext *root_ = nullptr;
};
auto c_cast(AppOptions *opts) -> clingo_options_t * {
// NOLINTNEXTLINE
return reinterpret_cast<clingo_options_t *>(opts);
}
auto cpp_cast(clingo_options_t *opts) -> AppOptions * {
// NOLINTNEXTLINE
return reinterpret_cast<AppOptions *>(opts);
}
class AppAdapter {
public:
AppAdapter(clingo_application_t const *app, void *data) : app_(app), data_(data) {}
[[nodiscard]] auto get_name() const -> std::string_view {
if (app_ != nullptr && app_->program_name != nullptr) {
clingo_string_t str;
app_->program_name(data_, &str);
return {str.data, str.size};
}
return CLINGO_EXECUTABLE;
}
[[nodiscard]] auto get_version() const -> std::string_view {
if (app_ != nullptr && app_->version != nullptr) {
clingo_string_t str;
app_->version(data_, &str);
return {str.data, str.size};
}
return CLINGO_VERSION;
}
void register_options(Potassco::ProgramOptions::OptionContext &root) {
if (app_ != nullptr && app_->register_options != nullptr) {
AppOptions opts(root);
handle_error(app_->register_options(c_cast(&opts), data_));
}
}
void validate_options() const {
if (app_ != nullptr && app_->validate_options != nullptr) {
handle_error(app_->validate_options(data_));
}
}
[[nodiscard]] auto has_print_model() const -> bool { return app_ != nullptr && app_->print_model != nullptr; }
template <class T> void print_model(CppClingo::Control::Model &mdl, T &prt) const {
assert(has_print_model());
auto cprt = [](void *data) -> bool {
CLINGO_TRY {
(*static_cast<T const *>(data))();
}
CLINGO_CATCH;
};
// NOLINTNEXTLINE
auto *cmdl = reinterpret_cast<clingo_model_t *>(&mdl);
handle_error(app_->print_model(cmdl, cprt, static_cast<void *>(&prt), data_));
}
[[nodiscard]] auto has_main() const -> bool { return app_ != nullptr && app_->main != nullptr; }
void main(clingo_control_t *ctl, std::span<std::string const> const &input) {
assert(has_main());
auto vec =
CppClingo::Util::to_vec(input, [](auto const &str) { return clingo_string_t{str.data(), str.size()}; });
handle_error(app_->main(ctl, vec.data(), vec.size(), data_));
}
private:
clingo_application_t const *app_;
void *data_;
};
class ClingoApp : public Clasp::Cli::ClaspAppBase {
public:
ClingoApp(clingo_lib_t &lib, clingo_application_t const *app = nullptr, void *data = nullptr)
: ctl_{new clingo_control_t{&lib}}, app_{app, data}, opts_{ctl_->lib->log, *ctl_->lib->store} {}
[[nodiscard]] auto getName() const -> std::string_view override { return app_.get_name(); }
[[nodiscard]] auto getVersion() const -> std::string_view override { return app_.get_version(); }
[[nodiscard]] auto getUsage() const -> std::string_view override { return "[number] [options] [files]"; }
private:
using AppMode = CppClingo::Control::AppMode;
enum class Mode : uint8_t {
parse = static_cast<uint8_t>(AppMode::parse),
rewrite = static_cast<uint8_t>(AppMode::rewrite),
ground = static_cast<uint8_t>(AppMode::ground),
solve = static_cast<uint8_t>(AppMode::solve),
clasp = static_cast<uint8_t>(AppMode::solve) + 1,
};
using ClaspOutput = Clasp::Cli::Output;
using ProblemType = Clasp::ProblemType;
using BaseType = Clasp::Cli::ClaspAppBase;
using BaseType::run;
auto getProblemType() -> ProblemType override {
return mode_ != Mode::clasp ? Clasp::ProblemType::asp : detectProblemType();
}
auto onEvent(const Clasp::Event &ev) -> void override {
BaseType::onEvent(ev);
if (const auto *g = Clasp::event_cast<Control::Grounded>(ev); g != nullptr && !g->params.empty()) {
ctl_->slv->print_summary(false);
}
}
void initOptions(Potassco::ProgramOptions::OptionContext &root) override {
using namespace Potassco::ProgramOptions;
BaseType::initOptions(root);
opts_.init(root);
auto group_basic = OptionGroup{"Basic Options"};
auto parse_mode = [this](std::string_view str) {
if (opts_.init_app_mode(str)) {
mode_ = static_cast<Mode>(opts_.mode());
return true;
}
if (str == "clasp") {
mode_ = Mode::clasp;
return true;
}
return false;
};
group_basic.addOptions() //
("mode", parse(parse_mode),
"Run in mode\n"
" %A: <mode {parse|rewrite|solve|clasp}>\n"
" parse : Print parsed program and exit\n"
" rewrite : Print rewritten program and exit\n"
" solve : Ground and solve the program (default)\n"
" clasp : Invoke clasp on the input");
root.add(group_basic);
app_.register_options(root);
}
void validateOptions(const Potassco::ProgramOptions::OptionContext &root,
const Potassco::ProgramOptions::ParsedOptions &parsed) override {
BaseType::validateOptions(root, parsed);
opts_.validate_options(parsed);
// --convert may redefine execution mode (e.g., text -> ground)
if (parsed.contains("convert")) {
mode_ = static_cast<Mode>(opts_.mode());
}
setExitCode(Clasp::Cli::exit_no_run);
app_.validate_options();
setExitCode(0);
}
auto createOutput(Clasp::Cli::OutputSink sink, ProblemType f, Clasp::Cli::ClaspAppOptions::OutputFormat outf)
-> std::unique_ptr<Clasp::Cli::Output> override {
if ((mode_ != Mode::solve && mode_ != Mode::clasp) ||
opts_.backend_type() != CppClingo::Control::BackendType::clasp) {
return nullptr;
}
auto om = mode_ == Mode::clasp ? Clasp::Cli::Output::mode_default : Clasp::Cli::Output::mode_clingo;
if (!app_.has_print_model() || !Clasp::Cli::ClaspAppOptions::isTextOutput(outf)) {
return ClaspAppBase::createOutput(sink, f, outf, om);
}
auto output = createTextOutput(sink, f, om);
output->setModelPrinter(
[this](Clasp::Cli::TextOutput &out, const Clasp::SharedContext &ctx, const Clasp::Model &mdl) {
auto prt = [&]() { out.printModelValues(ctx, mdl); };
// NOTE: the function can only be called while the solve handle is alive
auto guard = CppClingo::Control::unlock_guard{ctl_->slv->get_lock()};
app_.print_model(ctl_->slv->map_model(mdl), prt);
});
return output;
}
void run(Clasp::ClaspFacade &clasp) override {
if (mode_ != Mode::clasp) {
if (mode_ == Mode::solve) {
clasp.startAsp(config(), false);
}
opts_.mode() = static_cast<AppMode>(mode_);
auto slv = CppClingo::Control::Solver{clasp,
config(),
ctl_->lib->log,
*ctl_->lib->store,
ctl_->lib->scripts,
opts_.rewrite_options(),
opts_.solver_options(),
stdout};
opts_.apply(slv);
// NOTE: member for createTextOutput
ctl_->bind(&slv, &slv.config().clasp(), &slv.clasp_facade());
POTASSCO_SCOPE_EXIT({ ctl_->slv->print_summary(true); });
if (auto in = input(); app_.has_main()) {
if (mode_ == Mode::solve) {
ctl_->clasp->enableProgramUpdates();
}
app_.main(ctl_.get(), in);
} else {
ctl_->slv->main(std::vector<std::string_view>{in.begin(), in.end()});
}
} else {
BaseType::run(clasp);
}
}
struct release_control {
void operator()(clingo_control_t *ctl) const { clingo_control_release(ctl); }
};
Mode mode_ = Mode::solve;
std::unique_ptr<clingo_control_t, release_control> ctl_;
AppAdapter app_;
CppClingo::CAPI::ClingoOptions opts_;
};
} // namespace
} // namespace CppClingo::CAPI
extern "C" auto clingo_options_add(clingo_options_t *options, char const *group, size_t group_size, char const *option,
size_t option_size, char const *description, size_t description_size,
clingo_option_parser_t parser, void *data, bool multi, char const *argument,
size_t argument_size) -> bool {
CLINGO_TRY {
auto *opts = cpp_cast(options);
opts->add_option(
{group, group_size}, {option, option_size}, {description, description_size},
[parser, data](std::string_view value) {
if (!parser(value.data(), value.size(), data)) {
clingo_result_t code = clingo_result_success;
clingo_get_error(&code, nullptr);
if (code == clingo_result_invalid) {
clingo_clear_error();
return false;
}
raise_error();
}
return true;
},
argument != nullptr ? std::make_optional<std::string_view>(argument, argument_size) : std::nullopt, multi);
}
CLINGO_CATCH;
}
extern "C" auto clingo_options_add_flag(clingo_options_t *options, char const *group, size_t group_size,
char const *option, size_t option_size, char const *description,
size_t description_size, bool *target) -> bool {
CLINGO_TRY {
auto *opts = cpp_cast(options);
opts->add_flag({group, group_size}, {option, option_size}, {description, description_size}, *target);
}
CLINGO_CATCH;
}
extern "C" auto clingo_options_set_default_value(clingo_options_t *options, char const *option, size_t option_size,
char const *value, size_t value_size) -> bool {
CLINGO_TRY {
auto *opts = cpp_cast(options);
opts->set_default_value({option, option_size}, {value, value_size});
}
CLINGO_CATCH;
}
extern "C" auto clingo_main(clingo_lib_t *lib, clingo_string_t const *arguments, size_t size,
clingo_application_t const *app, void *data, int *code) -> bool {
CLINGO_TRY {
if (code != nullptr) {
*code = 1;
}
if (lib == nullptr || (arguments == nullptr && size > 0)) {
return fail_arguments();
}
auto capp = ClingoApp{*lib, app, data};
auto args = CppClingo::Util::to_vec(std::span{arguments, size},
[](auto const &str) { return std::string{str.data, str.size}; });
auto cargs = CppClingo::Util::to_vec(args, [](auto const &str) { return str.c_str(); });
auto res = capp.main(cargs);
if (code != nullptr) {
*code = res;
}
}
CLINGO_CATCH;
}