File: application.c

package info (click to toggle)
gringo 5.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,128 kB
  • sloc: cpp: 210,867; ansic: 37,507; python: 11,271; yacc: 825; javascript: 627; sh: 368; xml: 364; makefile: 102
file content (122 lines) | stat: -rw-r--r-- 3,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <clingo.h>
#include <stdio.h>
#include <stdlib.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_solve_handle_t *handle;
    clingo_model_t const *model;
    clingo_solve_result_bitset_t result;

    // 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);
}