KiWi Reasoner
The KiWi triplestore supports reasoning via a datalog-like rule language over triples, known as SKRWL. The rules are applied in a forward-chaining fashion, and all inferences are added to the triplestore once either the rules or triples are added. KiWi also supports truth maintenance (so all inferred tripples are deleted if one deletes a necessary assumption or rule in the derivation of the triple).
SKWRL rules
SKWRL rules have the form:
(s1 p1 o1), (s2 p2 o2), ..., (sN pN oN) -> (s p o)
Where each (sI, pI, oI)
denote a regular RDF triple, but
where each element of the triple can either be a constant (IRI, blank
node or literal), or a variable. Variables are prefixed with
$
.
For example:
($x rdf:type $y), ($y rdfs:subClassOf $z) -> ($x rdf:type $z)
Prefixes are written in a similar way as for Turtle, but without a dot at the end, e.g.:
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Rules are gathered in files, typically with the suffix
skwrl
, and with prefixes on top. They can then be loaded
into KiWi via Toko.
All programs have a name, and Toko will use the filename as the program name.
The sKWRL program defining (most of) the RDFS semantics can be found here, and a program defining (some of) the OWL semantics can be found here.
Relational Structure
When a program is added, all of its rules are stored in the database.
Reasoning is performed when the rules are added, and all inferred
triples are added to the triples
-table with
inferred
set to true
. However, reasoning is
only performed when adding programs. Thus, triples inserted after the
program is added will not be used for reasoning unless the program is
reinserted again.
This however, makes insert operations more efficient, and one can pospone reasoning to when all data is inserted.
The reason KiWi stores the programs and rules in the database is for truth maintenance and justifications.
Programs and rules
KiWi stores programs (sets of rules given a name) in the table
reasoner_programs
:
reasoner_programs (id bigint PRIMARY KEY,
varchar(64) NOT NULL, -- defaults to filename if loaded via Toko
name
description text )
Programs contain rules, as described via the following table:
reasoner_program_rules (REFERENCES reasoner_programs(id),
program_id bigint REFERENCES reasoner_rules(id)
rule_id bigint )
The rules of the programs are stored in the following table:
reasoner_rules (id bigint PRIMARY KEY,
varchar(64),
name
description text,body text NOT NULL, -- The rule in plain text
timestamp NOT NULL
createdat )
Justifications
For each inferred triple, KiWi stores its jusitificaiton, i.e. the (non-inferred) triples and rules used to derive the inferred triple. Note that there can be many justifications for the same triple.
Justifications are represented with three tables, the first of them
being reasoner_justifications
:
reasoner_justifications (id bigint PRIMARY KEY,
REFERENCES triples(id), -- id of inferred triple
triple_id bigint timestamp NOT NULL
createdat )
The second table, relating justifications to the tripples used as assumptions for the justification, is as follows:
reasoner_just_supp_triples (REFERENCES reasoner_justifications(id),
justification_id bigint REFERENCES triples(id) -- id of triple used as assumption
triple_id bigint )
Finally, the table relating justifications to the rules used in the justification is:
reasoner_just_supp_rules ( REFERENCES reasoner_justifications(id),
justification_id bigint REFERENCES reasoner_rules(id) -- id of rule used in the derivation of inferred triple
rule_id bigint )