This quickstart walks you through the complete lifecycle of a Datalog evaluation using AbcDatalog’s Java API: writing a program that computes the transitive closure of a directed graph, parsing it, initialising an evaluation engine, and running queries against the derived facts. The example below is drawn directly fromDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/HarvardPL/AbcDatalog/llms.txt
Use this file to discover all available pages before exploring further.
EngineExample.java in the AbcDatalog source repository.
Add the Maven dependency
Write a Datalog program
A Datalog program consists of EDB facts (base facts known before evaluation) and IDB rules (logical rules that derive new facts). The example below defines a graph with four directed edges and two rules that derive the full transitive closure relation
tc:edge(a, b).— a ground fact (EDB). Constants begin with lowercase letters.tc(X, Y) :- edge(X, Y).— a rule:tcholds for every direct edge.tc(X, Y) :- tc(X, Z), tc(Z, Y).— a recursive rule:tcis closed transitively. Variables begin with uppercase letters.
example/tc.dtlg in the repository contains a richer version of this program:example/tc.dtlg
Parse the program
Use
DatalogTokenizer to tokenise your input and DatalogParser.parseProgram() to produce a Set<Clause>. In EngineExample.java the program is read from a StringReader, but in practice you would pass a FileReader pointing to a .dtlg file:Create and initialize an engine
Instantiate a
SemiNaiveEngine — a sequential, bottom-up evaluation engine — and call init() with the parsed program. The engine evaluates all derivable facts eagerly during initialisation.init() throws DatalogValidationException if the program violates Datalog safety or stratification constraints. Catch or declare it in your method signature.Build a query
A query is a Both options produce an equivalent query. Use the AST approach when you are constructing queries dynamically at runtime.
PositiveAtom with variables standing for the positions you want to enumerate. You can construct one by parsing a query string, or by building the AST programmatically:Run the query and iterate over results
Call For the transitive closure program above, this prints every
engine.query(q) to retrieve all facts that unify with the query atom. The result is a Set<PositiveAtom> — each element is a ground atom representing one derived fact.tc(_, _) pair reachable via the defined edges, for example:Complete example
Here is the fullmain method from EngineExample.java, combining all steps above:
EngineExample.java
SemiNaiveEngine is a good default for most programs, but AbcDatalog offers concurrent bottom-up engines and top-down QSQ engines as well. See Evaluation engines for a comparison and guidance on when to choose each one.