-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathcontrol.cc
More file actions
265 lines (234 loc) · 8.56 KB
/
control.cc
File metadata and controls
265 lines (234 loc) · 8.56 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
#include <clingo/util/algorithm.hh>
#include <clingo/control.h>
#include <clingo/script.h>
#include <clasp/cli/clasp_options.h>
#include <potassco/program_opts/program_options.h>
#include <potassco/program_opts/typed_value.h>
#include "ast.hh" // IWYU pragma: keep
#include "control.hh"
#include "lib.hh"
#include "opts.hh"
using namespace CppClingo::CAPI;
namespace CppClingo::CAPI {
namespace {
auto c_cast(CppClingo::Input::ConstMap const *map) {
// NOLINTNEXTLINE
return reinterpret_cast<clingo_const_map_t const *>(map);
}
auto cpp_cast(clingo_const_map_t const *map) {
// NOLINTNEXTLINE
return reinterpret_cast<CppClingo::Input::ConstMap const *>(map);
}
} // namespace
} // namespace CppClingo::CAPI
extern "C" auto clingo_control_new(clingo_lib_t *lib, clingo_string_t const *arguments, size_t size,
clingo_control_t **control) -> bool {
CLINGO_TRY {
using namespace Potassco::ProgramOptions;
auto opts = CppClingo::CAPI::ClingoOptions{lib->log, *lib->store};
auto slv_cfg = std::make_unique<Clasp::Cli::ClaspCliConfig>();
auto clasp = std::make_unique<Clasp::ClaspFacade>();
// parse options
auto ctx = OptionContext{"<libclingo>"};
opts.init(ctx);
auto group_basic = OptionGroup{"Basic Options"};
auto parse_mode = [&opts](std::string_view str) { return opts.init_app_mode(str); };
group_basic.addOptions() //
("mode", parse(parse_mode),
"Run in the specified mode\n"
" %A: <mode {parse|rewrite|solve}>\n"
" parse : Stop processing after parsing\n"
" rewrite : Stop processing after rewriting\n"
" solve : Stop processing after solving");
ctx.add(group_basic);
slv_cfg->addOptions(ctx);
auto pos_parser = [](std::string_view str, std::string &out) {
if (int value = 0; Potassco::stringTo(str, value) == std::errc{}) {
out = "models";
return true;
}
return false;
};
std::vector<std::string> bargs;
std::vector<char const *> cargs;
bargs.reserve(size);
cargs.reserve(size + 1);
for (auto const &str : std::span{arguments, size}) {
bargs.emplace_back(std::string_view{str.data, str.size});
cargs.emplace_back(bargs.back().c_str());
}
cargs.emplace_back(nullptr);
DefaultParseContext pc{ctx};
parseCommandArray(pc, {cargs.data(), size}, pos_parser);
ctx.assignDefaults(pc.parsed());
opts.validate_options(pc.parsed());
slv_cfg->finalize(pc.parsed(), Clasp::ProblemType::asp, true);
// setup control
if (opts.mode() == CppClingo::Control::AppMode::solve) {
clasp->startAsp(*slv_cfg, !opts.solver_options().single_shot);
}
auto slv = std::make_unique<CppClingo::Control::Solver>(*clasp, *slv_cfg, lib->log, *lib->store, lib->scripts,
opts.rewrite_options(), opts.solver_options(), nullptr);
opts.apply(*slv);
*control = new clingo_control{lib, std::move(slv), std::move(slv_cfg), std::move(clasp)};
}
CLINGO_CATCH;
}
extern "C" void clingo_control_acquire(clingo_control_t *control) {
if (control != nullptr) {
++control->ref_count;
}
}
extern "C" void clingo_control_release(clingo_control_t *control) {
if (control != nullptr && --control->ref_count == 0) {
delete control;
}
}
extern "C" auto clingo_control_mode(clingo_control_t *control, clingo_mode_t *mode) -> bool {
CLINGO_TRY {
*mode = static_cast<clingo_mode_t>(control->slv->get_mode());
}
CLINGO_CATCH;
}
extern "C" auto clingo_control_parse_files(clingo_control_t *control, clingo_string_t const *files, size_t size)
-> bool {
CLINGO_TRY {
auto vec = CppClingo::Util::to_vec(std::span{files, size},
[](auto const &x) { return std::string_view{x.data, x.size}; });
control->slv->parse(vec);
}
CLINGO_CATCH;
}
extern "C" auto clingo_control_parse_string(clingo_control_t *control, char const *program, size_t size) -> bool {
CLINGO_TRY {
control->slv->parse({program, size});
}
CLINGO_CATCH;
}
extern "C" auto clingo_control_join(clingo_control_t *control, clingo_program_t const *program) -> bool {
CLINGO_TRY {
control->slv->join(program->program);
}
CLINGO_CATCH;
}
extern "C" void clingo_control_interrupt(clingo_control_t *control) {
control->slv->interrupt();
}
extern "C" auto clingo_control_main(clingo_control_t *control) -> bool {
CLINGO_TRY {
control->slv->main();
}
CLINGO_CATCH;
}
extern "C" auto clingo_control_buffer(clingo_control_t *control, clingo_string_t *result) -> bool {
CLINGO_TRY {
auto str = control->slv->buf().view();
result->data = str.data();
result->size = str.size();
}
CLINGO_CATCH;
}
extern "C" auto clingo_control_get_parts(clingo_control_t *control, clingo_part_t const **parts, size_t *size,
bool *has_value) -> bool {
CLINGO_TRY {
auto const &x = control->slv->get_parts();
if (has_value != nullptr) {
*has_value = x.has_value();
}
if (x.has_value()) {
if (parts != nullptr) {
thread_local std::vector<clingo_part_t> res;
res.clear();
for (auto const &part : x->elems()) {
res.emplace_back(part.first->data(), part.first->size(), c_cast(part.second.data()),
part.second.size());
}
*parts = res.data();
}
if (size != nullptr) {
*size = x->elems().size();
}
}
}
CLINGO_CATCH;
}
extern "C" auto clingo_control_set_parts(clingo_control_t *control, clingo_part_t const *parts, size_t size,
bool has_value) -> bool {
CLINGO_TRY {
if (has_value) {
control->slv->set_parts(convert(control, parts, size));
} else {
control->slv->set_parts(std::nullopt);
}
}
CLINGO_CATCH;
}
extern "C" auto clingo_control_const_map(clingo_control_t *control, clingo_const_map_t const **map) -> bool {
CLINGO_TRY {
if (control == nullptr || map == nullptr) {
return fail_arguments();
}
*map = c_cast(&control->slv->const_map());
}
CLINGO_CATCH;
}
extern "C" auto clingo_const_map_find(clingo_const_map_t const *map, char const *name, size_t size,
clingo_symbol_t *symbol, bool *found) -> bool {
CLINGO_TRY {
if (map == nullptr || (size > 0 && name == nullptr)) {
return fail_arguments();
}
auto const *cmap = cpp_cast(map);
auto it = cmap->find(std::string_view{name, size});
if (it != cmap->end()) {
if (found != nullptr) {
*found = true;
}
if (symbol != nullptr) {
*symbol = *c_cast(&*it->second.second);
}
} else if (found != nullptr) {
*found = false;
}
}
CLINGO_CATCH;
}
extern "C" auto clingo_const_map_at(clingo_const_map_t const *map, size_t index, clingo_string_t *name,
clingo_symbol_t *symbol) -> bool {
CLINGO_TRY {
if (map == nullptr) {
return fail_arguments();
}
auto const *cmap = cpp_cast(map);
auto it = cmap->nth(index);
if (symbol != nullptr) {
*symbol = *c_cast(&*it->second.second);
}
if (name != nullptr) {
name->data = it->first->data();
name->size = it->first->size();
}
}
CLINGO_CATCH;
}
extern "C" auto clingo_const_map_size(clingo_const_map_t const *map, size_t *size) -> bool {
CLINGO_TRY {
if (map == nullptr || size == nullptr) {
return fail_arguments();
}
auto const *cmap = cpp_cast(map);
*size = cmap->size();
}
CLINGO_CATCH;
}
extern "C" auto clingo_control_discard(clingo_control_t *control, clingo_discard_type_t type) -> bool {
CLINGO_TRY {
if ((type & clingo_discard_type_e::minimize) != 0) {
control->clasp->asp()->removeMinimize();
}
if ((type & clingo_discard_type_e::project) != 0) {
control->clasp->asp()->removeProject();
}
}
CLINGO_CATCH;
}