C Extension API

Frontends

Translate a native policy format into validated facts and rules.

Use a native frontend to translate your own policy format into facts and rules that the core validates. The Frontends guide executes a small grant-command frontend and then queries its policy with the ordinary solver.

Handle: program_builder_t

C · handleSince v0.1.0-alpha.4
program_builder_t

#include <maelys/datalog_frontend.h>

typedef struct maelys_datalog_program_builder maelys_datalog_program_builder_t;
Callback-scoped builder supplied by the loader to a frontend.

Functions

program_add_fact()

C · functionSince v0.1.0-alpha.4
program_add_fact()

#include <maelys/datalog_frontend.h>

maelys_datalog_status_t
maelys_datalog_program_add_fact(
maelys_datalog_program_builder_t *builder,
const maelys_datalog_ir_atom_t *atom
);
Copy one static policy fact into the program being built by a frontend.
Arguments
buildermaelys_datalog_program_builder_t *handlemodified
Builder supplied to the active lowering callback; it cannot be retained after that callback returns.
atomconst maelys_datalog_ir_atom_t *in
Static policy fact whose ground terms use the host-registered domain. The record and referenced data are copied synchronously.
Return status

maelys_datalog_status_t — OK on success.

Returns OK after copying the record. Invalid records or exhausted limits fail; a builder error is sticky, so ignoring it cannot make the policy load succeed.

Example
maelys_datalog_status_t status = maelys_datalog_program_add_fact(builder, &atom);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("fact predicate: %s\n", atom.predicate);
status: ok
fact predicate: approved

program_add_rule()

C · functionSince v0.1.0-alpha.4
program_add_rule()

#include <maelys/datalog_frontend.h>

maelys_datalog_status_t
maelys_datalog_program_add_rule(
maelys_datalog_program_builder_t *builder,
const maelys_datalog_ir_rule_t *rule
);
Copy one normalized rule into the program being built by a frontend.
Arguments
buildermaelys_datalog_program_builder_t *handlemodified
Builder supplied to the active lowering callback; it cannot be retained after that callback returns.
ruleconst maelys_datalog_ir_rule_t *in
Normalized rule whose predicates, variables, expressions and literals will be checked by the core. The record and referenced data are copied synchronously.
Return status

maelys_datalog_status_t — OK on success.

Returns OK after copying the record. Invalid records or exhausted limits fail; a builder error is sticky, so ignoring it cannot make the policy load succeed.

Example
maelys_datalog_status_t status = maelys_datalog_program_add_rule(builder, &rule);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("rule head: %s\n", rule.head.predicate);
status: ok
rule head: allow

Struct: frontend_t

C · structSince v0.1.0-alpha.4
frontend_t

#include <maelys/datalog_frontend.h>

Native frontend descriptor that lowers its input language into public program records.
Fields
abi_versionuint32_t
Program IR ABI version accepted by this frontend; use PROGRAM_ABI_VERSION from the installed SDK.
struct_sizesize_t
Complete descriptor size, supplied as sizeof the descriptor.
nameconst char *
Public component name used for explicit frontend selection.
semantic_idconst char *
Stable identity of the lowering behavior; change it when those semantics change.
lowermaelys_datalog_status_t (*)(const char *, size_t, maelys_datalog_program_builder_t *, maelys_datalog_diagnostic_t *)
Lower the supplied source bytes into the borrowed builder and return a status. Do not retain the source or builder, extend the host domain, or reenter policy loading. Successful lowering is followed by core validation.
Typedef
typedef struct maelys_datalog_frontend_t {
    uint32_t abi_version;
    size_t struct_size;
    const char *name;
    const char *semantic_id;
    maelys_datalog_status_t (*lower)(const char *source, size_t source_length, maelys_datalog_program_builder_t *builder,
                                     maelys_datalog_diagnostic_t *out_diagnostic);
} maelys_datalog_frontend_t;

Usage guides