precept.rules

compiling-cljs?

(compiling-cljs?)

Return true if we are currently generating cljs code. Useful because cljx does not provide a hook for conditional macro expansion.

define

macro

(define & forms)

Prolog-style rule.

(define [?e :derived-fact ?v] :- ?e :my-fact ?v)

Head/consequence is declared first followed by body/conditions. Uses :- as separator. Name is auto-generated. Auto-assigned to default activation group. Inserts are always logical. Does not support non-DSL syntax (e.g. println, let).

defquery

macro

(defquery name & body)

Clara’s defquery with precept DSL.

(defquery my-query [:v] [?fact <- [_ :my-fact ?v]])

Defines a named query that can be called with Clara’s query function with optional arguments.

defsub

macro

(defsub kw & body)

Defines subscription response.

(defsub :my-sub-name _ :my-fact ?v => {:my-fact ?v})

sub-name - keyword of subscription registered with subscribe LHS - any valid LHS syntax RHS - a hash-map to be passed to subscribers of sub-name

fire-rules

q

rule

macro

(rule name & body)

session

macro

(session name & sources-and-options)

Defines a session.

(session my-session ’my-proj/my-ns :db-schema my-schema)

Accepts same arguments as Clara’s defsession plus :db-schema and :client-schema options. Rules and queries are loaded from the provided namespace. To load rules from multiple namespaces, a vector of namespaces may be provided.

:db-schema - Datomic-format schema for persistent facts. Precept enforces cardinality and uniqueness similarly to Datomic. All facts are treated as non-unique and one-to-one cardinality by default.

:client-schema - Datomic-format schema for non-perstent facts. Precept enforces cardinality and uniqueness for attributes in this schema the same way it does for :db-schema. It serves two main purposes. 1. To define client-side facts as one-to-many or unique. 2. To allow Precept’s API to filter out facts that should not be persisted when writing to a database.

Defaults:

:fact-type-fn - :a Tells Clara to index facts by attribute. This is the preferred :fact-type-fn option for working with eav tuples.

:ancestors-fn - util/make-ancestors-fn If :db-schema and/or :client-schema are provided, uses Clojure’s make-hierarchy and derive functions to assign cardinality and uniqueness for each provided attribute. When no schemas are provided, all facts will be descendents of #{:all :one-to-one}. Because all facts descend from :all, rules written with the :all attribute can match facts independently of their attributes.

:activation-group-fn - (util/make-activation-group-fn :calc) Allows categorization and prioritization of some rules over others. Puts a rule into a prioritization group according to the optional first argument to rule. Assigns the default values {:group :calc :salience 0 :super false} to rules where the without these arguments. argument to rule. :salience determines precedence within the same group. Rules marked :super are active across all groups.

:activation-group-sort-fn - (util/make-activation-group-fn [:action :calc :report :cleanup]) Determines the scheme by which some rules are given priority over others. Rules in the :action group will be given the chance to fire before rules in the :calc group and so on. When determining the priority of two rules in the same group, the :salience property serves as a tiebreaker, with higher salience rules receiving precedence over lower salience ones.