Clingo
Loading...
Searching...
No Matches
backend.hh
1#pragma once
2
3#include <clingo/core.hh>
4#include <clingo/symbol.hh>
5
6#include <clingo/backend.h>
7
8#include <cassert>
9
10namespace Clingo {
11
15
22
25 public:
30 [[nodiscard]] auto number(int number) const -> ProgramId {
31 return Detail::call<clingo_backend_theory_term_number>(backend_.get(), number);
32 }
33
40 [[nodiscard]] auto string(std::string_view string) const -> ProgramId {
41 return Detail::call<clingo_backend_theory_term_string>(backend_.get(), string.data(), string.size());
42 }
43
48 [[nodiscard]] auto symbol(Symbol const &symbol) const -> ProgramId {
49 return Detail::call<clingo_backend_theory_term_symbol>(backend_.get(), c_cast(symbol));
50 }
51
57 [[nodiscard]] auto sequence(TheorySequenceType type, ProgramIdSpan elements) const -> ProgramId {
58 return Detail::call<clingo_backend_theory_term_sequence>(
59 backend_.get(), static_cast<clingo_theory_sequence_type_t>(type), elements.data(), elements.size());
60 }
61
67 [[nodiscard]] auto function(std::string_view name, ProgramIdSpan elements) const -> ProgramId {
68 return Detail::call<clingo_backend_theory_term_function>(backend_.get(), name.data(), name.size(),
69 elements.data(), elements.size());
70 }
71
78 return Detail::call<clingo_backend_theory_element>(backend_.get(), tuple.data(), tuple.size(), condition.data(),
79 condition.size());
80 }
81
96 [[nodiscard]] auto atom(std::optional<ProgramAtom> atom, Symbol const &name, ProgramIdSpan elements = {},
97 std::optional<std::pair<std::string_view, ProgramId>> const &guard = std::nullopt) const
98 -> ProgramAtom {
99 auto op = clingo_string_t{guard ? guard->first.data() : nullptr, guard ? guard->first.size() : 0};
100 return Detail::call<clingo_backend_theory_atom>(backend_.get(), c_cast(name), elements.data(), elements.size(),
101 guard ? &op : nullptr, guard ? guard->second : 0,
102 atom ? &*atom : nullptr);
103 }
104
105 private:
106 friend class ProgramBackend;
107
108 struct Free {
109 Free() {
110 // NOTE: We assume that backends are only created during normal
111 // operation - not during exception handling.
112 assert(std::uncaught_exceptions() == 0);
113 }
114 auto operator()(clingo_backend_t *backend) const noexcept(false) -> void {
115 if (std::uncaught_exceptions() == 0) {
116 Detail::handle_error(clingo_backend_close(std::exchange(backend, nullptr)));
117 }
118 }
119 };
120
121 explicit TheoryBackend(clingo_backend_t *backend) : backend_{backend} {}
122
123 ~TheoryBackend() = default;
124
125 std::unique_ptr<clingo_backend_t, Free> backend_;
126};
127
133 public:
139 explicit ProgramBackend(clingo_backend_t *backend) : TheoryBackend{backend} {}
140
145 void close() { Detail::handle_error(clingo_backend_close(backend_.release())); }
146
154 [[nodiscard]] auto atom(std::optional<Symbol> symbol) const -> ProgramAtom {
155 return Detail::call<clingo_backend_add_atom>(backend_.get(), symbol ? c_cast(&symbol.value()) : nullptr);
156 }
157
163 void rule(ProgramAtomSpan head, ProgramLiteralSpan body = {}, bool choice = false) const {
164 Detail::handle_error(
165 clingo_backend_rule(backend_.get(), choice, head.data(), head.size(), body.data(), body.size()));
166 }
167
174 void weight_rule(ProgramAtomSpan head, Weight lower, WeightedLiteralSpan body, bool choice = false) const {
175 Detail::handle_error(clingo_backend_weight_rule(backend_.get(), choice, head.data(), head.size(), lower,
176 body.data(), body.size()));
177 }
178
183 void minimize(WeightedLiteralSpan literals, Weight priority = 0) const {
184 Detail::handle_error(clingo_backend_minimize(backend_.get(), priority, literals.data(), literals.size()));
185 }
186
191 Detail::handle_error(clingo_backend_project(backend_.get(), atoms.data(), atoms.size()));
192 }
193
199 Detail::handle_error(clingo_backend_external(backend_.get(), atom, static_cast<clingo_external_type_t>(type)));
200 }
201
206 Detail::handle_error(clingo_backend_assume(backend_.get(), literals.data(), literals.size()));
207 }
208
216 void heuristic(ProgramAtom atom, HeuristicType type, int bias, unsigned priority = 0,
217 ProgramLiteralSpan condition = {}) const {
218 Detail::handle_error(clingo_backend_heuristic(backend_.get(), atom, static_cast<clingo_heuristic_type_t>(type),
219 bias, priority, condition.data(), condition.size()));
220 }
221
227 void edge(int node_u, int node_v, ProgramLiteralSpan condition) const {
228 Detail::handle_error(
229 clingo_backend_acyc_edge(backend_.get(), node_u, node_v, condition.data(), condition.size()));
230 }
231
235 [[nodiscard]] auto theory() const -> TheoryBackend const & { return *this; }
236};
237
239
240} // namespace Clingo
Program backend to add atoms and statements.
Definition backend.hh:132
auto atom(std::optional< Symbol > symbol) const -> ProgramAtom
Create a fresh program atom.
Definition backend.hh:154
void rule(ProgramAtomSpan head, ProgramLiteralSpan body={}, bool choice=false) const
Add a choice or disjunctive ground rule.
Definition backend.hh:163
void project(ProgramAtomSpan atoms) const
Add a project directive to the program.
Definition backend.hh:190
void edge(int node_u, int node_v, ProgramLiteralSpan condition) const
Add an edge directive.
Definition backend.hh:227
void heuristic(ProgramAtom atom, HeuristicType type, int bias, unsigned priority=0, ProgramLiteralSpan condition={}) const
Add a heuristic directive to the program.
Definition backend.hh:216
void weight_rule(ProgramAtomSpan head, Weight lower, WeightedLiteralSpan body, bool choice=false) const
Add a ground weight rule to the program.
Definition backend.hh:174
void close()
Finalize the backend.
Definition backend.hh:145
void external(ProgramAtom atom, ExternalType type) const
Add an external directive to the program.
Definition backend.hh:198
void assume(ProgramLiteralSpan literals) const
Add assumptions to the program.
Definition backend.hh:205
auto theory() const -> TheoryBackend const &
Get the associated theory backend.
Definition backend.hh:235
ProgramBackend(clingo_backend_t *backend)
Create a program atom from its C representation.
Definition backend.hh:139
void minimize(WeightedLiteralSpan literals, Weight priority=0) const
Add a minimize constraint to the program.
Definition backend.hh:183
Class modeling a symbol in Clingo.
Definition symbol.hh:68
Theory backend to build theory atoms.
Definition backend.hh:24
auto element(ProgramIdSpan tuple, ProgramLiteralSpan condition) const -> ProgramId
Add a theory element.
Definition backend.hh:77
auto atom(std::optional< ProgramAtom > atom, Symbol const &name, ProgramIdSpan elements={}, std::optional< std::pair< std::string_view, ProgramId > > const &guard=std::nullopt) const -> ProgramAtom
Add a theory atom.
Definition backend.hh:96
auto function(std::string_view name, ProgramIdSpan elements) const -> ProgramId
Add a theory function.
Definition backend.hh:67
auto number(int number) const -> ProgramId
Add a numeric theory term.
Definition backend.hh:30
auto sequence(TheorySequenceType type, ProgramIdSpan elements) const -> ProgramId
Add a theory term sequence.
Definition backend.hh:57
auto string(std::string_view string) const -> ProgramId
Add a string theory term.
Definition backend.hh:40
auto symbol(Symbol const &symbol) const -> ProgramId
Convert the given symbol into a theory term.
Definition backend.hh:48
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_project(clingo_backend_t *backend, clingo_atom_t const *atoms, size_t size)
Add a projection directive.
struct clingo_backend clingo_backend_t
Handle to the backend to add directives in aspif format.
Definition backend.h:47
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_assume(clingo_backend_t *backend, clingo_literal_t const *literals, size_t size)
Add an assumption directive.
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_rule(clingo_backend_t *backend, bool choice, clingo_atom_t const *head, size_t head_size, clingo_literal_t const *body, size_t body_size)
Add a rule to the program.
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_heuristic(clingo_backend_t *backend, clingo_atom_t atom, clingo_heuristic_type_t type, int bias, unsigned priority, clingo_literal_t const *condition, size_t size)
Add a heuristic directive.
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_weight_rule(clingo_backend_t *backend, bool choice, clingo_atom_t const *head, size_t head_size, clingo_weight_t lower_bound, clingo_weighted_literal_t const *body, size_t body_size)
Add a weight rule to the program.
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_minimize(clingo_backend_t *backend, clingo_weight_t priority, clingo_weighted_literal_t const *literals, size_t size)
Add a minimize constraint (or weak constraint) to the program.
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_acyc_edge(clingo_backend_t *backend, int node_u, int node_v, clingo_literal_t const *condition, size_t size)
Add an edge directive.
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_close(clingo_backend_t *backend)
Finalize the backend after using it.
int clingo_theory_sequence_type_t
Corresponding type to clingo_theory_sequence_type_e.
Definition backend.h:44
CLINGO_VISIBILITY_DEFAULT bool clingo_backend_external(clingo_backend_t *backend, clingo_atom_t atom, clingo_external_type_t type)
Add an external statement.
@ clingo_theory_sequence_type_list
Theory lists "[t1,...,tn]".
Definition backend.h:41
@ clingo_theory_sequence_type_tuple
Theory tuples "(t1,...,tn)".
Definition backend.h:39
@ clingo_theory_sequence_type_set
Theory sets "{t1,...,tn}".
Definition backend.h:40
int clingo_external_type_t
Corresponding type to clingo_external_type_e.
Definition shared.h:32
int clingo_heuristic_type_t
Corresponding type to clingo_heuristic_type_e.
Definition shared.h:22
TheorySequenceType
Enumeration of available theory sequence types.
Definition backend.hh:17
@ tuple
Theory tuples "(t1,...,tn)".
@ list
Theory lists "[t1,...,tn]".
@ set
Theory sets "{t1,...,tn}".
@ symbol
a symbol term, e.g., c
@ number
a number term, e.g., 42
clingo_atom_t ProgramAtom
A program atom.
Definition core.hh:387
std::span< ProgramId const > ProgramIdSpan
A span of program ids.
Definition core.hh:384
HeuristicType
Enumeration of heuristic types.
Definition core.hh:560
std::span< ProgramAtom const > ProgramAtomSpan
A span of program atoms.
Definition core.hh:389
clingo_weight_t Weight
A weight used in sum aggregates and minimize constraints.
Definition core.hh:408
std::span< WeightedLiteral const > WeightedLiteralSpan
A span of weighted literals.
Definition core.hh:415
clingo_id_t ProgramId
A program id used for various kinds of indices.
Definition core.hh:382
std::span< ProgramLiteral const > ProgramLiteralSpan
A span of program literals.
Definition core.hh:394
ExternalType
Enumeration of control modes.
Definition core.hh:552
@ atoms
Select all atoms.
Struct to capture strings that are not null-terminated.
Definition core.h:91