Clingo C API
C API for clingo providing high level functions to control grounding and solving.
|
The example shows how to inspect statistics.
Output
./statistics 0
Model: a
Model: b
problem:
lp:
atoms:
2
atoms_aux:
0
...
solving:
...
summary:
...
accu:
times:
...
models:
...
solving:
...
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;
}
(void)data;
(void)goon;
switch (type) {
uint64_t root, values, summary, value;
uint16_t n = 10, random = 1;
double sum = 0;
for (uint16_t i = 0; i < n; ++i) {
random = (random << 3) ^ (random ^ ((random & 0xf800) >> 13)) ^ i;
sum += random;
}
break;
}
}
return true;
}
bool ret = true;
while (true) {
if (model) { print_model(model); }
else { break; }
}
goto out;
error:
ret = false;
out:
}
void print_prefix(int depth) {
for (int i = 0; i < depth; ++i) {
printf(" ");
}
}
bool ret = true;
double value;
print_prefix(depth);
printf("%g\n", value);
break;
}
size_t size;
for (size_t i = 0; i < size; ++i) {
uint64_t subkey;
print_prefix(depth);
printf("%zu:\n", i);
if (!print_statistics(stats, subkey, depth+1)) { goto error; }
}
break;
}
size_t size;
for (size_t i = 0; i < size; ++i) {
char const *name;
uint64_t subkey;
print_prefix(depth);
printf("%s:\n", name);
if (!print_statistics(stats, subkey, depth+1)) { goto error; }
}
}
}
goto out;
error:
ret = false;
out:
return ret;
}
int main(int argc, char const **argv) {
char const *error_message;
int ret = 0;
uint64_t stats_key;
if (!solve(ctl, &solve_ret)) { goto error; }
if (!print_statistics(stats, stats_key, 0)) { goto error; }
goto out;
error:
printf("%s\n", error_message);
out:
return ret;
}
@ clingo_solve_event_type_statistics
Issued when the statistics can be updated.
Definition: clingo.h:2297
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_map_at(clingo_statistics_t const *statistics, uint64_t key, char const *name, uint64_t *subkey)
Lookup a subkey under the given name.
@ clingo_solve_mode_yield
Yield models in calls to clingo_solve_handle_model.
Definition: clingo.h:2288
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_map_add_subkey(clingo_statistics_t *statistics, uint64_t key, char const *name, clingo_statistics_type_t type, uint64_t *subkey)
Add a subkey with the given name.
@ 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.
int clingo_statistics_type_t
Corresponding type to clingo_statistics_type.
Definition: clingo.h:1925
unsigned clingo_solve_event_type_t
Corresponding type to clingo_solve_event_type_e.
Definition: clingo.h:2301
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_map_size(clingo_statistics_t const *statistics, uint64_t key, size_t *size)
Get the number of subkeys of a map entry.
@ clingo_statistics_type_value
the entry is a (double) value
Definition: clingo.h:1920
CLINGO_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control)
Free a control object created with clingo_control_new().
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_value_set(clingo_statistics_t *statistics, uint64_t key, double value)
Set the value of the given entry.
@ clingo_statistics_type_array
the entry is an array
Definition: clingo.h:1921
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_array_push(clingo_statistics_t *statistics, uint64_t key, clingo_statistics_type_t type, uint64_t *subkey)
Create the subkey at the end of an array entry.
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_value_get(clingo_statistics_t const *statistics, uint64_t key, double *value)
Get the value of the given entry.
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_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_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.
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_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_control_statistics(clingo_control_t const *control, clingo_statistics_t const **statistics)
Get a statistics object to inspect solver statistics.
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_map_subkey_name(clingo_statistics_t const *statistics, uint64_t key, size_t offset, char const **name)
Get the name associated with the offset-th subkey.
clingo_statistics_type_e
Enumeration for entries of the statistics.
Definition: clingo.h:1918
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
struct clingo_statistic clingo_statistics_t
Handle for the solver statistics.
Definition: clingo.h:1928
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_VISIBILITY_DEFAULT bool clingo_configuration_root(clingo_configuration_t const *configuration, clingo_id_t *key)
Get the root key of the configuration.
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_array_at(clingo_statistics_t const *statistics, uint64_t key, size_t offset, uint64_t *subkey)
Get the subkey at the given offset of an array entry.
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_control_configuration(clingo_control_t *control, clingo_configuration_t **configuration)
Get a configuration object to change the solver configuration.
uint64_t clingo_symbol_t
Represents a symbol.
Definition: clingo.h:330
@ clingo_statistics_type_map
the entry is a map
Definition: clingo.h:1922
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_root(clingo_statistics_t const *statistics, uint64_t *key)
Get the root key of the statistics.
@ clingo_statistics_type_empty
the entry is invalid (has neither of the types below)
Definition: clingo.h:1919
uint32_t clingo_id_t
Unsigned integer type used in various places.
Definition: clingo.h:125
CLINGO_VISIBILITY_DEFAULT bool clingo_configuration_map_at(clingo_configuration_t const *configuration, clingo_id_t key, char const *name, clingo_id_t *subkey)
Lookup a subkey under the given name.
CLINGO_VISIBILITY_DEFAULT bool clingo_configuration_value_set(clingo_configuration_t *configuration, clingo_id_t key, char const *value)
Set the value of an entry.
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_type(clingo_statistics_t const *statistics, uint64_t key, clingo_statistics_type_t *type)
Get the type of a key.
struct clingo_configuration clingo_configuration_t
Handle for to the solver configuration.
Definition: clingo.h:1744
@ clingo_error_bad_alloc
memory could not be allocated
Definition: clingo.h:145
CLINGO_VISIBILITY_DEFAULT bool clingo_statistics_array_size(clingo_statistics_t const *statistics, uint64_t key, size_t *size)
Get the size of an array entry.
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.