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

TypeScript · classSince v0.15.0
ResultTerm
A result-bound term whose symbol identity can only be resolved while its owner is alive.
Creation and access

No public constructor.

Attributes
kind'symbol' | 'integer' | 'boolean'readonly field
Whether the term is a symbol, integer, or boolean.
valuenumber | bigint | booleanreadonly field
A symbol ID local to its result, or a typed integer/boolean value.
Methods
Example
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

TypeScript · methodSince v0.15.0
ResultTerm.resolve
ResultTerm.resolve(): Value
Copy the typed value from this result-bound term.
Return value
Value

Returns Value. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.

Example
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

TypeScript · classSince v0.15.0
SolveResult
A leased result snapshot supporting queries, enumeration and canonical explanations.
Creation and access

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.

Attributes
fingerprintstringreadonly field
Semantic fingerprint of the policy selection.
executionFingerprintstringreadonly field
Fingerprint incorporating execution configuration.
Methods
Protocols
  • using resultLeave the scope to close this result; externally owned sessions, rulesets and input buffers remain open.
Example
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

TypeScript · methodSince v0.15.0
SolveResult.close
SolveResult.close(): void
Release the result lease and its private convenience session, if any. Closing is idempotent.
Return value
void

Returns nothing. Repeated closing succeeds; operational calls after closing fail with INVALID_STATE.

Example
const result = ruleset.solve(input)
result.close()
console.log('result: closed')

SolveResult.containsFact

TypeScript · methodSince v0.15.0
SolveResult.containsFact
SolveResult.containsFact(
predicate: string,
terms: readonly InputValue[]
): boolean
Check membership of a ground fact exposed by the domain query flags.
Arguments
predicatestring
Name of the predicate to query.
termsreadonly InputValue[]
Ordered typed terms matching its arity.
Return value
boolean

Returns true or false. An unknown symbol yields false; invalid predicates, arity, permissions or closed ownership throw.

Example
const result = ruleset.solve(input)
try {
  const allowed = result.containsFact('allow', ['alice', 'roadmap.pdf'])
  console.log('allowed:', allowed)
} finally { result.close() }

SolveResult.derivedFactCount

TypeScript · methodSince v0.15.0
SolveResult.derivedFactCount
SolveResult.derivedFactCount(): number
Count all derived facts in the retained result.
Return value
number

Returns number. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.

Example
const result = ruleset.solve(input)
try { console.log('derived facts:', result.derivedFactCount()) }
finally { result.close() }

SolveResult.enumeratePredicateFacts

TypeScript · methodSince v0.15.0
SolveResult.enumeratePredicateFacts
SolveResult.enumeratePredicateFacts(
predicate: string,
arity: number
): Value[][]
Copy resolved values for one queryable predicate and arity.
Arguments
predicatestring
Name of the predicate to query.
aritynumber
Declared number of terms, between zero and four.
Return value
Value[][]

Returns Value[][]. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.

Example
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

TypeScript · methodSince v0.15.0
SolveResult.enumerateRaw
SolveResult.enumerateRaw(
predicate: string,
arity: number
): ResultTerm[][]
Read immutable terms whose symbol identities remain bound to this result.
Arguments
predicatestring
Name of the predicate to query.
aritynumber
Declared number of terms, between zero and four.
Return value
ResultTerm[][]

Returns ResultTerm[][]. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.

Example
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

TypeScript · methodSince v0.15.0
SolveResult.explainFalse
SolveResult.explainFalse(
predicate: string,
terms: readonly InputValue[]
): string
Produce the canonical Why-false document for a ground fact.
Arguments
predicatestring
Name of the predicate to query.
termsreadonly InputValue[]
Ordered typed terms matching its arity.
Return value
string

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

Example
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

TypeScript · methodSince v0.15.0
SolveResult.explainTrue
SolveResult.explainTrue(
predicate: string,
terms: readonly InputValue[]
): string
Produce the canonical Why-true document for a ground fact.
Arguments
predicatestring
Name of the predicate to query.
termsreadonly InputValue[]
Ordered typed terms matching its arity.
Return value
string

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

Example
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

TypeScript · methodSince v0.15.0
SolveResult.resolveTerm
SolveResult.resolveTerm(
term: ResultTerm
): Value
Resolve a term owned by this same live result into a copied value.
Arguments
Term owned by this same live result.
Return value
Value

Returns Value. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.

Example
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

TypeScript · protocolSince v0.15.0
using result
Leave the scope to close this result; externally owned sessions, rulesets and input buffers remain open.
Behavior

Exiting the using scope disposes the resource, including on exceptional exit. Cleanup delegates to close() and is idempotent.

Example
{
  using result = ruleset.solve(input)
  console.log('derived facts:', result.derivedFactCount())
}
Explicit form

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

Usage guides

The examples are now in the Querying guide.