Clingo
Loading...
Searching...
No Matches
observe.hh
1#pragma once
2
3#include <clingo/base.hh>
4#include <clingo/core.hh>
5
6#include <clingo/observe.h>
7
8namespace Clingo {
9
13
15class Observer {
16 public:
18 Observer() = default;
19
21 Observer(Observer &&other) = delete;
22
24 virtual ~Observer() = default;
25
29 void init_program(bool incremental) { do_init_program(incremental); }
30
32 void begin_step() { do_begin_step(); }
33
35 void end_step(Base base) { do_end_step(base); }
36
42 void rule(ProgramAtomSpan head, ProgramLiteralSpan body, bool choice) { do_rule(head, body, choice); }
43
51 do_weight_rule(head, lower, body, choice);
52 }
53
58 void minimize(WeightedLiteralSpan literals, Weight priority) { do_minimize(literals, priority); }
59
63 void project(ProgramAtomSpan atoms) { do_project(atoms); }
64
69 void external(ProgramAtom atom, ExternalType type) { do_external(atom, type); }
70
75
81 void heuristic(ProgramAtom atom, HeuristicType type, int bias, unsigned priority, ProgramLiteralSpan condition) {
82 do_heuristic(atom, type, bias, priority, condition);
83 }
84
90 void edge(int node_u, int node_v, ProgramLiteralSpan condition) { do_edge(node_u, node_v, condition); }
91
92 private:
93 friend class Control;
94
95 void observe(clingo_control_t *ctl, bool preprocess) {
96 static constexpr auto cobs = clingo_observer_t{
97 [](bool incremental, void *data) -> bool {
98 CLINGO_TRY {
99 static_cast<Observer *>(data)->init_program(incremental);
100 }
101 CLINGO_CATCH;
102 },
103 [](void *data) -> bool {
104 CLINGO_TRY {
105 static_cast<Observer *>(data)->begin_step();
106 }
107 CLINGO_CATCH;
108 },
109 [](clingo_base_t const *base, void *data) -> bool {
110 CLINGO_TRY {
111 static_cast<Observer *>(data)->end_step(Base{base});
112 }
113 CLINGO_CATCH;
114 },
115 [](bool choice, clingo_atom_t const *head, size_t head_size, clingo_literal_t const *body, size_t body_size,
116 void *data) -> bool {
117 CLINGO_TRY {
118 static_cast<Observer *>(data)->rule(std::span{head, head_size}, std::span{body, body_size}, choice);
119 }
120 CLINGO_CATCH;
121 },
122 [](bool choice, clingo_atom_t const *head, size_t head_size, clingo_weight_t lower,
123 clingo_weighted_literal_t const *body, size_t body_size, void *data) -> bool {
124 CLINGO_TRY {
125 static_cast<Observer *>(data)->weight_rule(std::span{head, head_size}, lower,
126 std::span{body, body_size}, choice);
127 }
128 CLINGO_CATCH;
129 },
130 [](clingo_weight_t priority, clingo_weighted_literal_t const *body, size_t body_size, void *data) -> bool {
131 CLINGO_TRY {
132 static_cast<Observer *>(data)->minimize(std::span{body, body_size}, priority);
133 }
134 CLINGO_CATCH;
135 },
136 [](clingo_atom_t const *atoms, size_t size, void *data) -> bool {
137 CLINGO_TRY {
138 static_cast<Observer *>(data)->project(std::span{atoms, size});
139 }
140 CLINGO_CATCH;
141 },
142 [](clingo_atom_t atom, clingo_external_type_t type, void *data) -> bool {
143 CLINGO_TRY {
144 static_cast<Observer *>(data)->external(atom, static_cast<ExternalType>(type));
145 }
146 CLINGO_CATCH;
147 },
148 [](clingo_literal_t const *literals, size_t size, void *data) -> bool {
149 CLINGO_TRY {
150 static_cast<Observer *>(data)->assume(std::span{literals, size});
151 }
152 CLINGO_CATCH;
153 },
154 [](clingo_atom_t atom, clingo_heuristic_type_t type, int bias, unsigned priority,
155 clingo_literal_t const *condition, size_t size, void *data) -> bool {
156 CLINGO_TRY {
157 static_cast<Observer *>(data)->heuristic(atom, static_cast<HeuristicType>(type), bias, priority,
158 std::span{condition, size});
159 }
160 CLINGO_CATCH;
161 },
162 [](int node_u, int node_v, clingo_literal_t const *condition, size_t size, void *data) -> bool {
163 CLINGO_TRY {
164 static_cast<Observer *>(data)->edge(node_u, node_v, std::span{condition, size});
165 }
166 CLINGO_CATCH;
167 },
168 };
169 Detail::handle_error(clingo_control_observe(ctl, &cobs, static_cast<void *>(this), preprocess));
170 }
171
172 virtual void do_init_program([[maybe_unused]] bool incremental) {}
173 virtual void do_begin_step() {}
174 virtual void do_end_step([[maybe_unused]] Base base) {}
175 virtual void do_rule([[maybe_unused]] ProgramAtomSpan head, [[maybe_unused]] ProgramLiteralSpan body,
176 [[maybe_unused]] bool choice) {}
177 virtual void do_weight_rule([[maybe_unused]] ProgramAtomSpan head, [[maybe_unused]] Weight lower,
178 [[maybe_unused]] WeightedLiteralSpan body, [[maybe_unused]] bool choice) {}
179 virtual void do_minimize([[maybe_unused]] WeightedLiteralSpan literals, [[maybe_unused]] Weight priority) {}
180 virtual void do_project([[maybe_unused]] ProgramAtomSpan atoms) {}
181 virtual void do_external([[maybe_unused]] ProgramAtom atom, [[maybe_unused]] ExternalType type) {}
182 virtual void do_assume([[maybe_unused]] ProgramLiteralSpan literals) {}
183 virtual void do_heuristic([[maybe_unused]] ProgramAtom atom, [[maybe_unused]] HeuristicType type,
184 [[maybe_unused]] int bias, [[maybe_unused]] unsigned priority,
185 [[maybe_unused]] ProgramLiteralSpan condition) {}
186 virtual void do_edge([[maybe_unused]] int node_u, [[maybe_unused]] int node_v,
187 [[maybe_unused]] ProgramLiteralSpan condition) {}
188};
189
191
192} // namespace Clingo
A base that maps signatures to atom bases, and captures term and theory bases.
Definition base.hh:608
The main control class for grounding and solving logic programs.
Definition control.hh:179
Observer interface to inspect the current ground program.
Definition observe.hh:15
Observer(Observer &&other)=delete
Disable move and copy operations.
void end_step(Base base)
Callback for the end of a step.
Definition observe.hh:35
void init_program(bool incremental)
Callback for the beginning of the program.
Definition observe.hh:29
virtual ~Observer()=default
The default destructor.
void assume(ProgramLiteralSpan literals)
Callback for an assumption directive.
Definition observe.hh:74
void minimize(WeightedLiteralSpan literals, Weight priority)
Callback for a minimize constraint.
Definition observe.hh:58
void heuristic(ProgramAtom atom, HeuristicType type, int bias, unsigned priority, ProgramLiteralSpan condition)
Definition observe.hh:81
Observer()=default
The default constructor.
void edge(int node_u, int node_v, ProgramLiteralSpan condition)
Callback for an edge statement.
Definition observe.hh:90
void project(ProgramAtomSpan atoms)
Callback for a projection directive.
Definition observe.hh:63
void begin_step()
Callback for the beginning of a step.
Definition observe.hh:32
void external(ProgramAtom atom, ExternalType type)
Callback for an external statement.
Definition observe.hh:69
void weight_rule(ProgramAtomSpan head, Weight lower, WeightedLiteralSpan body, bool choice)
Callback for a weight rule.
Definition observe.hh:50
void rule(ProgramAtomSpan head, ProgramLiteralSpan body, bool choice)
Callback for a rule.
Definition observe.hh:42
struct clingo_base clingo_base_t
Object to inspect symbolic atoms in a program—the relevant Herbrand base gringo uses to instantiate p...
Definition base.h:70
uint32_t clingo_atom_t
Unsigned integer type used for aspif atoms.
Definition core.h:80
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition core.h:78
int32_t clingo_weight_t
Signed integer type for weights in sum aggregates and minimize constraints.
Definition core.h:84
CLINGO_VISIBILITY_DEFAULT bool clingo_control_observe(clingo_control_t *control, clingo_observer_t const *observer, void *data, bool preprocess)
Get an observer to inspect the ground program.
int clingo_external_type_t
Corresponding type to clingo_external_type_e.
Definition shared.h:32
int clingo_heuristic_type_t
Corresponding type to clingo_heuristic_type_e.
Definition shared.h:22
@ tuple
Theory tuples "(t1,...,tn)".
@ project
Discard project statements.
@ minimize
Discard minimize statements.
@ preprocess
Whether to preprocess the program before writing.
clingo_atom_t ProgramAtom
A program atom.
Definition core.hh:387
HeuristicType
Enumeration of heuristic types.
Definition core.hh:560
std::span< ProgramAtom const > ProgramAtomSpan
A span of program atoms.
Definition core.hh:389
clingo_weight_t Weight
A weight used in sum aggregates and minimize constraints.
Definition core.hh:408
std::span< WeightedLiteral const > WeightedLiteralSpan
A span of weighted literals.
Definition core.hh:415
std::span< ProgramLiteral const > ProgramLiteralSpan
A span of program literals.
Definition core.hh:394
ExternalType
Enumeration of control modes.
Definition core.hh:552
@ atoms
Select all atoms.
@ atom
Check if term is an atom.
An instance of this struct has to be registered with a solver to observe ground directives as they ar...
Definition observe.h:42
A literal with an associated weight.
Definition core.h:86