Clingo
Loading...
Searching...
No Matches
instantiator.hh
1#pragma once
2
3#include <clingo/ground/profile.hh>
4
5#include <clingo/core/logger.hh>
6#include <clingo/core/output.hh>
7#include <clingo/core/symbol.hh>
8
9#include <clingo/util/sync.hh>
10
11#include <memory>
12#include <utility>
13#include <vector>
14
15namespace CppClingo::Ground {
16
19
20class Instantiator;
21
24 public:
28 [[nodiscard]] auto log() const -> Logger & { return *log_; }
30 [[nodiscard]] auto store() const -> SymbolStore & { return *store_; }
32 [[nodiscard]] auto out() const -> OutputStm & { return *out_; }
33
34 private:
35 Logger *log_;
36 SymbolStore *store_;
37 OutputStm *out_;
38};
39
42 public:
47 [[nodiscard]] auto ass() const -> Assignment & { return *ass_; }
48
49 private:
50 Assignment *ass_;
51};
52
54enum class MatcherType : uint8_t {
55 new_atoms,
56 old_atoms,
58};
59
61inline auto operator<<(std::ostream &out, MatcherType type) -> std::ostream & {
62 switch (type) {
64 out << "a";
65 break;
66 }
68 out << "o";
69 break;
70 }
71 case MatcherType::new_atoms: {
72 out << "n";
73 break;
74 }
75 }
76 return out;
77}
78
80class Matcher {
81 public:
83 virtual ~Matcher() = default;
85 void init(InstantiationContext const &ctx, size_t gen) { do_init(ctx, gen); }
87 void match(EvalContext const &ctx) { do_match(ctx); }
92 [[nodiscard]] auto next(EvalContext const &ctx) -> bool { return do_next(ctx); }
94 void print(std::ostream &out) const { do_print(out); }
96 [[nodiscard]] auto type() const -> MatcherType { return do_type(); }
97
98 private:
99 virtual void do_init(InstantiationContext const &ctx, size_t gen) = 0;
100 virtual void do_match(EvalContext const &ctx) = 0;
101 [[nodiscard]] virtual auto do_next(EvalContext const &ctx) -> bool = 0;
102 virtual void do_print(std::ostream &out) const = 0;
103 [[nodiscard]] virtual auto do_type() const -> MatcherType { return MatcherType::all_atoms; }
104};
106using UMatcher = std::unique_ptr<Matcher>;
108using UMatcherVec = std::vector<UMatcher>;
109
110class Queue;
111
114 public:
116 virtual ~InstanceCallback() = default;
118 void init(size_t gen) { do_init(gen); }
120 [[nodiscard]] auto report(EvalContext const &ctx) -> bool { return do_report(ctx); }
122 void propagate(SymbolStore &store, OutputStm &out, Queue &queue) { do_propagate(store, out, queue); }
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); }
132 [[nodiscard]] auto profile_node() const -> ProfileNodeInternal * { return do_profile_node(); }
133
134 private:
135 virtual void do_init(size_t gen) = 0;
136 [[nodiscard]] virtual auto do_report(EvalContext const &ctx) -> bool = 0;
137 virtual void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) = 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;
142};
143
146 public:
148 using DependVec = std::vector<size_t>;
150 Instantiator(InstanceCallback &icb, size_t vars, size_t n) : icb_{&icb}, stats_{profile_node_(icb)}, ass_{vars} {
151 matchers_.reserve(n + 1);
152 }
156 void init(InstantiationContext const &ctx, size_t gen);
162 void add(UMatcher matcher, DependVec depends);
167 void finalize(DependVec depends);
172 [[nodiscard]] auto enqueue() -> bool;
176 [[nodiscard]] auto instantiate(Logger &log, SymbolStore &store, OutputStm &out, Util::StopFlag *stop)
177 -> GroundResult;
179 void propagate(SymbolStore &store, OutputStm &out, Queue &queue);
181 void print(std::ostream &out) const;
183 [[nodiscard]] auto priority() const { return icb_->priority(); }
184
186 friend auto operator<<(std::ostream &out, Instantiator const &inst) -> std::ostream & {
187 inst.print(out);
188 return out;
189 }
190
191 private:
192 static auto profile_node_(InstanceCallback const &icb) -> ProfileStats * {
193 auto *node = icb.profile_node();
194 return node != nullptr ? &node->add_child(std::make_unique<ProfileData>()).step_ : nullptr;
195 }
196 class BackjumpMatcher {
197 public:
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;
207 void block();
208
209 private:
210 UMatcher matcher_;
211 std::vector<size_t> depend_;
212 bool backjumpable_ = true;
213 };
214 InstanceCallback *icb_;
215 ProfileStats *stats_;
216 Assignment ass_;
217 std::vector<BackjumpMatcher> matchers_;
218 bool enqueued_ = false;
219};
220
222using InstantiatorVec = std::vector<Instantiator>;
223
225class Queue {
226 public:
228 Queue(Util::StopFlag *stop = nullptr) : stop_{stop} {}
230 void insert(Instantiator inst, std::optional<size_t> index);
232 void propagate(size_t index);
234 [[nodiscard]] auto process(Logger &log, SymbolStore &store, OutputStm &out) -> GroundResult;
236 auto release() -> std::vector<Instantiator> { return std::move(insts_); }
237
238 private:
240 void enter_(size_t i);
241
242 std::vector<Instantiator> insts_;
243 std::vector<std::vector<size_t>> update_;
244 std::vector<std::vector<Instantiator *>> queues_;
245 size_t size_ = 0;
246 size_t max_prio_ = 0;
247 Util::StopFlag *stop_;
248};
249
251
252} // namespace CppClingo::Ground
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