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,
    name varchar(64) NOT NULL, -- defaults to filename if loaded via Toko
    description text
)

Programs contain rules, as described via the following table:

reasoner_program_rules (
    program_id bigint REFERENCES reasoner_programs(id),
    rule_id bigint REFERENCES reasoner_rules(id)
)

The rules of the programs are stored in the following table:

reasoner_rules (
    id bigint PRIMARY KEY,
    name varchar(64),
    description text,
    body text NOT NULL,         -- The rule in plain text
    createdat timestamp NOT NULL
)

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,
    triple_id bigint REFERENCES triples(id), -- id of inferred triple
    createdat timestamp NOT NULL
)

The second table, relating justifications to the tripples used as assumptions for the justification, is as follows:

reasoner_just_supp_triples (
    justification_id bigint REFERENCES reasoner_justifications(id),
    triple_id bigint REFERENCES triples(id)                         -- id of triple used as assumption
)

Finally, the table relating justifications to the rules used in the justification is:

reasoner_just_supp_rules ( 
    justification_id bigint REFERENCES reasoner_justifications(id),
    rule_id bigint REFERENCES reasoner_rules(id)                    -- id of rule used in the derivation of inferred triple
)