Skip to content
Ryan Brush edited this page Sep 14, 2013 · 28 revisions

Developer documentation is in progress.

Defining rules

Rules are typically defined with defrule, which has this structure:

defrule railroad diagram

A simple rule looks like this:

(defrule free-lunch-with-gizmo
  "Anyone who purchases a gizmo gets a free lunch."
  [Purchase (= item :gizmo)]
  =>
  (insert! (->Promotion :free-lunch-with-gizmo :lunch)))

Where Purchase is a Clojure Record or Java JavaBean containing an item field. If there exists a purchase that matches the :gizmo keyword, a new promotion is inserted into working memory, by simply creating a new Promotion record.

The left-hand side of the rule -- everything prior to the "=>" in the above -- uses a constraint expression described in the Constraint Expressions section below.

The right-hand side of the rule -- everything past the "=>" in the above example -- is simply a Clojure S-expression and can invoke arbitrary code, or use insert! to insert new information into the working memory.

Defining queries

Queries are typically defined with defquery, which has the following structure:

defquery railroad diagram

A sample query looks like this:

(defquery get-promotions
  "Query to find promotions for the purchase."
  []
  [?promotion <- Promotion])

The first argument is a vector of keywords to indicate parameters to a query. For instance, if we wanted to run a query that retrieves only a certain type of promotions, we might write this:

(defquery get-promotions
  "Query to find promotions for the purchase."
  [:?type]
  [?promotion <- Promotion (== ?type type)]) ; Bind the ?type query to the promotion type.

A caller may then execute that query with arguments. So if we only wanted to find lunch promotions, we might perform the query like this:

(query session get-promotions :?type :lunch)

The conditions used by a query are the same structure as the left-hand side of a rule. See the Condition Expressions section below for usage.

Condition Expressions

Condition Expressions are the contents of the left-hand side of rules, or the constraints used in queries.

This part of rules and queries contains a series of expressions, each of which is one of the following:

  • A fact expression, which selects a fact based on some given criteria.
  • A boolean expression, which is simply a hierarchical and/or/not structure of other expressions.
  • An accumulator, which is mechanism to reason about collections of facts
  • A test, which is an arbitrary Clojure S-expression that can run predicate-style tests on variables bound earlier in the rule or query.

Details on each of these are below.

Fact Expressions

fact expression

TODO: add details

Boolean Expressions

TODO: add details.

Accumulators

TODO: add details

Tests

TODO: add details

Additional Resources

Clone this wiki locally