3#include <clingo/ground/profile.hh>
5#include <clingo/core/logger.hh>
6#include <clingo/core/output.hh>
7#include <clingo/core/symbol.hh>
9#include <clingo/util/sync.hh>
15namespace CppClingo::Ground {
28 [[nodiscard]]
auto log() const ->
Logger & {
return *log_; }
71 case MatcherType::new_atoms: {
94 void print(std::ostream &out)
const { do_print(out); }
101 [[nodiscard]]
virtual auto do_next(
EvalContext const &ctx) ->
bool = 0;
102 virtual void do_print(std::ostream &out)
const = 0;
118 void init(
size_t gen) { do_init(gen); }
124 [[nodiscard]]
auto priority() const ->
size_t {
return do_priority(); }
126 void print_head(std::ostream &out)
const { do_print_head(out); }
128 [[nodiscard]]
auto is_important(
size_t index)
const ->
bool {
return do_is_important(index); }
135 virtual void do_init(
size_t gen) = 0;
136 [[nodiscard]]
virtual auto do_report(
EvalContext const &ctx) ->
bool = 0;
138 [[nodiscard]]
virtual auto do_priority() const ->
size_t = 0;
139 virtual
void do_print_head(std::ostream &out) const = 0;
140 [[nodiscard]] virtual auto do_is_important([[maybe_unused]]
size_t index) const ->
bool {
return true; }
141 [[nodiscard]]
virtual auto do_profile_node() const -> ProfileNodeInternal * = 0;
151 matchers_.reserve(n + 1);
181 void print(std::ostream &out)
const;
183 [[nodiscard]]
auto priority()
const {
return icb_->priority(); }
193 auto *node = icb.profile_node();
194 return node !=
nullptr ? &node->add_child(std::make_unique<ProfileData>()).step_ :
nullptr;
196 class BackjumpMatcher {
198 BackjumpMatcher(UMatcher matcher, DependVec depend)
199 : matcher_{std::move(matcher)}, depend_{std::move(depend)} {}
200 void init(InstantiationContext
const &ctx,
size_t gen);
201 void match(EvalContext
const &ctx);
202 auto next(EvalContext
const &ctx) -> bool;
203 auto first(EvalContext
const &ctx) -> bool;
204 void print(std::ostream &out,
size_t index)
const;
205 [[nodiscard]]
auto depend() const -> DependVec const &;
206 [[nodiscard]] auto backjumpable() const ->
bool;
211 std::vector<
size_t> depend_;
212 bool backjumpable_ = true;
214 InstanceCallback *icb_;
215 ProfileStats *stats_;
217 std::vector<BackjumpMatcher> matchers_;
218 bool enqueued_ = false;
236 auto release() -> std::vector<Instantiator> {
return std::move(insts_); }
240 void enter_(
size_t i);
242 std::vector<Instantiator> insts_;
243 std::vector<std::vector<size_t>> update_;
244 std::vector<std::vector<Instantiator *>> queues_;
246 size_t max_prio_ = 0;
Context object to capture state used during instantiation.
Definition instantiator.hh:41
EvalContext(Logger &log, SymbolStore &store, OutputStm &out, Assignment &ass)
Construct an instantiation state.
Definition instantiator.hh:44
auto ass() const -> Assignment &
Get the assignment.
Definition instantiator.hh:47
Callbacks to notify statements during instantiations.
Definition instantiator.hh:113
auto report(EvalContext const &ctx) -> bool
Report an assignment giving rise to an instance for a statement.
Definition instantiator.hh:120
void propagate(SymbolStore &store, OutputStm &out, Queue &queue)
Notify a statement that instantiation has finished.
Definition instantiator.hh:122
void init(size_t gen)
Notify a statement that instantiation starts.
Definition instantiator.hh:118
auto priority() const -> size_t
The priority of the callback.
Definition instantiator.hh:124
auto profile_node() const -> ProfileNodeInternal *
Get a node to store profiling data for this statement.
Definition instantiator.hh:132
void print_head(std::ostream &out) const
Print representation for debugging.
Definition instantiator.hh:126
auto is_important(size_t index) const -> bool
Check if the literal with the given index is important.
Definition instantiator.hh:128
virtual ~InstanceCallback()=default
Destroy the callback.
Context object to capture state used during instantiation.
Definition instantiator.hh:23
auto out() const -> OutputStm &
Get the output.
Definition instantiator.hh:32
InstantiationContext(Logger &log, SymbolStore &store, OutputStm &out)
Construct an instantiation state.
Definition instantiator.hh:26
auto store() const -> SymbolStore &
Get the store.
Definition instantiator.hh:30
auto log() const -> Logger &
Get the logger.
Definition instantiator.hh:28
An instantiator implementing the basic grounding algorithm.
Definition instantiator.hh:145
std::vector< size_t > DependVec
A vector of Matcher indices.
Definition instantiator.hh:148
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:186
auto priority() const
The priority of the instantiator.
Definition instantiator.hh:183
void add(UMatcher matcher, DependVec depends)
Finalize the instantiator.
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:150
auto instantiate(Logger &log, SymbolStore &store, OutputStm &out, Util::StopFlag *stop) -> GroundResult
Find all assignments for the added matchers.
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:80
void match(EvalContext const &ctx)
Initialize matching.
Definition instantiator.hh:87
virtual ~Matcher()=default
Destroy the matcher.
auto type() const -> MatcherType
Get the type of the matcher.
Definition instantiator.hh:96
void print(std::ostream &out) const
Print the matcher to the given stream.
Definition instantiator.hh:94
void init(InstantiationContext const &ctx, size_t gen)
Notify that instantiation starts.
Definition instantiator.hh:85
auto next(EvalContext const &ctx) -> bool
Obtain the next match.
Definition instantiator.hh:92
Profile node that can hold children.
Definition profile.hh:155
A queue to process instantiators.
Definition instantiator.hh:225
auto process(Logger &log, SymbolStore &store, OutputStm &out) -> GroundResult
Process previously enqueued instantiators.
Queue(Util::StopFlag *stop=nullptr)
Construct an empty queue.
Definition instantiator.hh:228
auto release() -> std::vector< Instantiator >
Release the contained instantiators.
Definition instantiator.hh:236
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
Helper class to signal stopping of grounding.
Definition sync.hh:11
std::vector< std::optional< Symbol > > Assignment
Assignment mapping variables to symbols.
Definition symbol.hh:222
GroundResult
Available grounding results.
Definition core.hh:187
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:108
std::vector< Instantiator > InstantiatorVec
A vector of instantiators.
Definition instantiator.hh:222
MatcherType
Enumeration of matcher types.
Definition instantiator.hh:54
std::unique_ptr< Matcher > UMatcher
A unique pointer to a matcher.
Definition instantiator.hh:106
@ 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