Rules offer an expressive way to process and manipulate a knowledge graph. Rules help bring the intelligence layer closer to the data and can also help with query writing. This article aims to provide a list of best practices for getting the most out of your rules and queries. You can also read our introduction to rules here and download our rule guide for more concrete examples.
A rule is an ‘if-then’ statement. For example:
We express rules using datalog. Datalog is a declarative and formal logic-based programming language based on Prolog. Datalog uses the following format:
The formula to the left of the :- operator is the rule head (the ‘then’ part) and the formula to the right is the rule body (the ‘if’ part).
Intuitively, a rule says “if [ ?x , :hasParent , ?y] , [?y :hasBrother ?z] all hold, then [ ?x , :hasUncle , ?z ] holds as well”.
A query is a request for data or information from a database. Queries retrieve and model data stored within RDFox. The query language used by RDFox is SPARQL, which is the RDF standard query language.
Queries can be typed or pasted directly into the shell. For example:
This query would select all subject-predicate-object triples within RDFox.
Below are some tips and best practices for writing rules and queries with RDFox.
Add all rules before the facts or add rules and facts in an arbitrary order but grouped in a single transaction. This will usually increase the performance of the first reasoning operation.
Make rule bodies as selective as possible to improve the performance of a rule. This helps reduce the number of matches in the body that then propagate to the head.
Use rules to materialise costly and/or frequently used sub-queries. We recommend experimenting with the trade-off between reasoning and query answering time to make queries simpler to write, maintain and answer using rules.
Store your rules in separate files by purpose and import them incrementally when they are required. Rules in RDFox materialise as soon as they are imported which is why we recommend.
Restrict variables within your rule body. If types exist within your data use these to.
Start small and build on what you have. In the case of rules, incremental retraction/addition means that you don’t have to worry about rebooting the whole system every time you change a rule.
Write queries before you write rules. The query will let you know how much data will be affected by the rule.
Test your rules. Test whether the query you wrote before the rule (see above point) and the rule return the same result.
This can be done in a query like:
This will return the set of people where the numbers don’t match, so the query should return 0 results.
Don’t forget to define the type of a variable used in the rules. This won’t be an issue in most cases but can slow down performance if the total number of possible relations to evaluate is large. Example:
The types of ?customer and ?referralLink aren’t defined in the rule which would have to verify all the :has relations. Defining the type helps reduce the number of matches in the body that then propagate to the head. A more appropriate solution would be to do:
Replace joins in filters with regular joins: Consider the following rule, which marks as similar all pairs of entities whose labels are indistinguishable modulo case sensitivity.
While correct, the above rule is very inefficient as it forces RDFox to compare the labels of all pairs of entities, which becomes infeasible for moderately large number of entities (e.g. on a dataset with 1M entities this will result in 1T comparisons). The issue above is that for every triple that matches the first atom RDFox must iterate through all triples that match the second atom and apply the filter condition accordingly. RDFox has no way of reducing the number of compared pairs of entities.
An alternative solution is to precompute the values on which the join is performed in a separate rule and use a regular join in a second rule as illustrated next.
While more verbose, RDFox will evaluate the above program in time proportional to the number of similar pairs (as opposed to all pairs), which depending on the data could be the difference between terminating and not. The first rule simply computes the lower-case labels of entities, while the second rule uses a direct join on the computed labels to identify the similar entities. Because RDFox uses full indexing, it can efficiently identify, for every triple that matches the first atom, only the compatible triples that match the second atom. Note that this solution uses additional memory to store the relation :lcase_label, which could be a significant but also necessary overhead for solving the problem.
Avoid unnecessary Filter statements (in both rules and queries): you are effectively throwing away answers that you already spent (a lot of) time computing.
If you want to say that two variables of the same type should be equal, just call them the same thing.
Avoid cross-product blow-ups (a special case of the selective tip but might be worth mentioning specifically).
Do you have any best practises to add to our list? Or situations to avoid? Feel free to get in touch! We will continue to update this article.
To request an evaluation license click here.