clara.rules.accumulators

A set of common accumulators usable in Clara rules.

accum

(accum {:keys [initial-value reduce-fn combine-fn retract-fn convert-return-fn], :as accum-map})

Creates a new accumulator. Users are encouraged to use a pre-defined accumulator in this namespace if one fits their needs. (See min, max, all, distinct, and others in this namespace.) This function exists for cases where a custom accumulator is necessary.

The following properties are accepted.

  • An initial-value to be used with the reduced operations.
  • A reduce-fn that can be used with the Clojure Reducers library to reduce items.
  • A combine-fn that can be used with the Clojure Reducers library to combine reduced items.
  • A retract-fn that can remove a retracted fact from a previously reduced computation.
  • An optional convert-return-fn that converts the reduced data into something useful to the caller. Simply uses identity by default.

all

(all)(all field)

Returns an accumulator that preserves all accumulated items. If given a field, returns all values in that field.

average

(average field)

Returns an accumulator that returns the average value of a given field.

count

(count)

Returns an accumulator that simply counts the number of matching facts

distinct

(distinct)(distinct field)

Returns an accumulator producing a distinct set of facts. If given a field, returns a distinct set of values for that field.

max

(max field & {:keys [returns-fact supports-retract], :or {supports-retract true}})

Returns an accumulator that returns the maximum value of a given field.

The caller may provide the following options:

  • :returns-fact Returns the fact rather than the field value if set to true. Defaults to false.
  • :supports-retract Keeps the history of facts used so the minimum value can be updated if the previous fact with the minimum value is retracted. This guarantees the expected behavior in the face of retractions but comes at the cost of maintaining all matching facts.

min

(min field & {:keys [returns-fact supports-retract], :or {supports-retract true}})

Returns an accumulator that returns the minimum value of a given field.

The caller may provide the following options:

  • :returns-fact Returns the fact rather than the field value if set to true. Defaults to false.
  • :supports-retract Keeps the history of facts used so the minimum value can be updated if the previous fact with the minimum value is retracted. This guarantees the expected behavior in the face of retractions but comes at the cost of maintaining all matching facts.

reduce-to-accum

(reduce-to-accum reduce-fn)(reduce-to-accum reduce-fn initial-value)(reduce-to-accum reduce-fn initial-value convert-return-fn)(reduce-to-accum reduce-fn initial-value convert-return-fn combine-fn)

Creates an accumulator using a given reduce function with optional initial value and conversion to the final result.

For example, a a simple function that return a Temperature fact with the highest value:

(acc/reduce-to-accum (fn [previous value] (if previous (if (> (:temperature value) (:temperature previous)) value previous) value)))

Note that the above example produces the same result as (clara.rules.accumulators/max :temperature :returns-fact true), and users should prefer to use built-in accumulators when possible. This funciton exists to easily convert arbitrary reduce functions to an accumulator.

Callers may optionally pass in an initial value (which defaults to nil), a function to transform the value returned by the reduce (which defaults to identity), and a function to combine two reduced results (which uses the reduce-fn by default).

sum

(sum field)

Returns an accumulator that returns the sum of values of a given field