3#include <clingo/control/term.hh>
5#include <clingo/input/literal.hh>
6#include <clingo/input/program.hh>
7#include <clingo/input/term.hh>
9#include <clingo/ground/base.hh>
10#include <clingo/ground/program.hh>
11#include <clingo/ground/script.hh>
13#include <clingo/input/rewrite/analyze.hh>
15#include <clingo/util/small_vector.hh>
16#include <clingo/util/type_traits.hh>
18namespace CppClingo::Control {
46 : mbr_{&
mbr}, log_{&log}, store_{&
store}, theory_directives_{&theory_directives}, base_{&base}, comp_{&comp},
53 return static_cast<size_t>(it - comp_->
incomplete.begin());
62 if (
auto const *slit = std::get_if<Input::LitSymbolic>(&lit); slit !=
nullptr) {
71 std::ranges::all_of(*body_, [](
auto const &lit) {
return lit->single_pass(); });
84 [[nodiscard]]
auto context() const -> Ground::ScriptCallback * {
return context_; }
88 -> std::pair<Ground::UTerm, Ground::ProjectState *> {
93 [[nodiscard]]
auto type() const -> Input::ComponentType {
return comp_->
type; };
99 [[nodiscard]]
auto mbr() const -> std::pmr::monotonic_buffer_resource & {
return *mbr_; }
107 [[nodiscard]]
auto gcomp() const -> Ground::Component & {
return *gcomp_; }
109 [[nodiscard]]
auto body() const -> Ground::ULitVec & {
return *body_; }
112 template <
class T,
class... Args> [[nodiscard]]
auto state(Args &&...args) -> T & {
113 states_->emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
114 return static_cast<T &
>(*states_->back());
123 with_simple_lit(lit, [&res]([[maybe_unused]]
auto sig,
auto term,
auto &base,
auto provides) {
124 res.emplace(std::make_tuple(std::move(term), std::ref(base), std::move(provides)));
132 with_simple_lit(term, [&res]([[maybe_unused]]
auto sig,
auto term,
auto &base,
auto provides) {
133 res.emplace(std::make_tuple(std::move(term), std::ref(base), std::move(provides)));
136 return *std::move(res);
141 auto provides = std::vector<size_t>{};
142 auto sig = signature(term);
145 if (
auto it = def_map_->find(&term); it != def_map_->end()) {
146 provides = it->second;
148 std::invoke(std::forward<F>(fun), *sig,
build_term(*var_map_, term), base, std::move(provides));
154 [&]<
class T>(T
const &lit) {
155 if constexpr (Util::matches<T, Input::LitSymbolic>) {
159 }
else if constexpr (Util::matches<T, Input::LitBool>) {
160 if (lit.value() == expect_truth) {
164 throw std::runtime_error(
"unexpected literal in rule head");
171 return std::ranges::binary_search(*theory_directives_, sig);
175 std::pmr::monotonic_buffer_resource *mbr_;
Context object holding necessary data for translating from input to ground representation.
Definition context.hh:39
auto inc_priority() -> size_t
Increment the priority and return its previous value.
Definition context.hh:118
auto simple_lit(Input::Lit const &lit) -> Ground::AtomSimple
Translate the given simple input literal into a simple ground atom.
Definition context.hh:121
auto state(Args &&...args) -> T &
Add a new state object for a body aggregate literal.
Definition context.hh:112
auto next_index() -> size_t
Return a fresh atom index.
Definition context.hh:75
auto def_map() const -> DefMap &
Get the definition map.
Definition context.hh:102
auto single_pass(Input::Lit const &lit) const -> bool
Check if the given input literal is single pass.
Definition context.hh:58
auto store() const -> SymbolStore &
Get the symbol store.
Definition context.hh:81
auto var_map() const -> VarMap &
Get the variable map.
Definition context.hh:104
auto index(Input::LitSymbolic const &lit) const -> size_t
Get the index of the given symbolic literal.
Definition context.hh:50
auto context() const -> Ground::ScriptCallback *
Get the associated context to call scripts.
Definition context.hh:84
auto add_base(std::tuple< String, size_t, bool > sig) -> Ground::AtomBase &
Add an atom base for the given signature.
Definition context.hh:96
auto is_theory_directive(TheorySig sig) const -> bool
Check whether the given signature corresponds to a directive.
Definition context.hh:170
auto add_project(Ground::UTerm const &term, Ground::AtomBase &base) -> std::pair< Ground::UTerm, Ground::ProjectState * >
Add a base for a projection.
Definition context.hh:87
void with_simple_lit(Input::Term const &term, F &&fun)
Translate the given input term (for an atom) with a callback.
Definition context.hh:140
auto gcomp() const -> Ground::Component &
Get the current component.
Definition context.hh:107
auto logger() const -> Logger &
Get the logger.
Definition context.hh:78
auto single_pass_body() const -> bool
Check if the (current) body can be grounded in a single pass.
Definition context.hh:69
auto mbr() const -> std::pmr::monotonic_buffer_resource &
Get the monotonic allocator for the component.
Definition context.hh:99
void with_simple_lit(Input::Lit const &lit, F &&fun, bool expect_truth=false)
Translate the given simple input literal with a callback.
Definition context.hh:152
auto type() const -> Input::ComponentType
Get the component type.
Definition context.hh:93
BuildContext(std::pmr::monotonic_buffer_resource &mbr, Logger &log, SymbolStore &store, TheorySigVec const &theory_directives, Ground::Bases &base, Input::Component const &comp, DefMap &def_map, Ground::Component &gcomp, VarMap &var_map, Ground::ULitVec &body, Ground::UStateVec &states, Ground::ScriptCallback *context)
Construct the build context.
Definition context.hh:42
auto body() const -> Ground::ULitVec &
Get the current statement body.
Definition context.hh:109
auto simple_lit(Input::Term const &term) -> Ground::AtomSimple::value_type
Translate the given input term (for an atom) into a simple ground atom.
Definition context.hh:130
An atom base used to store derivable atoms and associated state.
Definition base.hh:212
The base for all atoms and terms.
Definition base.hh:407
auto add_base(std::tuple< String, size_t, bool > sig) -> Ground::AtomBase &
Add an atom base.
auto add_project(SymbolStore &store, Ground::UTerm const &term, Ground::AtomBase &base) -> std::pair< Ground::UTerm, ProjectState * >
Add a base for a projected atom.
Captures statements depending cyclically on each other.
Definition program.hh:13
Interface to call functions during parsing/grounding.
Definition script.hh:28
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
A store for symbols.
Definition symbol.hh:454
auto build_term(Util::unordered_map< String, size_t > const &var_map, Input::Term const &term, bool &has_projection) -> Ground::UTerm
Translates input theory terms to their ground representation.
Util::unordered_map< String, size_t > VarMap
A map from variable names (input) to their indices (ground).
Definition context.hh:32
CppClingo::Ground::BaseMap BaseMap
A map from signatures to atom bases.
Definition context.hh:29
Util::unordered_map< Input::Term const *, std::vector< size_t > > DefMap
A map from terms to the indices defining them.
Definition context.hh:35
CppClingo::Ground::ProjectMap ProjectMap
A map from a terms with projections associated state used during grounding.
Definition context.hh:26
std::tuple< String, size_t > TheorySig
A signature of a theory atom.
Definition core.hh:182
std::vector< TheorySig > TheorySigVec
A vector of theory atom signatures.
Definition core.hh:184
Util::ordered_map< std::tuple< String, size_t, bool >, UAtomBase > BaseMap
A map from signatures to atom bases.
Definition base.hh:404
Util::ordered_map< Ground::UTerm, UProjectState > ProjectMap
A map from a terms with projections associated state used during grounding.
Definition base.hh:401
std::vector< UState > UStateVec
A vector of states.
Definition base.hh:448
std::optional< std::tuple< Ground::UTerm, AtomBase &, std::vector< size_t > > > AtomSimple
Represents a simple head literal which is either represented by a symbol term or #false captured by s...
Definition literal.hh:231
std::vector< ULit > ULitVec
A vector of literals.
Definition literal.hh:31
constexpr auto stratified_index
Marker for stratified literals.
Definition literal.hh:189
std::unique_ptr< Term > UTerm
A unique pointer holding a term.
Definition term.hh:33
tsl::hopscotch_map< Key, T, Hash, KeyEqual, Allocator, NeighborhoodSize, StoreHash, GrowthPolicy > unordered_map
Alias for unordered maps.
Definition unordered_map.hh:17