1 #include <clingo.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 
print_model(clingo_model_t const * model)6 bool print_model(clingo_model_t const *model) {
7   bool ret = true;
8   clingo_symbol_t *atoms = NULL;
9   size_t atoms_n;
10   clingo_symbol_t const *it, *ie;
11   char *str = NULL;
12   size_t str_n = 0;
13 
14   // determine the number of (shown) symbols in the model
15   if (!clingo_model_symbols_size(model, clingo_show_type_shown, &atoms_n)) { goto error; }
16 
17   // allocate required memory to hold all the symbols
18   if (!(atoms = (clingo_symbol_t*)malloc(sizeof(*atoms) * atoms_n))) {
19     clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for atoms");
20     goto error;
21   }
22 
23   // retrieve the symbols in the model
24   if (!clingo_model_symbols(model, clingo_show_type_shown, atoms, atoms_n)) { goto error; }
25 
26   printf("Model:");
27 
28   for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {
29     size_t n;
30     char *str_new;
31 
32     // determine size of the string representation of the next symbol in the model
33     if (!clingo_symbol_to_string_size(*it, &n)) { goto error; }
34 
35     if (str_n < n) {
36       // allocate required memory to hold the symbol's string
37       if (!(str_new = (char*)realloc(str, sizeof(*str) * n))) {
38         clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for symbol's string");
39         goto error;
40       }
41 
42       str = str_new;
43       str_n = n;
44     }
45 
46     // retrieve the symbol's string
47     if (!clingo_symbol_to_string(*it, str, n)) { goto error; }
48 
49     printf(" %s", str);
50   }
51 
52   printf("\n");
53   goto out;
54 
55 error:
56   ret = false;
57 
58 out:
59   if (atoms) { free(atoms); }
60   if (str)   { free(str); }
61 
62   return ret;
63 }
64 
solve(clingo_control_t * ctl,clingo_solve_result_bitset_t * result,clingo_literal_t assumption)65 bool solve(clingo_control_t *ctl, clingo_solve_result_bitset_t *result, clingo_literal_t assumption) {
66   bool ret = true;
67   clingo_solve_handle_t *handle;
68   clingo_model_t const *model;
69 
70   // get a solve handle
71   if (!clingo_control_solve(ctl, clingo_solve_mode_yield, &assumption, assumption != 0 ? 1 : 0, NULL, NULL, &handle)) { goto error; }
72   // loop over all models
73   while (true) {
74     if (!clingo_solve_handle_resume(handle)) { goto error; }
75     if (!clingo_solve_handle_model(handle, &model)) { goto error; }
76     // print the model
77     if (model) { print_model(model); }
78     // stop if there are no more models
79     else       { break; }
80   }
81   // close the solve handle
82   if (!clingo_solve_handle_get(handle, result)) { goto error; }
83 
84   goto out;
85 
86 error:
87   ret = false;
88 
89 out:
90   // free the solve handle
91   return clingo_solve_handle_close(handle) && ret;
92 }
93 
main(int argc,char const ** argv)94 int main(int argc, char const **argv) {
95   char const *error_message;
96   int ret = 0;
97   size_t size;
98   clingo_solve_result_bitset_t solve_ret;
99   clingo_control_t *ctl = NULL;
100   clingo_part_t parts[] = {{ "base", NULL, 0 }};
101   clingo_theory_atoms_t const *atoms;
102   clingo_literal_t lit = 0;
103 
104   // create a control object and pass command line arguments
105   if (!clingo_control_new(argv+1, argc-1, NULL, NULL, 20, &ctl) != 0) { goto error; }
106 
107   // add a logic program to the base part
108   if (!clingo_control_add(ctl, "base", NULL, 0,
109     "#theory t {"
110     "  term   { + : 1, binary, left };"
111     "  &a/0 : term, any;"
112     "  &b/1 : term, {=}, term, any"
113     "}."
114     "x :- &a { 1+2 }."
115     "y :- &b(3) { } = 17."
116     )) { goto error; }
117 
118   // ground the base part
119   if (!clingo_control_ground(ctl, parts, 1, NULL, NULL)) { goto error; }
120 
121   // get the theory atoms container
122   if (!clingo_control_theory_atoms(ctl, &atoms)) { goto error; }
123 
124   // print the number of grounded theory atoms
125   if (!clingo_theory_atoms_size(atoms, &size)) { goto error; }
126   printf("number of grounded theory atoms: %zu\n", size);
127 
128   // verify that theory atom b has a guard
129   for (clingo_id_t atom = 0; atom < size; ++atom) {
130     clingo_id_t term;
131     char const *name;
132 
133     // get the term associated with the theory atom
134     if (!clingo_theory_atoms_atom_term(atoms, atom, &term)) { goto error; }
135     // get the name associated with the theory atom
136     if (!clingo_theory_atoms_term_name(atoms, term, &name)) { goto error; }
137     // we got theory atom b/1 here
138     if (strcmp(name, "b") == 0) {
139       bool guard;
140       if (!clingo_theory_atoms_atom_has_guard(atoms, atom, &guard)) { goto error; }
141       printf("theory atom b/1 has a guard: %s\n", guard ? "true" : "false");
142       // get the literal associated with the theory atom
143       if (!clingo_theory_atoms_atom_literal(atoms, atom, &lit)) { goto error; }
144       break;
145     }
146   }
147 
148   // solve using a model callback
149   if (!solve(ctl, &solve_ret, lit)) { goto error; }
150 
151   goto out;
152 
153 error:
154   if (!(error_message = clingo_error_message())) { error_message = "error"; }
155 
156   printf("%s\n", error_message);
157   ret = clingo_error_code();
158 
159 out:
160   if (ctl) { clingo_control_free(ctl); }
161 
162   return ret;
163 }
164 
165