Clingo
Loading...
Searching...
No Matches
ground.hh
1#pragma once
2
3#include <clingo/base.hh>
4#include <clingo/core.hh>
5#include <clingo/stats.hh>
6
7#include <clingo/ground.h>
8
9namespace Clingo {
10
14
16struct Part {
21 constexpr Part(std::string name, SymbolVector params = {}) : name{std::move(name)}, params(std::move(params)) {
22 assert(!this->name.empty());
23 }
24
26 std::string name;
29};
31using PartSpan = std::span<Part const>;
33using PartList = std::initializer_list<Part>;
35using PartVector = std::vector<Part>;
36
43
46 public:
48 GroundEventHandler() = default;
50 virtual ~GroundEventHandler() = default;
51
56 auto callable(std::string_view name, size_t args) -> bool { return do_callable(name, args); }
57
64 auto call(Location const &loc, std::string_view name, SymbolSpan args) -> SymbolVector {
65 return do_call(loc, name, args);
66 }
67
69 void finish(GroundResult result) noexcept { do_finish(result); }
70
71 private:
72 virtual auto do_callable([[maybe_unused]] std::string_view name, [[maybe_unused]] size_t args) -> bool {
73 return false;
74 }
75 virtual auto do_call([[maybe_unused]] Location const &loc, [[maybe_unused]] std::string_view name,
77 return {};
78 }
79 virtual void do_finish([[maybe_unused]] GroundResult result) noexcept {}
80};
81
84 public:
90 explicit GroundHandle(clingo_ground_handle_t *hnd) : hnd_{hnd} {}
91
95 friend auto c_cast(GroundHandle const &x) -> clingo_ground_handle_t * { return x.hnd_.get(); }
96
103 [[nodiscard]] auto get() const -> GroundResult {
104 return GroundResult{Detail::call<clingo_ground_handle_get>(hnd_.get())};
105 }
106
110 void cancel() { Detail::handle_error(clingo_ground_handle_cancel(hnd_.get())); }
111
115 void close() noexcept { hnd_.reset(); }
116
125 [[nodiscard]] auto wait(std::optional<double> timeout) -> bool {
126 return Detail::call<clingo_ground_handle_wait>(hnd_.get(), timeout.value_or(-1));
127 }
128
129 private:
130 struct Del {
131 void operator()(clingo_ground_handle_t *hnd) const noexcept { clingo_ground_handle_close(hnd); }
132 };
133 using Ptr = std::unique_ptr<clingo_ground_handle_t, Del>;
134
135 Ptr hnd_;
136};
137
139
140} // namespace Clingo
An interface for handling grounding events.
Definition ground.hh:45
auto callable(std::string_view name, size_t args) -> bool
Check whether a function is callable for the given number of arguments.
Definition ground.hh:56
virtual ~GroundEventHandler()=default
Default destructor.
void finish(GroundResult result) noexcept
Notify the handler that grounding has finished with the given result.
Definition ground.hh:69
GroundEventHandler()=default
Default constructor.
auto call(Location const &loc, std::string_view name, SymbolSpan args) -> SymbolVector
Call the given function with the given arguments.
Definition ground.hh:64
Class to control a running ground call.
Definition ground.hh:83
void close() noexcept
Closes the solve handle.
Definition ground.hh:115
GroundHandle(clingo_ground_handle_t *hnd)
Constructor from the underlying C representation.
Definition ground.hh:90
auto wait(std::optional< double > timeout) -> bool
Wait for a running ground call.
Definition ground.hh:125
void cancel()
Cancel the current search.
Definition ground.hh:110
friend auto c_cast(GroundHandle const &x) -> clingo_ground_handle_t *
Cast the solve handle to its C representation.
Definition ground.hh:95
auto get() const -> GroundResult
Get the solve result.
Definition ground.hh:103
Class representing a range in a file.
Definition core.hh:749
struct clingo_ground_handle clingo_ground_handle_t
Handle to an asynchronous ground call.
Definition ground.h:136
unsigned clingo_ground_result_t
Corresponding type to clingo_ground_result_e.
Definition ground.h:59
CLINGO_VISIBILITY_DEFAULT void clingo_ground_handle_close(clingo_ground_handle_t *handle)
Release the ground handle.
CLINGO_VISIBILITY_DEFAULT bool clingo_ground_handle_cancel(clingo_ground_handle_t *handle)
Stop the running ground call and block until done.
@ clingo_ground_result_ok
Grounding completed successfully.
Definition ground.h:54
@ clingo_ground_result_unsatisfiable
Grounding detected unsatisfiability.
Definition ground.h:55
@ clingo_ground_result_interrupted
Grounding was interrupted.
Definition ground.h:56
std::vector< Part > PartVector
A vector of program parts.
Definition ground.hh:35
std::span< Part const > PartSpan
A span of program parts.
Definition ground.hh:31
GroundResult
Enumeration of the results of a grounding process.
Definition ground.hh:38
std::initializer_list< Part > PartList
An initializer list of program parts.
Definition ground.hh:33
@ ok
Grounding completed successfully.
@ unsatisfiable
An inconsistency was detected during grounding.
@ interrupted
Grounding was interrupted.
std::vector< Symbol > SymbolVector
A vector of symbols.
Definition symbol.hh:42
std::span< Symbol const > SymbolSpan
A span of symbols, which is a view on a contiguous sequence of symbols.
Definition symbol.hh:38
A program part to provide inputs to program directives.
Definition ground.hh:16
constexpr Part(std::string name, SymbolVector params={})
Constructs a program part with the given name and parameters.
Definition ground.hh:21
std::string name
The name of the part.
Definition ground.hh:26
SymbolVector params
The parameters of the part.
Definition ground.hh:28