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
Exercises
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.).
- Introduce classes for solar systems, planets, stars and moons, and state that these are all astronomical objects.
- State that
exo:orbitsStar
has domain astronomical object and range star (use the classes introduced above). - State that
exo:orbitsPlanet
has domain moon and range planet. - Make a new property
exo:orbits
, and that bothexo:orbitStar
andexo:orbitsPlanet
are subproperties ofex:orbits
, and thatexo:orbits
relates astronomical objects - Add
rdf:type
,rdfs:domain
andrdfs:range
for all properties used in the data (i.e.rdfs:label
,exo:density
andexo: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
1.2 SPARQL
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>
.
- Use the R2RML mappings, Ontop and SPARQL to find all
exo:orbits
-relationships - Use the R2RML mappings, Ontop and SPARQL to find all planets.
- Why is e.g.
exd:earth
inferred to be a planet but not e.g.exd:mercury
norexd: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.
- 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
) - 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 toexo:equals
, we need to add a rule stating that$x exo:unequals $x
results in an inconsistency) - Now state that
exo:orbitsStar
andexo:orbitsPlanet
are functional, i.e. one can only orbit one star/planet, i.e. if$z
isexo:orbitsStar
- orexo:orbitsPlanet
-related to both$x
and$y
, then$x
must be equal to$y
. - State that
exo:orbits
is transitive. E.g. since the Moon orbits Earth, and Earth orbits the Sun, the Moon orbits the Sun. - Furthermore, state that if
$x
orbits (exo:orbits
)$y
, and$y
is a star, then$x exo:orbitsStar $y
. - Furthermore, state that if
$x
orbits (exo:orbits
)$y
, and$y
is a planet, then$x exo:orbitsPlanet $y
. - State that something cannot orbit itself (i.e. if
$x exo:orbits $y
then$x
is unequal$y
) - Add these rules to the triplestore used in the previous exercise.
Use SPARQL to query for all
exo:equals
-relationships. Also query for allexo:unequals
-relationships. - 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:
- 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 rangerdfs:Property
), and apply the RDFS rules to the triples above. What does the reasoner then say about the other resources? - 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 thatex:pong
is equivalent (or subproperty) ofowl:sameAs
). However, OWL will then treatex:pong
andex:pang
as punned individuals, and statements about the property does not get transferred to the individual. Thus, an OWL reasoner would not infer thatex:ping owl:sameAs ex:peng
, since the semantics still treats e.g. the individualex:pang
as different from the propertyex:pang
, despite them having the same IRI. Therefore, we need to encode this via rules directly. - Encode the information that
ex:pong
denotes (reflexive) superproperty (i.e. the inverse of subproperty) (e.g.ex:knows
is a superproperty ofex:hasFriend
, andex:inRelationshipWith
is a superproperty ofex: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?
Solution
The solution to the exercises can be seen here.