The example shows how to ground and solve a simple logic program, and print its answer sets.
The example shows how to ground and solve a simple logic program, and print its answer sets.
#include <clingo.h>
#define STDC_FORMAT_MACROS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _MSC_VER
#define GC(f) __attribute__((cleanup(f)))
#else
#define GC(f)
#endif
}
void free_ctl(clingo_control_t **ctl) {
}
void handle_error(bool ret) {
if (!ret) {
exit(1);
}
}
bool print_symbols(
clingo_symbol_t const *symbols,
size_t size,
void *data) {
(void)data;
bool res = true;
if (!res) {
goto out;
}
for (size_t i = 0; i != size; ++i) {
if (!res) {
goto out;
}
if (!res) {
goto out;
}
printf(
" %.*s", (
int)str.
size, str.
data);
}
out:
return res;
}
(void)data;
printf("Answer:");
if (!res) {
return res;
}
printf("\n");
*goon = true;
return true;
}
int main(
int argc,
char *argv[]) {
(void)argc;
(void)argv;
clingo_control_t *ctl GC(free_ctl) = NULL;
char const *prg = "1 {a; b} 1.";
printf("solving finished with result: %d\n", res);
return 0;
}
CLINGO_VISIBILITY_DEFAULT bool clingo_control_parse_string(clingo_control_t *control, char const *program, size_t size)
Extend the logic program with the given non-ground logic program in string form.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_ground(clingo_control_t *control, clingo_part_t const *parts, size_t size, clingo_ground_callback_t ground_callback, void *data)
Ground the selected parts of the current (non-ground) logic program.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_new(clingo_lib_t *lib, clingo_string_t const *arguments, size_t size, clingo_control_t **control)
Create a new control object.
CLINGO_VISIBILITY_DEFAULT void clingo_control_release(clingo_control_t *control)
Decrement the reference count of the given control object and destroy if zero.
CLINGO_VISIBILITY_DEFAULT void clingo_lib_release(clingo_lib_t *lib)
Release a library object created with clingo_lib_new().
CLINGO_VISIBILITY_DEFAULT void clingo_string_builder_free(clingo_string_builder_t const *bld)
Free the string builder.
CLINGO_VISIBILITY_DEFAULT void clingo_string_builder_clear(clingo_string_builder_t *bld)
Clear the string in the builder.
CLINGO_VISIBILITY_DEFAULT bool clingo_lib_new(clingo_lib_flags_t flags, clingo_log_level_t level, clingo_logger_t const *logger, void *data, size_t limit, clingo_lib_t **lib)
Create a library object.
struct clingo_lib clingo_lib_t
A library object storing global information.
Definition core.h:176
struct clingo_string_builder clingo_string_builder_t
A builder for strings.
Definition core.h:257
CLINGO_VISIBILITY_DEFAULT bool clingo_string_builder_string(clingo_string_builder_t const *bld, clingo_string_t *value)
Get the (zero-terminated) string in the builder.
CLINGO_VISIBILITY_DEFAULT bool clingo_string_builder_new(clingo_string_builder_t **bld)
Create a new string builder.
@ clingo_log_level_info
the info level
Definition core.h:149
CLINGO_VISIBILITY_DEFAULT bool clingo_model_symbols(clingo_model_t const *model, clingo_show_type_bitset_t show, clingo_symbol_callback_t callback, void *data)
Get the symbols of the selected types in the model.
struct clingo_model clingo_model_t
Object representing a model.
Definition model.h:43
@ clingo_show_type_shown
Select shown atoms and terms.
Definition model.h:56
struct clingo_solve_handle clingo_solve_handle_t
Search handle to a solve call.
Definition solve.h:110
unsigned clingo_solve_result_bitset_t
Corresponding type to clingo_solve_result_e.
Definition solve.h:60
CLINGO_VISIBILITY_DEFAULT bool clingo_control_solve(clingo_control_t *control, clingo_solve_mode_bitset_t mode, clingo_literal_t const *assumptions, size_t assumptions_size, clingo_solve_event_handler_t const *handler, void *data, clingo_solve_handle_t **handle)
Solve the currently grounded logic program enumerating its models.
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_get(clingo_solve_handle_t *handle, clingo_solve_result_bitset_t *result)
Get the next solve result.
@ clingo_solve_mode_async
Enable non-blocking search.
Definition solve.h:64
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, clingo_string_builder_t *builder)
Get the string representation of a symbol.
uint64_t clingo_symbol_t
Type to represent a symbol.
Definition symbol.h:51
auto main(Library &lib, std::span< std::string_view const > arguments={}, App *app=nullptr, bool raise_errors=false) -> int
Run a clingo based application with the given arguments.
Definition app.hh:219
Struct used to specify the program parts that have to be grounded.
Definition control.h:57
The solve event handler interface.
Definition solve.h:72
Struct to capture strings that are not null-terminated.
Definition core.h:91
size_t size
the length of the string
Definition core.h:93
char const * data
pointer to the beginning of the string
Definition core.h:92