TypeScript / JavaScript

Predicate

Predicate class, factory methods and registry declarations.

The factory examples use an open engine and the binding imports from the page reference context. The engine is created with Engine.create(). A factory returns a declaration; registerDomain installs it.

Class: Predicate

TypeScript · classSince v0.15.0
Predicate
Declaration
export class Predicate {
    constructor(name: string, arity: number, flags: number);
    readonly name: string;
    readonly arity: number;
    readonly flags: number;
}
A predicate name, arity and flag set. Creating this value does not register a domain.
Creation and access
Attributes
namestringreadonly field
Declared predicate name.
aritynumberreadonly field
Number of ordered terms (zero through four).
flagsnumberreadonly field
Bitwise combination of predicate flags.
Example
const declaration = new Predicate('user', 1, PRED_EDB | PRED_QUERY)
console.log('name:', declaration.name)
console.log('arity:', declaration.arity)
console.log('flags:', '0x' + declaration.flags.toString(16))
name: user
arity: 1
flags: 0x5

Constructor: Predicate()

TypeScript · constructorSince v0.15.0
Predicate.constructor
new Predicate(
name: string,
arity: number,
flags: number
): Predicate
Describe a predicate using an explicit combination of flags.
Arguments
namestring
Name of the predicate or export.
aritynumber
Declared number of terms, between zero and four.
flagsnumber
Predicate flags combined with bitwise OR.
Return value
Predicate

Returns a frozen Predicate declaration without validating its name, arity or flags. Name and arity are checked when the domain is registered; flag combinations are checked when a policy uses the declaration.

Example
const queryableUser = new Predicate('user', 1, PRED_EDB | PRED_QUERY)
engine.registerDomain('documents', [queryableUser], [])
console.log('name:', queryableUser.name)
console.log('arity:', queryableUser.arity)
console.log('flags:', '0x' + queryableUser.flags.toString(16))
name: user
arity: 1
flags: 0x5

Factories

Predicate.edb

TypeScript · static methodSince v0.15.0
Predicate.edb
Predicate.edb(
name: string,
arity: number
): Predicate
Create a runtime-input predicate declaration.
Arguments
namestring
Name of the predicate or export.
aritynumber
Declared number of terms, between zero and four.
Return value
Predicate

Returns a frozen Predicate declaration without validating its name, arity or flags. Name and arity are checked when the domain is registered; flag combinations are checked when a policy uses the declaration.

Example
const user = Predicate.edb('user', 1)
engine.registerDomain('documents', [user], [])
console.log('name:', user.name)
console.log('arity:', user.arity)
console.log('flags:', '0x' + user.flags.toString(16))
name: user
arity: 1
flags: 0x1

Predicate.edbQuery

TypeScript · static methodSince v0.15.0
Predicate.edbQuery
Predicate.edbQuery(
name: string,
arity: number
): Predicate
Create a queryable runtime-input predicate declaration.
Arguments
namestring
Name of the predicate or export.
aritynumber
Declared number of terms, between zero and four.
Return value
Predicate

Returns a frozen Predicate declaration without validating its name, arity or flags. Name and arity are checked when the domain is registered; flag combinations are checked when a policy uses the declaration.

Example
const request = Predicate.edbQuery('request', 2)
engine.registerDomain('documents', [request], [])
console.log('name:', request.name)
console.log('arity:', request.arity)
console.log('flags:', '0x' + request.flags.toString(16))
name: request
arity: 2
flags: 0x5

Predicate.idb

TypeScript · static methodSince v0.15.0
Predicate.idb
Predicate.idb(
name: string,
arity: number
): Predicate
Create a derived predicate declaration.
Arguments
namestring
Name of the predicate or export.
aritynumber
Declared number of terms, between zero and four.
Return value
Predicate

Returns a frozen Predicate declaration without validating its name, arity or flags. Name and arity are checked when the domain is registered; flag combinations are checked when a policy uses the declaration.

