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, EstimateFunction fun, EstimateSelector sel)
52 : mbr_{&mbr}, fun_{fun}, sel_{sel} {}
54 void start(Queue &queue);
57
58 private:
60 void build_(ULitVec const &lits);
62 auto order_(InstanceCallback &cb, std::vector<MatcherType> const &todo, VariableSet const &important,
63 ULitVec const &lits) -> std::pair<Instantiator, std::optional<size_t>>;
64
65 Queue *iqueue_ = nullptr;
66 std::pmr::monotonic_buffer_resource *mbr_;
67 std::vector<size_t> rec_;
68 std::vector<std::vector<MatcherType>> todos_;
69 std::vector<std::tuple<size_t, size_t, double>> queue_;
71 std::vector<std::tuple<size_t, std::vector<size_t>, std::vector<size_t>>> lit_map_;
73 std::vector<std::vector<size_t>> var_map_;
76};
77
79enum class RuleType : uint8_t {
80 normal,
81 choice
82};
83
85class StmRule : public Stm {
86 public:
89 : head_{head ? std::move(std::get<0>(*head)) : nullptr}, node_{node},
90 base_{head ? &std::get<1>(*head) : nullptr},
91 indices_{head ? std::move(std::get<2>(*head)) : std::vector<size_t>{}}, body_{std::move(body)}, type_{type} {
92 init_();
93 }
94
95 private:
96 void init_();
97
98 // Stm interface
99 void do_print(std::ostream &out) const override;
100 [[nodiscard]] auto do_body() const -> ULitVec const & override;
101 [[nodiscard]] auto do_important() const -> VariableSet override;
102
103 // InstanceCallback interface
104 void do_print_head(std::ostream &out) const override;
105 void do_init(size_t gen) override;
106 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
107 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
108 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
109 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
110
114 UTerm head_;
115 ProfileNodeInternal *node_ = nullptr;
116 AtomBase *base_;
121 std::vector<size_t> indices_;
122 ULitVec body_;
123 Symbol atom_ = SymbolStore::sup();
124 RuleType type_;
125};
126
128class StmExternal : public Stm {
129 public:
131 StmExternal(Ground::UTerm atom, AtomBase &base, std::vector<size_t> indices, ULitVec body,
132 std::optional<std::pair<Location, UTerm>> type, ProfileNodeInternal *node)
133 : type_{std::move(type)}, atom_{std::move(atom)}, base_{&base}, indices_{std::move(indices)},
134 body_{std::move(body)}, node_{node} {
135 init_();
136 }
137
138 private:
139 void init_();
140
141 // Stm interface
142 void do_print(std::ostream &out) const override;
143 [[nodiscard]] auto do_body() const -> ULitVec const & override;
144 [[nodiscard]] auto do_important() const -> VariableSet override;
145
146 // InstanceCallback interface
147 void do_print_head(std::ostream &out) const override;
148 void do_init(size_t gen) override;
149 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
150 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
151 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
152 [[nodiscard]] auto do_is_important([[maybe_unused]] size_t index) const -> bool override { return false; }
153 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
154
155 std::optional<std::pair<Location, UTerm>> type_;
159 UTerm atom_;
160 AtomBase *base_;
165 std::vector<size_t> indices_;
166 ULitVec body_;
167 Symbol res_atom_ = SymbolStore::sup();
168 ProfileNodeInternal *node_;
169 ExternalType res_type_ = ExternalType::free;
170};
171
173class StmWeakConstraint : public Stm {
174 public:
176 StmWeakConstraint(Location loc_weight, UTerm weight, std::optional<std::pair<Location, UTerm>> prio, UTermVec terms,
178 : loc_weight_{std::move(loc_weight)}, loc_prio_{prio ? std::move(prio->first) : loc_weight_},
179 weight_{std::move(weight)}, prio_{prio ? std::move(prio->second) : nullptr}, terms_{std::move(terms)},
180 body_{std::move(body)}, node_{node} {
181 init_();
182 }
183
184 private:
185 void init_();
186 // Stm interface
187 void do_print(std::ostream &out) const override;
188 [[nodiscard]] auto do_body() const -> ULitVec const & override;
189 [[nodiscard]] auto do_important() const -> VariableSet override;
190
191 // InstanceCallback interface
192 void do_print_head(std::ostream &out) const override;
193 void do_init(size_t gen) override;
194 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
195 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
196 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
197 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
198
199 Location loc_weight_;
200 Location loc_prio_;
201 UTerm weight_;
202 UTerm prio_;
203 UTermVec terms_;
204 ULitVec body_;
205 Symbol res_weight_;
206 std::optional<Symbol> res_prio_;
207 SymbolVec res_terms_;
208 ProfileNodeInternal *node_;
209};
210
212class StmHeuristic : public Stm {
213 public:
215 StmHeuristic(UTerm atom, AtomBase &base, ULitVec body, Location loc_weight, UTerm weight,
216 std::optional<std::pair<Location, UTerm>> prio, Location loc_type, UTerm type,
218 : loc_weight_{std::move(loc_weight)}, loc_prio_{prio ? std::move(prio->first) : loc_weight_},
219 loc_type_{std::move(loc_type)}, atom_{std::move(atom)}, base_{&base}, weight_{std::move(weight)},
220 prio_{prio ? std::move(prio->second) : nullptr}, type_{std::move(type)}, body_{std::move(body)}, node_{node} {
221 init_();
222 }
223
224 private:
225 void init_();
226 // Stm interface
227 void do_print(std::ostream &out) const override;
228 [[nodiscard]] auto do_body() const -> ULitVec const & override;
229 [[nodiscard]] auto do_important() const -> VariableSet override;
230
231 // InstanceCallback interface
232 void do_print_head(std::ostream &out) const override;
233 void do_init(size_t gen) override;
234 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
235 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
236 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
237 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
238
239 Location loc_weight_;
240 Location loc_prio_;
241 Location loc_type_;
242 UTerm atom_;
243 AtomBase *base_;
244 UTerm weight_;
245 UTerm prio_;
246 UTerm type_;
247 ULitVec body_;
248 ProfileNodeInternal *node_;
249 size_t offset_ = 0;
250 Symbol res_weight_;
251 Symbol res_prio_;
252 HeuristicType res_type_ = HeuristicType::level;
253};
254
256class StmEdge : public Stm {
257 public:
260 : src_{std::move(src)}, dst_{std::move(dst)}, body_{std::move(body)}, node_{node} {
261 init_();
262 }
263
264 private:
265 void init_();
266 // Stm interface
267 void do_print(std::ostream &out) const override;
268 [[nodiscard]] auto do_body() const -> ULitVec const & override;
269 [[nodiscard]] auto do_important() const -> VariableSet override;
270
271 // InstanceCallback interface
272 void do_print_head(std::ostream &out) const override;
273 void do_init(size_t gen) override;
274 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
275 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
276 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
277 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
278
279 UTerm src_;
280 UTerm dst_;
281 ULitVec body_;
282 ProfileNodeInternal *node_;
283 Symbol res_src_;
284 Symbol res_dst_;
285};
286
288class StmShow : public Stm {
289 public:
292 : term_{std::move(term)}, body_{std::move(body)}, node_{node} {
293 init_();
294 }
295
296 private:
297 void init_();
298 // Stm interface
299 void do_print(std::ostream &out) const override;
300 [[nodiscard]] auto do_body() const -> ULitVec const & override;
301 [[nodiscard]] auto do_important() const -> VariableSet override;
302
303 // InstanceCallback interface
304 void do_print_head(std::ostream &out) const override;
305 void do_init(size_t gen) override;
306 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
307 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
308 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
309 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
310
311 UTerm term_;
312 ULitVec body_;
313 ProfileNodeInternal *node_;
314 Symbol res_term_;
315};
316
318class StmProject : public Stm {
319 public:
322 : atom_{std::move(atom)}, base_{&base}, body_{std::move(body)}, node_{node} {
323 init_();
324 }
325
326 private:
327 void init_();
328 // Stm interface
329 void do_print(std::ostream &out) const override;
330 [[nodiscard]] auto do_body() const -> ULitVec const & override;
331 [[nodiscard]] auto do_important() const -> VariableSet override;
332
333 // InstanceCallback interface
334 void do_print_head(std::ostream &out) const override;
335 void do_init(size_t gen) override;
336 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
337 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
338 [[nodiscard]] auto do_priority() const -> size_t override { return std::numeric_limits<size_t>::max(); }
339 [[nodiscard]] auto do_is_important([[maybe_unused]] size_t index) const -> bool override { return false; }
340 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
341
342 UTerm atom_;
343 AtomBase *base_;
344 ULitVec body_;
345 ProfileNodeInternal *node_;
346 size_t offset_ = 0;
347};
348
350
351} // 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:41
Callbacks to notify statements during instantiations.
Definition instantiator.hh:113
Helper class to prepare statements for grounding.
Definition statement.hh:48
Linearizer(std::pmr::monotonic_buffer_resource &mbr, EstimateFunction fun, EstimateSelector sel)
Construct the linearizer.
Definition statement.hh:51
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.
Profile node that can hold children.
Definition profile.hh:156
A queue to process instantiators.
Definition instantiator.hh:225
A statement edge directives.
Definition statement.hh:256
StmEdge(UTerm src, UTerm dst, ULitVec body, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:259
A statement capturing normal rules and integrity constraints.
Definition statement.hh:128
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:131
Statement capturing heuristic directives.
Definition statement.hh:212
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:215
Statement capturing project directives.
Definition statement.hh:318
StmProject(UTerm atom, AtomBase &base, ULitVec body, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:321
A statement capturing normal rules and integrity constraints.
Definition statement.hh:85
StmRule(AtomSimple head, ULitVec body, RuleType type, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:88
A statement for show directives.
Definition statement.hh:288
StmShow(UTerm term, ULitVec body, ProfileNodeInternal *node)
Construct the statement.
Definition statement.hh:291
A statement capturing weak constraints.
Definition statement.hh:173
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:176
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:82
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
EstimateSelector
Selector for which estimates to consider.
Definition core.hh:204
HeuristicType
Available heuristic types.
Definition core.hh:172
EstimateFunction
Function to combine domain size estimates.
Definition core.hh:197
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:250
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:79
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