Clingo
Loading...
Searching...
No Matches
solver.hh
1#pragma once
2
3#include <clingo/control/config.hh>
4#include <clingo/control/grounder.hh>
5
6#include <clingo/output/backend.hh>
7
8#include <potassco/basic_types.h>
9
10#include <clasp/clasp_facade.h>
11#include <clasp/cli/clasp_options.h>
12
13namespace CppClingo::Control {
14
17
18class Solver;
19
24 public:
26 void main(Solver &slv) { do_main(slv); }
28 void exec(std::string_view code) { do_exec(code); }
29
30 private:
31 virtual void do_exec(std::string_view code) = 0;
32 virtual void do_main(Solver &slv) = 0;
33};
35using UScript = std::unique_ptr<Script>;
36
42 public:
44 void register_script(std::string_view name, UScript script);
46 void main(Solver &slv);
47
48 private:
49 void do_exec(Location const &loc, Logger &log, std::string_view name, std::string_view code) override;
50 auto do_callable(std::string_view name, size_t args) -> bool override;
51 void do_call(Location const &loc, std::string_view name, SymbolSpan args, SymbolVec &out) override;
52
53 std::vector<std::pair<std::string, UScript>> scripts_;
54};
55
57enum class IStop : uint8_t {
58 none,
59 sat,
60 unsat,
61 unknown
62};
63
65enum class AppMode : uint8_t {
66 parse,
67 rewrite,
68 ground,
69 solve
70};
71
73enum class BackendType : uint8_t {
74 clasp,
75 aspif,
76 smodels,
77 reify
78};
79
81enum class ReifyFlags : uint8_t {
82 reify_scc = 1U,
83 reify_step = 2U
84};
87
111
113enum class SymbolSelectFlags : uint8_t {
114 none = 0,
115 shown = 1,
116 atoms = 2,
117 terms = 4,
118 theory = 8,
119 all = 15,
120};
123
125enum class ModelType : uint8_t {
126 model = 0,
129};
130
132enum class ConsequenceType : uint8_t {
133 false_ = 0,
134 true_ = 1,
135 unknown = 2
136};
137
140 public:
143
152 template <class F> auto add(Symbol sym, F &&fun) -> prg_id_t {
153 auto [it, ins] = map_.emplace(sym, 0);
154 if (ins) {
155 it.value() = std::invoke(std::forward<F>(fun));
156 }
157 return it.value();
158 }
159
166 void add(Symbol sym, prg_id_t id) {
167 if (!map_.emplace(sym, id).second) {
168 throw std::runtime_error("collision of term ids");
169 }
170 }
171
176 [[nodiscard]] auto term_id(size_t i) const -> prg_id_t {
177 if (i < size()) {
178 return map_.nth(i).value();
179 }
180 throw std::range_error{"index out of range"};
181 }
182
187 [[nodiscard]] auto symbol(size_t i) const -> Symbol {
188 if (i < size()) {
189 return *map_.nth(i).key();
190 }
191 throw std::range_error{"index out of range"};
192 }
193
201 [[nodiscard]] auto index(Symbol sym) const -> size_t { return map_.find(sym) - map_.begin(); }
202
206 [[nodiscard]] auto size() const -> size_t { return map_.size(); }
207
212 [[nodiscard]] auto begin() const -> Map::const_iterator { return map_.cbegin(); }
213
218 [[nodiscard]] auto end() const -> Map::const_iterator { return map_.cend(); }
219
220 private:
222};
223
225class BaseView {
226 public:
228 virtual ~BaseView() = default;
230 [[nodiscard]] auto bases() const -> Ground::Bases const & { return do_bases(); }
232 [[nodiscard]] auto term_base() const -> TermBaseMap const & { return do_term_base(); }
234 [[nodiscard]] auto clasp_program() const -> Clasp::Asp::LogicProgram const & { return do_clasp_program(); }
236 [[nodiscard]] auto clasp_theory() const -> Potassco::TheoryData const & { return clasp_program().theoryData(); }
237
238 private:
239 [[nodiscard]] virtual auto do_bases() const -> Ground::Bases const & = 0;
240 [[nodiscard]] virtual auto do_term_base() const -> TermBaseMap const & = 0;
241 [[nodiscard]] virtual auto do_clasp_program() const -> Clasp::Asp::LogicProgram const & = 0;
242};
243
245class SolveControl : public BaseView {
246 public:
248 void add_clause(PrgLitSpan lits) { do_add_clause(lits); }
249
250 private:
251 virtual void do_add_clause(PrgLitSpan lits) = 0;
252};
253
255class Model {
256 public:
257 virtual ~Model() = default;
258
263 void symbols(SymbolSelectFlags type, SymbolVec &res) const { do_symbols(type, res); }
267 [[nodiscard]] auto number() const -> uint64_t { return do_number(); }
271 [[nodiscard]] auto type() const -> ModelType { return do_type(); }
276 [[nodiscard]] auto contains(Symbol sym) const -> bool { return do_contains(sym); }
281 [[nodiscard]] auto is_true(prg_lit_t lit) const -> bool { return do_is_true(lit); }
286 [[nodiscard]] auto is_consequence(prg_lit_t lit) const -> ConsequenceType { return do_is_consequence(lit); }
290 [[nodiscard]] auto costs() const -> std::span<prg_sum_t const> { return do_costs(); }
294 [[nodiscard]] auto priorities() const -> std::span<prg_weight_t const> { return do_priorities(); }
298 [[nodiscard]] auto optimality_proven() const -> bool { return do_optimality_proven(); }
302 [[nodiscard]] auto thread_id() const -> prg_id_t { return do_thread_id(); }
306 [[nodiscard]] auto context() -> SolveControl & { return do_control(); }
307
309 virtual void extend(SymbolSpan symbols) { do_extend(symbols); }
310
311 private:
312 virtual void do_symbols(SymbolSelectFlags type, SymbolVec &res) const = 0;
313 [[nodiscard]] virtual auto do_number() const -> uint64_t = 0;
314 [[nodiscard]] virtual auto do_type() const -> ModelType = 0;
315 [[nodiscard]] virtual auto do_contains(Symbol sym) const -> bool = 0;
316 virtual void do_extend(SymbolSpan symbols) = 0;
317 [[nodiscard]] virtual auto do_is_true(prg_lit_t lit) const -> bool = 0;
318 [[nodiscard]] virtual auto do_is_consequence(prg_lit_t lit) const -> ConsequenceType = 0;
319 [[nodiscard]] virtual auto do_costs() const -> std::span<prg_sum_t const> = 0;
320 [[nodiscard]] virtual auto do_priorities() const -> std::span<prg_weight_t const> = 0;
321 [[nodiscard]] virtual auto do_optimality_proven() const -> bool = 0;
322 [[nodiscard]] virtual auto do_thread_id() const -> prg_id_t = 0;
323 [[nodiscard]] virtual auto do_control() -> SolveControl & = 0;
324};
326using UModel = std::unique_ptr<Model>;
327
332enum class SolveResult : uint8_t {
333 empty = 0,
334 satisfiable = 1,
335 unsatisfiable = 2,
336 exhausted = 4,
337 interrupted = 8,
338};
341
344 public:
346 virtual ~SolveHandle() = default;
347
353 auto get() -> SolveResult { return do_get(); }
357 void cancel() { do_cancel(); }
363 void resume() { do_resume(); }
373 auto model() -> Model const * { return do_model(); }
379 auto last() -> Model const * { return do_last(); }
386 auto core() -> PrgLitSpan { return do_core(); }
396 auto wait(double timeout) -> bool { return do_wait(timeout); }
397
398 private:
399 virtual auto do_get() -> SolveResult = 0;
400 virtual void do_cancel() = 0;
401 virtual void do_resume() = 0;
402 virtual auto do_model() -> Model const * = 0;
403 virtual auto do_last() -> Model const * = 0;
404 virtual auto do_core() -> PrgLitSpan = 0;
405 virtual auto do_wait(double timeout) -> bool = 0;
406};
408using USolveHandle = std::unique_ptr<SolveHandle>;
409
412 public:
413 virtual ~SolveEventHandler() = default;
414
421 auto on_model(Model &mdl) -> bool { return do_on_model(mdl); }
429 void on_stats(Potassco::AbstractStatistics &stats) { do_on_stats(stats); }
436 void on_unsat(Clasp::SumView bound) { do_on_unsat(bound); }
441 void on_core(Potassco::LitSpan core) { do_on_core(core); }
448 void on_finish(SolveResult result) { do_on_finish(result); }
449
450 private:
451 virtual auto do_on_model([[maybe_unused]] Model &mdl) -> bool { return true; }
452 virtual void do_on_stats([[maybe_unused]] Potassco::AbstractStatistics &stats) {}
453 virtual void do_on_unsat([[maybe_unused]] Clasp::SumView bound) {}
454 virtual void do_on_core([[maybe_unused]] Potassco::LitSpan core) {}
455 virtual void do_on_finish([[maybe_unused]] SolveResult result) {}
456};
458using USolveEventHandler = std::unique_ptr<SolveEventHandler>;
459
463enum class SolveMode : uint8_t {
464 none = 0,
465 async = 1,
466 yield = 2,
467};
470
484 public:
488 [[nodiscard]] auto program() -> Clasp::Asp::LogicProgram & { return do_program(); }
492 [[nodiscard]] auto theory() -> Output::TheoryData & { return do_theory(); }
496 [[nodiscard]] auto store() -> SymbolStore & { return do_store(); }
503 [[nodiscard]] auto add_atom(Symbol atom) -> prg_lit_t { return do_add_atom(atom); }
508 void close() { do_close(); }
510 virtual ~BackendHandle() = default;
511
512 private:
513 virtual auto do_program() -> Clasp::Asp::LogicProgram & = 0;
514 virtual auto do_theory() -> Output::TheoryData & = 0;
515 virtual auto do_store() -> SymbolStore & = 0;
516 virtual auto do_add_atom(Symbol atom) -> prg_lit_t = 0;
517 virtual void do_close() = 0;
518};
520using UBackendHandle = std::unique_ptr<BackendHandle>;
521
523class Propagator : public Potassco::AbstractPropagator, public Potassco::AbstractHeuristic {
524 public:
526 [[nodiscard]] virtual auto hasHeuristic() const -> bool = 0;
527};
529using UPropagator = std::unique_ptr<Propagator>;
530
536 public:
538 void lock() {
539 if (mut_) {
540 mut_->lock();
541 }
542 }
544 void unlock() {
545 if (mut_) {
546 mut_->unlock();
547 }
548 }
550 void enable(bool state) {
551 if (!state) {
552 mut_.reset();
553 } else if (!mut_) {
554 mut_.emplace();
555 mut_->lock();
556 }
557 }
558
559 private:
560 std::optional<std::mutex> mut_;
561};
562
564template <class M> class unlock_guard {
565 public:
567 explicit unlock_guard(M &mut) : mut_{&mut} { mut_->unlock(); }
569 unlock_guard(const unlock_guard &) = delete;
570 ~unlock_guard() { mut_->lock(); }
571 auto operator=(const unlock_guard &) -> unlock_guard & = delete;
572
573 private:
574 M *mut_;
575};
576
579 public:
581 void init(CppClingo::Control::BaseView &view, std::ostream &out);
585 void end_step();
587 auto out() -> std::ostream & { return *out_; }
588
589 private:
590 struct State {
591 State() = default;
592 size_t atom : 1 = 0;
593 size_t term : 1 = 0;
594 size_t index : (8 * sizeof(size_t)) - 2 = 0;
595 };
596
597 auto output(CppClingo::Symbol const &sym) -> State &;
598
599 CppClingo::Control::BaseView *view_ = nullptr;
600 std::ostream *out_ = nullptr;
601 size_t ids_ = 0;
602 std::vector<size_t> buf_;
604};
606using USymbolTable = std::unique_ptr<SymbolTable>;
607
609// TODO: Simplify/Move on next output refactoring
610class Grounded : public Clasp::Event {
611 public:
613 explicit Grounded(Input::ProgramParamVec const &params)
614 : Event(this, subsystem_load, verbosity_quiet), params(params) {}
616 std::span<Input::ProgramParamVec::value_type const> params;
617};
618
619class Grounder;
620
626 public:
633 void finish(GroundResult result) noexcept { do_finish(result); }
634
635 private:
636 virtual void do_finish([[maybe_unused]] GroundResult result) noexcept {}
637};
638
640using UGroundEventHandler = std::unique_ptr<GroundEventHandler>;
641
646 public:
653 ~GroundHandle() noexcept;
654
656 GroundHandle(GroundHandle const &other) = delete;
658 GroundHandle(GroundHandle &&other) noexcept;
660 auto operator=(GroundHandle &&other) noexcept -> GroundHandle &;
662 auto operator=(GroundHandle const &other) -> GroundHandle & = delete;
663
674 [[nodiscard]] auto wait(double timeout) -> bool;
679 [[nodiscard]] auto get() -> GroundResult;
683 void cancel();
684
685 private:
686 class Impl;
687 std::unique_ptr<Impl> impl_;
688};
689
693class Solver : public BaseView {
694 public:
695 Solver(Solver const &) = delete;
696 Solver(Solver &&) = delete;
697 auto operator=(Solver const &) -> Solver & = delete;
698 auto operator=(Solver &&) -> Solver & = delete;
699
701 Solver(Clasp::ClaspFacade &clasp, Clasp::Cli::ClaspCliConfig &config, Logger &log, SymbolStore &store,
702 Scripts &scripts, SolverOptions opts, FILE *out = stdout);
703
705 void main(std::span<std::string_view const> const &files);
707 void main();
708
717 void parse(std::string_view str);
719 void parse(std::span<std::string_view const> const &files);
722
724 void add_const(String name, Symbol value);
726 [[nodiscard]] auto const_map() -> Input::ConstMap const &;
728 [[nodiscard]] auto start_ground(ProgramParamVec params, UGroundEventHandler handler) -> GroundHandle;
730 auto ground(ProgramParamVec const &params, Ground::ScriptCallback *ctx, Util::StopFlag *stop = nullptr)
731 -> GroundResult;
738 auto solve(USolveEventHandler handler = {}, PrgLitSpan assumptions = {}, SolveMode mode = SolveMode::none)
739 -> USolveHandle;
740
742 void output_unprocessed_program(std::ostream &out);
743
745 void output_program(std::ostream &out);
746
748 auto map_model(Clasp::Model const &mdl) -> Model &;
749
754 [[nodiscard]] auto buf() -> Util::OutputBuffer & { return stream_.buffer(); }
755
759 [[nodiscard]] auto stream() -> Util::OutputStream & { return stream_; }
760
765 [[nodiscard]] auto backend() -> UBackendHandle;
766
768 [[nodiscard]] auto clasp_facade() -> Clasp::ClaspFacade & { return *clasp_; }
769
771 [[nodiscard]] auto clasp_facade() const -> Clasp::ClaspFacade const & { return *clasp_; }
772
774 [[nodiscard]] auto config() -> ClingoConfig & { return config_; }
775
777 [[nodiscard]] auto clasp_stats() -> Potassco::AbstractStatistics const & {
778 auto const *stats = clasp_->getStats();
779 return stats != nullptr ? *stats : throw std::runtime_error("not in solving mode");
780 }
781
786
788 auto get_lock() -> CallbackLock & { return lock_; }
789
791 void block_main(bool block) { block_main_ = block; }
792
794 [[nodiscard]] auto get_mode() const -> AppMode { return opts_.mode; }
795
797 auto user_data() -> void *& { return data_; }
798
800 void interrupt() noexcept;
801
803 [[nodiscard]] auto get_parts() -> std::optional<Input::StmParts> const & { return grd_.get_parts(); }
804
806 void set_parts(std::optional<Input::StmParts> parts) { grd_.set_parts(std::move(parts)); }
809 auto pos = Position{*grd_.store().string("<cmd>"), 1, 1};
810 auto loc = Location{pos, pos};
811 grd_.set_parts(std::make_optional<Input::StmParts>(loc, Input::Precedence::override_, std::move(parts)));
812 }
813
815 void show(Input::SharedSig const &sig) { grd_.show(sig); }
816
818 auto sym_tab() -> SymbolTable & {
819 if (!sym_tab_) {
820 sym_tab_ = std::make_unique<SymbolTable>();
821 }
822 return *sym_tab_;
823 }
824
828 auto print_summary(bool final) { grd_.print_summary(final); }
829
831 void accept(Ground::ProfileNode::Visitor const &visit) const { grd_.accept(visit); }
832
833 private:
834 class AspifBackend;
835
843 enum class State : uint8_t {
844 initial, //< initial step
845 grounded, //< step has been grounded
846 prepared, //< step is prepared for solving
847 solved, //< step has been solved
848 };
849
859 auto make_output_(SymbolStore &store, AppMode mode, BackendType backend_type, ReifyFlags reify_flags) -> UOutputStm;
860
862 void prepare_();
863
865 void simplify_();
866
868 void enable_updates_();
869
870 [[nodiscard]] auto do_bases() const -> Ground::Bases const & override { return grd_.base(); }
871
872 [[nodiscard]] auto do_term_base() const -> TermBaseMap const & override { return terms_; }
873
874 [[nodiscard]] auto do_clasp_program() const -> Clasp::Asp::LogicProgram const & override {
875 return clasp_->asp() != nullptr ? *clasp_->asp() : throw std::runtime_error("not in solving mode");
876 }
877
878 void incmode_();
879
880 CallbackLock lock_;
881 std::vector<UPropagator> propagators_;
882 TermBaseMap terms_;
883 Clasp::ClaspFacade *clasp_;
884 ClingoConfig config_;
885 Util::OutputStream stream_;
886 UProgramBackend backend_;
887 std::unique_ptr<Output::TheoryData> theory_;
888 std::unique_ptr<Potassco::AbstractProgram> output_program_;
889 std::unique_ptr<Potassco::AbstractProgram> program_;
890 SolverOptions opts_;
891 UOutputStm out_;
892 UModel mdl_;
893 USymbolTable sym_tab_;
894 Grounder grd_;
895 Scripts *scripts_;
896 State state_ = State::initial;
897 BuiltinIncludes includes_ = BuiltinIncludes::empty;
898 void *data_ = nullptr;
899 bool block_main_ = false;
900};
901
903
904} // namespace CppClingo::Control
Object to provide access to the backend.
Definition solver.hh:483
auto store() -> SymbolStore &
The symbol store.
Definition solver.hh:496
auto theory() -> Output::TheoryData &
Get the theory.
Definition solver.hh:492
void close()
Close the handle.
Definition solver.hh:508
auto program() -> Clasp::Asp::LogicProgram &
Get the logic program.
Definition solver.hh:488
virtual ~BackendHandle()=default
Destroy the handle.
auto add_atom(Symbol atom) -> prg_lit_t
Add a literal for the given symbol.
Definition solver.hh:503
Interface providing the necessary data to inspect atom, term, and theory bases.
Definition solver.hh:225
auto clasp_theory() const -> Potassco::TheoryData const &
Get a reference to the underlying facade.
Definition solver.hh:236
virtual ~BaseView()=default
The default destructor.
auto bases() const -> Ground::Bases const &
Get a reference to the underlying atom bases.
Definition solver.hh:230
auto term_base() const -> TermBaseMap const &
Get a reference to the underlying term bases.
Definition solver.hh:232
auto clasp_program() const -> Clasp::Asp::LogicProgram const &
Get a reference to the underlying facade.
Definition solver.hh:234
This lock ensures that callbacks during solving are called in lock-step.
Definition solver.hh:535
void enable(bool state)
Enable or disable the lock.
Definition solver.hh:550
void unlock()
Release the lock.
Definition solver.hh:544
void lock()
Acquire the lock.
Definition solver.hh:538
This class provides a hierarchical configuration interface for clingo.
Definition config.hh:24
This callback interface provides grounding events.
Definition solver.hh:625
void finish(GroundResult result) noexcept
Callback to inform that the grounding has finished.
Definition solver.hh:633
A handle for asynchronous grounding.
Definition solver.hh:645
GroundHandle(Solver &solver, Input::ProgramParamVec params, UGroundEventHandler handler)
Start grounding with the given params and context.
~GroundHandle() noexcept
Destroy the ground handle.
Event emitted by a solver after the program is grounded.
Definition solver.hh:610
std::span< Input::ProgramParamVec::value_type const > params
The program parts that have been grounded.
Definition solver.hh:616
Grounded(Input::ProgramParamVec const &params)
Construct a grounded event.
Definition solver.hh:613
A grounder for logic programs.
Definition grounder.hh:26
The model class.
Definition solver.hh:255
auto contains(Symbol sym) const -> bool
Check if the model contains a (symbolic) atom.
Definition solver.hh:276
auto context() -> SolveControl &
Get the context object to control the search.
Definition solver.hh:306
auto is_true(prg_lit_t lit) const -> bool
Check if a program literal is true in a model.
Definition solver.hh:281
auto is_consequence(prg_lit_t lit) const -> ConsequenceType
Check whether the given literal is a consequence.
Definition solver.hh:286
auto number() const -> uint64_t
Get the running number of the model.
Definition solver.hh:267
auto optimality_proven() const -> bool
Check if the model corresponds to an optimal solution.
Definition solver.hh:298
auto thread_id() const -> prg_id_t
Get the solver/thread id the model was found in.
Definition solver.hh:302
auto type() const -> ModelType
Get the type of the model.
Definition solver.hh:271
auto priorities() const -> std::span< prg_weight_t const >
get the priorities of the costs.
Definition solver.hh:294
virtual void extend(SymbolSpan symbols)
Extend the model with the given symbols.
Definition solver.hh:309
void symbols(SymbolSelectFlags type, SymbolVec &res) const
Get the selected symbols in the model.
Definition solver.hh:263
auto costs() const -> std::span< prg_sum_t const >
Get the costs associated with a model.
Definition solver.hh:290
The propagator interface.
Definition solver.hh:523
virtual auto hasHeuristic() const -> bool=0
Can return false to not also register the propagator as a heuristic.
Script providing code execution, main, and callbacks.
Definition solver.hh:23
void main(Solver &slv)
Run the main function.
Definition solver.hh:26
void exec(std::string_view code)
Execute the given code.
Definition solver.hh:28
Helper to run specific code and callbacks.
Definition solver.hh:41
void register_script(std::string_view name, UScript script)
Register the given script.
void main(Solver &slv)
Run the main function.
Simple control class to add clauses while enumerating models.
Definition solver.hh:245
void add_clause(PrgLitSpan lits)
Add a clause over the given literal.
Definition solver.hh:248
The event handler interface.
Definition solver.hh:411
void on_unsat(Clasp::SumView bound)
Callback to intercept lower bounds.
Definition solver.hh:436
void on_finish(SolveResult result)
Callback to inform that the search has finished.
Definition solver.hh:448
auto on_model(Model &mdl) -> bool
Callback to intercept models.
Definition solver.hh:421
void on_core(Potassco::LitSpan core)
The unsatisfiable core of the current problem.
Definition solver.hh:441
void on_stats(Potassco::AbstractStatistics &stats)
Callback to update statistics.
Definition solver.hh:429
A handle to control a running search.
Definition solver.hh:343
auto last() -> Model const *
Get the last model after the search has finished.
Definition solver.hh:379
void resume()
Resume search after a model has been found to start search for the next one.
Definition solver.hh:363
auto wait(double timeout) -> bool
Wait for the given amount of time or until the next result is ready.
Definition solver.hh:396
auto get() -> SolveResult
Get the result of a search.
Definition solver.hh:353
auto model() -> Model const *
Get the current model or a nullptr if there is none.
Definition solver.hh:373
auto core() -> PrgLitSpan
Get a subset of the assumptions that made the problem unsatisfiable.
Definition solver.hh:386
virtual ~SolveHandle()=default
The default destructor.
void cancel()
Cancel the current search.
Definition solver.hh:357
A grounder and solver for logic programs.
Definition solver.hh:693
void parse(std::string_view str)
Parse a program from the given string.
void output_program(std::ostream &out)
Output the current program.
void join(Input::UnprocessedProgram const &prg)
Join with the given program.
void set_parts(Input::ProgramParamVec parts)
Set the program parts to ground.
Definition solver.hh:808
void main(std::span< std::string_view const > const &files)
Parse, ground, and solve a program.
auto stream() -> Util::OutputStream &
Get the output stream.
Definition solver.hh:759
auto clasp_facade() -> Clasp::ClaspFacade &
Get a pointer to the underlying clasp facade.
Definition solver.hh:768
void interrupt() noexcept
Interrupt the running (or next search).
void parse(std::span< std::string_view const > const &files)
Parse the given files.
auto backend() -> UBackendHandle
Get a handle that provides access to the backend to add atoms and rules.
void block_main(bool block)
Block execution of the main function in scripts.
Definition solver.hh:791
auto buf() -> Util::OutputBuffer &
Get the output buffer.
Definition solver.hh:754
void main()
Ground and solve a program.
void parse_with(std::function< BuiltinIncludes(ProgramBackend *, TheoryBackend *)> cb)
Parse with optional backends.
auto const_map() -> Input::ConstMap const &
Get the const map.
auto print_summary(bool final)
Print per step summaries.
Definition solver.hh:828
void join_includes(BuiltinIncludes includes)
Enable the given includes.
auto ground(ProgramParamVec const &params, Ground::ScriptCallback *ctx, Util::StopFlag *stop=nullptr) -> GroundResult
Ground the program.
auto clasp_stats() -> Potassco::AbstractStatistics const &
Get the statistics.
Definition solver.hh:777
Solver(Clasp::ClaspFacade &clasp, Clasp::Cli::ClaspCliConfig &config, Logger &log, SymbolStore &store, Scripts &scripts, SolverOptions opts, FILE *out=stdout)
Create a solver object.
auto get_lock() -> CallbackLock &
Get the solvers callback lock.
Definition solver.hh:788
void accept(Ground::ProfileNode::Visitor const &visit) const
Accept a visitor for the profile nodes.
Definition solver.hh:831
void add_const(String name, Symbol value)
Define a constant.
auto sym_tab() -> SymbolTable &
Get the symbol table.
Definition solver.hh:818
auto clasp_facade() const -> Clasp::ClaspFacade const &
Get a pointer to the underlying clasp facade.
Definition solver.hh:771
void set_parts(std::optional< Input::StmParts > parts)
Set the program parts to ground.
Definition solver.hh:806
void register_propagator(UPropagator propagator)
Register the given propagator with the control object.
auto map_model(Clasp::Model const &mdl) -> Model &
Map the given clasp model to the clingo one.
auto config() -> ClingoConfig &
Only non-null in solving mode.
Definition solver.hh:774
auto solve(USolveEventHandler handler={}, PrgLitSpan assumptions={}, SolveMode mode=SolveMode::none) -> USolveHandle
Solve the program.
void show(Input::SharedSig const &sig)
Show the given signature.
Definition solver.hh:815
void output_unprocessed_program(std::ostream &out)
Output the current unprocessed program.
auto start_ground(ProgramParamVec params, UGroundEventHandler handler) -> GroundHandle
Ground the program asynchronously.
auto user_data() -> void *&
Get user data for C integration.
Definition solver.hh:797
auto get_mode() const -> AppMode
Get the application mode.
Definition solver.hh:794
Helper to output symbols.
Definition solver.hh:578
void init(CppClingo::Control::BaseView &view, std::ostream &out)
Initialize the table before output.
void end_step()
Output atoms in extended aspif format.
auto out() -> std::ostream &
Get the underlying output stream.
Definition solver.hh:587
void begin_step()
Output ids of shown terms in extended aspif format.
Map from symbols to show term ids.
Definition solver.hh:139
auto size() const -> size_t
Get the number of mapped symbols.
Definition solver.hh:206
auto add(Symbol sym, F &&fun) -> prg_id_t
Add a new symbol to the map.
Definition solver.hh:152
auto end() const -> Map::const_iterator
Get an iterator over the symbol id pairs in the map pointing to the end of the sequence.
Definition solver.hh:218
void add(Symbol sym, prg_id_t id)
Add a symbol with the given id.
Definition solver.hh:166
auto index(Symbol sym) const -> size_t
Get the index of the symbol.
Definition solver.hh:201
auto term_id(size_t i) const -> prg_id_t
Get the id at the given index.
Definition solver.hh:176
auto begin() const -> Map::const_iterator
Get an iterator over the symbol id pairs in the map pointing to the beginning of the sequence.
Definition solver.hh:212
Util::ordered_map< SharedSymbol, prg_id_t > Map
The container storing the mapping (internal).
Definition solver.hh:142
auto symbol(size_t i) const -> Symbol
Get the symbol at the given index.
Definition solver.hh:187
RAII helper to unlock a mutex.
Definition solver.hh:564
unlock_guard(const unlock_guard &)=delete
Destructor re-locking the mutex.
unlock_guard(M &mut)
Constructor unlocking the mutex.
Definition solver.hh:567
std::function< void(std::variant< std::pair< std::string_view, bool >, std::pair< ProfileStats const *, ProfileType > >, size_t)> Visitor
The type of visitor function to use for visiting profile nodes.
Definition profile.hh:107
Interface to call functions during parsing/grounding.
Definition script.hh:28
void call(Location const &loc, std::string_view name, SymbolSpan args, SymbolVec &out)
Call the function with the given name and arguments.
Definition script.hh:35
Interface to execute code in source files.
Definition script.hh:12
Program grouping unprocessed statements.
Definition program.hh:70
The Location of an expression in an input source.
Definition location.hh:44
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Class similar to Potassco::TheoryData but with automatic id generation.
Definition backend.hh:17
A point in an input source.
Definition location.hh:15
Abstract class connecting grounder and solver.
Definition backend.hh:56
Reference to a string stored in a symbol store.
Definition symbol.hh:18
A store for symbols.
Definition symbol.hh:454
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
Abstract class connecting grounder and theory data.
Definition backend.hh:234
Create an output buffer that bears some similarities with C++'s iostreams.
Definition print.hh:26
Output stream with an underlying OutputBuffer.
Definition print.hh:202
Helper class to signal stopping of grounding.
Definition sync.hh:11
std::unique_ptr< Propagator > UPropagator
A unique pointer to a propagator.
Definition solver.hh:529
std::unique_ptr< SolveHandle > USolveHandle
A unique pointer for a solve handle.
Definition solver.hh:408
ModelType
Enumeration of available model flags.
Definition solver.hh:125
ReifyFlags
Enumeration of available options for the reification backend.
Definition solver.hh:81
IStop
Stop condition for incremental mode.
Definition solver.hh:57
SolveMode
The available solve modes.
Definition solver.hh:463
BuiltinIncludes
Bitset of enabled builtin includes.
Definition parse.hh:23
SymbolSelectFlags
A bit set of symbol selection flags.
Definition solver.hh:113
AppMode
Enumeration of available application modes.
Definition solver.hh:65
std::unique_ptr< SymbolTable > USymbolTable
A unique pointer to a symbol table.
Definition solver.hh:606
std::unique_ptr< SolveEventHandler > USolveEventHandler
A unique pointer for an event handler.
Definition solver.hh:458
SolveResult
The solve result.
Definition solver.hh:332
std::unique_ptr< Model > UModel
A unique pointer to a model.
Definition solver.hh:326
ConsequenceType
Enumeration of available consequence types.
Definition solver.hh:132
std::unique_ptr< BackendHandle > UBackendHandle
A unique pointer to a backend handle.
Definition solver.hh:520
BackendType
Enumeration of available output formats when in solving mode.
Definition solver.hh:73
std::unique_ptr< Script > UScript
A unique pointer to a script.
Definition solver.hh:35
std::unique_ptr< GroundEventHandler > UGroundEventHandler
A unique pointer to a ground event handler.
Definition solver.hh:640
@ cautious_consequences
The model represents a set of cautious consequences.
@ model
The model represents a stable model.
@ brave_consequences
The model represents a set of brave consequences.
@ reify_scc
Compute and print SCCs.
@ reify_step
Add step numbers.
@ none
Do not consider solve result.
@ sat
Stop when satisfiable.
@ unsat
Stop when unsat.
@ unknown
Stop when interrupted.
@ async
Solve asynchronously in background threads.
@ yield
Yield models while solving via SolveHandle::model().
@ shown
Select shown atoms and terms.
@ theory
Select symbols added by theory.
@ parse
Stop processing after parsing.
@ ground
Stop processing after grounding.
@ rewrite
Stop processing after rewriting.
@ solve
Stop processing after solving.
@ satisfiable
The search produced at least one model.
@ exhausted
The search has been exhausted.
@ true_
The literal is a consequence.
@ false_
The literal is not a consequence.
@ smodels
Print program in smodels format.
@ clasp
Standard solving mode: use the normal backend (Clasp)
@ aspif
Print program in ASP intermediate format.
@ reify
Print program as reified facts.
int32_t prg_lit_t
A program literal.
Definition backend.hh:27
std::unique_ptr< OutputStm > UOutputStm
Unique pointer for statement output.
Definition output.hh:250
std::span< prg_lit_t const > PrgLitSpan
A span of program literals.
Definition backend.hh:37
int32_t prg_weight_t
A weight used in weight and minimize constraints.
Definition backend.hh:33
int64_t prg_sum_t
Type to represent sums of weights.
Definition backend.hh:35
uint32_t prg_id_t
An id to refer to elements of a logic program.
Definition backend.hh:16
std::unique_ptr< ProgramBackend > UProgramBackend
A unique pointer for a program backend.
Definition backend.hh:231
std::span< Symbol const > SymbolSpan
A span of symbols.
Definition symbol.hh:218
std::vector< Symbol > SymbolVec
A vector of symbols.
Definition symbol.hh:220
GroundResult
Available grounding results.
Definition core.hh:187
@ unsatisfiable
The grounding was successful but an inconsistency was derived.
@ interrupted
The grounding was interrupted.
@ none
No sign.
Util::ordered_map< SharedString, std::pair< StmConst, SharedSymbol > > ConstMap
Map from identifiers to constants.
Definition program.hh:48
std::vector< ProgramParam > ProgramParamVec
A list of program params.
Definition statement.hh:764
std::tuple< SharedString, size_t, bool > SharedSig
The signature of a predicate.
Definition term.hh:42
tsl::ordered_map< Key, T, Hash, KeyEqual, Allocator, ValueTypeContainer, IndexType > ordered_map
Alias for ordered maps.
Definition ordered_map.hh:16
tsl::hopscotch_map< Key, T, Hash, KeyEqual, Allocator, NeighborhoodSize, StoreHash, GrowthPolicy > unordered_map
Alias for unordered maps.
Definition unordered_map.hh:17
#define CLINGO_ENABLE_BITSET_ENUM(E,...)
Opt-in macro for enabling bit operations for a given enum type.
Definition enum.hh:18
Options to fine-tune the grounding process.
Definition grounder.hh:16
Options for the solver.
Definition solver.hh:89
std::string iquery
The name of the query atom.
Definition solver.hh:107
BackendType backend_type
Output format to use when in solving mode.
Definition solver.hh:97
size_t imin
The minimum number of incremental steps.
Definition solver.hh:101
bool single_shot
Restrict to single shot-solving.
Definition solver.hh:109
AppMode mode
Operation mode of the solver.
Definition solver.hh:95
std::optional< size_t > imax
The maximum number of incremental steps.
Definition solver.hh:103
Input::RewriteOptions ropts
The rewrite options.
Definition solver.hh:91
ReifyFlags reify_flags
Reification flags.
Definition solver.hh:99
IStop istop
The stop condition for the incremental mode.
Definition solver.hh:105
GroundOptions gopts
Options to fine tune grounding.
Definition solver.hh:93
Options to configure rewriting.
Definition program.hh:38