Aggregation operators
Aggregation operators evaluate a given function over a set of facts. They can be used in expressions that are the RHS of assignments; this means that, unlike single fact-operators, they cannot be used in conditions.
The supported aggregations are:
monotonic increasing summation |
|
monotonic decreasing product |
|
monotonic increasing set union |
|
monotonic minimum |
|
monotonic maximum |
|
monotonic count |
|
minimum |
|
maximum |
|
count |
Note that aggregates are only implemented for floats.
A rule with an assignment using an aggregation operator has the general form:
q(K1, K2, Kn, J) :- body, J = maggr(x, <C1,…,Cm>)
Where:
-
K1, …, Knare zero or more group by arguments. -
bodyis the rule body. -
maggris a placeholder for an aggregation function (mmin,mmax,msum,mprod). -
C1, …, Cmare zero or more variables of the body, called contributor arguments (with Ci ≠ Kj, 1 ≤ i ≤ m, 1 ≤ j ≤ n). -
xis a constant, a body variable, or an expression containing only single-fact operators.
Contributors C1, …, Cm are not present in munion, mmax and mmin.
|
For each distinct n-tuple of K1, …, Kn, a monotonic decreasing (increasing) aggregate function maggr maps an input multi-set of vectors G, each of the form gi = (C1,…,Cm,xi) into a set D of values, such that xi is in D if xi is less (greater) than the previously observed value for the sub-vector of contributors (C1, …, Cm). Such aggregation functions are monotonic with respect to set containment and can be used in Vadalog together with recursive rules to calculate aggregates without resorting to stratification (separation of the program into ordered levels based on the dependencies between rules).
During the execution of a program:
-
The aggregation memorizes the current minimum (or maximum) value of
xfor each vector (C1, …, Cm). -
For each activation of a rule with the monotonic aggregation, an updated value for the group selected by
K1, …, Knis calculated by combining all the values in D for the various vectors of contributors. -
The combination depends on the semantics of
maggr(e.g., minimum, maximum, sum, product, count) and is calculated by memorizing a current aggregate, updated with new contributions from the sequence of rule activations.
Assumption:
-
If a position
posin a head for predicatepis calculated with an aggregate function, whenever a head forpappears in any other rule,posmust be existentially quantified and calculated with the same aggregate function.
This assumption ensures the homogeneity of the facts with existentially aggregated functions.
msum
Monotonic increasing summation.
msum(X, <C1,...,Cm>)
Where:
-
Xis the value to be summed. -
<C1,…,Cm>are zero or more variables of the body, which are called contributor arguments.
s(0.1, 2, "a").
s(0.2, 2, "a").
s(0.5, 3, "a").
s(0.6, 4, "b").
s(0.5, 5, "b").
f(J, Z) :- s(X, Y, Z), J = msum(X, <Y>).
@output("f").
f(0.1, "a")
f(0.2, "a")
f(0.7, "a")
f(0.6, "b")
f(1.1, "b")
mprod
Monotonic decreasing product.
mprod(X, <C1,...,Cm>)
Where:
-
Xis the value to be multiplied. -
<C1,…,Cm>are zero or more variables of the body, which are called contributor arguments.
s(0.1, 2, "a").
s(0.2, 2, "a").
s(0.5, 3, "a").
s(0.6, 4, "b").
s(0.5, 5, "b").
f(J, Z) :- s(X, Y, Z), J = mprod(X, <Y>).
@output("f").
f(0.1, "a")
f(0.1, "a")
f(0.05, "a")
f(0.6, "b")
f(0.3, "b")
munion
Monotonic increasing set union.
munion(X)
Where:
-
Xis the set to be united.
s({1, 2}, 2, "a").
s({3, 4}, 2, "a").
s({5, 6}, 3, "a").
s({7, 8}, 4, "b").
s({9, 10}, 5, "b").
f(J, Z) :- s(X, Y, Z), J = munion(X).
@output("f").
f({1, 2}, "a")
f({1, 2, 3, 4}, "a")
f({1, 2, 3, 4, 5, 6}, "a")
f({7, 8}, "b")
f({7, 8, 9, 10}, "b")
mmin
Monotonic minimum.
mmin(X)
Where:
-
Xis the value to find the minimum of.
s(0.1, 2, "a").
s(0.2, 2, "a").
s(0.5, 3, "a").
s(0.6, 4, "b").
s(0.5, 5, "b").
f(J, Z) :- s(X, Y, Z), J = mmin(X).
@output("f").
f(0.1, "a")
f(0.1, "a")
f(0.1, "a")
f(0.6, "b")
f(0.5, "b")
mmax
Monotonic maximum.
mmax(X)
Where:
-
Xis the value to find the maximum of.
s(0.1, 2, "a").
s(0.2, 2, "a").
s(0.5, 3, "a").
s(0.6, 4, "b").
s(0.5, 5, "b").
f(J, Z) :- s(X, Y, Z), J = mmax(X).
@output("f").
f(0.1, "a")
f(0.2, "a")
f(0.5, "a")
f(0.6, "b")
f(0.6, "b")
mcount
Monotonic count.
mcount(X, <C1,...,Cm>)
Where:
-
Xis the value to count. -
<C1,…,Cm>are zero or more variables of the body, which are called contributor arguments.
s(0.1, 2, "a").
s(0.2, 2, "a").
s(0.5, 3, "a").
s(0.6, 4, "b").
s(0.5, 5, "b").
f(J, Z) :- s(X, Y, Z), J = mcount(X, <Y>).
@output("f").
f(1, "a")
f(1, "a")
f(2, "a")
f(1, "b")
f(2, "b")
min
Minimum.
min(X)
Where:
-
Xis the value to find the minimum of.
b(1, 2).
b(1, 3).
b(2, 5).
b(2, 7).
h(X, Z) :- b(X, Y), Z = min(Y), X > 0.
@output("h").
h(1, 2)
h(2, 5)