Python

SessionInputs

Retained session input in the unified Python API.

Class: SessionInputs

Python · classSince v0.17.0
SessionInputs
Retains dynamic facts and a fixed vocabulary on one Session. Each replace or apply requires the current InputBase and returns a leased SolveResult. Close that result before the next transaction. Python/CFFI staging still allocates.
Creation and access

Internal constructor — do not call. Created by Session.inputs; the binding rejects direct construction without its private token.

  • returned by
    Session.inputs()

    Attaches fixed-vocabulary retained input before the first successful solve. The binding allocates its aligned attachment storage.

Attributes
baseInputBasereadonly property
Current attachment incarnation and committed input generation.
sessionSessionproperty
Session that owns this retained-input attachment.
Methods
  • SessionInputs.apply()Applies an atomic delta to retained input and solves it. Additions win when the same fact is added and removed.
  • SessionInputs.close()Releases this retained-input attachment and its aligned storage.
  • SessionInputs.replace()Atomically replaces the complete retained dynamic input and solves it. The raw fact-capacity bound applies before deduplication.
Protocols
  • with inputs:Leaving the block closes this retained-input attachment.
Example
with inputs.replace(inputs.base, [("owns", ["alice", "roadmap.pdf"])]) as result:
    print("allowed:", result.contains_fact("allow", ["alice", "roadmap.pdf"]))

Methods

Close the current result before replacing or applying input again. Each transaction uses the attachment’s current base.

SessionInputs.apply

Python · methodSince v0.17.0
SessionInputs.apply
SessionInputs.apply(
base: InputBase,
*,
added: Iterable[tuple[str, Sequence[object]]] = (),
removed: Iterable[tuple[str, Sequence[object]]] = ()
) -> SolveResult
Applies an atomic delta to retained input and solves it. Additions win when the same fact is added and removed.
Arguments
Current base read from this attachment; a stale base or a base from another attachment is rejected.
addedIterable[tuple[str, Sequence[object]]]
Dynamic facts to add, counted before deduplication against addition_capacity. Added symbols must belong to the fixed vocabulary.
removedIterable[tuple[str, Sequence[object]]]
Dynamic facts to remove, counted before deduplication against removal_capacity. Absent removals have no effect; an unknown removed symbol denotes an absent fact after record validation.
Return value
SolveResult

Returns a leased SolveResult and commits a new base on success, even for an unchanged set. Failure preserves the committed facts and base. A non-InputBase base raises TypeError; a busy session or live result raises RuntimeError. Exceeding a raw batch capacity raises MaelysDatalogError with PAYLOAD_TOO_LARGE. Native transaction failures raise MaelysDatalogError.

Example
with inputs.apply(inputs.base, added=[("owns", ["alice", "roadmap.pdf"])]) as result:
    print("allowed:", result.contains_fact("allow", ["alice", "roadmap.pdf"]))

SessionInputs.close

Python · methodSince v0.17.0
SessionInputs.close
SessionInputs.close() -> None
Releases this retained-input attachment and its aligned storage.
Return value
None

Returns None. After successful native release, the binding clears the session’s attachment. Repeated closure is harmless.

Example
inputs.close()
inputs.close()
print("inputs: closed")

SessionInputs.replace

Python · methodSince v0.17.0
SessionInputs.replace
SessionInputs.replace(
base: InputBase,
facts: Iterable[tuple[str, Sequence[object]]]
) -> SolveResult
Atomically replaces the complete retained dynamic input and solves it. The raw fact-capacity bound applies before deduplication.
Arguments
Current base read from this attachment; a stale base or a base from another attachment is rejected.
factsIterable[tuple[str, Sequence[object]]]
Complete replacement of dynamic facts as (predicate, terms) pairs, counted before deduplication against fact_capacity. Compiled policy facts remain separate.
Return value
SolveResult

Returns a leased SolveResult and commits a new base on success, even for an unchanged set. Failure preserves the committed facts and base. A non-InputBase base raises TypeError; a busy session or live result raises RuntimeError. Exceeding a raw batch capacity raises MaelysDatalogError with PAYLOAD_TOO_LARGE. Native transaction failures raise MaelysDatalogError.

Example
base = inputs.base
with inputs.replace(base, [("owns", ["alice", "roadmap.pdf"])]) as result:
    print("allowed:", result.contains_fact("allow", ["alice", "roadmap.pdf"]))
print("base changed:", inputs.base != base)
allowed: True
base changed: True

Protocols

with inputs:

Python · protocolSince v0.17.0
with inputs:
Leaving the block closes this retained-input attachment.
Behavior

Returns this open SessionInputs. Exiting delegates to close(), including on exceptional exit; exceptions are not suppressed.

Example
with inputs:
    with inputs.replace(inputs.base, [("owns", ["alice", "roadmap.pdf"])]) as result:
        print("allowed:", result.contains_fact("allow", ["alice", "roadmap.pdf"]))
Explicit form

Perform the same retained-input transaction with explicit cleanup.

try:
    with inputs.replace(inputs.base, [("owns", ["alice", "roadmap.pdf"])]) as result:
        print("allowed:", result.contains_fact("allow", ["alice", "roadmap.pdf"]))
finally:
    inputs.close()

Usage guides