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

The example shows how to iterate over symbolic atoms.

Output

./symbolic-atoms 0
Symbolic atoms:
b
c, external
a, fact

Code

#include <clingo.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct string_buffer {
char *string;
size_t string_n;
} string_buffer_t;
void free_string_buffer(string_buffer_t *buf) {
if (buf->string) {
free(buf->string);
buf->string = NULL;
buf->string_n = 0;
}
}
bool print_symbol(clingo_symbol_t symbol, string_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;
}
int main(int argc, char const **argv) {
char const *error_message;
string_buffer_t buf = {NULL, 0};
int ret = 0;
clingo_control_t *ctl = NULL;
clingo_part_t parts[] = {{ "base", NULL, 0 }};
clingo_symbolic_atom_iterator_t it_atoms, ie_atoms;
// 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, "a. {b}. #external c.")) { goto error; }
// ground the base part
if (!clingo_control_ground(ctl, parts, 1, NULL, NULL)) { goto error; }
// get symbolic atoms
if (!clingo_control_symbolic_atoms(ctl, &atoms)) { goto error; }
// get begin and end iterator
if (!clingo_symbolic_atoms_begin(atoms, NULL, &it_atoms)) { goto error; }
if (!clingo_symbolic_atoms_end(atoms, &ie_atoms)) { goto error; }
printf("Symbolic atoms:\n");
for (;;) {
bool equal, fact, external;
// check if we are at the end of the sequence
if (!clingo_symbolic_atoms_iterator_is_equal_to(atoms, it_atoms, ie_atoms, &equal)) { goto error; }
if (equal) { break; }
// get the associated symbol
if (!clingo_symbolic_atoms_symbol(atoms, it_atoms, &symbol)) { goto error; }
// determine if the atom is fact or external
if (!clingo_symbolic_atoms_is_fact(atoms, it_atoms, &fact)) { goto error; }
if (!clingo_symbolic_atoms_is_external(atoms, it_atoms, &external)) { goto error; }
printf(" ");
if (!print_symbol(symbol, &buf)) { goto error; }
if (fact) { printf(", fact"); }
if (external) { printf(", external"); }
printf("\n");
// advance the next element in the sequence
if (!clingo_symbolic_atoms_next(atoms, it_atoms, &it_atoms)) { goto error; }
}
goto out;
error:
if (!(error_message = clingo_error_message())) { error_message = "error"; }
printf("%s\n", error_message);
out:
free_string_buffer(&buf);
if (ctl) { clingo_control_free(ctl); }
return ret;
}
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_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_symbolic_atoms_symbol
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_symbol(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t iterator, clingo_symbol_t *symbol)
Get the symbolic representation of an atom.
clingo_symbolic_atoms_iterator_is_equal_to
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_iterator_is_equal_to(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t a, clingo_symbolic_atom_iterator_t b, bool *equal)
Check if two iterators point to the same element (or end of the sequence).
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.h
clingo_part
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
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_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_symbolic_atoms_end
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_end(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t *iterator)
Iterator pointing to the end of the sequence of symbolic atoms.
clingo_symbolic_atoms_next
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_next(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t iterator, clingo_symbolic_atom_iterator_t *next)
Get an iterator to the next element in the sequence of symbolic atoms.
clingo_symbolic_atoms_is_fact
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_is_fact(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t iterator, bool *fact)
Check whether an atom is a fact.
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_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_begin
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_begin(clingo_symbolic_atoms_t const *atoms, clingo_signature_t const *signature, clingo_symbolic_atom_iterator_t *iterator)
Get a forward iterator to the beginning of the sequence of all symbolic atoms optionally restricted t...
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_symbolic_atoms_is_external
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_is_external(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t iterator, bool *external)
Check whether an atom is external.