Example #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;
}
Example #2
0
int run(clingo_control_t *ctl) {
    char const *base_params[] = { 0 };
    if (report_error(clingo_control_add(ctl, "base", base_params, "a :- not b. b :- not a."))) { return 0; }
    clingo_value_t empty[] = {};
    clingo_part_t part_vec[] = { {"base", { empty, 0 } } };
    clingo_part_span_t part_span = { part_vec, 1 };
    if (report_error(clingo_control_ground(ctl, part_span, 0, 0))) { return 0; }
    if (!solve(ctl)) { return 0; }
    if (!solve_iter(ctl)) { return 0; }
    return 1;
}
Example #3
0
static foreign_t pl_clingo_add(term_t ccontrol, term_t params, term_t program) {
    char *prog;
    clingo_env *ctl;
    atom_t name;
    size_t arity;
    char *param_buf[FAST_PARAMS];
    char **prog_params = param_buf;
    term_t arg = PL_new_term_ref();
    int rc;

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

    if (!get_name_arity(params, &name, &arity)) {
        rc = PL_type_error("callable", params);
        goto out;
    }

    if (arity + 1 > FAST_PARAMS &&
        !(prog_params = malloc(sizeof(char *) * arity))) {
        rc = PL_resource_error("memory");
        goto out;
    }

    for (size_t i = 0; i < arity; i++) {
        _PL_get_arg(i + 1, params, arg);
        if (!(rc =
                  get_null_terminated_string(arg, &prog_params[i], CVT_ATOM))) {
            goto out;
        }
    }
    if (!(rc = get_null_terminated_string(program, &prog,
                                          CVT_ATOM | CVT_STRING | CVT_LIST |
                                              BUF_DISCARDABLE))) {
        goto out;
    }
    if (!(rc = clingo_status(
              clingo_control_add(ctl->control, PL_atom_chars(name),
                                 (const char **)prog_params, arity, prog)))) {
        goto out;
    }

out:
    if (prog_params != param_buf) {
        free(prog_params);
    }

    return rc;
}
Example #4
0
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;
}
Example #5
0
int main(int argc, char const **argv) {
  char const *error_message;
  int ret = 0;
  size_t offset;
  clingo_solve_result_bitset_t solve_ret;
  clingo_control_t *ctl;
  clingo_symbolic_atoms_t *atoms;
  clingo_backend_t *backend;
  clingo_atom_t atom_ids[4];
  char const *atom_strings[] = {"a", "b", "c"};
  clingo_literal_t body[2];
  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; b; c}.")) { goto error; }

  // ground the base part
  if (!clingo_control_ground(ctl, parts, 1, NULL, NULL)) { goto error; }

  // get the container for symbolic atoms
  if (!clingo_control_symbolic_atoms(ctl, &atoms)) { goto error; }
  // get the ids of atoms a, b, and c
  offset = 0;
  for (char const **it = atom_strings, **ie = it + sizeof(atom_strings) / sizeof(*atom_strings); it != ie; ++it) {
    clingo_symbol_t sym;
    clingo_symbolic_atom_iterator_t atom_it, atom_ie;
    clingo_literal_t lit;
    bool equal;

    // lookup the atom
    if (!clingo_symbol_create_id(*it, true, &sym)) { goto error; }
    if (!clingo_symbolic_atoms_find(atoms, sym, &atom_it)) { goto error; }
    if (!clingo_symbolic_atoms_end(atoms, &atom_ie)) { goto error; }
    if (!clingo_symbolic_atoms_iterator_is_equal_to(atoms, atom_it, atom_ie, &equal)) { goto error; }
    assert(!equal); (void)equal;

    // get the atom's id
    if (!clingo_symbolic_atoms_literal(atoms, atom_it, &lit)) { goto error; }
    atom_ids[offset++] = lit;
  }

  // get the backend
  if (!clingo_control_backend(ctl, &backend)) { goto error; }

  // prepare the backend for adding rules
  if (!clingo_backend_begin(backend)) { goto error; }

  // add an additional atom (called d below)
  if (!clingo_backend_add_atom(backend, NULL, &atom_ids[3])) { goto error; }

  // add rule: d :- a, b.
  body[0] = atom_ids[0];
  body[1] = atom_ids[1];
  if (!clingo_backend_rule(backend, false, &atom_ids[3], 1, body, sizeof(body)/sizeof(*body))) { goto error; }

  // add rule: :- not d, c.
  body[0] = -(clingo_literal_t)atom_ids[3];
  body[1] = atom_ids[2];
  if (!clingo_backend_rule(backend, false, NULL, 0, body, sizeof(body)/sizeof(*body))) { goto error; }

  // finalize the backend
  if (!clingo_backend_end(backend)) { goto error; }

  // solve
  if (!solve(ctl, &solve_ret)) { goto error; }

  goto out;

error:
  if (!(error_message = clingo_error_message())) { error_message = "error"; }

  printf("%s\n", error_message);
  ret = clingo_error_code();

out:
  if (ctl) { clingo_control_free(ctl); }

  return ret;
}