Example. Forester 5.0 › New datalog-based query engine › An example datalog query [013E]
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 @{...}.