Forester 5.0 › New datalog-based query engine [013A]
- October 20, 2024
- Jon Sterling, Kento Okura
As the query engine has evolved, it has become a bit unwieldy and misshapen. In order to prevent further churn and facilitate the long-term development and use of Forester, we are replacing the query engine and graph analysis with a more general tool: datalog. With this change comes the ability to introduce arbitrary relations of any arity; this is a powerful and dangerous tool, so use it wisely.
On performance trade-offs [013B]
- October 20, 2024
- Jon Sterling, Kento Okura
Switching to datalog makes forests that make heavy use of queries a bit slower to render. We considered this trade-off carefully, but believe that for the long-term health of Forester, this is acceptable. In the near future, Forester will render trees on demand rather than all at once (except when you are preparing a static archive for upload/deployment); in this mode of use, Forester will be faster than ever.
Renamed built-in relations [013C]
- October 20, 2024
- Jon Sterling, Kento Okura
Forester’s built-in binary relations have been accessible via macros like \rel/authors, etc. In this release, these relations are being renamed to make them more pleasant and consistent to use with datalog.
| Old name | New name |
|---|---|
\rel/tags |
\rel/has-tag |
\rel/taxa |
\rel/has-taxon |
\rel/authors |
\rel/has-author |
\rel/contributors |
\rel/has-direct-contributor |
\rel/transclusion |
\rel/transcludes |
\rel/links |
\rel/links-to |
New built-in relations [013D]
- October 20, 2024
- Jon Sterling, Kento Okura
A few new built-in relations have been added.
\rel/is-node ?Xis a unary relation that holds for any tree address?X. This is needed when specifying reflexive binary relations: the clauseR ?X ?X -:is not allowed by datalog, so one must useR ?X ?X -: {\rel/is-node X}.\rel/transcludes/transitive-closureis the transitive closure of\rel/transcludes.\rel/transcludes/reflexive-transitive-closureis the reflexive transitive closure of\rel/transcludes.\rel/is-reference ?Xholds when?Xis designated as a reference/bibliographical tree. Currently, the built-in rules of the database deduce\rel/is-reference ?Xfor any tree?Xwith taxonReference. The purpose of abstracting from the taxon is to enable non-English users of Forester to benefit from the computation of bibliographies.- Similarly,
\rel/is-person ?Xholds when?Xis designated as a personal/biographical tree. Currently, the built-in rules of the database deduce\rel/is-reference ?Xfor any tree?Xwith taxonPerson. \rel/in-bundle-closure ?X ?Yis the reflexive transitive closure of the union of the transclusion relation with the relation of?Xrequiring the asset?Y. The purpose of this relation is to easily bundle up fragments of a forest (see § [30FN]).
All of these built-in relations can be defined in Forester’s dialect of datalog; they are predefined for convenience, and because they are used in the computation of backmatter.
Example An example datalog query [013E]
- October 20, 2024
- Jon Sterling, Kento Okura
In order to illustrate the use of the datalog engine, I will show how to construct a query that will find all the reference trees written by jonmsterling that are tagged either accepted or refereed. The first thing we must do is define the smallest relation containing all trees tagged accepted or refereed; this is done by adding clauses to the database as follows.
First we define the name of a relation. In the current release, relations are global strings and there is no facility to generate a “fresh” one; that may change in the future.
\let\rel/accepted-or-refereed{jonmsterling.query.bibliography.accepted-or-refereed}Next, we add two defining clauses for our relation. In what follows, \datalog simply delimits content that should be parsed as a datalog expression, and \execute instructs Forester to execute the datalog expression.
\execute\datalog{
\rel/accepted-or-refereed ?X -: {\rel/has-tag ?X '{refereed}}
}
\execute\datalog{
\rel/accepted-or-refereed ?X -: {\rel/has-tag ?X '{accepted}}
}The left-hand side of the reversed turnstile -: is a proposition representing the goal; the right hand side is given by propositions representing the premises, each surrounded in braces and separated by whitespace. Metavariables are written with a question mark sigil. Literal content is embedded into a datalog proposition by surrounding it in '{...}; we will see that literal content is one kind of vertex in the graph database.
Now that we have actually defined the relation that we will use, we can run a query:
\query\datalog{
?X -:
{\rel/has-author ?X @{jonmsterling}}
{\rel/is-reference ?X}
{\rel/accepted-or-refereed ?X}
}The structure of a query identifies the metavariable being solved behind the turnstile, and then lists the clauses that must hold. Whereas a content vertex is addressed via '{...}, a URL vertex is addressed using @{...}.