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.
- An optional combine-fn that can be used with the Clojure Reducers library to combine reduced items.
- An optional 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.
grouping-by
(grouping-by field)
(grouping-by field convert-return-fn)
Return a generic grouping accumulator. Behaves like clojure.core/group-by.
field
- required - The field of a fact to group by.convert-return-fn
- optional - Converts the resulting grouped data. Defaults to clojure.core/identity.
max
(max field & {:keys [returns-fact]})
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.
min
(min field & {:keys [returns-fact]})
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.
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 to add new items to the same reduced value by default).
sum
(sum field)
Returns an accumulator that returns the sum of values of a given field