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

The example shows how to extend the clingo application.It behaves like a normal normal clingo but adds one option to override the default program part to ground.

Example calls

$ cat example.lp
b.
#program test.
t.
$ ./application --program test example.lp
example version 1.0.0
Reading from example.lp
Solving...
Answer: 1
t
SATISFIABLE
Models : 1+
Calls : 1
Time : 0.004s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time : 0.004s

Code

#include <clingo.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// struct to store parsed command line arguments
typedef struct options {
char const *program;
} options_t;
char const *name(void *data) {
(void)data;
// the name of the program printed in its help output
return "example";
}
char const *version(void *data) {
(void)data;
// the version of the program printed in its help output
return "1.0.0";
}
bool solve(clingo_control_t *ctl) {
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; }
if (!model) { 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;
}
bool parse_option(char const *value, void *data) {
char **program = (char **)data;
// allocate memory for program name
// note that we forgo freeing memory for the example
// (it could be done early in the main loop or after clingo_main finished)
if (!(*program = (char *)malloc(strlen(value) + 1))) {
return false;
}
strcpy(*program, value);
return true;
}
bool register_options(clingo_options_t *options, void *data) {
options_t *options_ = (options_t*)data;
// register an option to overwrite which program part to ground
return clingo_options_add(options, "Example", "program", "Override the default program part to ground", parse_option, &options_->program, false, "<prog>");
}
bool main_loop(clingo_control_t *ctl, char const *const *files, size_t size, void *data) {
options_t *options = (options_t*)data;
bool ret = true;
clingo_part_t parts[] = {{ options->program ? options->program : "base", NULL, 0 }};
char const *const *file;
// load files into the control object
for (file = files; file != files + size; ++file) {
if (!clingo_control_load(ctl, *file)) { goto error; }
}
// if no files are given read from stdin
if (size == 0) {
if (!clingo_control_load(ctl, "-")) { goto error; }
}
// ground
if (!clingo_control_ground(ctl, parts, 1, NULL, NULL)) { goto error; }
// solve
if (!solve(ctl)) { goto error; }
goto out;
error:
ret = false;
out:
return ret;
}
int main(int argc, char const **argv) {
options_t options = { NULL };
clingo_application_t app = { name, version, NULL, main_loop, NULL, NULL, register_options, NULL };
return clingo_main(&app, argv+1, argc-1, &options);
}
clingo_application
This struct contains a set of functions to customize the clingo application.
Definition: clingo.h:3835
clingo_main
CLINGO_VISIBILITY_DEFAULT int clingo_main(clingo_application_t *application, char const *const *arguments, size_t size, void *data)
Run clingo with a customized main function (similar to python and lua embedding).
clingo_options_t
struct clingo_options clingo_options_t
Object to add command-line options.
Definition: clingo.h:3805
clingo_solve_mode_yield
@ clingo_solve_mode_yield
Yield models in calls to clingo_solve_handle_model.
Definition: clingo.h:2288
clingo_control_t
struct clingo_control clingo_control_t
Control object holding grounding and solving state.
Definition: clingo.h:2693
clingo_options_add
CLINGO_VISIBILITY_DEFAULT bool clingo_options_add(clingo_options_t *options, char const *group, char const *option, char const *description, bool(*parse)(char const *value, void *data), void *data, bool multi, char const *argument)
Add an option that is processed with a custom parser.
clingo.h
clingo_part
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
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_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_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_control_load
CLINGO_VISIBILITY_DEFAULT bool clingo_control_load(clingo_control_t *control, char const *file)
Extend the logic program with a program in a file.
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_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_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).