RDFox Beginner Cheat Sheet
Part Two: Legislate Technologies tips and tricks
In our last article, we spoke to Vaishali Raghvani, a knowledge engineer from Legislate Technologies, on her experience of getting started with RDFox. Since then, Legislate has kindly shared their ‘cheat sheet’ which stores their tips and tricks for getting to grips with RDFox quickly…
We believe this cheat sheet will be useful for other RDFox beginners, so are sharing it with our community.
Do let us know what you think of this cheat sheet, or if you have anything to add to it from your own experiences…
To read the Q&A with Legislate Technologies Knowledge Engineer, click here…
HELPFUL DOCUMENTATION
- OST documentation (RDFox docs)
- W3c (SPARQL docs)
SETUP
- “start scripts” — always prepare them as they easily set everything up, especially if you want to restart from a specific point again later
- This also helps you remember what commands you need to run in RDFox
Example:
endpoint startdstore create myDataStore type par-complex-nn
active myDataStoreprefix : <https://legislate.tech/>set output out
set query.answer-format text/x.tab-separated-values-abbrevimport data.ttl
import rules.dlog
- The
query.answer-format
setting determines how your output looks. You can leave it set to default or change it to e.g.text/x.tab-separated-values-abbrev
to space the values out more - Separating rules into separate .dlog files really helps — it makes it easier to identify which rules are causing problems
- The
clear
command clears the datastore of any rules or data. It can help when you need a fresh start without restarting the shell
DATABASE NOTES
- Use the option
-persist-ds on|off
to turn persistence on/off when creating a new datastore - Keeping persistence off in early stages of development makes things easier
RDFox modes:
- Sandbox
- For those learning / experimenting with RDFox
- Provides full command line
- Does not persist roles/data by default
2. Shell
- Provides full command line
- Can be switched to daemon mode
- Persists roles and data by default
3. Daemon
- Intended for running a managed production environment
- Persists roles and data by default
SYNTAX, FUNCTIONS AND COMMANDS
- A single
a
character is a shorthand forrdf:type
- Data you want to import can be written in, for example, .ttl (Turtle) format while queries should be written in SPARQL
- Graph patterns in SPARQL are separated by the
.
symbol while atoms in Datalog rules are separated by,
- Always remember end Datalog rules with
.
Brackets
- Default graph atoms in rules are surrounded by square brackets - [], and have commas separating the subject, predicate, and object.
Thus, we will write an atom like so:[?subject, ?predicate, ?object]
or[?person, :hasNationality, ?nationality]
2. The WHERE
clause in SPARQL queries should be surrounded by a pair of curly brackets - {}
Dates
- RDFox supports the
xsd:date
andxsd:dateTime
datatypes (amongst others). There is in the formatYYYY-MM-DDThh:mm:ssTZ
.
E.g."2021-05-05T12:35:12+05:00"^^xsd:dateTime
is 5th May 2021 at 12:35:12, in the GMT+5 timezone (thus it’s the same as 07:35:12 GMT). - RDFox supports the
xsd:duration
type, it can be written for example like this:”P1Y17M42D"^^xsd:duration
(1 year, 17 months, 42 days). You can also add hours, minutes and seconds. - We can’t always divide durations — what is half of a month? It’s not definitive or easily divisible. That is also why RDFox will refuse to calculate the average of durations
ERRORS
- If you get an error, look at what line and column it’s reported on. It might also be useful to check the previous line as well
- While testing your rules initially, add them one by one and query their results after each addition. This will make it easier to know which rule to look at if you see unexpected results
- RDFox rules are declarative, so to avoid cycles make sure you are not making any property dependent on itself, e.g.
[?person, :hasDOB, "Unknown"] :- [?person, a, :Person], NOT EXISTS ?dob IN ([?person, :hasDOB, ?dob]) .
This rule would try to insert"Unknown"
as the date of birth for people who don’t have one, thus giving them a date of birth, which leads to a cycle
Aggregate:
- In an aggregate atom, you can omit the ON clause in order to take into account the whole datastore
- It’s good to assert the types of the variables in the body of your rule
- If you want to mark the element with the smallest value of some aggregate, use a rule like this:
[?person, :hasFewestPets, true]
:-
[?person, a, :PetOwner],
AGGREGATE(
[?otherPerson, a, :PetOwner],
[?otherPerson, :hasPetCount, ?otherPetCount],
BIND MIN(?otherPetCount) AS ?minPetCount
),
[?person, :hasPetCount, ?minPetCount] .
RULES
- Section 6 of the RDFox documentation is great for writing rules
- A
BIND
atom evaluates an expression and assigns its value to a variable, e.g.BIND(3*?daysOutCamping*?numberOfParticipants AS ?totMealsNeeded)
- If you imported a rule from the file rule.dlog and you wish to remove it, you can simply run
import — rule.dlog
to delete it (provided the contents of that file are still the same) - You can use a
FILTER
atom with operators such as!=
(not equal to) to eliminate certain query results
DATA MODELLING
- Use IRIs only for data that represents unique objects. Storing, for example, dates and numbers as literals often makes more sense
- It is good to add types to your data using the
rdf:type
property.:popeye :loves :olive_oyl .
is a valid triple, but it would be better to have::popeye :loves :olive_oyl .
:popeye rdf:type :CartoonCharacter, :Man .
:popeye rdf:type :CartoonCharacter, :Woman .
You can add multiple types by using,
as above - Structuring your data properly will help you work with the RDFox web console and visualisation tool
QUERIES
SELECT
,CONSTRUCT
andASK
queries do not modify the database, whileINSERT
DELETE
queries do- Oftentimes RDFox rules can achieve the same result as an
INSERT
query more neatly and efficiently - Regular
INSERT
queries need aWHERE
clause, whereasINSERT DATA
operations do not SELECT
queries are used for retrieving data (as a table) from the database,CONSTRUCT
queries are used for retrieving data (as a graph),ASK
queries only check if a given pattern is matched or not andDELETE
queries remove data from the data store when it matches a pattern
We hope this cheat sheet is helpful for our RDFox beginners. To try RDFox yourself, you can request a 30 day free trial or a demonstration. For more information on RDFox check out our website and medium publication.
About Legislate Technologies
Legislate is an early stage legal technology startup which allows large landlords and small businesses to easily create, sign and manage contracts that are prudent and fair. Legislate’s platform is built on a patent-pending knowledge graph which streamlines the contracting process and aggregates contract statistics to quickly unlock valuable insights. Legislate’s team marries technical and legal expertise to create a painless contracting experience for its users.
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). The author is proud to be a member of this team.