Clingo
Loading...
Searching...
No Matches
app.c

The example shows how to extend the clingo application.

The example shows how to extend the clingo application.It behaves like a 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/app.h>
#include <clingo/control.h>
#include <clingo/solve.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *program;
size_t size;
} app_data_t;
static void program_name(void *data, clingo_string_t *string) {
(void)data;
string->data = "example";
string->size = strlen(string->data);
}
static void version(void *data, clingo_string_t *string) {
(void)data;
string->data = "1.0.0";
string->size = strlen(string->data);
}
static bool program_option_parser(char const *value, size_t size, void *data) {
char *str = NULL;
str = (char *)malloc(size);
if (str == NULL) {
char const *msg = "failed to allocate memory for program option";
return false;
}
memcpy(str, value, size);
app_data_t *app = (app_data_t *)data;
free((void *)app->program);
app->program = str;
app->size = size;
return true;
}
static bool register_options(clingo_options_t *options, void *data) {
char const *group = "Example";
char const *desc = "Override the default program part to ground.";
char const *option = "program";
char const *argument = "NAME";
return clingo_options_add(options, group, strlen(group), option, strlen(option), desc, strlen(desc),
program_option_parser, data, false, argument, strlen(argument));
}
static bool validate_options(void *data) {
(void)data;
// NOTE: Here compatibility checks can be performed. The example is valid
// for all options.
return true;
}
void *printer_data, void *data);
static bool print_model(clingo_model_t const *model, clingo_default_model_printer_t printer, void *printer_data,
void *data) {
(void)data;
(void)model;
// NOTE: Here model printing can be customized. We simply use the default
// model printer here.
return printer(printer_data);
}
static bool run(clingo_control_t *ctl, clingo_string_t const *files, size_t size, void *data) {
app_data_t *app = (app_data_t *)data;
char const *part = app->program ? app->program : "base";
clingo_part_t part_obj = {part, app->program ? app->size : strlen(part), NULL, 0};
if (!clingo_control_parse_files(ctl, files, size)) {
return false;
};
if (!clingo_control_set_parts(ctl, &part_obj, 1, true)) {
return false;
}
if (!clingo_control_main(ctl)) {
return false;
}
return true;
}
int main(int argc, char *argv[]) {
app_data_t app = {NULL, 0};
clingo_lib_t *lib = NULL;
clingo_application_t application = {program_name, version, run, print_model, register_options, validate_options};
int code = EXIT_FAILURE;
size_t const limit = 20;
clingo_string_t *clingo_argv = malloc((argc - 1) * sizeof(clingo_string_t));
if (clingo_argv == NULL) {
fprintf(stderr, "failed to allocate memory for arguments\n");
goto cleanup;
}
for (int i = 1; i < argc; ++i) {
clingo_argv[i - 1].data = argv[i];
clingo_argv[i - 1].size = strlen(argv[i]);
}
if (!clingo_lib_new(flags, level, NULL, NULL, limit, &lib)) {
fprintf(stderr, "failed to create library\n");
goto cleanup;
};
if (!clingo_main(lib, clingo_argv, argc - 1, &application, &app, &code)) {
goto cleanup;
}
cleanup:
free(clingo_argv);
free(app.program);
return code;
}
bool(* clingo_default_model_printer_t)(void *data)
Callback to print a model in default format.
Definition app.h:62
struct clingo_options clingo_options_t
Object to add command-line options.
Definition app.h:46
bool(* clingo_model_printer_t)(clingo_model_t const *model, clingo_default_model_printer_t printer, void *printer_data, void *data)
Callback to customize model printing.
Definition app.h:71
CLINGO_VISIBILITY_DEFAULT bool clingo_main(clingo_lib_t *lib, clingo_string_t const *arguments, size_t size, clingo_application_t const *app, void *data, int *code)
Run an application with the given library and arguments.
CLINGO_VISIBILITY_DEFAULT bool clingo_options_add(clingo_options_t *options, char const *group, size_t group_size, char const *option, size_t option_size, char const *description, size_t description_size, clingo_option_parser_t parser, void *data, bool multi, char const *argument, size_t argument_size)
Add an option that is processed with a custom parser.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_parse_files(clingo_control_t *control, clingo_string_t const *files, size_t size)
Extend the logic program with a program in a file.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_main(clingo_control_t *control)
Execute the default ground and solve flow after parsing.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_set_parts(clingo_control_t *control, clingo_part_t const *parts, size_t size, bool has_value)
Set the program parts to ground.
CLINGO_VISIBILITY_DEFAULT void clingo_lib_release(clingo_lib_t *lib)
Release a library object created with clingo_lib_new().
CLINGO_VISIBILITY_DEFAULT bool clingo_set_error(clingo_result_t code, char const *message, size_t size)
Set an error in the current thread.
CLINGO_VISIBILITY_DEFAULT bool clingo_lib_new(clingo_lib_flags_t flags, clingo_log_level_t level, clingo_logger_t const *logger, void *data, size_t limit, clingo_lib_t **lib)
Create a library object.
int clingo_log_level_t
Corresponding type to clingo_log_level_e.
Definition core.h:149
struct clingo_lib clingo_lib_t
A library object storing global information.
Definition core.h:171
uint32_t clingo_lib_flags_t
Bitset of clingo_lib_flags_e.
Definition core.h:180
@ clingo_log_level_info
the info level
Definition core.h:144
@ clingo_result_bad_alloc
memory could not be allocated
Definition core.h:101
@ clingo_lib_flags_slotted
use custom allocator for storing symbols
Definition core.h:175
@ clingo_lib_flags_fast_release
whether to enable fast release of libraries
Definition core.h:177
struct clingo_model clingo_model_t
Object representing a model.
Definition model.h:43
@ model
The model represents a stable model.
auto main(Library &lib, std::span< std::string_view const > arguments={}, App *app=nullptr, bool raise_errors=false) -> int
Run a clingo based application with the given arguments.
Definition app.hh:221
auto version() -> std::tuple< int, int, int >
Get the version of the Clingo library as a tuple.
Definition core.hh:730
This struct contains a set of functions to customize the clingo application.
Definition app.h:75
Struct used to specify the program parts that have to be grounded.
Definition control.h:57
Struct to capture strings that are not null-terminated.
Definition core.h:86
size_t size
the length of the string
Definition core.h:88
char const * data
pointer to the beginning of the string
Definition core.h:87