TypeScript / JavaScript
Ruleset usage guide
Examples and practical guidance for the JavaScript and TypeScript Ruleset API.The Ruleset reference contains the complete class and method cards.
Usage and examples
Compile once, keep ownership explicit
After registering the documents domain:
const source = [
'can_read(User, Doc) :-',
' owns(User, Doc) or delegated(User, Doc),',
' not(blocked(User)).',
'has_any_document(User) :- owns(User, _).',
'allow(User, Doc) :- user(User), can_read(User, Doc).',
'',
].join('\n')
const rules = engine.loadInlineRuleset('documents', 'documents.main', source)
try {
console.log(rules.policyCount, rules.fingerprint)
const input = rules.edb()
const session = rules.prepare()
// Add request facts, solve, and inspect the result inside this lifetime.
} finally {
rules.close()
}rules.programCounts(0) reports compiled policy counts. The predicate count
includes registered declarations even when unused; policy facts exclude runtime
EDB input, and the rule count includes expanded or alternatives. These counts
are distinct from engine.limits and a prepared session's capacities.
Every successful load returns a new Ruleset. A failed load publishes no new object and leaves previously loaded rulesets usable. Inline loading creates a single-policy ruleset; a manifest can load a set with several enabled policies.
Objects created from a ruleset
| Operation | Result | Lifetime |
|---|---|---|
rules.edb(options) | Reusable bounded Edb | Belongs to these rules |
rules.prepare(options) | Prepared Session | Selects one policy and owns its execution storage |
rules.solve(input, options) | SolveResult using a private session | Closing the result also closes that private session |
rules.close() | Releases the ruleset and its descendants | Repeated close is harmless |
Use prepare() for repeated requests and solve() for a one-shot evaluation.
An EDB from a different ruleset or Engine is rejected, even if its facts look
compatible. Closing a Ruleset cascades to its inputs, sessions and live results;
keep the owner open for the entire evaluation. Closing the Engine cascades over
all its rulesets.
For a policy update, load and prepare another ruleset, create its own EDB, then retire the old ruleset when its results are no longer needed. Assigning a new JavaScript variable does not transfer an EDB or result to the new owner.