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 {
52 using Bound = std::variant<std::pair<Number, Number>, std::pair<Symbol, Symbol>>;
76 [[nodiscard]]
auto elems() const -> std::span<
size_t const>;
78 [[nodiscard]] auto
todo() -> std::span<
size_t const>;
81 [[nodiscard]] auto
uid() const -> std::optional<
size_t>;
88 std::vector<
size_t> elems_;
90 size_t propagated_ = 0;
91 size_t uid_ = invalid_offset;
92 bool enqueued_ = false;
93 bool matched_ = false;
110 [[nodiscard]]
auto size() const ->
size_t;
115 [[nodiscard]] auto index(
Symbol const *sym) const ->
size_t;
117 [[nodiscard]] auto nth(
size_t i) const ->
AtomMap::const_iterator;
119 [[nodiscard]] auto nth(
size_t i) ->
AtomMap::iterator;
155 [[nodiscard]]
auto fact() const ->
bool;
158 [[nodiscard]] auto size() const ->
size_t;
162 [[nodiscard]] auto hash() const ->
size_t;
171 CLINGO_IGNORE_ZERO_SIZED_ARRAY_B
173 CLINGO_IGNORE_ZERO_SIZED_ARRAY_E
188 : base_{global.size()}, global_{std::move(global)}, guards_{std::move(guards)}, bases_{std::move(bases)},
189 mbr_{&mbr}, index_{index}, fun_{fun}, single_pass_body_{single_pass_body} {}
196 [[nodiscard]] auto guards() const ->
GuardVec const &;
201 [[nodiscard]] auto single_pass_body() const ->
bool;
203 [[nodiscard]] auto index() const ->
size_t;
207 [[nodiscard]] auto indices() const -> std::vector<
size_t>;
222 void print(std::ostream &out,
bool print_index);
232 void enqueue_(
AtomMap::iterator it);
237 auto atom_index_(
AtomMap::iterator it) ->
size_t;
245 std::vector<
size_t> queue_;
246 std::pmr::monotonic_buffer_resource *mbr_;
247 AtomKey *atom_key_ =
nullptr;
250 bool single_pass_body_;
258 : state_{&state}, body_{std::move(body)}, priority_{priority} {}
262 void do_print(std::ostream &out)
const override;
264 [[nodiscard]]
auto do_body() const ->
ULitVec const & override;
265 [[nodiscard]] auto do_important() const ->
VariableSet override;
268 void do_print_head(std::ostream &out) const override;
269 void do_init(
size_t gen) override;
270 [[nodiscard]] auto do_report(
EvalContext const &ctx) ->
bool override;
272 [[nodiscard]] auto do_priority() const ->
size_t override;
285 : loc_weight_{std::move(loc_weight)}, state_{&state}, head_{head ? std::move(head->first) : nullptr},
286 base_{head ? head->second : nullptr}, tuple_{std::move(tuple)}, body_{std::move(body)} {}
292 [[nodiscard]]
auto do_body() const ->
ULitVec const & override;
293 [[nodiscard]] auto do_important() const ->
VariableSet override;
294 void do_init(
size_t gen) override;
295 [[nodiscard]] auto do_report(
EvalContext const &ctx) ->
bool override;
297 [[nodiscard]] auto do_priority() const ->
size_t override;
298 void do_print_head(std::ostream &out) const override;
299 void do_print(std::ostream &out) const override;
301 auto get_cond_(
EvalContext const &ctx) -> std::pair<
size_t,
bool>;
310 bool logged_ = false;
321 eval_.reserve(state_->
global().size() + state_->
guards().size());
337 friend auto operator<<(std::ostream &out,
MatchHdAggr const &m) -> std::ostream &;
343 std::vector<
Symbol> mutable eval_;
356 [[nodiscard]]
auto do_domain() const ->
bool override;
359 [[nodiscard]] auto do_single_pass() const ->
bool override;
361 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr,
MatcherType type,
362 std::vector<
bool> const &bound)
363 -> std::pair<
UMatcher, std::optional<
size_t>> override;
365 [[nodiscard]] auto do_score(std::vector<
bool> const &bound) const ->
double override;
367 void do_print(std::ostream &out) const override;
371 [[nodiscard]] auto do_copy() const ->
ULit override;
373 [[nodiscard]] auto do_hash() const ->
size_t override;
375 [[nodiscard]] auto do_equal_to(
Lit const &other) const ->
bool override;
377 [[nodiscard]] auto do_compare_to(
Lit const &other) const -> std::weak_ordering override;
379 size_t offset_ = invalid_offset;
An atom base used to store derivable atoms and associated state.
Definition base.hh:212
Extensible ground representation of head aggregates.
Definition head_aggregate.hh:46
auto enqueue() -> bool
Enqueue the atom for propagation.
auto propagate(GuardVec const &guards, Symbol const *vals) -> bool
Check if the aggregate matches the guards.
auto uid() const -> std::optional< size_t >
Get the unique id of the aggregate.
auto todo() -> std::span< size_t const >
Get the aggregate elements to propagate.
void dequeue()
Dequeue the atom after propagation.
std::variant< std::pair< Number, Number >, std::pair< Symbol, Symbol > > Bound
The lower and upper bound for the value an aggregate can take.
Definition head_aggregate.hh:52
void accumulate(AggregateFunction fun, SymbolSpan tup, bool fact)
Accumulate a tuple.
auto elems() const -> std::span< size_t const >
Get the aggregate elements.
AtomHdAggr(AggregateFunction fun)
Initialize for the given aggregate function.
Definition head_aggregate.hh:55
void add_elem(size_t idx)
Add a new element.
The base capturing derived head aggregate atoms.
Definition head_aggregate.hh:97
Util::ordered_map< Symbol const *, AtomHdAggr, Util::array_hash, Util::array_equal_to > AtomMap
Map containing the atoms.
Definition head_aggregate.hh:101
BaseHdAggr(size_t size)
Construct an empty base.
Definition head_aggregate.hh:104
auto add(Symbol const *sym, AggregateFunction fun) -> std::pair< AtomMap::iterator, bool >
Add an atom to the current generation.
auto size() const -> size_t
Get the number of derived atoms.
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 an aggregate.
Definition head_aggregate.hh:348
LitHdAggr(StateHdAggr &state)
Construct the aggregate literal.
Definition head_aggregate.hh:351
The base class for groundable literals.
Definition literal.hh:34
A term like object used to match head aggregates.
Definition head_aggregate.hh:314
MatchHdAggr(StateHdAggr &state)
Construct the matcher.
Definition head_aggregate.hh:320
Symbol const * Key
The key to match against.
Definition head_aggregate.hh:317
auto vars() const -> VariableSet
Get the variables of the matcher.
A queue to process instantiators.
Definition instantiator.hh:206
Keys for aggregate elements storing their tuple and their aggregate index.
Definition head_aggregate.hh:138
void mark_fact() const
Mark as fact.
ElementKey(ElementKey const &other)=delete
Prevent copying and moving.
auto fact() const -> bool
Check if is fact.
static auto construct(auto &mbr, EvalContext const &ctx, AggregateFunction fun, size_t atom_idx, StmHdAggrElem &elem) -> bool
Construct an element key evaluating the given tuple.
ElementKey(priv_tag tag, EvalContext const &ctx, AggregateFunction fun, size_t atom_idx, StmHdAggrElem &elem, bool &res)
Private constructor.
State storing all necessary information to ground head aggregates.
Definition head_aggregate.hh:132
auto guards() const -> GuardVec const &
Get the non-ground guards of the aggregate.
Util::ordered_map< ElementKey *, Util::small_vector< std::tuple< Symbol, size_t, size_t >, 1 > > ElementMap
A map from tuples to their head atoms and conditions.
Definition head_aggregate.hh:183
auto global() const -> VariableVec const &
Get the global variables in the aggregate.
BaseHdAggr::AtomMap AtomMap
A map from global variables (including the guards) to the aggregate representation.
Definition head_aggregate.hh:178
Class to store state for grounding.
Definition base.hh:438
Gather aggregate elements.
Definition head_aggregate.hh:280
StmHdAggrElem(StateHdAggr &state, std::optional< std::pair< UTerm, AtomBase * > > head, Location loc_weight, UTermVec tuple, ULitVec body)
Construct the statement.
Definition head_aggregate.hh:283
A statement deriving head aggregate atoms to trigger grounding of elements.
Definition head_aggregate.hh:254
StmHdAggr(StateHdAggr &state, Ground::ULitVec body, size_t priority)
Construct the statement.
Definition head_aggregate.hh:257
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
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
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
@ fact
Indicates that the atom is derived by a fact.
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< std::tuple< std::tuple< String, size_t, bool >, AtomBase *, std::vector< size_t > > > BaseVec
A vector of signatures, bases, and indices.
Definition literal.hh:372
std::vector< ULit > ULitVec
A vector of literals.
Definition literal.hh:31
std::vector< std::pair< Relation, UTerm > > GuardVec
A vector of guards.
Definition term.hh:37
std::unique_ptr< Term > UTerm
A unique pointer holding a term.
Definition term.hh:33
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