Python

SolveResult

SolveResult class and public members in the unified Python API.

Class: SolveResult

Python · classSince v0.10.0
SolveResult
Solved answer. Queries and raw term resolution require this result to remain open.
Creation and access

Internal constructor — do not call. Native result returned by Session.solve or Ruleset.solve; do not manufacture native handles.

  • returned by
    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.

  • returned by
    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.

  • returned by
    SessionInputs.apply()

    Applies an atomic delta to retained input and solves it. Additions win when the same fact is added and removed.

  • returned by
    SessionInputs.replace()

    Atomically replaces the complete retained dynamic input and solves it. The raw fact-capacity bound applies before deduplication.

Attributes
fingerprintstrreadonly property
Selected-policy authority.
execution_fingerprintstrreadonly property
Selected policy plus execution configuration.
Methods
  • SolveResult.close()Releases this result, including any private session created by Ruleset.solve(). An explicitly prepared session remains available for another request.
  • SolveResult.contains_fact()Checks whether one fully specified fact is present in the existing result. It does not solve the policy again.
  • SolveResult.derived_fact_count()Reads how many facts were derived during this solve, without enumerating the model or deriving more facts.
  • SolveResult.enumerate_predicate_facts()Lists a query-enabled derived relation as ordinary Python values copied from the result.
  • SolveResult.enumerate_raw()Lists a derived relation as typed ResultTerm records, keeping symbols as IDs local to this live result.
  • SolveResult.explain_false()Investigates obstacles to deriving an absent fact and produces a bounded Why-false document. It is not an exhaustive proof of all possible obstructions.
  • SolveResult.explain_true()Explains a derivation of the requested fact as a bounded Why-true document, using the existing solve result.
  • SolveResult.resolve_term()Converts a raw term from this result into its ordinary Python string, integer or boolean value.
Protocols
Example
with session.solve(edb) as result:
    print("fingerprint:", result.fingerprint)
    print("execution_fingerprint:", result.execution_fingerprint)
    print("allowed:", result.contains_fact("allow", ["alice", "roadmap.pdf"]))
fingerprint: c5ec08d836b313af6e49ab7c49e811d84e0e856884fab645282d74682ceac454
execution_fingerprint: 14c485089d7dfee8a190afa1f103684c77911b99a4cbd9ec27bc414dd919d141
allowed: True

Methods

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

SolveResult.close

Release the answer once its queries are finished, so an explicit Session can accept another request.

Python · methodSince v0.10.0
SolveResult.close
SolveResult.close() -> None
Releases this result, including any private session created by Ruleset.solve(). An explicitly prepared session remains available for another request.
Return value
None

Returns None after native result release. ResultTerm views can no longer be resolved. A private session created by Ruleset.solve() is also closed; an explicit prepared Session remains reusable.

Example
result.close()

SolveResult.contains_fact

Ask whether a fully specified, query-permitted fact is present in the solved model. This does not run the solver again.

Python · methodSince v0.10.0
SolveResult.contains_fact
SolveResult.contains_fact(
predicate: str,
terms: Sequence[object]
) -> bool
Checks whether one fully specified fact is present in the existing result. It does not solve the policy again.
Arguments
predicatestr
Nonempty predicate name declared by the selected policy; the operation also checks its arity and permissions.
termsSequence[object]
Concrete str, signed 64-bit int or bool values in predicate order. A string alone is not a term sequence.
Return value
bool

Returns True for a present fact and False for an absent fact. Invalid predicates, arity or query permissions raise MaelysDatalogError, not False. Runtime EDB, policy facts and derived IDB may be queried when permitted.

Example
allowed = result.contains_fact("allow", ["alice", "roadmap.pdf"])
print(allowed)

SolveResult.derived_fact_count

Read the total number of distinct IDB facts already derived in this result; this is not the number of input entries.

Python · methodSince v0.10.0
SolveResult.derived_fact_count
SolveResult.derived_fact_count() -> int
Reads how many facts were derived during this solve, without enumerating the model or deriving more facts.
Return value
int

Returns an int. It does not derive more facts or enumerate the model. A closed result raises RuntimeError; native failures raise MaelysDatalogError.

Example
print("derived_facts:", result.derived_fact_count())

SolveResult.enumerate_predicate_facts

Enumerate a queryable derived predicate when you need its rows rather than a single yes/no decision.

Python · methodSince v0.10.0
SolveResult.enumerate_predicate_facts
SolveResult.enumerate_predicate_facts(
predicate: str,
arity: int
) -> list[tuple[object, ...]]
Lists a query-enabled derived relation as ordinary Python values copied from the result.
Arguments
predicatestr
Nonempty predicate name declared by the selected policy; the operation also checks its arity and permissions.
arityint
Number of terms declared for the predicate. Supply an int, not a bool.
Return value
list[tuple[object, ...]]

Returns a list of tuples containing str, int and bool values, in native enumeration order. The copies survive result closure. Only derived IDB facts are enumerated; invalid queries raise exceptions.

Example
rows = result.enumerate_predicate_facts("allow", 2)
users = [user for user, doc in rows if doc == "roadmap.pdf"]
print(users)

SolveResult.enumerate_raw

