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.
| Used by | Engine.registerDomain |
|---|
Class: Predicate
export class Predicate {
constructor(name: string, arity: number, flags: number);
readonly name: string;
readonly arity: number;
readonly flags: number;
}- constructor
new Predicate(name, arity, flags)Describe a predicate using an explicit combination of flags.
- factory
Predicate.edb()Create a runtime-input predicate declaration.
- factory
Predicate.edbQuery()Create a queryable runtime-input predicate declaration.
- factory
Predicate.idb()Create a derived predicate declaration.
- factory
Predicate.idbQuery()Create a queryable derived predicate declaration.
- factory
Predicate.policyFact()Create a source-fact predicate declaration.
- factory
Predicate.policyFactQuery()Create a queryable source-fact predicate declaration.
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()
new Predicate(
name: string,
arity: number,
flags: number
): PredicatePredicateReturns 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.
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
Predicate.edb(
name: string,
arity: number
): PredicatePredicateReturns 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.
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
Predicate.edbQuery(
name: string,
arity: number
): PredicatePredicateReturns 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.
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
Predicate.idb(
name: string,
arity: number
): PredicatePredicateReturns 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.
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
Predicate.idbQuery(
name: string,
arity: number
): PredicatePredicateReturns 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.
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
Predicate.policyFact(
name: string,
arity: number
): PredicatePredicateReturns 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.
// 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
Predicate.policyFactQuery(
name: string,
arity: number
): PredicatePredicateReturns 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.
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
Constants
PRED_EDB · PRED_IDB · PRED_QUERY · PRED_POLICY_FACT.
PRED_EDB
export const PRED_EDB: 1;Predicate flag for application-supplied runtime facts.
1
console.log('PRED_EDB:', '0x' + PRED_EDB.toString(16))PRED_EDB: 0x1
PRED_IDB
export const PRED_IDB: 2;Predicate flag for derived facts.
2
console.log('PRED_IDB:', '0x' + PRED_IDB.toString(16))PRED_IDB: 0x2
PRED_QUERY
export const PRED_QUERY: 4;Predicate flag permitting public ground queries and enumeration.
4
console.log('PRED_QUERY:', '0x' + PRED_QUERY.toString(16))PRED_QUERY: 0x4
PRED_POLICY_FACT
export const PRED_POLICY_FACT: 8;Predicate flag for facts written in policy source.
8
console.log('PRED_POLICY_FACT:', '0x' + PRED_POLICY_FACT.toString(16))PRED_POLICY_FACT: 0x8
Usage guides
The examples are now in the Registries and Predicates.