Clingo
Loading...
Searching...
No Matches
control.hh
1#pragma once
2
3#include <clingo/backend.hh>
4#include <clingo/base.hh>
5#include <clingo/config.hh>
6#include <clingo/core.hh>
7#include <clingo/ground.hh>
8#include <clingo/observe.hh>
9#include <clingo/profile.hh>
10#include <clingo/propagate.hh>
11#include <clingo/solve.hh>
12#include <clingo/stats.hh>
13#include <clingo/symbol.hh>
14
15#include <clingo/control.h>
16
17#include <cassert>
18#include <exception>
19#include <optional>
20#include <span>
21
22namespace Clingo {
23
24namespace AST {
25
26class Program;
27auto c_cast(Program const &x) -> clingo_program_t *;
28
29} // namespace AST
30
31namespace Detail {
32
33void join(clingo_control_t *ctx, clingo_program_t const *prg);
34
35} // namespace Detail
36
41
45class ConstMap {
46 public:
48 using key_type = std::string_view;
52 using value_type = std::pair<key_type, mapped_type>;
54 using size_type = std::size_t;
56 using difference_type = std::ptrdiff_t;
60 using pointer = Detail::ArrowProxy<value_type>;
62 using iterator = Detail::RandomAccessIterator<ConstMap>;
63
67 explicit ConstMap(clingo_const_map_t const *map) : map_{map} {}
68
73 [[nodiscard]] auto contains(key_type name) const -> bool {
74 return Detail::call<clingo_const_map_find>(map_, name.data(), name.size(), nullptr);
75 }
76
81 [[nodiscard]] auto operator[](key_type name) const -> mapped_type {
82 clingo_symbol_t sym = 0;
83 bool found = false;
84 Detail::handle_error(clingo_const_map_find(map_, name.data(), name.size(), &sym, &found));
85 return found ? Symbol{sym, true} : throw std::out_of_range{"key not found"};
86 }
87
92 [[nodiscard]] auto at(size_t index) const -> value_type {
93 clingo_string_t name;
94 clingo_symbol_t sym = 0;
95 Detail::handle_error(clingo_const_map_at(map_, index, &name, &sym));
96 return {{name.data, name.size}, Symbol{sym, true}};
97 }
98
102 [[nodiscard]] auto size() const -> size_type { return Detail::call<clingo_const_map_size>(map_); }
103
107 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
108
112 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
113
114 private:
115 clingo_const_map_t const *map_;
116};
117
127
139
148
150enum class DiscardType {
151 minimize = clingo_discard_type_e::minimize,
152 project = clingo_discard_type_e::project,
153};
154
160class Control {
161 public:
167 using Context = std::function<SymbolVector(std::string_view, SymbolSpan)>;
168
173 explicit Control(Library const &lib, StringList arguments) : Clingo::Control{lib, StringSpan{arguments}} {}
174
176 explicit Control(Library const &lib, StringSpan arguments = {}) {
177 auto cstrs = Detail::transform(arguments, [](auto const &x) { return clingo_string_t{x.data(), x.size()}; });
178 ctl_.reset(Detail::call<clingo_control_new>(c_cast(lib), cstrs.data(), arguments.size()), false);
179 }
180
187 explicit Control(clingo_control_t *rep, bool acquire) : ctl_{rep, acquire} {}
188
193 [[nodiscard]] friend auto c_cast(Control const &ctl) -> clingo_control_t * { return ctl.ctl_.get(); }
194
198 [[nodiscard]] auto mode() const -> ControlMode {
199 return static_cast<ControlMode>(Detail::call<clingo_control_mode>(ctl_.get()));
200 }
201
218 void write_aspif(std::optional<std::string_view> path, WriteAspifFlags flags = WriteAspifFlags::none) const {
219 if (path && path->empty()) {
220 throw std::invalid_argument{"path cannot be empty"};
221 }
222 auto path_view = path.value_or(std::string_view{});
223 Detail::handle_error(clingo_control_write_aspif(ctl_.get(), path_view.data(), path_view.size(),
224 static_cast<clingo_write_aspif_mode_t>(flags)));
225 }
226
235 void parse_files(StringSpan files) const {
236 auto cfiles = Detail::transform(files, [](auto const &x) { return clingo_string_t{x.data(), x.size()}; });
237 Detail::handle_error(clingo_control_parse_files(ctl_.get(), cfiles.data(), cfiles.size()));
238 }
239
248 void parse_files(StringList files) const { parse_files(StringSpan{files.begin(), files.end()}); }
249
253 void parse_string(std::string_view program) const {
254 Detail::handle_error(clingo_control_parse_string(ctl_.get(), program.data(), program.size()));
255 }
256
265 void ground(std::optional<PartSpan> parts = std::nullopt, Context ctx = nullptr) const {
266 auto [default_parts, c_parts] = c_parts_(parts);
268 []([[maybe_unused]] char const *name, [[maybe_unused]] size_t name_size,
269 [[maybe_unused]] size_t arguments_size, [[maybe_unused]] void *data, bool *result) -> bool {
270 CLINGO_TRY {
271 *result = true;
272 }
273 CLINGO_CATCH;
274 },
275 []([[maybe_unused]] clingo_lib_t *lib, [[maybe_unused]] clingo_location_t const *location, char const *name,
276 size_t name_size, clingo_symbol_t const *arguments, size_t arguments_size, void *data,
277 clingo_symbol_callback_t symbol_callback, void *symbol_callback_data) -> bool {
278 CLINGO_TRY {
279 auto &cb = *static_cast<std::function<SymbolVector(std::string_view, SymbolSpan)> *>(data);
280 auto syms = cb({name, name_size}, {cpp_cast(arguments), arguments_size});
281 auto const *c_syms = c_cast(syms.data());
282 return symbol_callback(c_syms, syms.size(), symbol_callback_data);
283 }
284 CLINGO_CATCH;
285 },
286 nullptr,
287 nullptr,
288 };
289
290 Detail::handle_error(
291 clingo_control_ground(ctl_.get(), c_parts.data(), c_parts.size(), ctx ? &handler : nullptr, &ctx));
292 }
293
302 void ground(std::initializer_list<Part> parts, Context ctx = nullptr) const {
303 ground(PartSpan{parts}, std::move(ctx));
304 }
305
319 template <Detail::UserData<GroundEventHandler> Handler = std::nullptr_t>
320 [[nodiscard]] auto start_ground(std::optional<PartSpan> parts = std::nullopt, Handler &&handler = nullptr) const
321 -> GroundHandle {
322 auto [default_parts, c_parts] = c_parts_(parts);
323 using UserData = Detail::UserDataTraits<Handler>;
324 auto user_data = UserData::create(std::forward<Handler>(handler));
325 clingo_ground_event_handler_t const *c_handler_ptr = nullptr;
326 if constexpr (UserData::has_data) {
327 if (UserData::has_value(user_data)) {
328 static constexpr auto c_handler = clingo_ground_event_handler_t{
329 [](char const *name, size_t name_size, size_t arguments_size, void *data, bool *result) -> bool {
330 CLINGO_TRY {
331 *result = UserData::cast(data)->callable(std::string_view{name, name_size}, arguments_size);
332 }
333 CLINGO_CATCH;
334 },
335 []([[maybe_unused]] clingo_lib_t *lib, clingo_location_t const *location, char const *name,
336 size_t name_size, clingo_symbol_t const *arguments, size_t arguments_size, void *data,
337 clingo_symbol_callback_t symbol_callback, void *symbol_callback_data) -> bool {
338 CLINGO_TRY {
339 Location loc{location};
340 auto syms = UserData::cast(data)->call(loc, {name, name_size},
341 {cpp_cast(arguments), arguments_size});
342 auto const *c_syms = c_cast(syms.data());
343 return symbol_callback(c_syms, syms.size(), symbol_callback_data);
344 }
345 CLINGO_CATCH;
346 },
347 [](clingo_ground_result_t result, void *data) {
348 UserData::cast(data)->finish(static_cast<GroundResult>(result));
349 },
350 [](void *data) { UserData::free(data); },
351 };
352 c_handler_ptr = &c_handler;
353 }
354 }
355 clingo_ground_handle_t *handle = nullptr;
356 Detail::handle_error(clingo_control_start_ground(ctl_.get(), c_parts.data(), c_parts.size(), c_handler_ptr,
357 UserData::release(user_data), &handle));
358 return GroundHandle{handle};
359 }
360
362 template <Detail::UserData<GroundEventHandler> Handler = std::nullptr_t>
363 [[nodiscard]] auto start_ground(std::initializer_list<Part> parts, Handler &&handler = nullptr) const
364 -> GroundHandle {
365 return start_ground(PartSpan{parts}, std::forward<Handler>(handler));
366 }
367
371 [[nodiscard]] auto base() const -> Base { return Base{Detail::call<clingo_control_base>(ctl_.get())}; }
372
376 [[nodiscard]] auto stats() const -> StatsView {
377 auto const *stats = Detail::call<clingo_control_stats>(ctl_.get());
378 auto key = Detail::call<clingo_stats_root>(stats);
379 return StatsView{stats, key};
380 }
381
388 auto profile() -> std::vector<ProfileNode> {
389 struct Builder {
390 static auto internal(size_t depth, char const *key, size_t key_size, bool nested, void *data) -> bool {
391 CLINGO_TRY {
392 auto *self = static_cast<Builder *>(data);
393 assert(depth <= self->stack.size());
394 self->stack.resize(depth);
395 auto node = ProfileNodeInternal{std::string{key, key_size}, nested};
396 if (self->stack.empty()) {
397 self->roots.emplace_back(std::move(node));
398 self->stack.emplace_back(&self->roots.back());
399 } else {
400 auto &children = std::get<ProfileNodeInternal>(*self->stack.back()).children;
401 children.emplace_back(std::move(node));
402 self->stack.emplace_back(&children.back());
403 }
404 }
405 CLINGO_CATCH;
406 }
407 static auto leaf(size_t depth, clingo_profile_data_t *values, clingo_profile_type_t type, void *data)
408 -> bool {
409 CLINGO_TRY {
410 auto *self = static_cast<Builder *>(data);
411 assert(depth <= self->stack.size());
412 self->stack.resize(depth);
413 auto node = ProfileNodeLeaf{static_cast<ProfileType>(type), values->matches, values->instances,
414 values->time_instantiate, values->time_propagate};
415 if (self->stack.empty()) {
416 self->roots.emplace_back(node);
417 } else {
418 std::get<ProfileNodeInternal>(*self->stack.back()).children.emplace_back(node);
419 }
420 }
421 CLINGO_CATCH;
422 }
423
424 std::vector<ProfileNode *> stack;
425 std::vector<ProfileNode> roots;
426 } builder;
427
428 auto visitor = clingo_profile_visitor_t{&Builder::internal, &Builder::leaf};
429 clingo_control_profile(ctl_.get(), &visitor, &builder);
430 return std::move(builder.roots);
431 }
432
438 template <Detail::UserData<SolveEventHandler> Handler = std::nullptr_t>
439 [[nodiscard]] auto solve(ProgramLiteralSpan assumptions = {}, Handler &&handler = nullptr) const -> SolveResult {
440 return solve_(std::forward<Handler>(handler), assumptions, SolveFlags::empty).get();
441 }
442
444 template <Detail::UserData<SolveEventHandler> Handler = std::nullptr_t>
445 [[nodiscard]] auto solve(std::initializer_list<ProgramLiteral> assumptions, Handler &&handler = nullptr) const
446 -> SolveResult {
447 return solve_(std::forward<Handler>(handler), ProgramLiteralSpan{assumptions}, SolveFlags::empty).get();
448 }
449
460 template <Detail::UserData<SolveEventHandler> Handler = std::nullptr_t>
461 [[nodiscard]] auto start_solve(ProgramLiteralSpan assumptions = {}, SolveFlags flags = SolveFlags::yield,
462 Handler &&handler = nullptr) const -> SolveHandle {
463 return solve_(std::forward<Handler>(handler), assumptions, flags);
464 }
465
467 template <Detail::UserData<SolveEventHandler> Handler = std::nullptr_t>
468 [[nodiscard]] auto start_solve(std::initializer_list<ProgramLiteral> assumptions,
469 SolveFlags flags = SolveFlags::yield, Handler &&handler = nullptr) const
470 -> SolveHandle {
471 return solve_(std::forward<Handler>(handler), ProgramLiteralSpan{assumptions}, flags);
472 }
473
478 void main() const { Detail::handle_error(clingo_control_main(ctl_.get())); }
479
483 void interrupt() const { clingo_control_interrupt(ctl_.get()); }
484
488 void discard(DiscardType type) const {
489 Detail::handle_error(clingo_control_discard(ctl_.get(), static_cast<clingo_discard_type_t>(type)));
490 }
491
498 [[nodiscard]] auto buffer() const -> std::string_view {
499 auto [data, size] = Detail::call<clingo_control_buffer>(ctl_.get());
500 return {data, size};
501 }
502
509 [[nodiscard]] auto parts() const -> std::optional<PartVector> {
510 clingo_part_t const *parts = nullptr;
511 size_t size = 0;
512 bool has_value = false;
513 Detail::handle_error(clingo_control_get_parts(ctl_.get(), &parts, &size, &has_value));
514 if (has_value) {
515 return Detail::transform(std::span{parts, size}, [](auto const &part) {
516 auto params = std::span{cpp_cast(part.params), part.params_size};
517 return Part{{part.name, part.name_size}, {params.begin(), params.end()}};
518 });
519 }
520 return std::nullopt;
521 }
522
526 void parts(PartList parts) const { this->parts(std::span{parts.begin(), parts.end()}); }
527
533 void parts(std::optional<PartSpan> parts) const {
534 if (parts) {
535 auto cparts = Detail::transform(*parts, [](auto const &part) {
536 return clingo_part_t{part.name.data(), part.name.size(), c_cast(part.params.data()),
537 part.params.size()};
538 });
539 Detail::handle_error(clingo_control_set_parts(ctl_.get(), cparts.data(), cparts.size(), true));
540 } else {
541 Detail::handle_error(clingo_control_set_parts(ctl_.get(), nullptr, 0, true));
542 }
543 }
544
548 [[nodiscard]] auto config() const -> Config {
549 auto *config = Detail::call<clingo_control_config>(ctl_.get());
550 auto key = Detail::call<clingo_config_root>(config);
551 return Config{config, key};
552 }
553
557 [[nodiscard]] auto const_map() const -> ConstMap {
558 return ConstMap{Detail::call<clingo_control_const_map>(ctl_.get())};
559 }
560
565 void observe(Observer &obs, bool preprocess = true) const { obs.observe(ctl_.get(), preprocess); }
566
570 [[nodiscard]] auto backend() const -> ProgramBackend {
571 return ProgramBackend{Detail::call<clingo_control_backend>(ctl_.get())};
572 }
573
581 template <Detail::UserData<Propagator, false> P> auto register_propagator(P &&propagator) const -> decltype(auto) {
582 using UserData = Detail::UserDataTraits<P>;
583 auto user_data = UserData::create(std::forward<P>(propagator));
584 if (!UserData::has_value(user_data)) {
585 throw std::invalid_argument{"propagator cannot be null"};
586 }
587 auto &res = *UserData::get(user_data);
588
589 static constexpr auto decide = []() {
590 if constexpr (std::is_base_of_v<Heuristic, typename UserData::ValueType>) {
591 return [](clingo_assignment_t const *assignment, clingo_literal_t fallback, void *data,
592 clingo_literal_t *decision) -> bool {
593 CLINGO_TRY {
594 *decision = UserData::cast(data)->decide(Assignment{assignment}, fallback);
595 }
596 CLINGO_CATCH;
597 };
598 } else {
599 return nullptr;
600 }
601 };
602
603 static constexpr auto c_propagator = clingo_propagator_t{
604 [](clingo_assignment_t const *assignment, clingo_propagate_init_t *init, void *data) -> bool {
605 CLINGO_TRY {
606 UserData::cast(data)->init(Assignment{assignment}, PropagateInit{init});
607 }
608 CLINGO_CATCH;
609 },
610 [](clingo_assignment_t const *assignment, clingo_propagate_control_t *control, void *data) {
611 CLINGO_TRY {
612 UserData::cast(data)->attach(Assignment{assignment}, PropagateControl{control});
613 }
614 CLINGO_CATCH;
615 },
616 [](clingo_assignment_t const *assignment, clingo_propagate_control_t *control,
617 clingo_literal_t const *changes, size_t size, void *data) -> bool {
618 CLINGO_TRY {
619 UserData::cast(data)->propagate(Assignment{assignment}, PropagateControl{control},
620 SolverLiteralSpan{changes, size});
621 }
622 CLINGO_CATCH;
623 },
624 [](clingo_assignment_t const *assignment, clingo_literal_t const *changes, size_t size, void *data) {
625 try {
626 UserData::cast(data)->undo(Assignment{assignment}, SolverLiteralSpan{changes, size});
627 } catch (std::exception const &e) {
628 fprintf(stderr, "panic: %s\n", e.what());
629 std::terminate();
630 }
631 },
632 [](clingo_assignment_t const *assignment, clingo_propagate_control_t *control, void *data) -> bool {
633 CLINGO_TRY {
634 UserData::cast(data)->check(Assignment{assignment}, PropagateControl{control});
635 }
636 CLINGO_CATCH;
637 },
638 decide(),
639 [](void *data) { UserData::free(data); },
640 };
641
642 Detail::handle_error(
643 clingo_control_register_propagator(ctl_.get(), &c_propagator, UserData::release(user_data)));
644 return res;
645 }
646
650 void join(AST::Program const &prg) const { Detail::join(ctl_.get(), c_cast(prg)); }
651
652 private:
653 friend class Detail::intrusive_handle<Control, clingo_control_t>;
654
655 static auto acquire(clingo_control_t *ptr) { clingo_control_acquire(ptr); }
656
657 static auto release(clingo_control_t *ptr) { clingo_control_release(ptr); }
658
659 template <Detail::UserData<SolveEventHandler> Handler = std::nullptr_t>
660 [[nodiscard]] auto solve_(Handler &&handler, ProgramLiteralSpan const &assumptions, SolveFlags flags) const
661 -> SolveHandle {
662 using UserData = Detail::UserDataTraits<Handler>;
663 auto user_data = UserData::create(std::forward<Handler>(handler));
664 clingo_solve_event_handler_t const *c_handler_ptr = nullptr;
665 if constexpr (UserData::has_data) {
666 if (UserData::has_value(user_data)) {
667 static constexpr auto c_solve_event_handler = clingo_solve_event_handler_t{
668 [](clingo_model_t *model, void *data, bool *goon) -> bool {
669 CLINGO_TRY {
670 auto *hnd = UserData::cast(data);
671 assert(hnd != nullptr);
672 auto mdl = Model{model};
673 *goon = hnd->model(mdl);
674 return true;
675 }
676 CLINGO_CATCH;
677 },
678 [](int64_t const *values, size_t size, void *data) -> bool {
679 CLINGO_TRY {
680 auto *hnd = UserData::cast(data);
681 assert(hnd != nullptr);
682 hnd->unsat({values, size});
683 }
684 CLINGO_CATCH;
685 },
686 [](clingo_stats_t *stats, void *data) -> bool {
687 CLINGO_TRY {
688 auto *hnd = UserData::cast(data);
689 assert(hnd != nullptr);
690 uint64_t root = Detail::call<clingo_stats_root>(stats);
691 uint64_t step = Detail::call<clingo_stats_map_add_subkey>(
694 uint64_t accu = Detail::call<clingo_stats_map_add_subkey>(
697 hnd->stats(Stats{stats, step}, Stats{stats, accu});
698 }
699 CLINGO_CATCH;
700 },
701 [](clingo_solve_result_bitset_t result, void *data) -> void {
702 auto *hnd = UserData::cast(data);
703 assert(hnd != nullptr);
704 hnd->finish(static_cast<SolveResult>(result));
705 },
706 [](void *data) { UserData::free(data); },
707 };
708 c_handler_ptr = &c_solve_event_handler;
709 }
710 }
711
712 clingo_solve_handle_t *res = nullptr;
713 Detail::handle_error(clingo_control_solve(ctl_.get(), static_cast<clingo_solve_mode_bitset_t>(flags),
714 assumptions.data(), assumptions.size(), c_handler_ptr,
715 UserData::release(user_data), &res));
716 return SolveHandle{res};
717 }
718
726 [[nodiscard]] auto c_parts_(std::optional<PartSpan> parts = std::nullopt) const
727 -> std::pair<std::optional<PartVector>, std::vector<clingo_part_t>> {
728 auto res = std::pair<std::optional<PartVector>, std::vector<clingo_part_t>>{};
729 auto &[default_parts, c_parts] = res;
730 if (!parts) {
731 parts = default_parts = this->parts();
732 }
733 if (!parts) {
734 static auto default_part = Part{"base", {}};
735 parts = PartSpan{&default_part, 1};
736 }
737 assert(parts);
738 c_parts.reserve(parts->size());
739 for (auto const &part : *parts) {
740 c_parts.emplace_back(part.name.data(), part.name.size(), c_cast(part.params.data()), part.params.size());
741 }
742 return res;
743 }
744
745 Detail::intrusive_handle<Control, clingo_control_t> ctl_;
746};
747
749
750} // namespace Clingo
A program to add statements to.
Definition ast.hh:813
Represents a (partial) assignment of a particular solver.
Definition propagate.hh:140
A base that maps signatures to atom bases, and captures term and theory bases.
Definition base.hh:612
Class modeling a mutable configuration entry.
Definition config.hh:138
Class to providing a view on the const directives in a logic program.
Definition control.hh:45
auto operator[](key_type name) const -> mapped_type
Get the value of for the given key.
Definition control.hh:81
std::ptrdiff_t difference_type
The difference type.
Definition control.hh:56
std::size_t size_type
The size type.
Definition control.hh:54
Detail::RandomAccessIterator< ConstMap > iterator
The iterator type.
Definition control.hh:62
value_type reference
The reference type.
Definition control.hh:58
auto contains(key_type name) const -> bool
Check if the map contains the given key.
Definition control.hh:73
std::string_view key_type
The key type.
Definition control.hh:48
auto at(size_t index) const -> value_type
Get the key value pair at the given index.
Definition control.hh:92
std::pair< key_type, mapped_type > value_type
The value type.
Definition control.hh:52
Detail::ArrowProxy< value_type > pointer
The pointer type.
Definition control.hh:60
auto begin() const -> iterator
Get an iterator to the beginning of the map.
Definition control.hh:107
ConstMap(clingo_const_map_t const *map)
Construct from the underlying C representation.
Definition control.hh:67
auto end() const -> iterator
Get an iterator to the end of the map.
Definition control.hh:112
auto size() const -> size_type
Get the size of the map.
Definition control.hh:102
The main control class for grounding and solving logic programs.
Definition control.hh:160
auto start_ground(std::optional< PartSpan > parts=std::nullopt, Handler &&handler=nullptr) const -> GroundHandle
Ground the logic program with the given parameters.
Definition control.hh:320
auto config() const -> Config
Get the configuration of the control object.
Definition control.hh:548
void discard(DiscardType type) const
Discard the statements of the given types.
Definition control.hh:488
void parts(std::optional< PartSpan > parts) const
Set the parts to ground.
Definition control.hh:533
void observe(Observer &obs, bool preprocess=true) const
Inspect the current ground program held by the control object.
Definition control.hh:565
auto base() const -> Base
Get the base of the program.
Definition control.hh:371
Control(Library const &lib, StringSpan arguments={})
Constructs a control object with the given library and arguments.
Definition control.hh:176
void ground(std::initializer_list< Part > parts, Context ctx=nullptr) const
Ground the control object with the given parts.
Definition control.hh:302
auto start_ground(std::initializer_list< Part > parts, Handler &&handler=nullptr) const -> GroundHandle
Ground the logic program with the given parameters.
Definition control.hh:363
auto stats() const -> StatsView
Get the statistics of the control object.
Definition control.hh:376
auto parts() const -> std::optional< PartVector >
Get the parts to ground.
Definition control.hh:509
auto const_map() const -> ConstMap
Get the constant map of the control object.
Definition control.hh:557
auto start_solve(std::initializer_list< ProgramLiteral > assumptions, SolveFlags flags=SolveFlags::yield, Handler &&handler=nullptr) const -> SolveHandle
Start solving the grounded program with the given assumptions and flags.
Definition control.hh:468
Control(clingo_control_t *rep, bool acquire)
Constructs a control object from an existing C representation.
Definition control.hh:187
void parts(PartList parts) const
Set the parts to ground.
Definition control.hh:526
auto start_solve(ProgramLiteralSpan assumptions={}, SolveFlags flags=SolveFlags::yield, Handler &&handler=nullptr) const -> SolveHandle
Start solving the grounded program with the given assumptions and flags.
Definition control.hh:461
void interrupt() const
Interrupt the current solve operation.
Definition control.hh:483
void parse_files(StringSpan files) const
Parse files with the given paths.
Definition control.hh:235
auto solve(std::initializer_list< ProgramLiteral > assumptions, Handler &&handler=nullptr) const -> SolveResult
Solve the grounded program with the given assumptions.
Definition control.hh:445
friend auto c_cast(Control const &ctl) -> clingo_control_t *
Cast to the C representation of the control object.
Definition control.hh:193
auto solve(ProgramLiteralSpan assumptions={}, Handler &&handler=nullptr) const -> SolveResult
Solve the grounded program with the given assumptions.
Definition control.hh:439
auto backend() const -> ProgramBackend
Get the backend of the control object.
Definition control.hh:570
auto buffer() const -> std::string_view
Get the text buffer of the control object.
Definition control.hh:498
void main() const
Run the default ground and solve flow.
Definition control.hh:478
Control(Library const &lib, StringList arguments)
Constructs a control object with the given library and arguments.
Definition control.hh:173
void parse_files(StringList files) const
Parse files with the given paths.
Definition control.hh:248
auto register_propagator(P &&propagator) const -> decltype(auto)
Register a propagator with the control object.
Definition control.hh:581
void join(AST::Program const &prg) const
Join the given non-ground program to the current control object.
Definition control.hh:650
void ground(std::optional< PartSpan > parts=std::nullopt, Context ctx=nullptr) const
Ground the logic program with the given parameters.
Definition control.hh:265
void write_aspif(std::optional< std::string_view > path, WriteAspifFlags flags=WriteAspifFlags::none) const
Write the current program to an ASPIF file.
Definition control.hh:218
auto mode() const -> ControlMode
Get the control mode.
Definition control.hh:198
std::function< SymbolVector(std::string_view, SymbolSpan)> Context
Callbock for injecting symbols into the grounding process.
Definition control.hh:167
auto profile() -> std::vector< ProfileNode >
Obtain the profiling data of the control object.
Definition control.hh:388
void parse_string(std::string_view program) const
Parse a logic program from a string.
Definition control.hh:253
Class to control a running ground call.
Definition ground.hh:83
The main library class for managing global information and logging.
Definition core.hh:566
Class representing a range in a file.
Definition core.hh:751
Observer interface to inspect the current ground program.
Definition observe.hh:15
Program backend to add atoms and statements.
Definition backend.hh:146
Class to control a user-defined propagator.
Definition propagate.hh:311
Object to initialize a user-defined propagator before each solving step.
Definition propagate.hh:434
Class to control a running search.
Definition solve.hh:329
Class to capture the result of solve calls.
Definition solve.hh:16
Class modeling an immutable view on a statistics entry.
Definition stats.hh:29
Class modeling a symbol in Clingo.
Definition symbol.hh:68
struct clingo_program clingo_program_t
Object to store.
Definition ast.h:505
unsigned clingo_discard_type_t
Corresponding type to clingo_discard_type_e.
Definition control.h:224
CLINGO_VISIBILITY_DEFAULT bool clingo_const_map_find(clingo_const_map_t const *map, char const *name, size_t name_size, clingo_symbol_t *symbol, bool *found)
Get the constant with the given name.
int clingo_mode_t
The corresponding type to clingo_mode_e.
Definition control.h:45
CLINGO_VISIBILITY_DEFAULT bool clingo_control_parse_string(clingo_control_t *control, char const *program, size_t size)
Extend the logic program with the given non-ground logic program in string form.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_get_parts(clingo_control_t *control, clingo_part_t const **parts, size_t *size, bool *has_value)
Get the program parts to ground.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_discard(clingo_control_t *control, clingo_discard_type_t type)
Discard the statements of the given types.
CLINGO_VISIBILITY_DEFAULT bool clingo_const_map_at(clingo_const_map_t const *map, size_t index, clingo_string_t *name, clingo_symbol_t *symbol)
Get the name and value of the contestant at the given index.
struct clingo_const_map clingo_const_map_t
A map from constants to their values.
Definition control.h:48
CLINGO_VISIBILITY_DEFAULT void clingo_control_acquire(clingo_control_t *control)
Increment the reference count of the given control object.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_parse_files(clingo_control_t *control, clingo_string_t const *files, size_t size)
Extend the logic program with a program in a file.
CLINGO_VISIBILITY_DEFAULT void clingo_control_interrupt(clingo_control_t *control)
Interrupt the running search.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_main(clingo_control_t *control)
Execute the default ground and solve flow after parsing.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_set_parts(clingo_control_t *control, clingo_part_t const *parts, size_t size, bool has_value)
Set the program parts to ground.
CLINGO_VISIBILITY_DEFAULT void clingo_control_release(clingo_control_t *control)
Decrement the reference count of the given control object and destroy if zero.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_ground(clingo_control_t *control, clingo_part_t const *parts, size_t size, clingo_ground_event_handler_t const *handler, void *data)
Ground the selected parts of the current (non-ground) logic program.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_start_ground(clingo_control_t *control, clingo_part_t const *parts, size_t size, clingo_ground_event_handler_t const *handler, void *data, clingo_ground_handle_t **handle)
Start grounding a program asynchronously.
@ clingo_mode_ground
parse, rewrite, ground
Definition control.h:41
@ clingo_mode_parse
parse only
Definition control.h:39
@ clingo_mode_rewrite
parse and rewrite
Definition control.h:40
@ clingo_mode_solve
parse, rewrite, ground, and solve
Definition control.h:42
struct clingo_lib clingo_lib_t
A library object storing global information.
Definition core.h:171
struct clingo_location clingo_location_t
Represents a source code location marking its beginning and end.
Definition core.h:354
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition core.h:73
struct clingo_ground_handle clingo_ground_handle_t
Handle to an asynchronous ground call.
Definition ground.h:136
unsigned clingo_ground_result_t
Corresponding type to clingo_ground_result_e.
Definition ground.h:59
struct clingo_model clingo_model_t
Object representing a model.
Definition model.h:43
unsigned clingo_write_aspif_mode_t
Corresponding type to clingo_write_aspif_mode_e.
Definition observe.h:29
CLINGO_VISIBILITY_DEFAULT bool clingo_control_write_aspif(clingo_control_t *control, char const *path, size_t size, clingo_write_aspif_mode_t mode)
Write the current logic program in aspif format to a file.
@ clingo_write_aspif_mode_preamble_auto
Write preamble for newly created files.
Definition observe.h:23
@ clingo_write_aspif_mode_symbols
Whether to write symbols in a structured format.
Definition observe.h:26
@ clingo_write_aspif_mode_preamble
Write preamble.
Definition observe.h:22
@ clingo_write_aspif_mode_preprocess
Whether to preprocess the program before writing.
Definition observe.h:25
@ clingo_write_aspif_mode_append
Append to an existing file (or create it).
Definition observe.h:24
CLINGO_VISIBILITY_DEFAULT bool clingo_control_profile(clingo_control_t const *control, clingo_profile_visitor_t const *visit, void *data)
Visit the profiling data of a control object.
int clingo_profile_type_t
Corresponding type to clingo_profile_type_e.
Definition profile.h:36
CLINGO_VISIBILITY_DEFAULT bool clingo_control_register_propagator(clingo_control_t *control, clingo_propagator_t const *propagator, void *data)
Register a custom propagator with the control object.
struct clingo_propagate_control clingo_propagate_control_t
This object can be used to add clauses and propagate literals while solving.
Definition propagate.h:242
struct clingo_assignment clingo_assignment_t
Represents a (partial) assignment of a particular solver.
Definition propagate.h:46
struct clingo_propagate_init clingo_propagate_init_t
Object to initialize a user-defined propagator before each solving step.
Definition propagate.h:255
struct clingo_solve_handle clingo_solve_handle_t
Search handle to a solve call.
Definition solve.h:112
unsigned clingo_solve_result_bitset_t
Corresponding type to clingo_solve_result_e.
Definition solve.h:61
CLINGO_VISIBILITY_DEFAULT bool clingo_control_solve(clingo_control_t *control, clingo_solve_mode_bitset_t mode, clingo_literal_t const *assumptions, size_t assumptions_size, clingo_solve_event_handler_t const *handler, void *data, clingo_solve_handle_t **handle)
Solve the currently grounded logic program enumerating its models.
unsigned clingo_solve_mode_bitset_t
Corresponding type to clingo_solve_mode_e.
Definition solve.h:71
@ clingo_solve_mode_yield
Yield models in calls to clingo_solve_handle_model.
Definition solve.h:67
@ clingo_solve_mode_async
Enable non-blocking search.
Definition solve.h:66
CLINGO_VISIBILITY_DEFAULT clingo_string_t const clingo_user_stats_accu
Well-known key name intended to be used for user-specific accu statistics.
CLINGO_VISIBILITY_DEFAULT clingo_string_t const clingo_user_stats_step
Well-known key name intended to be used for user-specific step statistics.
struct clingo_statistic clingo_stats_t
Handle for the solver stats.
Definition stats.h:59
@ clingo_stats_type_map
the entry is a map
Definition stats.h:53
bool(* clingo_symbol_callback_t)(clingo_symbol_t const *symbols, size_t symbols_size, void *data)
Callback function to inject symbols.
Definition symbol.h:60
uint64_t clingo_symbol_t
Type to represent a symbol.
Definition symbol.h:51
SolveResult
The solve result.
Definition solver.hh:332
@ model
The model represents a stable model.
@ map
The configuration entry is a map of configurations.
SolveFlags
Enumeration of the flags for solving a logic program.
Definition control.hh:141
DiscardType
Enumeration of the types of statements that can be discarded.
Definition control.hh:150
WriteAspifFlags
Enumeration of the flags for writing ASPIF files.
Definition control.hh:129
ControlMode
Enumeration of the control modes.
Definition control.hh:121
@ async
Asynchronously solve in the background.
@ yield
Yield models as they are found.
@ empty
Standard event-based solving.
@ project
Discard project statements.
@ minimize
Discard minimize statements.
@ preamble_auto
Write preamble for newly created files.
@ preamble
Write preamble.
@ symbols
Whether to write symbols in a structured format.
@ append
Append to an existing file (or create it).
@ preprocess
Whether to preprocess the program before writing.
@ parse
Parse only.
@ ground
Parse, rewrite, ground.
@ rewrite
Parse and rewrite.
@ solve
Parse, rewrite, ground, and solve.
std::span< std::string_view const > StringSpan
A span of string views.
Definition core.hh:518
std::initializer_list< std::string_view const > StringList
A list of string views.
Definition core.hh:520
std::span< SolverLiteral const > SolverLiteralSpan
A span of solver literals.
Definition core.hh:496
std::span< ProgramLiteral const > ProgramLiteralSpan
A span of program literals.
Definition core.hh:489
@ init
Modify the initial VSIDS score of an atom.
@ release
No longer treat an atom as external.
std::vector< Part > PartVector
A vector of program parts.
Definition ground.hh:35
std::span< Part const > PartSpan
A span of program parts.
Definition ground.hh:31
GroundResult
Enumeration of the results of a grounding process.
Definition ground.hh:38
std::initializer_list< Part > PartList
An initializer list of program parts.
Definition ground.hh:33
ProfileType
Enumeration of the types of profiling data.
Definition profile.hh:18
@ accu
Indicate accumulated profiling data.
@ step
Indicate per step profiling data.
std::vector< Symbol > SymbolVector
A vector of symbols.
Definition symbol.hh:42
std::span< Symbol const > SymbolSpan
A span of symbols, which is a view on a contiguous sequence of symbols.
Definition symbol.hh:38
auto cpp_cast(clingo_symbol_t const *sym) -> Symbol const *
Cast a C symbol to its C++ representation.
Definition symbol.hh:52
#define CLINGO_ENABLE_BITSET_ENUM(E,...)
Opt-in macro for enabling bit operations for a given enum type.
Definition enum.hh:18
A program part to provide inputs to program directives.
Definition ground.hh:16
std::string name
The name of the part.
Definition ground.hh:26
Class to hold profiling data for an expression in a logic program.
Definition profile.hh:49
std::vector< ProfileNode > children
The children of the profile node.
Definition profile.hh:61
Class to hold profiling data for an expression in a logic program.
Definition profile.hh:24
The ground event handler interface.
Definition ground.h:62
Struct used to specify the program parts that have to be grounded.
Definition ground.h:44
char const * name
name of the program part
Definition ground.h:45
Per node performance statistics gathered while grounding a logic program.
Definition profile.h:39
uint64_t time_propagate
The time in nanoseconds spent propagating.
Definition profile.h:47
uint64_t time_instantiate
The time in nanoseconds spent instantiating.
Definition profile.h:45
uint64_t matches
The number of matches produced by the instantiator.
Definition profile.h:41
uint64_t instances
The number of instances produced by the instantiator.
Definition profile.h:43
Visitor for profiling data.
Definition profile.h:51
An instance of this struct has to be registered with a solver to implement a custom propagator.
Definition propagate.h:493
The solve event handler interface.
Definition solve.h:74
Struct to capture strings that are not null-terminated.
Definition core.h:86
size_t size
the length of the string
Definition core.h:88
char const * data
pointer to the beginning of the string
Definition core.h:87