3#include <clingo/ground/literal.hh>
4#include <clingo/ground/matcher.hh>
5#include <clingo/ground/statement.hh>
7#include <clingo/util/small_vector.hh>
9#include <memory_resource>
12namespace CppClingo::Ground {
31 using Bound = std::variant<std::pair<Number, Number>, std::pair<Symbol, Symbol>>;
72 [[nodiscard]] auto
elems() const -> std::span<
size_t const>;
74 [[nodiscard]] auto
todo() -> std::span<
size_t const>;
77 [[nodiscard]] auto
uid() const -> std::optional<
size_t>;
85 std::vector<
size_t> elems_;
87 size_t propagated_ = 0;
88 size_t derived_idx_ = 0;
89 size_t uid_ = invalid_offset;
91 bool enqueued_ = false;
112 void add(AtomMap::iterator it);
115 [[nodiscard]]
auto size() const ->
size_t;
120 auto index(
Symbol const *sym) const ->
size_t;
122 auto nth(
size_t i) const ->
AtomMap::const_iterator;
131 Util::index_sequence<
size_t>
derived_;
161 [[nodiscard]] auto hash() const ->
size_t;
170 CLINGO_IGNORE_ZERO_SIZED_ARRAY_B
172 CLINGO_IGNORE_ZERO_SIZED_ARRAY_E
188 size_t index,
bool domain,
bool monotone,
bool single_pass_elems)
189 : base_{global.size()}, global_{std::move(global)}, guards_{std::move(guards)}, mbr_{&mbr}, index_{index},
190 fun_{fun}, domain_{domain}, monotone_{monotone}, single_pass_elems_{single_pass_elems} {}
197 [[nodiscard]] auto guards() const ->
GuardVec const &;
206 [[nodiscard]] auto domain() const ->
bool;
210 [[nodiscard]] auto monotone() const ->
bool;
215 [[nodiscard]] auto single_pass_elems() const ->
bool;
217 [[nodiscard]] auto index() const ->
size_t;
220 auto propagate() ->
bool;
237 void print(std::ostream &out,
bool print_index);
247 void enqueue_(
AtomMap::iterator it);
252 auto atom_index_(
AtomMap::iterator it) ->
size_t;
259 std::vector<
size_t> queue_;
260 std::pmr::monotonic_buffer_resource *mbr_;
261 AtomKey *atom_key_ =
nullptr;
266 bool single_pass_elems_;
277 eval_.reserve(state_->global().size() + state_->guards().size());
294 friend auto operator<<(std::ostream &out,
MatchBdAggr const &m) -> std::ostream &;
300 std::vector<
Symbol> mutable eval_;
321 [[nodiscard]]
auto do_domain() const ->
bool override;
324 [[nodiscard]] auto do_single_pass() const ->
bool override;
326 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr,
MatcherType type,
327 std::vector<
bool> const &bound)
328 -> std::pair<
UMatcher, std::optional<
size_t>> override;
330 [[nodiscard]] auto do_score([[maybe_unused]] std::vector<
bool> const &bound) const ->
double override;
332 void do_print(std::ostream &out) const override;
336 [[nodiscard]] auto do_copy() const ->
ULit override;
338 [[nodiscard]] auto do_hash() const ->
size_t override;
340 [[nodiscard]] auto do_equal_to(
Lit const &other) const ->
bool override;
342 [[nodiscard]] auto do_compare_to(
Lit const &other) const -> std::weak_ordering override;
344 size_t offset_ = invalid_offset;
345 Symbol const *symbol_ =
nullptr;
364 : state_{&state}, loc_weight_{std::move(loc_weight)}, tuple_{std::move(tuple)}, body_{std::move(body)},
365 num_cond_{num_cond}, priority_{priority} {}
369 : state_{other.state_}, loc_weight_{other.loc_weight_}, tuple_{
copy_uvec(other.tuple_)},
370 body_{
copy_uvec(other.body_)}, num_cond_{other.num_cond_}, priority_{other.priority_} {};
382 [[nodiscard]]
auto do_body() const ->
ULitVec const & override;
383 [[nodiscard]] auto do_important() const ->
VariableSet override;
384 [[nodiscard]] auto do_is_important(
size_t index) const ->
bool override;
385 void do_init([[maybe_unused]]
size_t gen) override;
386 [[nodiscard]] auto do_report(
EvalContext const &ctx) ->
bool override;
388 [[nodiscard]] auto do_priority() const ->
size_t override;
389 void do_print_head(std::ostream &out) const override;
390 void do_print(std::ostream &out) const override;
392 auto get_cond_(
EvalContext const &ctx) -> std::pair<
size_t,
bool>;
401 bool logged_ = false;
404static_assert(std::is_nothrow_move_constructible_v<
StmBdAggrElem>);
405static_assert(std::is_nothrow_move_assignable_v<
StmBdAggrElem>);
413 : state_{&state}, elems_{std::move(elems)}, sign_{sign} {}
424 [[nodiscard]]
auto do_domain() const ->
bool override;
426 [[nodiscard]] auto do_single_pass() const ->
bool override;
427 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr,
MatcherType type,
428 std::vector<
bool> const &bound)
429 -> std::pair<
UMatcher, std::optional<
size_t>> override;
430 [[nodiscard]] auto do_score([[maybe_unused]] std::vector<
bool> const &bound) const ->
double override;
431 void do_print(std::ostream &out) const override;
433 [[nodiscard]] auto do_copy() const ->
ULit override;
434 [[nodiscard]] auto do_hash() const ->
size_t override;
435 [[nodiscard]] auto do_equal_to(
Lit const &other) const ->
bool override;
436 [[nodiscard]] auto do_compare_to(
Lit const &other) const -> std::weak_ordering override;
440 size_t offset_ = invalid_offset;
Extensible ground representation of body aggregates.
Definition body_aggregate.hh:25
auto propagate(GuardVec const &guards, Symbol const *vals) -> std::pair< bool, bool >
Check if the aggregate matches the guards (first) and is a fact (second).
auto enqueue() -> bool
Enqueue the atom for propagation.
AtomBdAggr(AggregateFunction fun)
Initialize for the given aggregate function.
Definition body_aggregate.hh:34
std::variant< std::pair< Number, Number >, std::pair< Symbol, Symbol > > Bound
The lower and upper bound for the value an aggregate can take.
Definition body_aggregate.hh:31
void accumulate(AggregateFunction fun, SymbolSpan tup, bool fact)
Accumulate a tuple.
auto state() const -> AtomBdAggrState
Get the derived state of the aggregate atom.
auto elems() const -> std::span< size_t const >
Get the aggregate elements.
auto todo() -> std::span< size_t const >
Get the aggregate elements to propagate.
auto uid() const -> std::optional< size_t >
Get the unique id of the aggregate.
auto derived_idx() const -> size_t
Get the index of the aggregate.
void dequeue()
Dequeue the atom after propagation.
void add_elem(size_t idx)
Add a new element.
The base capturing derived body aggregate atoms.
Definition body_aggregate.hh:95
auto size() const -> size_t
Get the number of derived atoms.
BaseBdAggr(size_t size)
Construct an empty base.
Definition body_aggregate.hh:102
void add(AtomMap::iterator it)
Add an atom to the base.
Util::ordered_map< Symbol const *, AtomBdAggr, Util::array_hash, Util::array_equal_to > AtomMap
Map containing the atoms.
Definition body_aggregate.hh:99
auto is_fact(Symbol const *sym) const -> bool
Check if the given atom is a fact.
The base implementation of an atom base.
Definition base.hh:148
auto contains(Key const &sym, MatcherType type) const -> std::optional< size_t >
Check if the base contains the given atom with in the given generation.
Definition base.hh:160
Context object to capture state used during instantiation.
Definition instantiator.hh:35
Literal representing a stratified body aggregate.
Definition body_aggregate.hh:409
LitBdAggrStrat(StateBdAggr &state, std::vector< StmBdAggrElem > elems, Sign sign)
Construct the aggregate literal.
Definition body_aggregate.hh:412
Literal representing an aggregate.
Definition body_aggregate.hh:305
LitBdAggr(StateBdAggr &state, Sign sign)
Construct the aggregate literal.
Definition body_aggregate.hh:308
The base class for groundable literals.
Definition literal.hh:34
A term like object used to match body aggregates.
Definition body_aggregate.hh:270
Symbol const * Key
The key to match against.
Definition body_aggregate.hh:273
auto vars() const -> VariableSet
Get the variables of the matcher.
MatchBdAggr(StateBdAggr &state)
Construct the matcher.
Definition body_aggregate.hh:276
A queue to process instantiators.
Definition instantiator.hh:206
Keys for aggregate elements storing their tuple and their aggregate index.
Definition body_aggregate.hh:144
static auto construct(auto &mbr, EvalContext const &ctx, AggregateFunction fun, size_t atom_idx, StmBdAggrElem &elem) -> bool
Construct an element key evaluating the given tuple.
ElementKey(priv_tag tag, EvalContext const &ctx, AggregateFunction fun, size_t atom_idx, StmBdAggrElem &elem, bool &res)
Construct the element.
ElementKey(ElementKey const &other)=delete
Prevent copying and moving.
auto span() const -> SymbolSpan
Get the tuple.
State storing all necessary information to ground body aggregates.
Definition body_aggregate.hh:138
auto global() const -> VariableVec const &
Get the global variables in the aggregate.
BaseBdAggr::AtomMap AtomMap
A map from global variables (including the guards) to the aggregate representation.
Definition body_aggregate.hh:177
Util::ordered_map< ElementKey *, Util::small_vector< size_t > > ElementMap
A map from tuples to their conditions.
Definition body_aggregate.hh:184
Class to store state for grounding.
Definition base.hh:438
Gather aggregate elements.
Definition body_aggregate.hh:355
StmBdAggrElem(StmBdAggrElem const &other)
Copy constructor.
Definition body_aggregate.hh:368
auto operator=(StmBdAggrElem &&other) noexcept -> StmBdAggrElem &=default
Move assignment.
StmBdAggrElem(StateBdAggr &state, Location loc_weight, UTermVec tuple, ULitVec body, size_t num_cond, size_t priority)
Construct the statement.
Definition body_aggregate.hh:362
auto operator=(StmBdAggrElem const &other) -> StmBdAggrElem &=default
Copy assignment.
StmBdAggrElem(StmBdAggrElem &&other) noexcept=default
Move constructor.
Base class for groundable statements.
Definition statement.hh:17
The Location of an expression in an input source.
Definition location.hh:44
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
An arbitrary precision integer.
Definition number.hh:27
Interface to output literals.
Definition output.hh:61
Interface to output statements.
Definition output.hh:90
A store for symbols.
Definition symbol.hh:454
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
Container to store integer sequences.
Definition index_sequence.hh:15
std::span< Symbol const > SymbolSpan
A span of symbols.
Definition symbol.hh:218
std::vector< Symbol > SymbolVec
A vector of symbols.
Definition symbol.hh:220
Sign
Enumeration of signs (default negation).
Definition core.hh:16
AggregateFunction
Enumeration of aggregate functions.
Definition core.hh:87
VariableSet::values_container_type VariableVec
A vector of variables.
Definition base.hh:21
Util::ordered_set< size_t > VariableSet
A set of variables.
Definition base.hh:19
@ derived
Indicates that the atom is derived by some rule but not a fact.
@ unknown
Indicates that the atom has not yet been derived by a rule.
@ fact
Indicates that the atom is derived by a fact.
AtomBdAggrState
Derivation state of body aggregates.
Definition body_aggregate.hh:18
MatcherType
Enumeration of matcher types.
Definition instantiator.hh:48
std::unique_ptr< Matcher > UMatcher
A unique pointer to a matcher.
Definition instantiator.hh:100
std::unique_ptr< Lit > ULit
A unique pointer holding a literal.
Definition literal.hh:29
VarSelectMode
Available variable selection modes.
Definition literal.hh:21
std::vector< ULit > ULitVec
A vector of literals.
Definition literal.hh:31
auto copy_uvec(std::vector< std::unique_ptr< T > > const &vec)
Helper to copy vectors with copyable elements.
Definition term.hh:44
std::vector< std::pair< Relation, UTerm > > GuardVec
A vector of guards.
Definition term.hh:37
std::vector< UTerm > UTermVec
A vector of terms.
Definition term.hh:35
tsl::ordered_map< Key, T, Hash, KeyEqual, Allocator, ValueTypeContainer, IndexType > ordered_map
Alias for ordered maps.
Definition ordered_map.hh:16