Python

Session

Session class and public members in the unified Python API.

Class: Session

Python · classSince v0.10.0
Session
Reusable selected-policy owner. Only one result may remain live at a time.
Creation and access

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.session

    Read the Session that owns retained input.

Attributes
capacitiesSessionCapacitiesSince v0.14.0readonly property
Effective fixed quotas.
fingerprintstrreadonly property
Selected-policy authority.
execution_fingerprintstrreadonly property
Selected policy plus backend and execution configuration.
Methods
  • 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.
Protocols
  • with session:Leaving the block closes this Session and its active result.
Example
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.

Python · methodSince v0.10.0
Session.close
Session.close() -> None
Closes the prepared session, its active Python result and any attached retained input.
Return value
None

Returns None; any active result is closed first, then attached retained input is closed and native session storage is released. Repeated closure is harmless.

Example
session.close()

Session.inputs

Python · methodSince v0.17.0
Session.inputs
Session.inputs(
*,
fact_capacity: int | None = None,
addition_capacity: int | None = None,
removal_capacity: int | None = None,
symbols: Iterable[str] = ()
) -> SessionInputs
Attaches fixed-vocabulary retained input before the first successful solve. The binding allocates its aligned attachment storage.
Arguments
fact_capacityint | None
Capacity of the retained fact set; None uses this session’s input_facts capacity.
addition_capacityint | None
Raw addition-batch capacity; None uses fact_capacity.
removal_capacityint | None
Raw removal-batch capacity; None uses fact_capacity.
symbolsIterable[str]
Fixed additional vocabulary supplied as an iterable of strings.
Return value
SessionInputs

Returns 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.

Example
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.

Python · methodSince v0.10.0
Session.solve
Session.solve(
edb: Edb
) -> SolveResult
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.
Arguments
edbEdb
Open input Edb belonging to this same Ruleset; the entire stored batch is evaluated.
Return value
SolveResult

Returns 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.

Example
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:

Python · protocolSince v0.10.0
with session:
Leaving the block closes this Session and its active result.
Behavior

Exiting the context delegates to close(), including on exceptional exit. Cleanup does not suppress the exception.

Example
with ruleset.prepare() as session:
    with session.solve(edb) as result:
        print("derived_facts:", result.derived_fact_count())
Explicit form

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()

Usage guides