TypeScript / JavaScript
Session
Session capacity and execution classes with their methods.Unless the snippet creates them, the examples use engine, a loaded ruleset, document-access input and the binding imports from the page reference context.
Class: SessionCapacities
export class SessionCapacities implements CapacityOptions {
constructor(options?: CapacityOptions);
readonly inputFacts?: number;
readonly derivedFacts?: number;
readonly symbols?: number;
readonly textBytes?: number;
}- constructor
new SessionCapacities(options)Validate explicit fixed session quotas.
const capacities = new SessionCapacities({ inputFacts: 7, derivedFacts: 16 })
const session = ruleset.prepare({ capacities })
try {
console.log('input facts:', session.capacities.inputFacts)
console.log('derived facts:', session.capacities.derivedFacts)
} finally { session.close() }input facts: 7 derived facts: 16
Constructor: SessionCapacities()
new SessionCapacities(
options?: CapacityOptions
): SessionCapacitiesSessionCapacitiesReturns a SessionCapacities value. Each explicit quota must be an integer between zero and UINT32_MAX; other values throw RangeError.
const capacities = new SessionCapacities({ inputFacts: 7, derivedFacts: 16 })
const session = ruleset.prepare({ capacities })
try {
console.log('input facts:', session.capacities.inputFacts)
console.log('derived facts:', session.capacities.derivedFacts)
} finally { session.close() }input facts: 7 derived facts: 16
Class: Session
No public constructor.
- returned by
Ruleset.prepare()Reserve reusable execution storage for the selected policy before request processing.
- Session.close()Release this resource and its owned descendants. Closing is idempotent.
- Session.inputs()Attach retained dynamic input before this session's first successful solve. The binding allocates its storage; ordinary solve is unavailable while it remains attached.
- Session.solve()Evaluate an EDB from the same ruleset using this prepared session. Ordinary solve is unavailable while retained input is attached.
- using sessionLeave the scope to close this Session and its active result.
const session = ruleset.prepare()
try {
const result = session.solve(input)
try { console.log('allowed:', result.containsFact('allow', ['alice', 'roadmap.pdf'])) }
finally { result.close() }
} finally { session.close() }allowed: true
Methods
Session.close
Session.close(): voidvoidReturns nothing. Repeated closing succeeds; operational calls after closing fail with INVALID_STATE.
const session = ruleset.prepare()
session.close()
console.log('session: closed')session: closed
Session.inputs
Session.inputs(
options?: InputOptions
): SessionInputsSessionInputsReturns SessionInputs. A closed or busy session, an existing attachment or a live result is rejected. Invalid capacities throw RangeError; invalid vocabulary strings throw TypeError. Engine failures throw MaelysDatalogError with a copied diagnostic.
const session = ruleset.prepare()
try {
const retained = session.inputs({ factCapacity: 2, symbols: ['alice', 'roadmap.pdf'] })
try {
const result = retained.replace(retained.base, [
{ predicate: 'user', terms: ['alice'] },
{ predicate: 'owns', terms: ['alice', 'roadmap.pdf'] },
])
try { console.log('allowed:', result.containsFact('allow', ['alice', 'roadmap.pdf'])) }
finally { result.close() }
} finally { retained.close() }
} finally { session.close() }allowed: true
Session.solve
Session.solve(
edb: Edb
): SolveResultSolveResultReturns SolveResult. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
const session = ruleset.prepare({ policyIndex: 0 })
try {
const result = session.solve(input)
try { console.log('allowed:', result.containsFact('allow', ['alice', 'roadmap.pdf'])) }
finally { result.close() }
} finally { session.close() }allowed: true
Protocols
using session
Exiting the using scope disposes the resource, including on exceptional exit. Cleanup delegates to close() and is idempotent.
{
using session = ruleset.prepare()
const result = session.solve(input)
console.log('derived facts:', result.derivedFactCount())
result.close()
}derived facts: 1
Perform the same operations with explicit cleanup in a finally block.
const session = ruleset.prepare()
try {
const result = session.solve(input)
console.log('derived facts:', result.derivedFactCount())
result.close()
} finally {
session.close()
}derived facts: 1
Interfaces
CapacityOptions · SessionOptions.
CapacityOptions
- read from
session.capacitiesEffective fixed capacities, or requested optional quotas when preparing a session.
const options: CapacityOptions = { inputFacts: 7, derivedFacts: 16 }
const session = ruleset.prepare({ capacities: options })
try {
console.log('input facts:', session.capacities.inputFacts)
console.log('derived facts:', session.capacities.derivedFacts)
} finally { session.close() }input facts: 7 derived facts: 16
SessionOptions
const options: SessionOptions = { policyIndex: 0, requiredCapabilities: Capability.POSITIVE | Capability.NEGATION }
const session = ruleset.prepare(options)
try {
const result = session.solve(input)
try { console.log('allowed:', result.containsFact('allow', ['alice', 'roadmap.pdf'])) }
finally { result.close() }
} finally { session.close() }allowed: true
| Used by | Ruleset.prepare · Ruleset.solve |
|---|
Objects
Capability
const required = Capability.POSITIVE | Capability.NEGATION
console.log('required capabilities:', '0x' + required.toString(16))required capabilities: 0x3
ExplanationKind
const kinds = ExplanationKind.TRUE | ExplanationKind.FALSE
console.log('explanation kinds:', '0x' + kinds.toString(16))explanation kinds: 0x3
Usage guides
The examples are now in the Session and solving guide.