Python
Session
Session class and public members in the unified Python API.Class: Session
Internal constructor — do not call. Prepared native execution resource returned by Ruleset.prepare; do not manufacture native handles.
- returned by
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.
- read from
sessionInputs.sessionRead the Session that owns retained input.
- Session.close()Closes the prepared session, its active Python result and any attached retained input.
- Session.inputs()Attaches fixed-vocabulary retained input before the first successful solve. The binding allocates its aligned attachment storage.
- Session.solve()Evaluates a complete input batch using this prepared session. Release its previous result before solving another request; facts are not implicitly inherited. Ordinary solving is unavailable while retained input is attached.
- with session:Leaving the block closes this Session and its active result.
with ruleset.prepare() as session:
print("fingerprint:", session.fingerprint)
print("execution_fingerprint:", session.execution_fingerprint)
with session.solve(edb) as result:
print("derived_facts:", result.derived_fact_count())fingerprint: c5ec08d836b313af6e49ab7c49e811d84e0e856884fab645282d74682ceac454 execution_fingerprint: 14c485089d7dfee8a190afa1f103684c77911b99a4cbd9ec27bc414dd919d141 derived_facts: 1
Methods
Each method has its own signature, arguments, return behavior and stable link.
Session.close
Close the session once the prepared policy and its optional explanation workspace are no longer needed.
Session.close() -> NoneNoneReturns None; any active result is closed first, then attached retained input is closed and native session storage is released. Repeated closure is harmless.
Session.inputs
Session.inputs(
*,
fact_capacity: int | None = None,
addition_capacity: int | None = None,
removal_capacity: int | None = None,
symbols: Iterable[str] = ()
) -> SessionInputsSessionInputsReturns the attached SessionInputs. A busy session, a live result or an existing attachment raises RuntimeError. Capacities must be integers fitting size_t; invalid capacity types raise TypeError and invalid values raise ValueError. Invalid vocabulary types raise TypeError; too many entries raise ValueError. Native sizing or attachment failures raise MaelysDatalogError.
inputs = session.inputs(symbols=("alice", "roadmap.pdf"))
try:
print("input attached:", inputs.session is session)
with inputs.replace(inputs.base, [("owns", ["alice", "roadmap.pdf"])]) as result:
print("allowed:", result.contains_fact("allow", ["alice", "roadmap.pdf"]))
finally:
inputs.close()input attached: True allowed: True
Session.solve
Reuse the prepared policy, not the previous request facts. Close the current result before solving again on this session.
Session.solve(
edb: Edb
) -> SolveResultSolveResultReturns a new SolveResult. A live previous result or an Edb from another or closed Ruleset raises RuntimeError. Native solving failures raise MaelysDatalogError without publishing a result.
with session.solve(edb) as result:
allowed = result.contains_fact("allow", ["alice", "roadmap.pdf"])
# The result is now closed; this session can solve the next request.
print(allowed)Protocols
with session:
Exiting the context delegates to close(), including on exceptional exit. Cleanup does not suppress the exception.
with ruleset.prepare() as session:
with session.solve(edb) as result:
print("derived_facts:", result.derived_fact_count())derived_facts: 1
Perform the same operations with explicit cleanup in a finally block.
session = ruleset.prepare()
try:
result = session.solve(edb)
try:
print("derived_facts:", result.derived_fact_count())
finally:
result.close()
finally:
session.close()derived_facts: 1