int main(int argc, char const **argv) { char const *error_message; int ret = 0; clingo_solve_iteratively_t *it = NULL; clingo_model_t *model; clingo_control_t *ctl = NULL; clingo_part_t parts[] = {{ "base", NULL, 0 }}; // create a control object and pass command line arguments if (clingo_control_new(argv+1, argc-1, NULL, NULL, 20, &ctl) != 0) { goto error; } // add a logic program to the base part if (clingo_control_add(ctl, "base", NULL, 0, "a :- not b. b :- not a.")) { goto error; } // ground the base part if (clingo_control_ground(ctl, parts, 1, NULL, NULL)) { goto error; } // solve using a model callback if (clingo_control_solve_iteratively(ctl, NULL, 0, &it)) { goto error; } for (;;) { // get the next model if (clingo_solve_iteratively_next(it, &model)) { goto error; } // stop if the search space has been exhausted or the requested number of models found if (!model) { break; } if (print_model(model)) { goto error; } } goto out; error: if (!(error_message = clingo_error_message())) { error_message = "error"; } printf("%s\n", error_message); ret = 1; out: if (it) { clingo_solve_iteratively_close(it); } if (ctl) { clingo_control_free(ctl); } return ret; }
bool solve(clingo_control_t *ctl, clingo_solve_result_bitset_t *result) { bool ret = true; clingo_solve_handle_t *handle; 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; } // print the model if (model) { print_model(model); } // stop if there are no more models else { 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; }
int Model_print_model (FILE *fp, Model_t *h) /*{{{*/ { Model_t *m; if (NULL == fp) return -1; fprintf (fp, "# id Temp Dens Abund Norm Vturb redshift\n"); fprintf (fp, "# (K) (cm^-3) (km/s) \n"); if (NULL == h) return 0; for (m = h; m != NULL; m = m->next) { if (-1 == print_model (fp, m)) return -1; } return 0; }