Clingo
Loading...
Searching...
No Matches
statement.hh
1#pragma once
2
3#include <clingo/ground/literal.hh>
4
5#include <clingo/ground/instantiator.hh>
6
7#include <clingo/core/core.hh>
8
9#include <memory_resource>
10
11namespace CppClingo::Ground {
12
15
17class Stm : public InstanceCallback {
18 public:
20 [[nodiscard]] auto body() const -> ULitVec const & { return do_body(); }
21
28 [[nodiscard]] auto important() const -> VariableSet { return do_important(); }
29
31 friend auto operator<<(std::ostream &out, Stm const &stm) -> std::ostream & {
32 stm.do_print(out);
33 return out;
34 }
35
36 private:
37 virtual void do_print(std::ostream &out) const = 0;
38 [[nodiscard]] virtual auto do_body() const -> ULitVec const & = 0;
39 [[nodiscard]] virtual auto do_important() const -> VariableSet = 0;
40};
41
43using UStm = std::unique_ptr<Stm>;
45using UStmVec = std::vector<UStm>;
46
49 public:
51 Linearizer(std::pmr::monotonic_buffer_resource &mbr) : mbr_{&mbr} {}
53 void start(Queue &queue);
56
57 private:
59 void build_(ULitVec const &lits);
61 auto order_(InstanceCallback &cb, std::vector<MatcherType> const &todo, VariableSet const &important,
62 ULitVec const &lits) -> std::pair<Instantiator, std::optional<size_t>>;
63
64 Queue *iqueue_ = nullptr;
65 std::pmr::monotonic_buffer_resource *mbr_;
66 std::vector<size_t> rec_;
67 std::vector<std::vector<MatcherType>> todos_;
68 std::vector<std::tuple<size_t, size_t, double>> queue_;
70 std::vector<std::tuple<size_t, std::vector<size_t>, std::vector<size_t>>> lit_map_;
72 std::vector<std::vector<size_t>> var_map_;
73};
74
76enum class RuleType : uint8_t {
77 normal,
78 choice
79};
80
82class StmRule : public Stm {
83 public:
86 : head_{head ? std::move(std::get<0>(*head)) : nullptr}, node_{node},
87 base_{head ? &std::get<1>(*head) : nullptr},
88 indices_{head ? std::move(std::get<2>(*head)) : std::vector<size_t>{}}, body_{std::move(body)}, type_{type} {
89 init_();
90 }
91
92 private:
93 void init_();
94
95 // Stm interface
96 void do_print(std::ostream &out) const override;
97 [[nodiscard]] auto do_body() const -> ULitVec const & override;
98 [[nodiscard]] auto do_important() const -> VariableSet override;
99
100 // InstanceCallback interface
101 void do_print_head(std::ostream &out) const override;
102 void do_init(size_t gen) override;
103 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
104 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
105 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
106 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
107
111 UTerm head_;
112 ProfileNodeInternal *node_ = nullptr;
113 AtomBase *base_;
118 std::vector<size_t> indices_;
119 ULitVec body_;
120 Symbol atom_ = SymbolStore::sup();
121 RuleType type_;
122};
123
125class StmExternal : public Stm {
126 public:
128 StmExternal(Ground::UTerm atom, AtomBase &base, std::vector<size_t> indices, ULitVec body,
129 std::optional<std::pair<Location, UTerm>> type, ProfileNodeInternal *node)
130 : type_{std::move(type)}, atom_{std::move(atom)}, base_{&base}, indices_{std::move(indices)},
131 body_{std::move(body)}, node_{node} {
132 init_();
133 }
134
135 private:
136 void init_();
137
138 // Stm interface
139 void do_print(std::ostream &out) const override;
140 [[nodiscard]] auto do_body() const -> ULitVec const & override;
141 [[nodiscard]] auto do_important() const -> VariableSet override;
142
143 // InstanceCallback interface
144 void do_print_head(std::ostream &out) const override;
145 void do_init(size_t gen) override;
146 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
147 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
148 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
149 [[nodiscard]] auto do_is_important([[maybe_unused]] size_t index) const -> bool override { return false; }
150 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
151
152 std::optional<std::pair<Location, UTerm>> type_;
156 UTerm atom_;
157 AtomBase *base_;
162 std::vector<size_t> indices_;
163 ULitVec body_;
164 Symbol res_atom_ = SymbolStore::sup();
165 ProfileNodeInternal *node_;
166 ExternalType res_type_ = ExternalType::free;
167};
168
170class StmWeakConstraint : public Stm {
171 public:
173 StmWeakConstraint(Location loc_weight, UTerm weight, std::optional<std::pair<Location, UTerm>> prio, UTermVec terms,
175 : loc_weight_{std::move(loc_weight)}, loc_prio_{prio ? std::move(prio->first) : loc_weight_},
176 weight_{std::move(weight)}, prio_{prio ? std::move(prio->second) : nullptr}, terms_{std::move(terms)},
177 body_{std::move(body)}, node_{node} {
178 init_();
179 }
180
181 private:
182 void init_();
183 // Stm interface
184 void do_print(std::ostream &out) const override;
185 [[nodiscard]] auto do_body() const -> ULitVec const & override;
186 [[nodiscard]] auto do_important() const -> VariableSet override;
187
188 // InstanceCallback interface
189 void do_print_head(std::ostream &out) const override;
190 void do_init(size_t gen) override;
191 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
192 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
193 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
194 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
195
196 Location loc_weight_;
197 Location loc_prio_;
198 UTerm weight_;
199 UTerm prio_;
200 UTermVec terms_;
201 ULitVec body_;
202 Symbol res_weight_;
203 std::optional<Symbol> res_prio_;
204 SymbolVec res_terms_;
205 ProfileNodeInternal *node_;
206};
207
209class StmHeuristic : public Stm {
210 public:
212 StmHeuristic(UTerm atom, AtomBase &base, ULitVec body, Location loc_weight, UTerm weight,
213 std::optional<std::pair<Location, UTerm>> prio, Location loc_type, UTerm type,
215 : loc_weight_{std::move(loc_weight)}, loc_prio_{prio ? std::move(prio->first) : loc_weight_},
216 loc_type_{std::move(loc_type)}, atom_{std::move(atom)}, base_{&base}, weight_{std::move(weight)},
217 prio_{prio ? std::move(prio->second) : nullptr}, type_{std::move(type)}, body_{std::move(body)}, node_{node} {
218 init_();
219 }
220
221 private:
222 void init_();
223 // Stm interface
224 void do_print(std::ostream &out) const override;
225 [[nodiscard]] auto do_body() const -> ULitVec const & override;
226 [[nodiscard]] auto do_important() const -> VariableSet override;
227
228 // InstanceCallback interface
229 void do_print_head(std::ostream &out) const override;
230 void do_init(size_t gen) override;
231 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
232 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
233 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
234 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
235
236 Location loc_weight_;
237 Location loc_prio_;
238 Location loc_type_;
239 UTerm atom_;
240 AtomBase *base_;
241 UTerm weight_;
242 UTerm prio_;
243 UTerm type_;
244 ULitVec body_;
245 ProfileNodeInternal *node_;
246 size_t offset_ = 0;
247 Symbol res_weight_;
248 Symbol res_prio_;
249 HeuristicType res_type_ = HeuristicType::level;
250};
251
253class StmEdge : public Stm {
254 public:
257 : src_{std::move(src)}, dst_{std::move(dst)}, body_{std::move(body)}, node_{node} {
258 init_();
259 }
260
261 private:
262 void init_();
263 // Stm interface
264 void do_print(std::ostream &out) const override;
265 [[nodiscard]] auto do_body() const -> ULitVec const & override;
266 [[nodiscard]] auto do_important() const -> VariableSet override;
267
268 // InstanceCallback interface
269 void do_print_head(std::ostream &out) const override;
270 void do_init(size_t gen) override;
271 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
272 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
273 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
274 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
275
276 UTerm src_;
277 UTerm dst_;
278 ULitVec body_;
279 ProfileNodeInternal *node_;
280 Symbol res_src_;
281 Symbol res_dst_;
282};
283
285class StmShow : public Stm {
286 public:
289 : term_{std::move(term)}, body_{std::move(body)}, node_{node} {
290 init_();
291 }
292
293 private:
294 void init_();
295 // Stm interface
296 void do_print(std::ostream &out) const override;
297 [[nodiscard]] auto do_body() const -> ULitVec const & override;
298 [[nodiscard]] auto do_important() const -> VariableSet override;
299
300 // InstanceCallback interface
301 void do_print_head(std::ostream &out) const override;
302 void do_init(size_t gen) override;
303 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
304 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
305 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
306 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
307
308 UTerm term_;
309 ULitVec body_;
310 ProfileNodeInternal *node_;
311 Symbol res_term_;
312};
313
315class StmProject : public Stm {
316 public:
319 : atom_{std::move(atom)}, base_{&base}, body_{std::move(body)}, node_{node} {
320 init_();
321 }
322
323 private:
324 void init_();
325 // Stm interface
326 void do_print(std::ostream &out) const override;
327 [[nodiscard]] auto do_body() const -> ULitVec const & override;
328 [[nodiscard]] auto do_important() const -> VariableSet override;
329
330 // InstanceCallback interface
331 void do_print_head(std::ostream &out) const override;
332 void do_init(size_t gen) override;
333 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
334 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
335 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
336 [[nodiscard]] auto do_is_important([[maybe_unused]] size_t index) const -> bool override { return false; }
337 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
338
339 UTerm atom_;
340 AtomBase *base_;
341 ULitVec body_;
342 ProfileNodeInternal *node_;
343 size_t offset_ = 0;
344};
345
347
348} // namespace CppClingo::Ground
An atom base used to store derivable atoms and associated state.
Definition base.hh:212
Context object to capture state used during instantiation.
Definition instantiator.hh:39
Callbacks to notify statements during instantiations.
Definition instantiator.hh:111
Helper class to prepare statements for grounding.
Definition statement.hh:48
void start(Queue &queue)
Indicate that a new domain is being prepared.
void prepare(InstanceCallback &cb, ULitVec const &body, VariableSet important)
Prepare a statement for grounding.
Linearizer(std::pmr::monotonic_buffer_resource &mbr)
Construct the linearizer.
Definition statement.hh:51
Profile node that can hold children.
Definition profile.hh:155
A queue to process instantiators.
Definition instantiator.hh:222
A statement edge directives.
Definition statement.hh:253
StmEdge(UTerm src, UTerm dst, ULitVec body, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:256
A statement capturing normal rules and integrity constraints.
Definition statement.hh:125
StmExternal(Ground::UTerm atom, AtomBase &base, std::vector< size_t > indices, ULitVec body, std::optional< std::pair< Location, UTerm > > type, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:128
Statement capturing heuristic directives.
Definition statement.hh:209
StmHeuristic(UTerm atom, AtomBase &base, ULitVec body, Location loc_weight, UTerm weight, std::optional< std::pair< Location, UTerm > > prio, Location loc_type, UTerm type, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:212
Statement capturing project directives.
Definition statement.hh:315
StmProject(UTerm atom, AtomBase &base, ULitVec body, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:318
A statement capturing normal rules and integrity constraints.
Definition statement.hh:82
StmRule(AtomSimple head, ULitVec body, RuleType type, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:85
A statement for show directives.
Definition statement.hh:285
StmShow(UTerm term, ULitVec body, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:288
A statement capturing weak constraints.
Definition statement.hh:170
StmWeakConstraint(Location loc_weight, UTerm weight, std::optional< std::pair< Location, UTerm > > prio, UTermVec terms, ULitVec body, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:173
Base class for groundable statements.
Definition statement.hh:17
auto important() const -> VariableSet
Get the important variables in the statement.
Definition statement.hh:28
friend auto operator<<(std::ostream &out, Stm const &stm) -> std::ostream &
Print the statement.
Definition statement.hh:31
auto body() const -> ULitVec const &
Get the body of the statement.
Definition statement.hh:20
The Location of an expression in an input source.
Definition location.hh:44
Interface to output statements.
Definition output.hh:90
A store for symbols.
Definition symbol.hh:454
static auto sup() noexcept -> Symbol
Construct the infimum constant (#inf).
std::vector< Symbol > SymbolVec
A vector of symbols.
Definition symbol.hh:220
HeuristicType
Available heuristic types.
Definition core.hh:172
ExternalType
Available external types.
Definition core.hh:177
Util::ordered_set< size_t > VariableSet
A set of variables.
Definition base.hh:19
std::optional< std::tuple< Ground::UTerm, AtomBase &, std::vector< size_t > > > AtomSimple
Represents a simple head literal which is either represented by a symbol term or #false captured by s...
Definition literal.hh:231
std::vector< ULit > ULitVec
A vector of literals.
Definition literal.hh:31
std::vector< UStm > UStmVec
A vector of statements.
Definition statement.hh:45
std::unique_ptr< Stm > UStm
A unique pointer holding a statement.
Definition statement.hh:43
RuleType
Enumeration of available rule types.
Definition statement.hh:76
std::unique_ptr< Term > UTerm
A unique pointer holding a term.
Definition term.hh:34
std::vector< UTerm > UTermVec
A vector of terms.
Definition term.hh:36