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

The example shows how to rewrite a non-ground logic program.

Output

./ast 0
Solving with enable = false...
Model:
Solving with enable = true...
Model: enable a
Model: enable b
Solving with enable = false...
Model:

Code

#include <clingo.h>
#include <stdlib.h>
#include <stdio.h>
bool print_model(clingo_model_t const *model) {
bool ret = true;
clingo_symbol_t *atoms = NULL;
size_t atoms_n;
clingo_symbol_t const *it, *ie;
char *str = NULL;
size_t str_n = 0;
// determine the number of (shown) symbols in the model
if (!clingo_model_symbols_size(model, clingo_show_type_shown, &atoms_n)) { goto error; }
// allocate required memory to hold all the symbols
if (!(atoms = (clingo_symbol_t*)malloc(sizeof(*atoms) * atoms_n))) {
clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for atoms");
goto error;
}
// retrieve the symbols in the model
if (!clingo_model_symbols(model, clingo_show_type_shown, atoms, atoms_n)) { goto error; }
printf("Model:");
for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {
size_t n;
char *str_new;
// determine size of the string representation of the next symbol in the model
if (!clingo_symbol_to_string_size(*it, &n)) { goto error; }
if (str_n < n) {
// allocate required memory to hold the symbol's string
if (!(str_new = (char*)realloc(str, sizeof(*str) * n))) {
clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for symbol's string");
goto error;
}
str = str_new;
str_n = n;
}
// retrieve the symbol's string
if (!clingo_symbol_to_string(*it, str, n)) { goto error; }
printf(" %s", str);
}
printf("\n");
goto out;
error:
ret = false;
out:
if (atoms) { free(atoms); }
if (str) { free(str); }
return ret;
}
typedef struct {
clingo_ast_t *atom;
} on_statement_data;
// adds atom enable to all rule bodies
bool on_statement (clingo_ast_t *stm, on_statement_data *data) {
bool ret = true;
clingo_ast_t *lit = NULL;
size_t size;
if (!clingo_ast_get_type(stm, &type)) { goto error; }
// pass through all statements that are not rules
if (type != clingo_ast_type_rule) {
if (!clingo_program_builder_add(data->builder, stm)) { goto error; }
goto out;
}
// create literal "enable"
if (!clingo_ast_build(clingo_ast_type_literal, &lit, data->loc, clingo_ast_sign_no_sign, data->atom)) {
goto error;
}
if (!clingo_ast_attribute_size_ast_array(stm, clingo_ast_attribute_body, &size)) {
goto error;
}
// append the literal to the rule body
if (!clingo_ast_attribute_insert_ast_at(stm, clingo_ast_attribute_body, size, lit)) {
goto error;
}
// add the rewritten statement to the program
if (!clingo_program_builder_add(data->builder, stm)) { goto error; }
goto out;
error:
ret = false;
out:
if (lit != NULL) {
}
return ret;
}
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_model(model); }
// 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_symbolic_atoms_t const *atoms = NULL;
clingo_solve_handle_t *handle = NULL;
clingo_ast_t *term = NULL;
on_statement_data data = {NULL, NULL, NULL};
clingo_part_t parts[] = {{ "base", 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; }
// get the program builder
if (!clingo_program_builder_init(ctl, &data.builder)) { goto error; }
// initialize the location
location.begin_line = location.end_line = 0;
location.begin_column = location.end_column = 0;
location.begin_file = location.end_file = "<rewrite>";
data.loc = &location;
// initilize atom to add
if (!clingo_symbol_create_id("enable", true, &sym)) { goto error; }
if (!clingo_ast_build(clingo_ast_type_symbolic_term, &term, data.loc, sym)) {
goto error;
}
if (!clingo_ast_build(clingo_ast_type_symbolic_atom, &data.atom, term)) {
goto error;
}
// begin building a program
if (!clingo_program_builder_begin(data.builder)) { goto error; }
// get the AST of the program
if (!clingo_ast_parse_string("a :- not b. b :- not a.", (clingo_ast_callback_t)on_statement, &data, NULL, NULL, NULL, 20)) { goto error; }
// finish building a program
if (!clingo_program_builder_end(data.builder)) { goto error; }
// add the external statement: #external enable.
if (!clingo_control_add(ctl, "base", NULL, 0, "#external enable.")) {
goto error;
}
// ground the base part
if (!clingo_control_ground(ctl, parts, 1, NULL, NULL)) { goto error; }
// get the program literal coresponding to the external atom
if (!clingo_control_symbolic_atoms(ctl, &atoms)) { goto error; }
if (!clingo_symbolic_atoms_find(atoms, sym, &atm_it)) { goto error; }
if (!clingo_symbolic_atoms_literal(atoms, atm_it, &atm)) { goto error; }
// solve with external enable = false
printf("Solving with enable = false...\n");
if (!solve(ctl, &solve_ret)) { goto error; }
// solve with external enable = true
printf("Solving with enable = true...\n");
if (!solve(ctl, &solve_ret)) { goto error; }
// solve with external enable = false
printf("Solving with enable = false...\n");
if (!solve(ctl, &solve_ret)) { goto error; }
goto out;
error:
if (!(error_message = clingo_error_message())) { error_message = "error"; }
printf("%s\n", error_message);
out:
if (term) { clingo_ast_release(term); }
if (data.atom) { clingo_ast_release(data.atom); }
if (handle) { clingo_solve_handle_close(handle); }
if (ctl) { clingo_control_free(ctl); }
return ret;
}
clingo_truth_value_true
@ clingo_truth_value_true
true
Definition: clingo.h:200
clingo_ast_t
struct clingo_ast clingo_ast_t
This struct provides a view to nodes in the AST.
Definition: clingo.h:3287
clingo_location::end_line
size_t end_line
the line where the location ends
Definition: clingo.h:215
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_location::begin_line
size_t begin_line
the line where the location begins
Definition: clingo.h:214
clingo_symbolic_atoms_literal
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_literal(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t iterator, clingo_literal_t *literal)
Returns the (numeric) aspif literal corresponding to the given symbolic atom.
clingo_ast_get_type
CLINGO_VISIBILITY_DEFAULT bool clingo_ast_get_type(clingo_ast_t *ast, clingo_ast_type_t *type)
Get the type of an AST node.
clingo_ast_type_t
int clingo_ast_type_t
Corresponding type to clingo_ast_type_e.
Definition: clingo.h:3187
clingo_control_symbolic_atoms
CLINGO_VISIBILITY_DEFAULT bool clingo_control_symbolic_atoms(clingo_control_t const *control, clingo_symbolic_atoms_t const **atoms)
Get an object to inspect symbolic atoms (the relevant Herbrand base) used for grounding.
clingo_location::begin_file
const char * begin_file
the file where the location begins
Definition: clingo.h:212
clingo_control_free
CLINGO_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control)
Free a control object created with clingo_control_new().
clingo_symbolic_atoms_t
struct clingo_symbolic_atoms clingo_symbolic_atoms_t
Object to inspect symbolic atoms in a program—the relevant Herbrand base gringo uses to instantiate p...
Definition: clingo.h:542
clingo_ast_parse_string
CLINGO_VISIBILITY_DEFAULT bool clingo_ast_parse_string(char const *program, clingo_ast_callback_t callback, void *callback_data, clingo_control_t *control, clingo_logger_t logger, void *logger_data, unsigned message_limit)
Parse the given program and return an abstract syntax tree for each statement via a callback.
clingo_literal_t
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition: clingo.h:121
clingo.h
clingo_ast_release
CLINGO_VISIBILITY_DEFAULT void clingo_ast_release(clingo_ast_t *ast)
Decrement the reference count of an AST node.
clingo_part
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
clingo_program_builder_end
CLINGO_VISIBILITY_DEFAULT bool clingo_program_builder_end(clingo_program_builder_t *builder)
End building a program.
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_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_symbol_create_id
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_create_id(char const *name, bool positive, clingo_symbol_t *symbol)
Construct a symbol representing an id.
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_ast_attribute_size_ast_array
CLINGO_VISIBILITY_DEFAULT bool clingo_ast_attribute_size_ast_array(clingo_ast_t *ast, clingo_ast_attribute_t attribute, size_t *size)
Get the size of an attribute of type "clingo_ast_attribute_type_ast_array".
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_control_assign_external
CLINGO_VISIBILITY_DEFAULT bool clingo_control_assign_external(clingo_control_t *control, clingo_literal_t literal, clingo_truth_value_t value)
Assign a truth value to an external atom.
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_ast_build
CLINGO_VISIBILITY_DEFAULT bool clingo_ast_build(clingo_ast_type_t type, clingo_ast_t **ast,...)
Construct an AST of the given type.
clingo_program_builder_add
CLINGO_VISIBILITY_DEFAULT bool clingo_program_builder_add(clingo_program_builder_t *builder, clingo_ast_t *ast)
Adds a statement to the program.
clingo_truth_value_false
@ clingo_truth_value_false
false
Definition: clingo.h:201
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_program_builder_init
CLINGO_VISIBILITY_DEFAULT bool clingo_program_builder_init(clingo_control_t *control, clingo_program_builder_t **builder)
Get an object to add non-ground directives to the program.
clingo_program_builder_t
struct clingo_program_builder clingo_program_builder_t
Object to build non-ground programs.
Definition: clingo.h:3702
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_location::end_column
size_t end_column
the column where the location ends
Definition: clingo.h:217
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_program_builder_begin
CLINGO_VISIBILITY_DEFAULT bool clingo_program_builder_begin(clingo_program_builder_t *builder)
Begin building a program.
clingo_ast_sign_no_sign
@ clingo_ast_sign_no_sign
For positive literals.
Definition: clingo.h:3070
clingo_location::end_file
const char * end_file
the file where the location ends
Definition: clingo.h:213
clingo_symbol_t
uint64_t clingo_symbol_t
Represents a symbol.
Definition: clingo.h:330
clingo_location
Represents a source code location marking its beginnig and end.
Definition: clingo.h:211
clingo_symbolic_atom_iterator_t
uint64_t clingo_symbolic_atom_iterator_t
Object to iterate over symbolic atoms.
Definition: clingo.h:552
clingo_error_bad_alloc
@ clingo_error_bad_alloc
memory could not be allocated
Definition: clingo.h:145
clingo_symbolic_atoms_find
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_find(clingo_symbolic_atoms_t const *atoms, clingo_symbol_t symbol, clingo_symbolic_atom_iterator_t *iterator)
Find a symbolic atom given its symbolic representation.
clingo_location::begin_column
size_t begin_column
the column where the location begins
Definition: clingo.h:216
clingo_ast_callback_t
bool(* clingo_ast_callback_t)(clingo_ast_t *ast, void *data)
Callback function to intercept AST nodes.
Definition: clingo.h:3663
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.
clingo_ast_attribute_insert_ast_at
CLINGO_VISIBILITY_DEFAULT bool clingo_ast_attribute_insert_ast_at(clingo_ast_t *ast, clingo_ast_attribute_t attribute, size_t index, clingo_ast_t *value)
Insert a value into an attribute of type "clingo_ast_attribute_type_ast_array" at the given index.