DanNet can queried in a variety of ways. This plurality is intentional as explained in the rationale. The directly supported query methods include:
- SPARQL: the official RDF query language
- SPARQL Algebra: the underlying algebraic expressions that SPARQL compiles to
- Aristotle queries: a Clojure DSL based on the SPARQL Algebra
- Graph traversal: various algorithms for traversing directed graphs
Furthermore, by importing DanNet into another RDF-supporting database, the query language of this database may also be used to query DanNet, e.g. Neo4j's Cypher query language.
Note: when querying Apache Jena's persisted TDB rather than an in-memory graph, you will need to wrap the querying code inside transactions. The
dk.cst.dannet.db.query
namespace contains functionality to help with this aspect of using Apache Jena. Transactions are always required for TDB 2, while they are only required for TDB 1 if at least one transaction has already occurred!
SPARQL is the official query language for querying RDF graphs. It is superficially similar to SQL, although queries take the form of dynamically joined sets of triples rather than the explicit table joins found in SQL.
SPARQL queries are compiled to an algebraic form before being run. This is similar to how SQL queries are also compiled to some form of relational algebra.
Although the algebraic form is mapped directly to Java classes in Apache Jena, this algebraic representation is usually illustrated using s-expressions - i.e. Lisp syntax - in the various documents on it. This makes it a suitable target for a DSL in a Lisp such as Clojure.
bgp
- i.e. Basic Graph Pattern, an expression enclosing a set of triples.
- Convert SPARQL to SPARQL Algebra
- W3C document on SPARQL Algebra
- Jena tutorial on SPARQL Algebra
- SPARQL S-Expressions
While Aristotle accepts regular SPARQL queries, its primary query language is based on Jena's SPARQL Algebra and superficially resembles Datomic-style Datalog.
- Square brackets - i.e. Clojure vectors - are used rather than parentheses.
- Triples are inferred from vectors, e.g.
[?s ?p ?o]
is equivalent to(triple ?s ?p ?o)
in the SPARQL Algebra.- Note: in Jena's implementation of SPARQL Algebra, square brackets already auto-expand into triples. See: https://jena.apache.org/documentation/query/service.html#algebra
The query language of Aristotle isn't well-specified, but its close relationship to the SPARQL Algebra can be gauged from the following namespaces:
arachne.aristotle.query.compiler
arachne.aristotle.query.spec
Since RDF triples constitute a directed graph, this graph can be queried programmatically using graph traversal algorithms.
One way to do this is by invoking the igraph Clojure library which has Apache Jena integration.
Note that currently some functions available in igraph don't work well with models that perform triple inferencing, e.g. OWL-enabled graphs. They are too slow (in the realm of several minutes) for any production application to use them.