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 auto operator=(Observer &&other) -> Observer & = delete;
25
27 virtual ~Observer() = default;
28
32 void init_program(bool incremental) { do_init_program(incremental); }
33
35 void begin_step() { do_begin_step(); }
36
38 void end_step(Base base) { do_end_step(base); }
39
45 void rule(ProgramAtomSpan head, ProgramLiteralSpan body, bool choice) { do_rule(head, body, choice); }
46
53 void weight_rule(ProgramAtomSpan head, Weight lower, WeightedLiteralSpan body, bool choice) {
54 do_weight_rule(head, lower, body, choice);
55 }
56
61 void minimize(WeightedLiteralSpan literals, Weight priority) { do_minimize(literals, priority); }
62
66 void project(ProgramAtomSpan atoms) { do_project(atoms); }
67
72 void external(ProgramAtom atom, ExternalType type) { do_external(atom, type); }
73
77 void assume(ProgramLiteralSpan literals) { do_assume(literals); }
78
84 void heuristic(ProgramAtom atom, HeuristicType type, int bias, unsigned priority, ProgramLiteralSpan condition) {
85 do_heuristic(atom, type, bias, priority, condition);
86 }
87
93 void edge(int node_u, int node_v, ProgramLiteralSpan condition) { do_edge(node_u, node_v, condition); }
94
95 private:
96 friend class Control;
97
98 void observe(clingo_control_t *ctl, bool preprocess) {
99 static constexpr auto cobs = clingo_observer_t{
100 [](bool incremental, void *data) -> bool {
101 CLINGO_TRY {
102 static_cast<Observer *>(data)->init_program(incremental);
103 }
104 CLINGO_CATCH;
105 },
106 [](void *data) -> bool {
107 CLINGO_TRY {
108 static_cast<Observer *>(data)->begin_step();
109 }
110 CLINGO_CATCH;
111 },
112 [](clingo_base_t const *base, void *data) -> bool {
113 CLINGO_TRY {
114 static_cast<Observer *>(data)->end_step(Base{base});
115 }
116 CLINGO_CATCH;
117 },
118 [](bool choice, clingo_atom_t const *head, size_t head_size, clingo_literal_t const *body, size_t body_size,
119 void *data) -> bool {
120 CLINGO_TRY {
121 static_cast<Observer *>(data)->rule(std::span{head, head_size}, std::span{body, body_size}, choice);
122 }
123 CLINGO_CATCH;
124 },
125 [](bool choice, clingo_atom_t const *head, size_t head_size, clingo_weight_t lower,
126 clingo_weighted_literal_t const *body, size_t body_size, void *data) -> bool {
127 CLINGO_TRY {
128 static_cast<Observer *>(data)->weight_rule(std::span{head, head_size}, lower,
129 std::span{body, body_size}, choice);
130 }
131 CLINGO_CATCH;
132 },
133 [](clingo_weight_t priority, clingo_weighted_literal_t const *body, size_t body_size, void *data) -> bool {
134 CLINGO_TRY {
135 static_cast<Observer *>(data)->minimize(std::span{body, body_size}, priority);
136 }
137 CLINGO_CATCH;
138 },
139 [](clingo_atom_t const *atoms, size_t size, void *data) -> bool {
140 CLINGO_TRY {
141 static_cast<Observer *>(data)->project(std::span{atoms, size});
142 }
143 CLINGO_CATCH;
144 },
145 [](clingo_atom_t atom, clingo_external_type_t type, void *data) -> bool {
146 CLINGO_TRY {
147 static_cast<Observer *>(data)->external(atom, static_cast<ExternalType>(type));
148 }
149 CLINGO_CATCH;
150 },
151 [](clingo_literal_t const *literals, size_t size, void *data) -> bool {
152 CLINGO_TRY {
153 static_cast<Observer *>(data)->assume(std::span{literals, size});
154 }
155 CLINGO_CATCH;
156 },
157 [](clingo_atom_t atom, clingo_heuristic_type_t type, int bias, unsigned priority,
158 clingo_literal_t const *condition, size_t size, void *data) -> bool {
159 CLINGO_TRY {
160 static_cast<Observer *>(data)->heuristic(atom, static_cast<HeuristicType>(type), bias, priority,
161 std::span{condition, size});
162 }
163 CLINGO_CATCH;
164 },
165 [](int node_u, int node_v, clingo_literal_t const *condition, size_t size, void *data) -> bool {
166 CLINGO_TRY {
167 static_cast<Observer *>(data)->edge(node_u, node_v, std::span{condition, size});
168 }
169 CLINGO_CATCH;
170 },
171 };
172 Detail::handle_error(clingo_control_observe(ctl, &cobs, static_cast<void *>(this), preprocess));
173 }
174
175 virtual void do_init_program([[maybe_unused]] bool incremental) {}
176 virtual void do_begin_step() {}
177 virtual void do_end_step([[maybe_unused]] Base base) {}
178 virtual void do_rule([[maybe_unused]] ProgramAtomSpan head, [[maybe_unused]] ProgramLiteralSpan body,
179 [[maybe_unused]] bool choice) {}
180 virtual void do_weight_rule([[maybe_unused]] ProgramAtomSpan head, [[maybe_unused]] Weight lower,
181 [[maybe_unused]] WeightedLiteralSpan body, [[maybe_unused]] bool choice) {}
182 virtual void do_minimize([[maybe_unused]] WeightedLiteralSpan literals, [[maybe_unused]] Weight priority) {}
183 virtual void do_project([[maybe_unused]] ProgramAtomSpan atoms) {}
184 virtual void do_external([[maybe_unused]] ProgramAtom atom, [[maybe_unused]] ExternalType type) {}
185 virtual void do_assume([[maybe_unused]] ProgramLiteralSpan literals) {}
186 virtual void do_heuristic([[maybe_unused]] ProgramAtom atom, [[maybe_unused]] HeuristicType type,
187 [[maybe_unused]] int bias, [[maybe_unused]] unsigned priority,
188 [[maybe_unused]] ProgramLiteralSpan condition) {}
189 virtual void do_edge([[maybe_unused]] int node_u, [[maybe_unused]] int node_v,
190 [[maybe_unused]] ProgramLiteralSpan condition) {}
191};
192
194
195} // namespace Clingo
A base that maps signatures to atom bases, and captures term and theory bases.
Definition base.hh:607
The main control class for grounding and solving logic programs.
Definition control.hh:228
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:38
void init_program(bool incremental)
Callback for the beginning of the program.
Definition observe.hh:32
virtual ~Observer()=default
The default destructor.
void assume(ProgramLiteralSpan literals)
Callback for an assumption directive.
Definition observe.hh:77
void minimize(WeightedLiteralSpan literals, Weight priority)
Callback for a minimize constraint.
Definition observe.hh:61
void heuristic(ProgramAtom atom, HeuristicType type, int bias, unsigned priority, ProgramLiteralSpan condition)
Definition observe.hh:84
auto operator=(Observer &&other) -> Observer &=delete
Disable move and copy operations.
Observer()=default
The default constructor.
void edge(int node_u, int node_v, ProgramLiteralSpan condition)
Callback for an edge statement.
Definition observe.hh:93
void project(ProgramAtomSpan atoms)
Callback for a projection directive.
Definition observe.hh:66
void begin_step()
Callback for the beginning of a step.
Definition observe.hh:35
void external(ProgramAtom atom, ExternalType type)
Callback for an external statement.
Definition observe.hh:72
void weight_rule(ProgramAtomSpan head, Weight lower, WeightedLiteralSpan body, bool choice)
Callback for a weight rule.
Definition observe.hh:53
void rule(ProgramAtomSpan head, ProgramLiteralSpan body, bool choice)
Callback for a rule.
Definition observe.hh:45
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:75
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition core.h:73
int32_t clingo_weight_t
Signed integer type for weights in sum aggregates and minimize constraints.
Definition core.h:79
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
@ 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:81