TypeScript / JavaScript
ResultTerm and SolveResult
Result term and solve result classes with their methods.The examples use the loaded ruleset and populated input from the page reference context, which also supplies engine and the binding imports. Each example keeps its result open while resolving raw terms.
Class: ResultTerm
No public constructor.
- returned by
SolveResult.enumerateRaw()Read immutable terms whose symbol identities remain bound to this result.
- ResultTerm.resolve()Copy the typed value from this result-bound term.
const result = ruleset.solve(input)
try {
const term = result.enumerateRaw('allow', 2)[0][0]
console.log('kind:', term.kind)
console.log('value:', term.value)
console.log('resolved:', term.resolve())
} finally { result.close() }kind: symbol value: 1 resolved: alice
Methods
ResultTerm.resolve
ResultTerm.resolve(): ValueValueReturns Value. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
const result = ruleset.solve(input)
try {
// Keep the owning result open while resolving raw terms.
const rows = result.enumerateRaw('allow', 2)
for (const row of rows) {
console.log('resolved:', row.map(term => term.resolve()))
}
} finally { result.close() }resolved: [ 'alice', 'roadmap.pdf' ]
Class: SolveResult
No public constructor.
- returned by
Ruleset.solve()Evaluate an EDB using a private session owned by the returned result.
- returned by
Session.solve()Evaluate an EDB from the same ruleset using this prepared session. Ordinary solve is unavailable while retained input is attached.
- returned by
SessionInputs.apply()Apply retained-input changes and return their solved snapshot.
- returned by
SessionInputs.replace()Replace all retained input and return its solved snapshot.
- SolveResult.close()Release the result lease and its private convenience session, if any. Closing is idempotent.
- SolveResult.containsFact()Check membership of a ground fact exposed by the domain query flags.
- SolveResult.derivedFactCount()Count all derived facts in the retained result.
- SolveResult.enumeratePredicateFacts()Copy resolved values for one queryable predicate and arity.
- SolveResult.enumerateRaw()Read immutable terms whose symbol identities remain bound to this result.
- SolveResult.explainFalse()Produce the canonical Why-false document for a ground fact.
- SolveResult.explainTrue()Produce the canonical Why-true document for a ground fact.
- SolveResult.resolveTerm()Resolve a term owned by this same live result into a copied value.
- using resultLeave the scope to close this result; externally owned sessions, rulesets and input buffers remain open.
const result = ruleset.solve(input)
try {
console.log('fingerprint:', result.fingerprint)
console.log('execution fingerprint:', result.executionFingerprint)
console.log('allowed:', result.containsFact('allow', ['alice', 'roadmap.pdf']))
} finally { result.close() }fingerprint: c5ec08d836b313af6e49ab7c49e811d84e0e856884fab645282d74682ceac454 execution fingerprint: 14c485089d7dfee8a190afa1f103684c77911b99a4cbd9ec27bc414dd919d141 allowed: true
Methods
SolveResult.close
SolveResult.close(): voidvoidReturns nothing. Repeated closing succeeds; operational calls after closing fail with INVALID_STATE.
const result = ruleset.solve(input)
result.close()
console.log('result: closed')result: closed
SolveResult.containsFact
SolveResult.containsFact(
predicate: string,
terms: readonly InputValue[]
): booleanbooleanReturns true or false. An unknown symbol yields false; invalid predicates, arity, permissions or closed ownership throw.
const result = ruleset.solve(input)
try {
const allowed = result.containsFact('allow', ['alice', 'roadmap.pdf'])
console.log('allowed:', allowed)
} finally { result.close() }allowed: true
SolveResult.derivedFactCount
SolveResult.derivedFactCount(): numbernumberReturns number. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
const result = ruleset.solve(input)
try { console.log('derived facts:', result.derivedFactCount()) }
finally { result.close() }derived facts: 1
SolveResult.enumeratePredicateFacts
SolveResult.enumeratePredicateFacts(
predicate: string,
arity: number
): Value[][]Value[][]Returns Value[][]. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
const result = ruleset.solve(input)
let rows
try { rows = result.enumeratePredicateFacts('allow', 2) }
finally { result.close() }
console.log('facts:', rows) // Resolved copies remain usable after close.facts: [ [ 'alice', 'roadmap.pdf' ] ]
SolveResult.enumerateRaw
SolveResult.enumerateRaw(
predicate: string,
arity: number
): ResultTerm[][]ResultTerm[][]Returns ResultTerm[][]. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
const result = ruleset.solve(input)
try {
const rows = result.enumerateRaw('allow', 2)
for (const row of rows) console.log('resolved:', row.map(term => term.resolve()))
} finally { result.close() } // Raw terms cannot resolve after this.resolved: [ 'alice', 'roadmap.pdf' ]
SolveResult.explainFalse
SolveResult.explainFalse(
predicate: string,
terms: readonly InputValue[]
): stringstringReturns canonical text, including complete, truncated, not-derived or not-applicable status as appropriate. Unknown symbols fail with NOT_FOUND; truncation is a returned document, not a thrown capacity error.
const session = ruleset.prepare({ explanations: ExplanationKind.FALSE })
try {
const result = session.solve(input)
try {
const text = result.explainFalse('allow', ['mallory', 'roadmap.pdf'])
for (const line of text.trimEnd().split('\n')) console.log('explanation:', line)
}
finally { result.close() }
} finally { session.close() }Show output (10 lines)
explanation: MAELYS-DATALOG-v2
explanation: document=why-false
explanation: status=complete
explanation: query="allow"("mallory","roadmap.pdf") origin=none
explanation: summary=none
explanation: limit-hits=none candidate-rules=1 substitutions=0 diagnostics=1 filter-cost=0
explanation: diagnostic=0 rule=1 depth=0 target="allow"("mallory","roadmap.pdf")
explanation: binding=0 value="mallory"
explanation: binding=1 value="roadmap.pdf"
explanation: obstacle=positive-no-match body=0 origin=none pattern="owns"("mallory","roadmap.pdf")
SolveResult.explainTrue
SolveResult.explainTrue(
predicate: string,
terms: readonly InputValue[]
): stringstringReturns canonical text, including complete, truncated, not-derived or not-applicable status as appropriate. Unknown symbols fail with NOT_FOUND; truncation is a returned document, not a thrown capacity error.
const session = ruleset.prepare({ explanations: ExplanationKind.TRUE })
try {
const result = session.solve(input)
try {
const text = result.explainTrue('allow', ['alice', 'roadmap.pdf'])
for (const line of text.trimEnd().split('\n')) console.log('explanation:', line)
}
finally { result.close() }
} finally { session.close() }Show output (8 lines)
explanation: MAELYS-DATALOG-v2
explanation: document=why-true
explanation: status=complete
explanation: steps=1 premises=2
explanation: step=0 rule=1 fact="allow"("alice","roadmap.pdf")
explanation: premise=0 body=0 kind=positive origin=edb fact="owns"("alice","roadmap.pdf") parent=-
explanation: premise=1 body=1 kind=negative-absence origin=edb fact="blocked"("alice") parent=-
explanation: result-step=0
SolveResult.resolveTerm
SolveResult.resolveTerm(
term: ResultTerm
): ValueValueReturns Value. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
const result = ruleset.solve(input)
try {
const rows = result.enumerateRaw('allow', 2)
for (const row of rows) console.log('resolved:', row.map(term => result.resolveTerm(term)))
} finally { result.close() }resolved: [ 'alice', 'roadmap.pdf' ]
Protocols
using result
Exiting the using scope disposes the resource, including on exceptional exit. Cleanup delegates to close() and is idempotent.
{
using result = ruleset.solve(input)
console.log('derived facts:', result.derivedFactCount())
}derived facts: 1
Perform the same operations with explicit cleanup in a finally block.
const result = ruleset.solve(input)
try {
console.log('derived facts:', result.derivedFactCount())
} finally {
result.close()
}derived facts: 1
Usage guides
The examples are now in the Querying guide.