Use raw enumeration to inspect kinds and result-local identity without resolving every symbol immediately.

Python · methodSince v0.10.0
SolveResult.enumerate_raw
SolveResult.enumerate_raw(
predicate: str,
arity: int
) -> list[tuple[ResultTerm, ...]]
Lists a derived relation as typed ResultTerm records, keeping symbols as IDs local to this live result.
Arguments
predicatestr
Nonempty predicate name declared by the selected policy; the operation also checks its arity and permissions.
arityint
Number of terms declared for the predicate. Supply an int, not a bool.
Return value
list[tuple[ResultTerm, ...]]

Returns tuples of immutable ResultTerm values. Symbol IDs belong only to this live result and cannot be used as later inputs. Resolve them before closing the result.

Example
rows = result.enumerate_raw("allow", 2)
for user, document in rows:
    print("raw:", user, document)
    print("resolved:", user.resolve(), document.resolve())
raw: ResultTerm(kind='symbol', value=1) ResultTerm(kind='symbol', value=3)
resolved: alice roadmap.pdf

SolveResult.explain_false

For a successful negative membership answer, inspect the obstacles found by the bounded Why-false exploration.

Python · methodSince v0.10.0
SolveResult.explain_false
SolveResult.explain_false(
predicate: str,
terms: Sequence[object]
) -> str
Investigates obstacles to deriving an absent fact and produces a bounded Why-false document. It is not an exhaustive proof of all possible obstructions.
Arguments
predicatestr
Nonempty predicate name declared by the selected policy; the operation also checks its arity and permissions.
termsSequence[object]
Concrete str, signed 64-bit int or bool values in predicate order. A string alone is not a term sequence.
Return value
str

Returns an independent str containing the native MAELYS-DATALOG-v2 document with document=why-false. Exploration may be truncated; this is not a proof of every possible obstruction. For a fact already present, the document is not-applicable. Unknown symbol values and native query or capability errors raise MaelysDatalogError.

Example
if not result.contains_fact("allow", ["mallory", "roadmap.pdf"]):
    print(result.explain_false("allow", ["mallory", "roadmap.pdf"]))
Show output (11 lines)
MAELYS-DATALOG-v2
document=why-false
status=complete
query="allow"("mallory","roadmap.pdf") origin=none
summary=none
limit-hits=none candidate-rules=1 substitutions=0 diagnostics=1 filter-cost=0
diagnostic=0 rule=1 depth=0 target="allow"("mallory","roadmap.pdf")
binding=0 value="mallory"
binding=1 value="roadmap.pdf"
obstacle=positive-no-match body=0 origin=none pattern="owns"("mallory","roadmap.pdf")

SolveResult.explain_true

After a successful membership check, ask for a witness supporting a derived IDB fact. An explanation does not solve the policy again.

Python · methodSince v0.10.0
SolveResult.explain_true
SolveResult.explain_true(
predicate: str,
terms: Sequence[object]
) -> str
Explains a derivation of the requested fact as a bounded Why-true document, using the existing solve result.
Arguments
predicatestr
Nonempty predicate name declared by the selected policy; the operation also checks its arity and permissions.
termsSequence[object]
Concrete str, signed 64-bit int or bool values in predicate order. A string alone is not a term sequence.
Return value
str

Returns an independent str containing the native MAELYS-DATALOG-v2 document with document=why-true. Inspect its status: it may be complete, truncated or not-derived. Unknown symbol values and native query or capability errors raise MaelysDatalogError.

Example
if result.contains_fact("allow", ["alice", "roadmap.pdf"]):
    print(result.explain_true("allow", ["alice", "roadmap.pdf"]))
Show output (9 lines)
MAELYS-DATALOG-v2
document=why-true
status=complete
steps=1 premises=2
step=0 rule=1 fact="allow"("alice","roadmap.pdf")
premise=0 body=0 kind=positive origin=edb fact="owns"("alice","roadmap.pdf") parent=-
premise=1 body=1 kind=negative-absence origin=edb fact="blocked"("alice") parent=-
result-step=0

SolveResult.resolve_term

Convert a raw output term to a plain Python value through the same live result that produced it.

Python · methodSince v0.10.0
SolveResult.resolve_term
SolveResult.resolve_term(
term: ResultTerm
) -> object
Converts a raw term from this result into its ordinary Python string, integer or boolean value.
Arguments
ResultTerm produced by this same, still-open result; a term from another result is rejected.
Return value
object

Returns str for a symbol, int for an integer, or bool for a boolean. A foreign or non-ResultTerm value raises ValueError; use after result closure raises RuntimeError.

Example
term = result.enumerate_raw("allow", 2)[0][0]
user = result.resolve_term(term)
print("user:", user)

Protocols

with result:

Python · protocolSince v0.10.0
with result:
Leaving the block closes this result.
Behavior

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

Example
with session.solve(edb) as result:
    print(result.contains_fact("allow", ["alice", "roadmap.pdf"]))
Explicit form

Perform the same operations with explicit cleanup in a finally block.

result = session.solve(edb)
try:
    print(result.contains_fact("allow", ["alice", "roadmap.pdf"]))
finally:
    result.close()

Usage guides