Python

Predicate

Predicate class and public members in the unified Python API.

Class: Predicate

A Predicate names a relation, fixes its arity and declares its fact origin and query permission.

Python · dataclassSince v0.10.0
Predicatefrozen=True
Immutable declaration of one predicate; the class methods below provide convenient constructors for common flag combinations.
Creation and access
  • constructor
    Predicate(name, arity, flags)

    Creates an immutable predicate declaration from its name, arity and integer flags. Combine a fact origin with optional PRED_QUERY using |; engine.register_domain() validates and registers the declaration before rules are loaded.

  • factory
    Predicate.edb()

    Constructs a declaration for a predicate whose facts come from runtime request input. Creating this value does not register it with the engine.

  • factory
    Predicate.edb_query()

    Constructs a runtime-input predicate declaration with query permission, so callers may test its facts in a result.

  • factory
    Predicate.idb()

    Constructs a declaration for a predicate whose facts are derived by policy rules, without enabling public queries.

  • factory
    Predicate.idb_query()

    Constructs a derived-predicate declaration with query permission, so callers may inspect the relation after solving.

  • factory
    Predicate.policy_fact()

    Constructs a declaration for a predicate whose ground facts may be written directly in policy source.

  • factory
    Predicate.policy_fact_query()

    Constructs a policy-source fact declaration with query permission, so callers may test those facts in a result.

Fields
namestrproperty
Relation name used in policy rules, facts and queries.
arityintproperty
Number of terms in each fact or rule atom.
flagsintproperty
Integer fact-origin and optional query-permission bitmask; see the Constants cards.
Example
from maelys_datalog import Predicate, PRED_EDB

predicate = Predicate("owns", 2, PRED_EDB)
print(predicate)
Outputv0.27.0, small profile
Predicate(name='owns', arity=2, flags=1)

Constructor: Predicate()

Python · constructorSince v0.10.0
Predicate
Predicate(
name: str,
arity: int,
flags: int
) -> Predicate
Creates an immutable predicate declaration from its name, arity and integer flags. Combine a fact origin with optional PRED_QUERY using |; engine.register_domain() validates and registers the declaration before rules are loaded.
Arguments
namestr
Relation name used by policy rules, facts and queries.
arityint
Number of terms in each fact or rule atom.
flagsint
One fact origin, optionally combined with PRED_QUERY using | (for example PRED_IDB | PRED_QUERY). Query permission does not change the fact's origin.
Return value
Predicate

Returns an immutable Predicate declaration.

Example
from maelys_datalog import Predicate, PRED_EDB

predicate = Predicate("owns", 2, PRED_EDB)
print(predicate)
Outputv0.27.0, small profile
Predicate(name='owns', arity=2, flags=1)

Factories

The six class methods below each construct a new immutable declaration. They do not register the domain.

Each factory has its own signature, arguments, return behavior and stable link.

Predicate.edb

Python · class methodSince v0.10.0
Predicate.edb
Predicate.edb(
name: str,
arity: int
) -> Predicate
Constructs a declaration for a predicate whose facts come from runtime request input. Creating this value does not register it with the engine.
Arguments
namestr
Predicate name used by policy rules and facts.
arityint
Number of terms in each fact of this predicate.
Return value
Predicate

Returns a new immutable Predicate with PRED_EDB; it does not register the domain.

Example
predicate = Predicate.edb("owns", 2)
print(predicate)
Predicate(name='owns', arity=2, flags=1)

Predicate.edb_query

Python · class methodSince v0.10.0
Predicate.edb_query
Predicate.edb_query(
name: str,
arity: int
) -> Predicate
Constructs a runtime-input predicate declaration with query permission, so callers may test its facts in a result.
Arguments
namestr
Predicate name used by policy rules and facts.
arityint
Number of terms in each fact of this predicate.
Return value
Predicate

Returns a new immutable Predicate with PRED_EDB | PRED_QUERY; it does not query or register anything.

Example
predicate = Predicate.edb_query("owns", 2)
print(predicate)
Predicate(name='owns', arity=2, flags=5)

Predicate.idb

Python · class methodSince v0.10.0
Predicate.idb
Predicate.idb(
name: str,
arity: int
) -> Predicate
Constructs a declaration for a predicate whose facts are derived by policy rules, without enabling public queries.
Arguments
namestr
Predicate name used by policy rules and facts.
arityint
Number of terms in each fact of this predicate.
Return value
Predicate

Returns a new immutable Predicate with PRED_IDB; it does not evaluate rules.

Example
predicate = Predicate.idb("allow", 2)
print(predicate)
Predicate(name='allow', arity=2, flags=2)

Predicate.idb_query

Python · class methodSince v0.10.0
Predicate.idb_query
Predicate.idb_query(
name: str,
arity: int
) -> Predicate
Constructs a derived-predicate declaration with query permission, so callers may inspect the relation after solving.
Arguments
namestr
Predicate name used by policy rules and facts.
arityint
Number of terms in each fact of this predicate.
Return value
Predicate

Returns a new immutable Predicate with PRED_IDB | PRED_QUERY; it does not run a query.

Example
predicate = Predicate.idb_query("allow", 2)
print(predicate)
Predicate(name='allow', arity=2, flags=6)

Predicate.policy_fact

Python · class methodSince v0.10.0
Predicate.policy_fact
Predicate.policy_fact(
name: str,
arity: int
) -> Predicate
Constructs a declaration for a predicate whose ground facts may be written directly in policy source.
Arguments
namestr
Predicate name used by policy rules and facts.
arityint
Number of terms in each fact of this predicate.
Return value
Predicate

Returns a new immutable Predicate with PRED_POLICY_FACT; it does not add a fact.

Example
predicate = Predicate.policy_fact("trusted", 1)
print(predicate)
Predicate(name='trusted', arity=1, flags=8)

Predicate.policy_fact_query

Python · class methodSince v0.10.0
Predicate.policy_fact_query
Predicate.policy_fact_query(
name: str,
arity: int
) -> Predicate
Constructs a policy-source fact declaration with query permission, so callers may test those facts in a result.
Arguments
namestr
Predicate name used by policy rules and facts.
arityint
Number of terms in each fact of this predicate.
Return value
Predicate

Returns a new immutable Predicate with PRED_POLICY_FACT | PRED_QUERY; it does not add or query a fact.

Example
predicate = Predicate.policy_fact_query("trusted", 1)
print(predicate)
Predicate(name='trusted', arity=1, flags=12)

These integer constants come from the CFFI extension's public C constants; they are not callable builders. See the cards below for their values and roles, and the usage examples for explicit declarations and factories.

PRED_EDB · PRED_IDB · PRED_QUERY · PRED_POLICY_FACT.

PRED_EDB

Python · constantSince v0.10.0
PRED_EDB

Runtime input facts supplied in the EDB for one evaluation.

Value

1 (int)

PRED_IDB

Python · constantSince v0.10.0
PRED_IDB

Facts derived by policy rules.

Value

2 (int)

PRED_QUERY

Python · constantSince v0.10.0
PRED_QUERY

Permission to observe a predicate through the query API; not a fact origin by itself.

Value

4 (int)

PRED_POLICY_FACT

Python · constantSince v0.10.0
PRED_POLICY_FACT

Base facts written in trusted policy source.

Value

8 (int)

Usage guides