Lecture 5: 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


Exercise 1: TripleLore

1.1 RDFS

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 exo to denote <http://example.org/ont/>, and use this as prefix for all new resources (e.g. exo:Planet, exo:orbitsStar, etc.). Remember that all properties must be typed with either owl:ObjectProperty or owl:DatatypeProperty, and that all owl:DatatypeProperties needs an rdfs:range (except the standard properties like rdfs:label, rdf:type, rdfs:subClassOf, etc.).

  1. Introduce classes for solar systems, planets, stars and moons, and state that these are all astronomical objects.
  2. State that exo:orbitsStar has domain astronomical object and range star (use the classes introduced above).
  3. State that exo:orbitsPlanet has domain moon and range planet.
  4. Make a new property exo:orbits, and that both exo:orbitStar and exo:orbitsPlanet are subproperties of ex:orbits, and that exo:orbits relates astronomical objects
  5. Add rdf:type, rdfs:domain and rdfs:range for all properties used in the data (i.e. rdfs:label, exo:density and exo:discovered)

Finally use TripleLore and Lore to load the file into your database. Note that you first need to use TripleLore to transform the RDF-file into a .lore-file, and then use Lore to execute the script over your database. When using TripleLore you should list both RDF-files in the command, e.g.:

java -jar triplelore.jar -o astronomy.lore astronomy_data.ttl astonomy_ont.ttl


We will now map the triplestore created above to proper RDF with Triplelore. So use Triplelore to make a mapping file e.g.:

java -jar triplelore.jar -m r2rml -d <dbname> -U <username> -h <host> -o mappings.ttl

where <dbname> and <host> is the name and host of the database you loaded the Lore-script into, and <username> is you username for the database.

Download Ontop (we will learn more about Ontop and R2RML later), unzip the folder and locate the ontop-executable. You also need to add a JDBC-driver for PostgreSQL for Ontop to work. This can be done by downloading this PostreSQL-driver and adding it to the jdbc-folder of Ontop. You can now query your triplestore with:

ontop query \
    --db-driver=org.postgresql.Driver \
    --db-url=jdbc:postgresql://<host>/<dbname> \
    --db-user=<username> \
    --db-password=<password> \
    -m mappings.ttl
    -q <queryfile>

where your SPARQL-query is in the file <queryfile>.

  1. Use the R2RML mappings, Ontop and SPARQL to find all exo:orbits-relationships
  2. Use the R2RML mappings, Ontop and SPARQL to find all planets.
  3. Why is e.g. exd:earth inferred to be a planet but not e.g. exd:mercury nor exd:neptune?

2 Jena

Use Jena to perform the reasoning and querying from exercise 1.2. A simple CLI-wrapper around the Jena Rules Reasoner can be downloaded here.

Rules for doing simple RDFS/OWL reasoning can be found here.

3. Rules

In this exercise we will use Jena-rules to directly define a richer semantics for the properties introduces in the previous exercise.

For the semantics in this exercise, we need some representation of an inconsistency (i.e. a contradiction within the data), so use triples of the form

_:inconsistency exo:inconsitency <description> .

where <description> is a string simply describing the inconsistency, e.g.

[] exo:inconsistency "1 is set to equal 2." .

For making these descriptions, you need to concat strings, as they may use values bound to variables. You also need to make blank nodes. For Jena-rules, see Builtin Primitives on this page for builtin functions. In particular, see makeSkolem and strConcat.

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

  1. Make a property denoting equality, exo:equals, i.e. it should be transitive, symmetric, and if two things are equal they should have the same triples (e.g. if $x exo: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 exo:equals $x)
  2. Now make a property denoting unequals, exo: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 exo:equals, we need to add a rule stating that $x exo:unequals $x results in an inconsistency)
  3. Now state that exo:orbitsStar and exo:orbitsPlanet are functional, i.e. one can only orbit one star/planet, i.e. if $z is exo:orbitsStar- or exo:orbitsPlanet-related to both $x and $y, then $x must be equal to $y.
  4. State that exo: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 (exo:orbits) $y, and $y is a star, then $x exo:orbitsStar $y.
  6. Furthermore, state that if $x orbits (exo:orbits) $y, and $y is a planet, then $x exo:orbitsPlanet $y.
  7. State that something cannot orbit itself (i.e. if $x exo: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 exo:equals-relationships. Also query for all exo:unequals-relationships.
  9. Add a triple that triggers one of the inconsitency-rules (i.e. producing an inconsitency triple), and write a query finding all inconsistencies.

4. 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 use Jena’s Rule Reasoner:

  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 equivalent (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 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 rules for the same reason as above. What does the reasoner now say about the resources in the above graph?

5 Bonus Exercise

Would it be possible to solve exercises 3 and 4 with Lore-rules? If so, how? And if not, why?


The solution to the exercises can be seen here.