Aggregations
Monotonic aggregations are functions for incremental and recursion-friendly computation of aggregate values. They maintain state outside of the program, allowing you to perform calculations across recursive steps. The functions are:msum(X, [K1, …, Kn])— incremental summprod(X, [K1, …, Kn])— incremental productmcount([K1])— incremental countmmin(X, [K1, …, Kn])— incremental minimummmax(X, [K1, …, Kn])— incremental maximummunion(X, [K1, …, Kn])— incremental union of setsmavg(X)— incremental averagemmedian(X, "variant")— incremental median
mcount take as first argument the value to be used in the incremental computation.
Group-by behavior is achieved by having the same variables appear in both the rule head and body. The aggregation functions themselves do NOT take group-by variables as parameters.
Basic Example
Simple Sum Example
Contributors
Contributors allow controlling which facts contribute to the aggregation:Using mmax Without Intermediate Results
mmin and mmax produce no intermediate results:
Computing Sum Without Intermediate Results
Set Union with munion
Graph Indegree Example
found(2).
Median Aggregation
Themmedian aggregate computes the median of a dataset. Three variants are available:
| Variant | Best For | Accuracy | Memory |
|---|---|---|---|
"exact" | Small to medium datasets (< 10,000 values) | 100% | All values |
"p2_algorithm" | Very large datasets (millions+) | 95-99% | 40 bytes |
"reservoir_sampling" | Large datasets (10,000-1,000,000+) | High | 8KB |
maxcount
Returns the key tuple with the highest frequency:
hotspot("Component2", 3).
