The example shows how to rewrite a non-ground logic program.
./ast 0
Solving with enable = false...
Model:
Solving with enable = true...
Model: enable a
Model: enable b
Solving with enable = false...
Model:
#include <stdlib.h>
#include <stdio.h>
(void)data;
bool ret = true;
size_t atoms_n;
char *str = NULL;
size_t str_n = 0;
goto error;
}
printf("Model:");
for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {
size_t n;
char *str_new;
if (str_n < n) {
if (!(str_new = (char*)realloc(str, sizeof(*str) * n))) {
goto error;
}
str = str_new;
str_n = n;
}
printf(" %s", str);
}
printf("\n");
*goon = true;
goto out;
error:
ret = false;
out:
if (atoms) { free(atoms); }
if (str) { free(str); }
return ret;
}
typedef struct {
} on_statement_data;
bool ret = true;
if (stm->
type != clingo_ast_statement_type_rule) {
goto out;
}
if (!body) {
goto error;
}
for (size_t i = 0; i < stm->rule->size; ++i) {
body[i] = stm->rule->
body[i];
}
lit.
symbol = &data->atom;
lit.
location = data->atom.location;
lit.
type = clingo_ast_literal_type_symbolic;
lit.
sign = clingo_ast_sign_none;
body[stm->rule->size].
location = data->atom.location;
body[stm->rule->size].
type = clingo_ast_body_literal_type_literal;
body[stm->rule->size].
sign = clingo_ast_sign_none;
body[stm->rule->size].
literal = &lit;
rule.
head = stm->rule->head;
rule.size = stm->rule->size + 1;
rule.body = body;
stm2.
location = stm->location;
stm2.type = stm->type;
stm2.rule = &rule;
goto out;
error:
ret = false;
out:
if (body) { free(body); }
return ret;
}
int main(int argc, char const **argv) {
char const *error_message;
int ret = 0;
on_statement_data data;
data.atom.location = location;
data.atom.type = clingo_ast_term_type_symbol;
data.atom.symbol = sym;
if (!
clingo_parse_program(
"a :- not b. b :- not a.", (clingo_ast_callback_t*)on_statement, &data, NULL, NULL, 20)) {
goto error; }
stm.location = location;
stm.type = clingo_ast_statement_type_external;
printf("Solving with enable = false...\n");
printf("Solving with enable = true...\n");
printf("Solving with enable = false...\n");
goto out;
error:
printf("%s\n", error_message);
out:
return ret;
}