static int unify_model(term_t t, int show, clingo_model_t *model) { clingo_symbol_t *atoms = NULL; size_t alen; int rc; if (!(rc = clingo_status(clingo_model_symbols_size(model, show, &alen)))) { goto out; } atoms = malloc(sizeof(*atoms) * alen); if (!atoms) { rc = PL_resource_error("memory"); goto out; } if (!(rc = clingo_status(clingo_model_symbols(model, show, atoms, alen)))) { goto out; } if (!(rc = unify_list_from_span(t, atoms, alen))) { goto out; } out: free(atoms); return rc; }
bool print_model(clingo_model_t const *model) { bool ret = true; clingo_symbol_t *atoms = NULL; size_t atoms_n; clingo_symbol_t const *it, *ie; char *str = NULL; size_t str_n = 0; // determine the number of (shown) symbols in the model if (!clingo_model_symbols_size(model, clingo_show_type_shown, &atoms_n)) { goto error; } // allocate required memory to hold all the symbols if (!(atoms = (clingo_symbol_t*)malloc(sizeof(*atoms) * atoms_n))) { clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for atoms"); goto error; } // retrieve the symbols in the model if (!clingo_model_symbols(model, clingo_show_type_shown, atoms, atoms_n)) { goto error; } printf("Model:"); for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) { size_t n; char *str_new; // determine size of the string representation of the next symbol in the model if (!clingo_symbol_to_string_size(*it, &n)) { goto error; } if (str_n < n) { // allocate required memory to hold the symbol's string if (!(str_new = (char*)realloc(str, sizeof(*str) * n))) { clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for symbol's string"); goto error; } str = str_new; str_n = n; } // retrieve the symbol's string if (!clingo_symbol_to_string(*it, str, n)) { goto error; } printf(" %s", str); } printf("\n"); goto out; error: ret = false; out: if (atoms) { free(atoms); } if (str) { free(str); } return ret; }
int main(int argc, char const **argv) { clingo_error_t ret; clingo_control_t *ctl = NULL; clingo_solve_iteratively_t *solve_it = NULL; clingo_part_t parts[] = {{ "base", NULL, 0 }}; clingo_symbol_t *atoms = NULL; size_t n; size_t atoms_n = 0; char *str = NULL; size_t str_n = 0; E(clingo_control_new(argv+1, argc-1, &logger, NULL, 20, &ctl)); E(clingo_control_add(ctl, "base", NULL, 0, "a :- not b. b :- not a.")); E(clingo_control_ground(ctl, parts, 1, NULL, NULL)); E(clingo_control_solve_iteratively(ctl, NULL, 0, &solve_it)); for (;;) { clingo_model_t *m; clingo_symbol_t const *atoms_it, *atoms_ie; E(clingo_solve_iteratively_next(solve_it, &m)); if (!m) { break; } E(clingo_model_symbols_size(m, clingo_show_type_atoms | clingo_show_type_csp, &n)); A(atoms, clingo_symbol_t, atoms_n, n, "failed to allocate memory for atoms"); E(clingo_model_symbols(m, clingo_show_type_atoms | clingo_show_type_csp, atoms, n)); printf("Model:"); for (atoms_it = atoms, atoms_ie = atoms + n; atoms_it != atoms_ie; ++atoms_it) { E(clingo_symbol_to_string_size(*atoms_it, &n)); A(str, char, str_n, n, "failed to allocate memory for symbol's string"); E(clingo_symbol_to_string(*atoms_it, str, n)); printf(" %s", str); } printf("\n"); } cleanup: if (str) { free(str); } if (atoms) { free(atoms); } if (solve_it) { clingo_solve_iteratively_close(solve_it); } if (ctl) { clingo_control_free(ctl); } return ret; }