Example
const canRead = Predicate.idb('can_read', 2)
engine.registerDomain('documents', [canRead], [])
console.log('name:', canRead.name)
console.log('arity:', canRead.arity)
console.log('flags:', '0x' + canRead.flags.toString(16))
name: can_read
arity: 2
flags: 0x2

Predicate.idbQuery

TypeScript · static methodSince v0.15.0
Predicate.idbQuery
Predicate.idbQuery(
name: string,
arity: number
): Predicate
Create a queryable derived predicate declaration.
Arguments
namestring
Name of the predicate or export.
aritynumber
Declared number of terms, between zero and four.
Return value
Predicate

Returns a frozen Predicate declaration without validating its name, arity or flags. Name and arity are checked when the domain is registered; flag combinations are checked when a policy uses the declaration.

Example
const allow = Predicate.idbQuery('allow', 2)
engine.registerDomain('documents', [allow], [])
console.log('name:', allow.name)
console.log('arity:', allow.arity)
console.log('flags:', '0x' + allow.flags.toString(16))
name: allow
arity: 2
flags: 0x6

Predicate.policyFact

TypeScript · static methodSince v0.15.0
Predicate.policyFact
Predicate.policyFact(
name: string,
arity: number
): Predicate
Create a source-fact predicate declaration.
Arguments
namestring
Name of the predicate or export.
aritynumber
Declared number of terms, between zero and four.
Return value
Predicate

Returns a frozen Predicate declaration without validating its name, arity or flags. Name and arity are checked when the domain is registered; flag combinations are checked when a policy uses the declaration.

Example
// For a fact written in policy source rather than request input:
const publicDocument = Predicate.policyFact('public_document', 1)
engine.registerDomain('documents', [publicDocument], [])
console.log('name:', publicDocument.name)
console.log('arity:', publicDocument.arity)
console.log('flags:', '0x' + publicDocument.flags.toString(16))
name: public_document
arity: 1
flags: 0x8

Predicate.policyFactQuery

TypeScript · static methodSince v0.15.0
Predicate.policyFactQuery
Predicate.policyFactQuery(
name: string,
arity: number
): Predicate
Create a queryable source-fact predicate declaration.
Arguments
namestring
Name of the predicate or export.
aritynumber
Declared number of terms, between zero and four.
Return value
Predicate

Returns a frozen Predicate declaration without validating its name, arity or flags. Name and arity are checked when the domain is registered; flag combinations are checked when a policy uses the declaration.

Example
const publicDocument = Predicate.policyFactQuery('public_document', 1)
engine.registerDomain('documents', [publicDocument], [])
console.log('name:', publicDocument.name)
console.log('arity:', publicDocument.arity)
console.log('flags:', '0x' + publicDocument.flags.toString(16))
name: public_document
arity: 1
flags: 0xc

PRED_EDB · PRED_IDB · PRED_QUERY · PRED_POLICY_FACT.

PRED_EDB

TypeScript · constantSince v0.15.0
PRED_EDB
export const PRED_EDB: 1;

Predicate flag for application-supplied runtime facts.

Value

1

Example
console.log('PRED_EDB:', '0x' + PRED_EDB.toString(16))

PRED_IDB

TypeScript · constantSince v0.15.0
PRED_IDB
export const PRED_IDB: 2;

Predicate flag for derived facts.

Value

2

Example
console.log('PRED_IDB:', '0x' + PRED_IDB.toString(16))

PRED_QUERY

TypeScript · constantSince v0.15.0
PRED_QUERY
export const PRED_QUERY: 4;

Predicate flag permitting public ground queries and enumeration.

Value

4

Example
console.log('PRED_QUERY:', '0x' + PRED_QUERY.toString(16))

PRED_POLICY_FACT

TypeScript · constantSince v0.15.0
PRED_POLICY_FACT
export const PRED_POLICY_FACT: 8;

Predicate flag for facts written in policy source.

Value

8

Example
console.log('PRED_POLICY_FACT:', '0x' + PRED_POLICY_FACT.toString(16))

Usage guides

The examples are now in the Registries and Predicates.