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
No public constructor.
- returned by
Engine.loadInlineRuleset()Compile one source policy against an already registered domain.
- returned by
Engine.loadManifest()Validate and load enabled policies from a manifest, including source hashes and permissions.
- 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.
- using rulesetLeave the scope to close this Ruleset and its sessions, results and input buffers.
console.log('policies:', ruleset.policyCount)policies: 1
Methods
Ruleset.close
Ruleset.close(): voidvoidReturns nothing. Repeated closing succeeds; operational calls after closing fail with INVALID_STATE.
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
Ruleset.edb(
options?: EdbOptions
): EdbEdbReturns Edb. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
// 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() }facts: 1
Ruleset.prepare
Ruleset.prepare(
options?: SessionOptions
): SessionSessionReturns Session. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
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() }input facts: 7
Ruleset.programCounts
Ruleset.programCounts(
policyIndex?: number
): Readonly<ProgramCounts>Readonly<ProgramCounts>Returns a frozen count object. Invalid indices or closed handles fail; it does not create a session or count request EDB facts.
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
Ruleset.readsPredicate(
name: string,
arity: number,
policyIndex?: number
): booleanbooleanReturns 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.
const reads = ruleset.readsPredicate('user', 1)
console.log('reads user/1:', reads ? 'yes' : 'no')reads user/1: yes
Ruleset.solve
Ruleset.solve(
edb: Edb,
options?: SessionOptions
): SolveResultSolveResultReturns SolveResult. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
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() }derived facts: 1
Protocols
using ruleset
Exiting the using scope disposes the resource, including on exceptional exit. Cleanup delegates to close() and is idempotent.
{
using ruleset = engine.loadInlineRuleset('documents', 'documents.main', source)
console.log('policies:', ruleset.policyCount)
}policies: 1
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()
}
}policies: 1
Interfaces
ProgramCounts
- returned by
Ruleset.programCounts()Read the predicate, policy-fact and normalized-rule counts of a selected policy without preparing a session.
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.