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

TypeScript · classSince v0.15.0
SessionCapacities
Declaration
export class SessionCapacities implements CapacityOptions {
    constructor(options?: CapacityOptions);
    readonly inputFacts?: number;
    readonly derivedFacts?: number;
    readonly symbols?: number;
    readonly textBytes?: number;
}
Validated portable quotas between zero and UINT32_MAX; omitted fields retain engine defaults.
Creation and access
Attributes
inputFacts?numberreadonly field
Maximum session input facts.
derivedFacts?numberreadonly field
Maximum session derived facts.
symbols?numberreadonly field
Maximum session symbols.
textBytes?numberreadonly field
Used text bytes, or the explicit session text quota in capacity options.
Example
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()

TypeScript · constructorSince v0.15.0
SessionCapacities.constructor
new SessionCapacities(
options?: CapacityOptions
): SessionCapacities
Validate explicit fixed session quotas.
Arguments
Optional inputFacts, derivedFacts, symbols and textBytes quotas; omitted fields remain unspecified.
Return value
SessionCapacities

Returns a SessionCapacities value. Each explicit quota must be an integer between zero and UINT32_MAX; other values throw RangeError.

Example
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

TypeScript · classSince v0.15.0
Session
A prepared execution context with fixed capacities and at most one live result.
Creation and access

No public constructor.

  • returned by
    Ruleset.prepare()

    Reserve reusable execution storage for the selected policy before request processing.

Attributes
capacitiesReadonly<Required<CapacityOptions>>readonly field
Effective fixed capacities, or requested optional quotas when preparing a session.
fingerprintstringreadonly field
Semantic fingerprint of the policy selection.
executionFingerprintstringreadonly field
Fingerprint incorporating execution configuration.
Methods
  • 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.
Protocols
  • using sessionLeave the scope to close this Session and its active result.
Example
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() }

Methods

Session.close

TypeScript · methodSince v0.15.0
Session.close
Session.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 session = ruleset.prepare()
session.close()
console.log('session: closed')

Session.inputs

TypeScript · methodSince v0.17.0
Session.inputs
Session.inputs(
options?: InputOptions
): SessionInputs
Attach retained dynamic input before this session's first successful solve. The binding allocates its storage; ordinary solve is unavailable while it remains attached.
Arguments
options?InputOptions
Optional retained-input capacities and additional vocabulary. factCapacity defaults to the session's input-fact quota; omitted additionCapacity and removalCapacity default to factCapacity. Zero capacities are legal. symbols extends the immutable program vocabulary.
Return value
SessionInputs

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

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

Session.solve

TypeScript · methodSince v0.15.0
Session.solve
Session.solve(
edb: Edb
): SolveResult
Evaluate an EDB from the same ruleset using this prepared session. Ordinary solve is unavailable while retained input is attached.
Arguments
edbEdb
An open EDB owned by the same ruleset.
Return value
SolveResult

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

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

Protocols

using session

TypeScript · protocolSince v0.15.0
using session
Leave the scope to close this Session and its active result.
Behavior

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

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

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

Interfaces

CapacityOptions · SessionOptions.

CapacityOptions

TypeScript · interfaceSince v0.15.0
CapacityOptions
Fixed per-session quotas. An omitted quota uses the profile default; zero is an explicit request.
Creation and access
  • read from
    session.capacities

    Effective fixed capacities, or requested optional quotas when preparing a session.

Fields
inputFacts?numberfield
Maximum session input facts.
derivedFacts?numberfield
Maximum session derived facts.
symbols?numberfield
Maximum session symbols.
textBytes?numberfield
Used text bytes, or the explicit session text quota in capacity options.
Example
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

TypeScript · interfaceSince v0.15.0
SessionOptions
Select a policy and the capabilities, work budget, capacities and explanation storage required before solving.
Fields
policyIndex?numberfield
Zero-based policy selection; defaults to zero.
requiredCapabilities?bigint | numberfield
Required backend capability bits; unsupported requests fail preparation.
workLimit?bigint | numberfield
Requested work budget; a backend without this capability rejects a nonzero limit.
explanations?numberfield
Optional TRUE/FALSE workspace mask; zero reserves no workspace.
capacities?CapacityOptionsfield
Effective fixed capacities, or requested optional quotas when preparing a session.
Example
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() }

Capability · ExplanationKind.

Capability

TypeScript · objectSince v0.15.0
Capability
Backend feature requirements. These bits are separate from explanation workspace kinds.
Members
POSITIVE0x1
Positive rule evaluation capability bit.
NEGATION0x2
Stratified negation capability bit.
COMPARISONS0x4
Typed comparisons capability bit.
ARITHMETIC0x8
Arithmetic capability bit.
FILTERS0x10
Filters capability bit.
EXPLAIN_TRUE0x20
Why-true explanations capability bit.
WORK_LIMIT0x40
Bounded work admission capability bit.
EXPLAIN_FALSE0x80
Why-false explanations capability bit.
AGGREGATES0x100
Aggregate support capability bit.
MIN0x200
Minimum aggregates capability bit.
MAX0x400
Maximum aggregates capability bit.
SUM0x800
Sum aggregates capability bit.
Example
const required = Capability.POSITIVE | Capability.NEGATION
console.log('required capabilities:', '0x' + required.toString(16))
required capabilities: 0x3

ExplanationKind

TypeScript · objectSince v0.15.0
ExplanationKind
Optional explanation workspace kinds, combined with bitwise OR.
Members
TRUE1
Reserve Why-true workspace.
FALSE2
Reserve Why-false workspace.
Example
const kinds = ExplanationKind.TRUE | ExplanationKind.FALSE
console.log('explanation kinds:', '0x' + kinds.toString(16))

Usage guides

The examples are now in the Session and solving guide.