Skip to content

Defining Rules

Jesse Bouwman edited this page Apr 7, 2017 · 2 revisions

Rules

Rules can be built directly through an API, or can be constructed by a parser.

Rule parser

The current parser is called Pabu. This is a Datalog parser, and is based on relations between entities in the database graph. (TODO: link to grammar)

Namespace: naga.lang.pabu
Functions:
    (read-stream java.io.InputStream)
    (read-str java.lang.String)

Reads a rule program written in Datalog. Returns a structure containing both rules and axioms (new data to be inserted).

{:rules rule-sequence  
 :axioms axiom-sequence}

Rule API

Rules can be created directly with the API.

Namespace: naga.rules
Functions:
    (rule head body)
    (rule head body name)
Macros:
    (r head... :- body...)
    (r name head... :- body...)

Creates a rule.
All rules have 3 elements: name; head; body Name: If a name is not provided, then a unique name will be created from the first predicate found in the head.
Head: A sequence of triplets. A triple is a pattern to be inserted into the graph, and is of the form:
[entity attribute value]
The entity and attribute should be either keywords or variables. The value may be a keyword, a variable, or a scalar value (string, number, date object, etc). Variables are either of the form $varname or %varname. The $varname form indicates a bound variable from the body. The %varname form indicates an unbound var, and will instantiate a new entity instance for each match of the rule.
Each bindings of variables come from the body matching data in the database.
Note: If a $varname appears in the head that is unbound, it will currently be automatically be converted to a %varname form. These vars will eventually be used to allow the dangerous operation of new-entity recursion, but this is not yet supported.
Body: A sequence of triples and lists. The triples are identical in form to the sequence in the head, with the exception that the %varname form is not allowed. These are patterns that will be matched against the database. Each pattern must be matched for a binding to be formed and the rule to produce output. This means that all triples are in an AND pattern (a conjunction). More operations are scheduled to be implemented.
Lists in the body indicate a filtering operation. The will execute the first element of the list (as a function) against the arguments. Truthy values will permit a set of bindings to trigger the rule.

Examples:

(rule [[$b :parent $cid]] [[$a :sibling $b] [$b :id $bin] [$a :parent $cid] [$c :id $cid]])
Clone this wiki locally