Getting started with RDFox

Felicity Mulford
Oxford Semantic Technologies
7 min readMay 13, 2020

--

This article will show you how to very quickly set up RDFox locally, import triples, rules and run queries. You can request an RDFox license here and download the latest release here. Once you’ve received your license key, place it is a directory with your RDFox download.

Please note, this article has been updated since the release of RDFox Version 4.0.0.

The article consists of the following:

  1. Creating a data store
  2. Importing data
  3. Running queries
  4. Inserting data using SPARQL
  5. Adding rules
  6. Deleting Facts
  7. Stopping and Re-starting RDFox
  8. Getting started with the web console
  9. Visualising data in the web console

If you would like a visual aid, you can watch our Getting Started Webinar. The webinar was created for Version 3.0.0, but remains applicable to Version 4.0.0.

1. Creating a datastore

Open the directory with RDFox and your license key, in the IDE of your choice. Open a new terminal and then launch RDFox by typing one of the following:

./RDFox sandbox (Linux or Mac)

RDFox.exe sandbox (windows)

This should result in a command prompt, where you can run any of the commands described in Section 10.2.2 of the documentation.

Data stores hold all facts and rules in RDFox. At present, there is no data store in the directory, so we must create one before we can load any triples. To create a data store we use the dstore command as follows:

dstore create family par-complex-nn

N.B. This example uses a family dataset, hence the data store name ‘family’.

The above command initializes our family data store as a parallel store with a complex indexing strategy. Other types of data store do exist, and the documentation goes through these, see Section 6.2.1 for details.

Next, to ensure that subsequent shell commands address our new data store, we use the active command to set the family data store active within the shell:

active family

2. Importing data

We are going to load a small RDF graph in turtle format into the active data store. This can be downloaded from here and saved in the same directory as the RDFox executable and license key.

To import the data into the active datastore we use the following command:

import data.ttl

TIP: remember to use the command set output out if you would like the terminal to print results. This command should report the time taken by the import and that 21 data items have been loaded from the file.

3. Running Queries

The primary query language recognized by RDFox is SPARQL. SPARQL queries can be typed or pasted directly into the shell. For more information on RDFox and SPARQL, read our Basic introduction here.

Copy and paste the following SPARQL into the shell and hit enter to print all of the triples in the store:

SELECT ?s ?p ?o WHERE { ?s ?p ?o }

You should see a few lines beginning @prefix followed by a blank line and then the original 21 triples (facts) from data.ttl. After the triples, a summary of the number of answers returned and the time taken for the query is printed.

To demonstrate a simple conjunction, print each person’s forename:

SELECT ?p ?n WHERE { ?p rdf:type :Person . ?p :forename ?n }

Note that :brian is not returned as this entity is not of type :Person as required by the first part of the where clause.

4. Inserting Data Using SPARQL

It is also possible to modify the contents of the data store via queries. For example, make the :marriedTo relationship symmetric:

INSERT { ?x :marriedTo ?y } WHERE { ?y :marriedTo ?x }

This adds one new triple to the data store reflecting the fact that Lois is married to Peter, which was derived from the fact that Peter is married to Lois. Running the query from step 3 again should now return 22 triples (use the up arrow key to step back through your command history).

5. Adding Rules

Reasoning is the ability to calculate the logical consequences of applying a set of rules to a set of facts. RDFox uses the Datalog language for expressing rules. For more detailed explanation on Datalog and RDFox, read our basic introduction here.

Reasoning is useful because it allows us to enrich a data store. For example, we can add a rule to state that if ?c has a parent ?p then ?p has a child ?c. With this rule, RDFox can then determine all of the :hasChild relationships — including for any new families that we add to the data store later on.

The above rule can be added with the following command:

import ! [?p, :hasChild, ?c] :- [?c, :hasParent, ?p] .

To check that we now have some triples with :hasChild as the predicate, evaluate the following query:

SELECT ?p ?c WHERE { ?p :hasChild ?c }

Four results are returned, all of which have all been added by RDFox by evaluating the rule defined above.

6. Deleting Facts

To prove that the rule really is ‘live’, delete the triple that says :stewie has :lois as a parent and check that the corresponding :hasChild relationship goes away too. Do this with the following SPARQL:

DELETE { :stewie :hasParent :lois } WHERE { :stewie :hasParent :lois }

Now re-run the query from step 2 and note that the answe :lois :stewie . no longer appears.

7. Stopping and Re-starting RDFox

Shut down RDFox by typing the quit command. Since RDFox is an in-memory database, and because we started the process using the sandbox command, which disables any form of persistence, the contents of the data store will be dropped when the process exits. While experimenting with RDFox, it may therefore be useful to write the commands to initialize the data store and load data into a script which can be passed to RDFox at startup.

The following script repeats the whole of this tutorial, including starting an endpoint.

dstore create family par-complex-nnactive familyimport data.ttlset output outprefix : <https://oxfordsemantic.tech/RDFox/getting-started/>SELECT ?s ?p ?o WHERE { ?s ?p ?o }SELECT ?p ?n WHERE { ?p rdf:type :Person . ?p :forename ?n }INSERT { ?x :marriedTo ?y } WHERE { ?y :marriedTo ?x }import ! [?p, :hasChild, ?c] :- [?c, :hasParent, ?p] .SELECT ?p ?c WHERE { ?p :hasChild ?c }DELETE { :stewie :hasParent :lois } WHERE { :stewie :hasParent :lois }

For ease, you can save this script to a new file e.g. <working_directory>/start.txt and then run it when you restart RDFox with the following command:

./RDFox sandbox . start.txt (on Linux or Mac)

RDFox.exe sandbox . start.txt (on Windows).

To find out more about how scripts can help, read the article on script writing here.

8. Getting started with the web console

RDFox also offers a UI console which provides a user friendly approach to writing and visualising SPARQL queries. To launch the console, RDFox needs to expose a REST API which includes a SPARQL over HTTP endpoint. We can do this from the terminal with the following command:

endpoint start

The terminal will print the port number. In a web browser, navigate to http://localhost:<port>/console, and substitute <port> with the port number printed in the terminal.

At present the web-based console is an experimental feature, so for furthur instructions on how to use the console for querying see the documentation here.

9. Visualising data in the web console

The data explorer was released as part of RDFox Version 4.0.0, and provides users with a user friendly method for representing and exploring their graph data.

For example, the graph representation of the family data store would be the following:

For a more detailed walkthrough, please visit our documentation.

To request a demo or start a free trial, click here.

Team and Resources

The team behind Oxford Semantic Technologies started working on RDFox in 2011 at the Computer Science Department of the University of Oxford with the conviction that flexible and high-performance reasoning was a possibility for data intensive applications without jeopardising the correctness of the results. RDFox is the first market-ready knowledge graph designed from the ground up with reasoning in mind. Oxford Semantic Technologies is a spin out of the University of Oxford and is backed by leading investors including Samsung Venture Investment Corporation (SVIC), Oxford Sciences Innovation (OSI) and Oxford University’s investment arm (OUI).

Photo credit: Alex Radelich

--

--

Felicity Mulford
Oxford Semantic Technologies

Employee at Oxford Semantic Technologies and Ox Mountain. OST have developed RDFox, a high performance knowledge graph and semantic reasoning engine.