Clingo C API
C API for clingo providing high level functions to control grounding and solving.
model.c

The example shows how to inspect a model.

Output

$ ./model 0
Stable model:
shown: c
atoms: b
terms: c
~atoms: a
Stable model:
shown: a
atoms: a
terms:
~atoms: b

Code

#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <clingo.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct model_buffer {
clingo_symbol_t *symbols;
size_t symbols_n;
char *string;
size_t string_n;
} model_buffer_t;
void free_model_buffer(model_buffer_t *buf) {
if (buf->symbols) {
free(buf->symbols);
buf->symbols = NULL;
buf->symbols_n = 0;
}
if (buf->string) {
free(buf->string);
buf->string = NULL;
buf->string_n = 0;
}
}
bool print_symbol(clingo_symbol_t symbol, model_buffer_t *buf) {
bool ret = true;
char *string;
size_t n;
// determine size of the string representation of the next symbol in the model
if (!clingo_symbol_to_string_size(symbol, &n)) { goto error; }
if (buf->string_n < n) {
// allocate required memory to hold the symbol's string
if (!(string = (char*)realloc(buf->string, sizeof(*buf->string) * n))) {
clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for symbol's string");
goto error;
}
buf->string = string;
buf->string_n = n;
}
// retrieve the symbol's string
if (!clingo_symbol_to_string(symbol, buf->string, n)) { goto error; }
printf("%s", buf->string);
goto out;
error:
ret = false;
out:
return ret;
}
bool print_model(clingo_model_t const *model, model_buffer_t *buf, char const *label, clingo_show_type_bitset_t show) {
bool ret = true;
clingo_symbol_t *symbols;
size_t n;
clingo_symbol_t const *it, *ie;
// determine the number of (shown) symbols in the model
if (!clingo_model_symbols_size(model, show, &n)) { goto error; }
// allocate required memory to hold all the symbols
if (buf->symbols_n < n) {
if (!(symbols = (clingo_symbol_t*)malloc(sizeof(*buf->symbols) * n))) {
clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for atoms");
goto error;
}
buf->symbols = symbols;
buf->symbols_n = n;
}
// retrieve the symbols in the model
if (!clingo_model_symbols(model, show, buf->symbols, n)) { goto error; }
printf("%s:", label);
for (it = buf->symbols, ie = buf->symbols + n; it != ie; ++it) {
printf(" ");
if (!print_symbol(*it, buf)) { goto error; }
}
printf("\n");
goto out;
error:
ret = false;
out:
return ret;
}
bool print_solution(clingo_model_t const *model, model_buffer_t *data) {
bool ret = true;
uint64_t number;
char const *type_string = "";
// get model type
if (!clingo_model_type(model, &type)) { goto error; }
switch ((enum clingo_model_type_e)type) {
case clingo_model_type_stable_model: { type_string = "Stable model"; break; }
case clingo_model_type_brave_consequences: { type_string = "Brave consequences"; break; }
case clingo_model_type_cautious_consequences: { type_string = "Cautious consequences"; break; }
}
// get running number of model
if (!clingo_model_number(model, &number)) { goto error; }
printf("%s %" PRIu64 ":\n", type_string, number);
if (!print_model(model, data, " shown", clingo_show_type_shown)) { goto error; }
if (!print_model(model, data, " atoms", clingo_show_type_atoms)) { goto error; }
if (!print_model(model, data, " terms", clingo_show_type_terms)) { goto error; }
if (!print_model(model, data, " ~atoms", clingo_show_type_complement
| clingo_show_type_atoms)) { goto error; }
goto out;
error:
ret = false;
out:
return ret;
}
bool solve(clingo_control_t *ctl, model_buffer_t *data, clingo_solve_result_bitset_t *result) {
bool ret = true;
clingo_model_t const *model;
// get a solve handle
if (!clingo_control_solve(ctl, clingo_solve_mode_yield, NULL, 0, NULL, NULL, &handle)) { goto error; }
// loop over all models
while (true) {
if (!clingo_solve_handle_resume(handle)) { goto error; }
if (!clingo_solve_handle_model(handle, &model)) { goto error; }
// print the model
if (model) { print_solution(model, data); }
// stop if there are no more models
else { break; }
}
// close the solve handle
if (!clingo_solve_handle_get(handle, result)) { goto error; }
goto out;
error:
ret = false;
out:
// free the solve handle
return clingo_solve_handle_close(handle) && ret;
}
int main(int argc, char const **argv) {
char const *error_message;
int ret = 0;
clingo_control_t *ctl = NULL;
clingo_part_t parts[] = {{ "base", NULL, 0 }};
model_buffer_t buf = {NULL, 0, NULL, 0};
// create a control object and pass command line arguments
if (!clingo_control_new(argv+1, argc-1, NULL, NULL, 20, &ctl) != 0) { goto error; }
// add a logic program to the base part
if (!clingo_control_add(ctl, "base", NULL, 0, "1 {a; b} 1. #show c : b. #show a/0.")) { goto error; }
// ground the base part
if (!clingo_control_ground(ctl, parts, 1, NULL, NULL)) { goto error; }
// solve
if (!solve(ctl, &buf, &solve_ret)) { goto error; }
goto out;
error:
if (!(error_message = clingo_error_message())) { error_message = "error"; }
printf("%s\n", error_message);
out:
free_model_buffer(&buf);
if (ctl) { clingo_control_free(ctl); }
return ret;
}
clingo_solve_mode_yield
@ clingo_solve_mode_yield
Yield models in calls to clingo_solve_handle_model.
Definition: clingo.h:2288
clingo_show_type_shown
@ clingo_show_type_shown
Select shown atoms and terms.
Definition: clingo.h:2098
clingo_control_t
struct clingo_control clingo_control_t
Control object holding grounding and solving state.
Definition: clingo.h:2693
clingo_control_new
CLINGO_VISIBILITY_DEFAULT bool clingo_control_new(char const *const *arguments, size_t arguments_size, clingo_logger_t logger, void *logger_data, unsigned message_limit, clingo_control_t **control)
Create a new control object.
clingo_model_type_brave_consequences
@ clingo_model_type_brave_consequences
The model represents a set of brave consequences.
Definition: clingo.h:2090
clingo_control_free
CLINGO_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control)
Free a control object created with clingo_control_new().
clingo_model_number
CLINGO_VISIBILITY_DEFAULT bool clingo_model_number(clingo_model_t const *model, uint64_t *number)
Get the running number of the model.
clingo.h
clingo_part
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
clingo_model_type
CLINGO_VISIBILITY_DEFAULT bool clingo_model_type(clingo_model_t const *model, clingo_model_type_t *type)
Get the type of the model.
clingo_solve_handle_close
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_close(clingo_solve_handle_t *handle)
Stops the running search and releases the handle.
clingo_model_type_e
clingo_model_type_e
Enumeration for the different model types.
Definition: clingo.h:2088
clingo_symbol_to_string
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, char *string, size_t size)
Get the string representation of a symbol.
clingo_control_ground
CLINGO_VISIBILITY_DEFAULT bool clingo_control_ground(clingo_control_t *control, clingo_part_t const *parts, size_t parts_size, clingo_ground_callback_t ground_callback, void *ground_callback_data)
Ground the selected parts of the current (non-ground) logic program.
clingo_control_add
CLINGO_VISIBILITY_DEFAULT bool clingo_control_add(clingo_control_t *control, char const *name, char const *const *parameters, size_t parameters_size, char const *program)
Extend the logic program with the given non-ground logic program in string form.
clingo_solve_handle_get
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_control_solve
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_callback_t notify, void *data, clingo_solve_handle_t **handle)
Solve the currently grounded logic program enumerating its models.
clingo_model_type_t
int clingo_model_type_t
Corresponding type to clingo_model_type_e.
Definition: clingo.h:2094
clingo_model_t
struct clingo_model clingo_model_t
Object representing a model.
Definition: clingo.h:2085
clingo_solve_handle_resume
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_resume(clingo_solve_handle_t *handle)
Discards the last model and starts the search for the next one.
clingo_symbol_to_string_size
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string_size(clingo_symbol_t symbol, size_t *size)
Get the size of the string representation of a symbol (including the terminating 0).
clingo_model_symbols_size
CLINGO_VISIBILITY_DEFAULT bool clingo_model_symbols_size(clingo_model_t const *model, clingo_show_type_bitset_t show, size_t *size)
Get the number of symbols of the selected types in the model.
clingo_model_type_stable_model
@ clingo_model_type_stable_model
The model represents a stable model.
Definition: clingo.h:2089
clingo_solve_handle_t
struct clingo_solve_handle clingo_solve_handle_t
Search handle to a solve call.
Definition: clingo.h:2322
clingo_solve_result_bitset_t
unsigned clingo_solve_result_bitset_t
Definition: clingo.h:2248
clingo_solve_handle_model
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_model(clingo_solve_handle_t *handle, clingo_model_t const **model)
Get the next model (or zero if there are no more models).
clingo_show_type_terms
@ clingo_show_type_terms
Select all terms.
Definition: clingo.h:2100
clingo_show_type_bitset_t
unsigned clingo_show_type_bitset_t
Corresponding type to clingo_show_type_e.
Definition: clingo.h:2106
clingo_show_type_complement
@ clingo_show_type_complement
Select false instead of true atoms (clingo_show_type_atoms) or terms (clingo_show_type_terms).
Definition: clingo.h:2103
clingo_error_code
CLINGO_VISIBILITY_DEFAULT clingo_error_t clingo_error_code()
Get the last error code set by a clingo API call.
clingo_error_message
CLINGO_VISIBILITY_DEFAULT const char * clingo_error_message()
Get the last error message set if an API call fails.
clingo_symbol_t
uint64_t clingo_symbol_t
Represents a symbol.
Definition: clingo.h:330
clingo_model_type_cautious_consequences
@ clingo_model_type_cautious_consequences
The model represents a set of cautious consequences.
Definition: clingo.h:2091
clingo_show_type_atoms
@ clingo_show_type_atoms
Select all atoms.
Definition: clingo.h:2099
clingo_error_bad_alloc
@ clingo_error_bad_alloc
memory could not be allocated
Definition: clingo.h:145
clingo_set_error
CLINGO_VISIBILITY_DEFAULT void clingo_set_error(clingo_error_t code, char const *message)
Set a custom error code and message in the active thread.
clingo_model_symbols
CLINGO_VISIBILITY_DEFAULT bool clingo_model_symbols(clingo_model_t const *model, clingo_show_type_bitset_t show, clingo_symbol_t *symbols, size_t size)
Get the symbols of the selected types in the model.