prelimit
Sometimes it is useful to limit an output relation to a fixed number of tuples.
One can achieve this in two different way with the use of the post-processing
annotations limit
and prelimit
as shown below.
@post("atomName", "limit(N)").
@post("atomName", "prelimit(N)").
In the snippet atomName
is the name of the atom at hand and N
the limit on the number of tuples.
The limit
annotation acts just as limit in SQL: after populating the output relation and after
applying all additional post-processing operations, the relation is trimmed to the required size.
Consider for example the following program:
p(2).
p(4).
p(3).
p(7).
p(1).
q(X) :- p(X).
@output("q").
@post("q", "orderBy(1)").
@post("q", "limit(3)").
After transferring all tuples from relation p
to relation q
and after performing the required
ordering, the result of the execution will be
q(1).
q(2).
q(3).
In contrast, the prelimit
annotation limits the number of tuples that are loaded into the
relation and only then the remaining post-processing annotations are applied. Consider for
example the following variant of the above program, wherein prelimit
is used instead of limit
.
p(2).
p(4).
p(3).
p(7).
p(1).
q(X) :- p(X).
@output("q").
@post("q", "orderBy(1)").
@post("q", "prelimit(3)").
After transferring the first three tuples from relation p
to relation q
and after performing the required ordering, the result of the execution will be
q(2).
q(3).
q(4).
Note that unlike limit
, which comes after reasoning and does not affect the program
execution, prelimit
effectively terminates reasoning once the required number of tuples
have been loaded into the output relation.