Clingo C API
C API for clingo providing high level functions to control grounding and solving.
|
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 <stdlib.h>
#include <stdio.h>
bool ret = true;
size_t atoms_n;
char *str = NULL;
size_t str_n = 0;
goto error;
}
printf("Model:");
for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {
size_t n;
char *str_new;
if (str_n < n) {
if (!(str_new = (char*)realloc(str, sizeof(*str) * n))) {
goto error;
}
str = str_new;
str_n = n;
}
printf(" %s", str);
}
printf("\n");
goto out;
error:
ret = false;
out:
if (atoms) { free(atoms); }
if (str) { free(str); }
return ret;
}
typedef struct {
} on_statement_data;
bool on_statement (
clingo_ast_t *stm, on_statement_data *data) {
bool ret = true;
size_t size;
if (type != clingo_ast_type_rule) {
goto out;
}
goto error;
}
goto error;
}
goto error;
}
goto out;
error:
ret = false;
out:
if (lit != NULL) {
}
return ret;
}
bool ret = true;
while (true) {
if (model) { print_model(model); }
else { break; }
}
goto out;
error:
ret = false;
out:
}
int main(int argc, char const **argv) {
char const *error_message;
int ret = 0;
on_statement_data data = {NULL, NULL, NULL};
data.loc = &location;
goto error;
}
goto error;
}
goto error;
}
printf("Solving with enable = false...\n");
if (!solve(ctl, &solve_ret)) { goto error; }
printf("Solving with enable = true...\n");
if (!solve(ctl, &solve_ret)) { goto error; }
printf("Solving with enable = false...\n");
if (!solve(ctl, &solve_ret)) { goto error; }
goto out;
error:
printf("%s\n", error_message);
out:
return ret;
}
@ clingo_truth_value_true
true
Definition: clingo.h:200
struct clingo_ast clingo_ast_t
This struct provides a view to nodes in the AST.
Definition: clingo.h:3287
size_t end_line
the line where the location ends
Definition: clingo.h:215
@ clingo_solve_mode_yield
Yield models in calls to clingo_solve_handle_model.
Definition: clingo.h:2288
@ clingo_show_type_shown
Select shown atoms and terms.
Definition: clingo.h:2098
struct clingo_control clingo_control_t
Control object holding grounding and solving state.
Definition: clingo.h:2693
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.
size_t begin_line
the line where the location begins
Definition: clingo.h:214
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_VISIBILITY_DEFAULT bool clingo_ast_get_type(clingo_ast_t *ast, clingo_ast_type_t *type)
Get the type of an AST node.
int clingo_ast_type_t
Corresponding type to clingo_ast_type_e.
Definition: clingo.h:3187
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.
const char * begin_file
the file where the location begins
Definition: clingo.h:212
CLINGO_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control)
Free a control object created with clingo_control_new().
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_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.
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition: clingo.h:121
CLINGO_VISIBILITY_DEFAULT void clingo_ast_release(clingo_ast_t *ast)
Decrement the reference count of an AST node.
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
CLINGO_VISIBILITY_DEFAULT bool clingo_program_builder_end(clingo_program_builder_t *builder)
End building a program.
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_close(clingo_solve_handle_t *handle)
Stops the running search and releases the handle.
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_VISIBILITY_DEFAULT bool clingo_symbol_create_id(char const *name, bool positive, clingo_symbol_t *symbol)
Construct a symbol representing an id.
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_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_VISIBILITY_DEFAULT bool clingo_solve_handle_get(clingo_solve_handle_t *handle, clingo_solve_result_bitset_t *result)
Get the next solve result.
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_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_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.
struct clingo_model clingo_model_t
Object representing a model.
Definition: clingo.h:2085
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_VISIBILITY_DEFAULT bool clingo_ast_build(clingo_ast_type_t type, clingo_ast_t **ast,...)
Construct an AST of the given type.
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
false
Definition: clingo.h:201
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_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_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.
struct clingo_program_builder clingo_program_builder_t
Object to build non-ground programs.
Definition: clingo.h:3702
struct clingo_solve_handle clingo_solve_handle_t
Search handle to a solve call.
Definition: clingo.h:2322
unsigned clingo_solve_result_bitset_t
Definition: clingo.h:2248
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).
size_t end_column
the column where the location ends
Definition: clingo.h:217
CLINGO_VISIBILITY_DEFAULT clingo_error_t clingo_error_code()
Get the last error code set by a clingo API call.
CLINGO_VISIBILITY_DEFAULT const char * clingo_error_message()
Get the last error message set if an API call fails.
CLINGO_VISIBILITY_DEFAULT bool clingo_program_builder_begin(clingo_program_builder_t *builder)
Begin building a program.
@ clingo_ast_sign_no_sign
For positive literals.
Definition: clingo.h:3070
const char * end_file
the file where the location ends
Definition: clingo.h:213
uint64_t clingo_symbol_t
Represents a symbol.
Definition: clingo.h:330
Represents a source code location marking its beginnig and end.
Definition: clingo.h:211
uint64_t clingo_symbolic_atom_iterator_t
Object to iterate over symbolic atoms.
Definition: clingo.h:552
@ clingo_error_bad_alloc
memory could not be allocated
Definition: clingo.h:145
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.
size_t begin_column
the column where the location begins
Definition: clingo.h:216
bool(* clingo_ast_callback_t)(clingo_ast_t *ast, void *data)
Callback function to intercept AST nodes.
Definition: clingo.h:3663
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_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_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.