Lecture 4: Semantics and Reasoning

In this lecture we will look at semantics for datasets, how it can be defined, and how it can be used for reasoning.


Relevant Wiki-pages


Below are some exercises that requires you to use Toko and KiWi, and will show you some of the reasoning features of KiWi and the sKWRL rule language.

Note: For some reason, if one loads a program (file with rules) multiple times, the reasoner doesn’t seem to finish. Therefore, whenever you want to reapply the rules, in particular between on exercise 4, you should clean the triplestore and load the data fresh before applying the rules. There does not seem to be a problem with loading multiple different programs, though.


Download this RDF dataset about astronomical bodies.

We will make an RDFS ontology encoding information the classes and properties used, and we will use the prefix ex-o to denote <http://example.org/ont/>, and use this as prefix for all new resources (e.g. ex-o:Planet, ex-o:orbitsStar, etc.).

  1. Introduce classes for solar systems, planets, stars and moons, and state that these are all astronomical objects.
  2. State that ex-o:orbitsStar has domain astronomical object and range star (use the classes introduced above).
  3. State that ex-o:orbitsPlanet has domain moon and range planet.
  4. Make a new property ex-o:orbits, and that both ex-o:orbitStar and ex-o:orbitsPlanet are subproperties of ex:orbits, and that ex-o:orbits relates astronomical objects

Add the dataset, the ontology and the RDFS rules to a clean KiWi triplestore.

2. Planets and Orbits

  1. Use SPARQL to find all ex-o:orbits-relationships
  2. Use SPARQL to find all planets.
  3. Why is e.g. ex-d:earth inferred to be a planet but not e.g. ex-d:mercury nor ex-d:neptune?

3. sKWRL

In this exercise we will use the sKRWL rule language to directly define a richer semantics for the properties introduces in the previous exercise. However, for this we need some representation of an inconsistency, and for this we will use the triple ex-o:inconsistency rdfs:label "<description>", where <description> is a string simply describing the inconsistency.

Note that the rules made in this exercise should be added to a new file.

  1. Make a property denoting equality, ex-o:equals, i.e. it should be transitive, symmetric, and if two things are equal they should have the same triples (e.g. if $x ex-o:equals $y and $x $r $z then $y $r $z) (Note: it should of course also be reflexive, but if we add this then our triplestore will be filled with uninteresting triples of the form $x ex-o:equals $x)

  2. Now make a property denoting unequals, ex-o:unequals, i.e. it should be symmetric, and raise an inconsistency if two things are both equal and unequal (since we did not add reflexivity to ex-o:equals, we need to add a rule stating that $x ex-o:unequals $x results in an inconsistency)

  3. Now state that ex-o:orbitsStar and ex-o:orbitsPlanet are functional, i.e. one can only orbit one star/planet, i.e. if $z is ex-o:orbitsStar- or ex-o:orbitsPlanet-related to both $x and $y, then $x must be equal to $y.

  4. State that ex-o:orbits is transitive. E.g. since the Moon orbits Earth, and Earth orbits the Sun, the Moon orbits the Sun.

  5. Furthermore, state that if $x orbits (ex-o:orbits) $y, and $y is a star, then $x ex-o:orbitsStar $y.

  6. Furthermore, state that if $x orbits (ex-o:orbits) $y, and $y is a planet, then $x ex-o:orbitsPlanet $y.

  7. State that something cannot orbit itself (i.e. if $x ex-o:orbits $y then $x is unequal $y)

  8. Add these rules to the triplestore used in the previous exercise. Use SPARQL to query for all ex-o:equals-relationships. Also query for all ex-o:unequals-relationships.

4. Justifications

Try to add the following triples to your triplestore (the same as above, containing all of the other triples and rules):

ex-d:comet123 ex-o:orbits ex-d:sun .
ex-d:sun ex-o:orbits ex-d:comet123 .

(available here) and load the rules you made in the above exercise again. Note: loading these rules takes a bit of time (a few seconds), as there are quite a lot of triples inferred, but more importantly, many ways of inferring the same triple. This is due to the rules for equality. For large datasets, equality reasoning is known to not scale! (See Section 3 in this paper for more info on this.)

Use the justifications in the triplestore to find out why we now have an inconsistency (i.e. we have inferred triples with ex-o:inconsistency as subject). You can use the following query to see the assumptions and rules used in the shortest derivation of one of the inconsistencies:

SELECT assumptions, rules
FROM triples_str AS ts
     JOIN justifications_str AS js ON (ts.id = js.triple_id)
WHERE subject = 'http://example.org/ont/inconsistency'
ORDER BY js.num_assumptions -- Get the derivation with the least assumptions

5. Meaning from structure in RDF, revisited

Recall exercise 4 from the exercises on data structure with the following triples:

@prefix ex: <http://example.org/> .

ex:ping ex:pang ex:peng .
ex:pong ex:pong ex:pang .
ex:peng ex:pong ex:peng .

available here.

We will now encode the information you were given in that exercise in an ontology, and let the reasoner come to the same conclusion as what you were supposed to come to in that exercise (you should run Toko’s modes clean and init between each exercise):

  1. Encode the information that ex:pong is a property that only relates properties to other properties in RDFS (i.e. ex:pong has domain and range rdfs:Property), and apply the RDFS rules to the triples above. What does the reasoner then say about the other resources?
  2. Encode the information that ex:pong denotes equality. What does the reasoner now say about the resources in the above graph? Note: One could imagine encoding this by using OWL and stating that ex:pong is equivelent (or subproperty) of owl:sameAs). However, OWL will then treat ex:pong and ex:pang as punned individuals, and statements about the property does not get transferred to the individual. Thus, an OWL reasoner would not infer that ex:ping owl:sameAs ex:peng, since the semantics still treats e.g. the individual ex:pang as different from the property ex:pang, despite them having the same IRI. Therefore, we need to encode this via sKWRL rules directly.
  3. Encode the information that ex:pong denotes (reflexive) superproperty (i.e. the inverse of subproperty) (e.g. ex:knows is a superproperty of ex:hasFriend, and ex:inRelationshipWith is a superproperty of ex:isMarriedTo). Again, this needs to be encoded via sKWRL rules for the same reason as above. What does the reasoner now say about the resources in the above graph?


The solution to the exercises can be seen here.