TypeScript / JavaScript

Ruleset

Ruleset class and methods for compiled policies.

Unless a snippet creates it, ruleset uses the policy allow(User) :- user(User). in the page reference context. The context supplies engine, source and the binding imports; the engine has registered user/1 and allow/1 in the documents domain, and source contains this policy text.

Class: Ruleset

TypeScript · classSince v0.15.0
Ruleset
An immutable loaded policy set that owns its EDBs, sessions and results.
Creation and access

No public constructor.

Attributes
policyCountnumberreadonly field
Number of loaded policies.
fingerprintstringreadonly field
Semantic fingerprint of the policy selection.
Methods
  • Ruleset.close()Release this resource and its owned descendants. Closing is idempotent.
  • Ruleset.edb()Reserve a reusable bounded input buffer owned by this ruleset.
  • Ruleset.prepare()Reserve reusable execution storage for the selected policy before request processing.
  • Ruleset.programCounts()Read the predicate, policy-fact and normalized-rule counts of a selected policy without preparing a session.
  • Ruleset.readsPredicate()Checks whether the selected compiled policy reads a named predicate in its rule body, without preparing a session.
  • Ruleset.solve()Evaluate an EDB using a private session owned by the returned result.
Protocols
  • using rulesetLeave the scope to close this Ruleset and its sessions, results and input buffers.
Example
console.log('policies:', ruleset.policyCount)

Methods

Ruleset.close

TypeScript · methodSince v0.15.0
Ruleset.close
Ruleset.close(): void
Release this resource and its owned descendants. Closing is idempotent.
Return value
void

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

Example
const loaded = engine.loadInlineRuleset('documents', 'documents.main', source)
const counts = loaded.programCounts(0)
console.log('predicates:', counts.predicates)
console.log('facts:', counts.facts)
console.log('rules:', counts.rules)
loaded.close()
console.log('ruleset: closed')
predicates: 2
facts: 0
rules: 1
ruleset: closed

Ruleset.edb

TypeScript · methodSince v0.15.0
Ruleset.edb
Ruleset.edb(
options?: EdbOptions
): Edb
Reserve a reusable bounded input buffer owned by this ruleset.
Arguments
options?EdbOptions
Input fact and text capacities; omitted fields use profile defaults.
Return value
Edb

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

Example
// Given a loaded ruleset from the full program:
const input = ruleset.edb({ factCapacity: 7, textCapacity: 256 })
try {
  input.addFact('user', ['alice'])
  console.log('facts:', input.count)
} finally { input.close() }

Ruleset.prepare

TypeScript · methodSince v0.15.0
Ruleset.prepare
Ruleset.prepare(
options?: SessionOptions
): Session
Reserve reusable execution storage for the selected policy before request processing.
Arguments
Policy selection, capability admission, workspaces and fixed session capacities.
Return value
Session

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

Example
const session = ruleset.prepare({
  policyIndex: 0,
  capacities: { inputFacts: 7, derivedFacts: 16 },
  explanations: ExplanationKind.TRUE | ExplanationKind.FALSE,
})
try { console.log('input facts:', session.capacities.inputFacts) }
finally { session.close() }

Ruleset.programCounts

TypeScript · methodSince v0.16.0
Ruleset.programCounts
Ruleset.programCounts(
policyIndex?: number
): Readonly<ProgramCounts>
Read the predicate, policy-fact and normalized-rule counts of a selected policy without preparing a session.
Arguments
policyIndex?number
Zero-based enabled policy index; defaults to 0.
Return value
Readonly<ProgramCounts>

Returns a frozen count object. Invalid indices or closed handles fail; it does not create a session or count request EDB facts.

Example
const counts = ruleset.programCounts(0)
console.log('predicates:', counts.predicates)
console.log('facts:', counts.facts)
console.log('rules:', counts.rules)
// These are compiled-program counts, not session quotas.
predicates: 2
facts: 0
rules: 1

Ruleset.readsPredicate

TypeScript · methodSince v0.25.0
Ruleset.readsPredicate
Ruleset.readsPredicate(
name: string,
arity: number,
policyIndex?: number
): boolean
Checks whether the selected compiled policy reads a named predicate in its rule body, without preparing a session.
Arguments
namestring
Predicate name to inspect.
aritynumber
Number of arguments in that predicate.
policyIndex?number
Zero-based index of an enabled policy; defaults to the first policy.
Return value
boolean

Returns true for a body read, including a negated atom or aggregate source, and false otherwise. Rule heads and static facts do not count. Invalid names, arities or policy indices are rejected by the binding; the ruleset must remain open.

Example
const reads = ruleset.readsPredicate('user', 1)
console.log('reads user/1:', reads ? 'yes' : 'no')

Ruleset.solve

TypeScript · methodSince v0.15.0
Ruleset.solve
Ruleset.solve(
edb: Edb,
options?: SessionOptions
): SolveResult
Evaluate an EDB using a private session owned by the returned result.
Arguments
edbEdb
An open EDB owned by the same ruleset.
Policy selection, capability admission, workspaces and fixed session capacities.
Return value
SolveResult

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

Example
const input = ruleset.edb()
try {
  input.addFact('user', ['alice'])
  const result = ruleset.solve(input, { policyIndex: 0 })
  try { console.log('derived facts:', result.derivedFactCount()) }
  finally { result.close() } // Releases its private session.
} finally { input.close() }

Protocols

using ruleset

TypeScript · protocolSince v0.15.0
using ruleset
Leave the scope to close this Ruleset and its sessions, results and input buffers.
Behavior

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

Example
{
  using ruleset = engine.loadInlineRuleset('documents', 'documents.main', source)
  console.log('policies:', ruleset.policyCount)
}
Explicit form

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

{
const ruleset = engine.loadInlineRuleset('documents', 'documents.main', source)
try {
  console.log('policies:', ruleset.policyCount)
} finally {
  ruleset.close()
}
}

Interfaces

ProgramCounts.

ProgramCounts

TypeScript · interfaceSince v0.16.0
ProgramCounts
Immutable counts of one compiled policy, independent of request facts and session quotas.
Creation and access
  • returned by
    Ruleset.programCounts()

    Read the predicate, policy-fact and normalized-rule counts of a selected policy without preparing a session.

Fields
predicatesnumberreadonly field
Compiled predicate declarations, including unused registry declarations.
factsnumberreadonly field
Facts compiled from policy source, excluding request EDB facts.
rulesnumberreadonly field
Normalized compiled rules, including expanded OR alternatives.
Example
const counts = ruleset.programCounts(0)
console.log('predicates:', counts.predicates)
console.log('facts:', counts.facts)
console.log('rules:', counts.rules)
predicates: 2
facts: 0
rules: 1

Usage guides

The examples are now in the Ruleset usage guide.