示例#1
0
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;
}
示例#2
0
文件: main.c 项目: Horrowind/clingo
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;
}
示例#3
0
static foreign_t pl_clingo_solve(term_t ccontrol, term_t assumptions,
                                 term_t Show, term_t Model, control_t h) {
    int rc = TRUE;
    solve_state *state = NULL;
    clingo_symbolic_literal_t *assump_vec = NULL;
    int control = PL_foreign_control(h);
    if (control == PL_FIRST_CALL) {
        size_t alen = 0;

        if (!(state = malloc(sizeof(*state)))) {
            rc = PL_resource_error("memory");
            goto out;
        }
        memset(state, 0, sizeof(*state));

        if (!(rc = get_clingo(ccontrol, &state->ctl))) {
            goto out;
        }

        if (PL_skip_list(assumptions, 0, &alen) != PL_LIST) {
            rc = PL_type_error("list", assumptions);
            goto out;
        }

        term_t tail = PL_copy_term_ref(assumptions);
        term_t head = PL_new_term_ref();

        if (!(assump_vec = malloc(sizeof(*assump_vec) * alen))) {
            rc = PL_resource_error("memory");
            goto out;
        }
        memset(assump_vec, 0, sizeof(*assump_vec) * alen);
        for (size_t i = 0; PL_get_list(tail, head, tail); i++) {
            if (!(rc = clingo_status(get_assumption(head, &assump_vec[i])))) {
                goto out;
            }
        }

        if (!(rc = clingo_status(clingo_control_solve_iteratively(
                  state->ctl->control, assump_vec, alen, &state->it)))) {
            goto out;
        }
    } else {
        state = PL_foreign_context_address(h);
    }

    while (control != PL_PRUNED) {
        clingo_model_t *model;

        if (!(rc = clingo_status(
                  clingo_solve_iteratively_next(state->it, &model)))) {
            goto out;
        }
        if (model) {
            int show;

            if (!(rc = get_show_map(Show, &show))) {
                goto out;
            }

            if (!(rc = unify_model(Model, show, model))) {
                if (PL_exception(0)) {
                    goto out;
                }
            } else {
                PL_retry_address(state);
                state = NULL;
                break;
            }

        } else {
            rc = FALSE;
            break;
        }
    }

out:
    if (assump_vec) {
        free(assump_vec);
    }
    if (state) {
        if (state->it) {
            clingo_solve_iteratively_close(state->it);
        }
        free(state);
    }
    return rc;
}