Python
Ruleset
Ruleset class and public members in the unified Python API.| Used by | Edb() |
|---|
Class: Ruleset
Internal constructor — do not call. Native policy resource returned by Engine loaders; do not manufacture native handles.
- returned by
Engine.load_inline_ruleset()Loads one policy directly from Datalog source text against a registered domain, without reading a manifest.
- returned by
Engine.load_manifest()Loads a policy set from a manifest file, applying its source-hash checks, domain validation and public query permissions.
- Ruleset.close()Closes the loaded policy set and its dependent Python resources when the application has finished using them.
- Ruleset.edb()Creates an empty input buffer associated with this ruleset. Fill it with runtime facts before solving a request.
- Ruleset.prepare()Prepares one selected policy for reuse across requests, with the chosen session options. Preparation does not yet supply facts or compute an answer.
- Ruleset.program_counts()Read compiled predicate, policy-fact and normalized-rule counts for one policy.
- Ruleset.reads_predicate()Checks whether the selected compiled policy reads a named predicate in its rule body, without preparing a session.
- Ruleset.solve()Evaluates one complete input batch using a private session created for this call. Use prepare() when the same session should be reused for multiple requests.
- with ruleset:Leaving the block closes this Ruleset and its sessions, results and input buffers.
print("policies:", ruleset.policy_count)
print("fingerprint:", ruleset.fingerprint)policies: 1 fingerprint: 4dca17fe86e7af4535ed3b9c718bfb545d8a748feb9ea5ea6ac3c5f541c0b514
Methods
Each method has its own signature, arguments, return behavior and stable link.
Ruleset.close
Close the ruleset when none of its sessions, results or input buffers are needed. Its children are closed first.
Ruleset.close() -> NoneNoneReturns None after releasing the ruleset and remaining native children. Repeated closure is harmless; those children cannot subsequently be used.
Ruleset.edb
Keep request facts separate from the compiled policy. Optional capacities reserve bounded input storage; they do not change solver limits.
Ruleset.edb(
*,
fact_capacity: int | None = None,
text_capacity: int | None = None
) -> EdbEdbReturns a new, empty Edb associated with this Ruleset. Invalid capacities raise TypeError or ValueError; native creation failures raise MaelysDatalogError.
edb = ruleset.edb()
edb.add_fact("owns", ["alice", "roadmap.pdf"])
print("facts:", len(edb))facts: 1
Ruleset.prepare
Prepare one selected policy once, then evaluate successive complete requests. Optional explanation storage is reserved here, not during policy loading.
Ruleset.prepare(
policy_index: int = 0,
*,
required_capabilities: int = 0,
work_limit: int = 0,
explanations: ExplanationKind | int = 0,
capacities: SessionCapacities | None = None
) -> SessionSessionReturns a new Session without solving any facts. Invalid options raise Python exceptions; unsupported backend requirements raise MaelysDatalogError. One session may retain only one live result.
from maelys_datalog import ExplanationKind
with ruleset.prepare(
explanations=ExplanationKind.TRUE | ExplanationKind.FALSE,
) as session:
with session.solve(edb) as result:
print(result.contains_fact("allow", ["alice", "roadmap.pdf"]))Ruleset.program_counts
Read counts for one enabled policy without preparing a session. This is introspection of the loaded program, not a request EDB or solve result.
Ruleset.program_counts(
policy_index: int = 0
) -> ProgramCountsProgramCountsReturns an immutable ProgramCounts. An invalid index raises IndexError, a non-integer index raises TypeError, and a closed owner raises RuntimeError.
counts = ruleset.program_counts(0)
print(counts)ProgramCounts(predicates=4, facts=0, rules=1)
Ruleset.reads_predicate
Ruleset.reads_predicate(
name: str,
arity: int,
policy_index: int = 0
) -> boolboolReturns True for a body read, including a negated atom or aggregate source, and False otherwise. Rule heads and static facts do not count. Invalid integer types raise TypeError; negative values raise ValueError; an out-of-range policy index raises IndexError; a closed ruleset raises RuntimeError.
reads = ruleset.reads_predicate("owns", 2)
print("reads owns/2:", "yes" if reads else "no")reads owns/2: yes
Ruleset.solve
Choose this convenience method for an independent evaluation. For successive requests that share prepared memory, use Ruleset.prepare() instead.
Ruleset.solve(
edb: Edb,
*,
policy_index: int = 0,
required_capabilities: int = 0,
work_limit: int = 0,
explanations: ExplanationKind | int = 0,
capacities: SessionCapacities | None = None
) -> SolveResultSolveResultReturns a SolveResult owning its private session. Closing the result closes that session. A failed solve raises and publishes no partial result; successful solving freezes the Edb until reset().
with ruleset.solve(edb, policy_index=0) as result:
print(result.contains_fact("allow", ["alice", "roadmap.pdf"]))Protocols
with ruleset:
Exiting the context delegates to close(), including on exceptional exit. Cleanup does not suppress the exception.
with engine.load_inline_ruleset(domain, policy_id, source) as ruleset:
print("policies:", ruleset.policy_count)
print("fingerprint:", ruleset.fingerprint)policies: 1 fingerprint: 154259eb7775622e9770eedddb331661f2d7751f5afe8a49db220e2940851d8b
Perform the same operations with explicit cleanup in a finally block.
ruleset = engine.load_inline_ruleset(domain, policy_id, source)
try:
print("policies:", ruleset.policy_count)
print("fingerprint:", ruleset.fingerprint)
finally:
ruleset.close()policies: 1 fingerprint: 154259eb7775622e9770eedddb331661f2d7751f5afe8a49db220e2940851d8b