3#include <clingo/ground/profile.hh>
5#include <clingo/core/logger.hh>
6#include <clingo/core/output.hh>
7#include <clingo/core/symbol.hh>
13namespace CppClingo::Ground {
26 [[nodiscard]]
auto log() const ->
Logger & {
return *log_; }
69 case MatcherType::new_atoms: {
92 void print(std::ostream &out)
const { do_print(out); }
99 [[nodiscard]]
virtual auto do_next(
EvalContext const &ctx) ->
bool = 0;
100 virtual void do_print(std::ostream &out)
const = 0;
116 void init(
size_t gen) { do_init(gen); }
122 [[nodiscard]]
auto priority() const ->
size_t {
return do_priority(); }
124 void print_head(std::ostream &out)
const { do_print_head(out); }
126 [[nodiscard]]
auto is_important(
size_t index)
const ->
bool {
return do_is_important(index); }
133 virtual void do_init(
size_t gen) = 0;
134 [[nodiscard]]
virtual auto do_report(
EvalContext const &ctx) ->
bool = 0;
136 [[nodiscard]]
virtual auto do_priority() const ->
size_t = 0;
137 virtual
void do_print_head(std::ostream &out) const = 0;
138 [[nodiscard]] virtual auto do_is_important([[maybe_unused]]
size_t index) const ->
bool {
return true; }
139 [[nodiscard]]
virtual auto do_profile_node() const -> ProfileNodeInternal * = 0;
149 matchers_.reserve(n + 1);
178 void print(std::ostream &out)
const;
180 [[nodiscard]]
auto priority()
const {
return icb_->priority(); }
190 auto *node = icb.profile_node();
191 return node !=
nullptr ? &node->add_child(std::make_unique<ProfileData>()).step_ :
nullptr;
193 class BackjumpMatcher {
195 BackjumpMatcher(UMatcher matcher, DependVec depend)
196 : matcher_{std::move(matcher)}, depend_{std::move(depend)} {}
197 void init(InstantiationContext
const &ctx,
size_t gen);
198 void match(EvalContext
const &ctx);
199 auto next(EvalContext
const &ctx) -> bool;
200 auto first(EvalContext
const &ctx) -> bool;
201 void print(std::ostream &out,
size_t index)
const;
202 [[nodiscard]]
auto depend() const -> DependVec const &;
203 [[nodiscard]] auto backjumpable() const ->
bool;
208 std::vector<
size_t> depend_;
209 bool backjumpable_ = true;
211 InstanceCallback *icb_;
212 ProfileStats *stats_;
214 std::vector<BackjumpMatcher> matchers_;
215 bool enqueued_ = false;
233 auto release() -> std::vector<Instantiator> {
return std::move(insts_); }
237 void enter_(
size_t i);
239 std::vector<Instantiator> insts_;
240 std::vector<std::vector<size_t>> update_;
241 std::vector<std::vector<Instantiator *>> queues_;
243 size_t max_prio_ = 0;
Context object to capture state used during instantiation.
Definition instantiator.hh:39
EvalContext(Logger &log, SymbolStore &store, OutputStm &out, Assignment &ass)
Construct an instantiation state.
Definition instantiator.hh:42
auto ass() const -> Assignment &
Get the assignment.
Definition instantiator.hh:45
Callbacks to notify statements during instantiations.
Definition instantiator.hh:111
auto report(EvalContext const &ctx) -> bool
Report an assignment giving rise to an instance for a statement.
Definition instantiator.hh:118
void propagate(SymbolStore &store, OutputStm &out, Queue &queue)
Notify a statement that instantiation has finished.
Definition instantiator.hh:120
void init(size_t gen)
Notify a statement that instantiation starts.
Definition instantiator.hh:116
auto priority() const -> size_t
The priority of the callback.
Definition instantiator.hh:122
auto profile_node() const -> ProfileNodeInternal *
Get a node to store profiling data for this statement.
Definition instantiator.hh:130
void print_head(std::ostream &out) const
Print representation for debugging.
Definition instantiator.hh:124
auto is_important(size_t index) const -> bool
Check if the literal with the given index is important.
Definition instantiator.hh:126
virtual ~InstanceCallback()=default
Destroy the callback.
Context object to capture state used during instantiation.
Definition instantiator.hh:21
auto out() const -> OutputStm &
Get the output.
Definition instantiator.hh:30
InstantiationContext(Logger &log, SymbolStore &store, OutputStm &out)
Construct an instantiation state.
Definition instantiator.hh:24
auto store() const -> SymbolStore &
Get the store.
Definition instantiator.hh:28
auto log() const -> Logger &
Get the logger.
Definition instantiator.hh:26
An instantiator implementing the basic grounding algorithm.
Definition instantiator.hh:143
std::vector< size_t > DependVec
A vector of Matcher indices.
Definition instantiator.hh:146
void print(std::ostream &out) const
Print the instantiator.
void propagate(SymbolStore &store, OutputStm &out, Queue &queue)
Add instantiators that need grounding to queue.
friend auto operator<<(std::ostream &out, Instantiator const &inst) -> std::ostream &
Print the instantiator.
Definition instantiator.hh:183
auto priority() const
The priority of the instantiator.
Definition instantiator.hh:180
void add(UMatcher matcher, DependVec depends)
Finalize the instantiator.
auto instantiate(Logger &log, SymbolStore &store, OutputStm &out) -> bool
Find all assignments for the added matchers.
auto enqueue() -> bool
Mark enqueued for instantiation.
Instantiator(InstanceCallback &icb, size_t vars, size_t n)
Construct an instantiator with the given instance callback and number of variables.
Definition instantiator.hh:148
void finalize(DependVec depends)
Finalize the instantiator.
void init(InstantiationContext const &ctx, size_t gen)
Prepare the instantiator for the first grounding step (with generation 0).
A matcher to match expressions.
Definition instantiator.hh:78
void match(EvalContext const &ctx)
Initialize matching.
Definition instantiator.hh:85
virtual ~Matcher()=default
Destroy the matcher.
auto type() const -> MatcherType
Get the type of the matcher.
Definition instantiator.hh:94
void print(std::ostream &out) const
Print the matcher to the given stream.
Definition instantiator.hh:92
void init(InstantiationContext const &ctx, size_t gen)
Notify that instantiation starts.
Definition instantiator.hh:83
auto next(EvalContext const &ctx) -> bool
Obtain the next match.
Definition instantiator.hh:90
Profile node that can hold children.
Definition profile.hh:155
A queue to process instantiators.
Definition instantiator.hh:222
Queue()=default
Construct an empty queue.
auto process(Logger &log, SymbolStore &store, OutputStm &out) -> bool
Process previously enqueued instantiators.
auto release() -> std::vector< Instantiator >
Release the contained instantiators.
Definition instantiator.hh:233
void propagate(size_t index)
Propagate instantiators with the given index.
void insert(Instantiator inst, std::optional< size_t > index)
Register an instantiator with the queue.
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Interface to output statements.
Definition output.hh:90
A store for symbols.
Definition symbol.hh:454
std::vector< std::optional< Symbol > > Assignment
Assignment mapping variables to symbols.
Definition symbol.hh:222
auto operator<<(std::ostream &out, LitCondLitType type) -> std::ostream &
Print the type of a conditional literal grounding literal.
std::vector< UMatcher > UMatcherVec
A vector of matchers.
Definition instantiator.hh:106
std::vector< Instantiator > InstantiatorVec
A vector of instantiators.
Definition instantiator.hh:219
MatcherType
Enumeration of matcher types.
Definition instantiator.hh:52
std::unique_ptr< Matcher > UMatcher
A unique pointer to a matcher.
Definition instantiator.hh:104
@ all_atoms
Indicates a matcher for previously added atoms.
@ old_atoms
Indicates a matcher for freshly added atoms.
The profiling data.
Definition profile.hh:63