Clingo C API
C API for clingo providing high level functions to control grounding and solving.
|
The example shows how to solve in the background.
Output (approximately)
./solve-async 0
pi = 3.
1415926535 8979323846 2643383279 5028841971 6939937510 5820974944
5923078164 0628620899 8628034825 3421170679 8214808651 3282306647
0938446095 5058223172 5359408128 4811174502 8410270193 8521105559
6446229489 5493038196 4428810975 6659334461 2847564823 3786783165
2712019091 4564856692 3460348610 4543266482 1339360726 0249141273
7245870066 0631558817 4881520920 9628292540 9171536436 7892590360
0113305305 4882046652 1384146951 9415116094 3305727036 5759591953
0921861173 8193261179 3105118548 0744623799 6274956735 1885752724
8912279381 8301194912 ...
Code
#ifndef WIN32
#include <stdlib.h>
#include <stdio.h>
#include <stdatomic.h>
#include <assert.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)type;
(void)event;
(void)goon;
atomic_flag *running = (atomic_flag*)data;
atomic_flag_clear(running);
}
return true;
}
int main(int argc, char const **argv) {
char const *error_message;
int ret = 0;
atomic_flag running = ATOMIC_FLAG_INIT;
uint64_t samples = 0;
uint64_t incircle = 0;
uint64_t x, y;
"1 { p(X); q(X) } 1 :- X = 1..n."
":- not n+1 { p(1..n); q(1..n) }.")) { goto error; }
atomic_flag_test_and_set(&running);
do {
++samples;
x = rand();
y = rand();
if (x * x + y * y <= (uint64_t)RAND_MAX * RAND_MAX) { incircle+= 1; }
}
while (atomic_flag_test_and_set(&running));
printf("pi = %g\n", 4.0*incircle/samples);
goto out;
error:
printf("%s\n", error_message);
out:
return ret;
}
#else
#include <stdio.h>
int main(int argc, char const **argv) {
printf("example requires c11, which is not available on windows");
return 0;
}
#endif
@ 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.
unsigned clingo_solve_event_type_t
Corresponding type to clingo_solve_event_type_e.
Definition: clingo.h:2301
CLINGO_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control)
Free a control object created with clingo_control_new().
@ clingo_solve_event_type_finish
Issued if the search has completed.
Definition: clingo.h:2298
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
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_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.
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_solve_mode_async
Enable non-blocking search.
Definition: clingo.h:2287
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.
uint64_t clingo_symbol_t
Represents a symbol.
Definition: clingo.h:330
@ clingo_error_bad_alloc
memory could not be allocated
Definition: clingo.h:145
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.