3#include <clingo/control/context.hh>
4#include <clingo/control/term.hh>
6#include <clingo/input/literal.hh>
7#include <clingo/input/print.hh>
9#include <clingo/ground/literal.hh>
11#include <clingo/input/rewrite/analyze.hh>
13namespace CppClingo::Control {
23template <
class F,
bool stratify = false>
class BuilderLit {
26 BuilderLit(BuildContext &ctx, F cb) : cb_{std::move(cb)}, ctx_{&ctx} {}
30 void operator()(Input::LitBool
const &lit)
const { cb_(std::make_unique<Ground::LitBool>(
lit.value())); }
36 void operator()(Input::LitComparison
const &lit)
const {
39 auto const &rng = std::get<Input::TermBinary>(
lit.rhs().front().second);
40 auto lower =
build_term(ctx_->var_map(), *rng.lhs());
41 auto upper =
build_term(ctx_->var_map(), *rng.rhs());
42 cb_(std::make_unique<Ground::LitInterval>(std::move(lhs), std::move(lower), std::move(upper)));
44 if (ctx_->context() ==
nullptr) {
46 throw std::runtime_error(
"script context unavailable");
49 auto const &rng = std::get<Input::TermFunction>(
lit.rhs().front().second);
51 return build_term(ctx_->var_map(), std::get<Input::Term>(elem));
53 cb_(std::make_unique<Ground::LitExternal>(*ctx_->context(),
lit.loc(), rng.name(), std::move(lhs),
56 auto add_cmp = [
this](
auto const &lhs,
auto rel,
auto const &rhs) {
59 cb_(std::make_unique<Ground::LitComparison>(std::move(l), rel, std::move(r)));
61 auto const &lhs =
lit.lhs();
62 auto const &rhs =
lit.rhs().front().second;
63 auto rel =
lit.rhs().front().first;
64 add_cmp(lhs, rel, rhs);
66 add_cmp(rhs, rel, lhs);
71 void operator()(Input::LitSymbolic
const &lit)
const {
72 auto has_projection =
false;
73 auto term =
build_term(ctx_->var_map(),
lit.term(), has_projection);
78 auto [p_term, state] = ctx_->add_project(term, base);
79 cb_(std::make_unique<Ground::LitProject>(*state,
lit.sign(), std::move(term), std::move(p_term), idx,
80 ctx_->gcomp().domain()));
82 cb_(std::make_unique<Ground::LitSymbolic>(base,
lit.sign(), std::move(term), idx, ctx_->gcomp().domain()));
97 std::visit(Detail::BuilderLit{ctx, std::forward<F>(fun)}, lit);
104 std::visit(Detail::BuilderLit<std::decay_t<F>,
true>{ctx, std::forward<F>(fun)}, lit);
Context object holding necessary data for translating from input to ground representation.
Definition context.hh:39
void build_stratified_lit(BuildContext &ctx, Input::Lit const &lit, F &&fun)
Translate input literals to their ground representation.
Definition literal.hh:103
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.
void build_lit(BuildContext &ctx, Input::Lit const &lit, F &&fun)
Translate input literals to their ground representation.
Definition literal.hh:96
#define CLINGO_REPORT_LOC(p, id, loc)
Report messages of the given type and location.
Definition logger.hh:223
@ equal
The equal to symbol (=).
@ lit
Conditional literals derived during grounding.
constexpr auto stratified_index
Marker for stratified literals.
Definition literal.hh:189
auto to_vec(It begin, Sent end, Pred &&pred, Args &&...args)
Use a transformation function to build a vector from a range.
Definition algorithm.hh:48