Difference between revisions of "Lab: RDFS"

 
(41 intermediate revisions by 5 users not shown)
Line 1: Line 1:
  
=Lab 5: RDFS Programming with rdflib and owlurl=
+
=Lab 7: RDFS Programming with rdflib and owlrl=
  
 
==Topics==
 
==Topics==
Line 9: Line 9:
 
owlrl.RDFSClosure (RDFS_Semantics, closure, flush_stored_triples)
 
owlrl.RDFSClosure (RDFS_Semantics, closure, flush_stored_triples)
  
Vocabularies:  
+
'''Vocabularies: '''
  
RDF(type)
+
RDF.type
  
RDFS (label, comment, subClassOf, subPropertyOf, domain, range)
+
RDFS.subClassOf, RDFS.subPropertyOf, RDFS.domain, RDFS.range, RDFS.label, RDFS.comment,
  
 
==Tasks==
 
==Tasks==
Pip install owlurl
+
First, pip install owlrl.
Consider the following extensions to the task from lab 2:
+
The RDFS Vocabulary can be imported from rdflib.namespace, just like FOAF or RDF.
"University of California, Berkeley and University of Valencia are both Universities.
 
All universities are higher education instituttions (HEIs). Having a B.Sc. from a HEI and having a M.Sc.
 
from a HEI are special cases of gradutating from that HEI. Only persons can graduate from a HEI. That a person has a degree in a subject means
 
that the person has expertise in that subject. Only persons can have expertise, and what they have expertise
 
in is always a subject."
 
  
Create and output the RDFS graph in RDFlib (as an InfModel that wraps a default Model) - if you can, try to build on
+
'''Consider the following Scenario:'''
your example from lab 2!
+
"University of California and University of Valencia are both Universities.
 +
All universities are higher education institutions (HEIs). Only persons can have an expertise, and what they have expertise in is always a subject. Only persons can graduate from a HEI. If you are a student, you are in fact a person as well. That a person is married to someone, means that they know them. Finally, if a person has a name, that name is also the label of that entity."
 +
 
 +
'''Create RDFS triples corresponding to the text above with RDFlib''' - if you can, try to build on
 +
your example from lab 2!  
 +
 
 +
To create the graph in python, you can just use the g.add syntax as we have done previously, or you can use the following code sample to parse a file into a graph:
 +
 
 +
<syntaxhighlight>
 +
from rdflib import Graph, Namespace
 +
import owlrl
 +
 
 +
 
 +
# Create the graph
 +
g = Graph()
 +
 
 +
# Parse input data into the graph, format is dependent on the file format. Here turtle (ttl). And location is the path to the local file
 +
g.parse(location="input.ttl", format="turtle")
 +
</syntaxhighlight>
 +
 
 +
 
 +
Using these three lines we can add automatically the inferred triples (like ex:University rdf:type ex:Higher_Education_Institute) :
 +
<syntaxhighlight>
 +
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
 +
rdfs.closure()
 +
rdfs.flush_stored_triples()
 +
</syntaxhighlight>
 +
 
 +
After you have done this, try to add the following scenario to you graph as well:
 +
"Having a degree from a HEI means that you have also graduated from that HEI. That a city is a capital of a country means that this city is located in that country. That someone was involved in a meeting, means that they have met the other participants. If someone partook in a meeting somewhere, means that they have visited that place"
 +
To do this, you will have to swap out the line
 +
<syntaxhighlight>
 +
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
 +
</syntaxhighlight>
 +
with
 +
<syntaxhighlight>
 +
rdfs = owlrl.OWLRL.OWLRL_Semantics(g, False, False, False)
 +
</syntaxhighlight>
 +
As last two sentences require more advanced reasoning with OWL. Or you can use what we already learned: g.query(), CONSTRUCT the relevant triples, and add them to the graph.
  
 
Check that simple inference works -  make sure that your graph contains triples like these, even if
 
Check that simple inference works -  make sure that your graph contains triples like these, even if
 
you have not asserted them explicitly:
 
you have not asserted them explicitly:
* that UCB and UV are HEIs
+
* that University of California and Valencia are HEIs
 +
* that Cade, Emma, and Mary are all persons
 
* that Cade and Emma have both graduated from some HEI
 
* that Cade and Emma have both graduated from some HEI
* that Cade and Emma both have expertises
+
* that Cade knows Mary
* that Cade and Emma are both persons
+
 
* that biology and chemistry are both subjects
+
One way to check if the triples are there:
 +
<syntaxhighlight>
 +
universities = g.query("""
 +
PREFIX ex: <http://example.org/>
 +
ASK {
 +
    ex:University_of_California rdf:type ex:Higher_Education_Institution.
 +
}
 +
""")
 +
print(bool(universities))
 +
</syntaxhighlight>
  
 
Rewrite some of your existing code to use rdfs:label in a triple and add an rdfs:comment to the same resource.
 
Rewrite some of your existing code to use rdfs:label in a triple and add an rdfs:comment to the same resource.
  
 
==If you have more time...==
 
==If you have more time...==
Create a new RDFS graph (or InfModel) that wraps an empty base (or raw) model. This graph contains only RDFS axioms. Write it out in Turtle and check that you understand  the meaning and purpose of each axiom.
+
Create a new RDFS graph that wraps an empty graph. This graph contains only RDFS axioms. Write it out in Turtle and check that you understand  the meaning and purpose of each axiom.
  
 
Create an RDF (not RDFS) graph that contains all the triples in your first graph (the one with all the people and universities). Subtract all the triples in the axiom graph from the people/university graph. Write it out to see that you are left with only the asserted and entailed triples and that none of the axioms remain.
 
Create an RDF (not RDFS) graph that contains all the triples in your first graph (the one with all the people and universities). Subtract all the triples in the axiom graph from the people/university graph. Write it out to see that you are left with only the asserted and entailed triples and that none of the axioms remain.
  
Download the SKOS vocabulary from https://www.w3.org/2009/08/skos-reference/skos.rdf and save it to a file called, e.g., SKOS.rdf .
+
<!-- Download the SKOS vocabulary from https://www.w3.org/2009/08/skos-reference/skos.rdf and save it to a file called, e.g., SKOS.rdf .
 
Use the schemagen tool (it is inside your Jena folders, for example under apache-jena-3.1.1/bin) to generate a Java class for the SKOS vocabulary.  
 
Use the schemagen tool (it is inside your Jena folders, for example under apache-jena-3.1.1/bin) to generate a Java class for the SKOS vocabulary.  
 
You need to do this from a console window, using a command like "<path>/schemagen -i <infile.rdf> -o <outfile.java>".
 
You need to do this from a console window, using a command like "<path>/schemagen -i <infile.rdf> -o <outfile.java>".
Line 48: Line 91:
 
Copy the SKOS.java file into your project in the same package as your other Java files,  and try to use SKOS properties  
 
Copy the SKOS.java file into your project in the same package as your other Java files,  and try to use SKOS properties  
 
where they fit, for example to organise the keywords for interests and expertise.
 
where they fit, for example to organise the keywords for interests and expertise.
 +
-->
 +
 +
==Useful Readings==
 +
*[https://wiki.uib.no/info216/index.php/File:S05-RDFS-11.pdf Lecture Notes]
 +
*[https://wiki.uib.no/info216/index.php/Python_Examples Example page]

Latest revision as of 16:03, 10 March 2022

Lab 7: RDFS Programming with rdflib and owlrl

Topics

Basic RDFS graph programming in RDFlib. Entailments and axioms with owlrl.

Classes/Methods/Vocabularies

owlrl.RDFSClosure (RDFS_Semantics, closure, flush_stored_triples)

Vocabularies:

RDF.type

RDFS.subClassOf, RDFS.subPropertyOf, RDFS.domain, RDFS.range, RDFS.label, RDFS.comment,

Tasks

First, pip install owlrl. The RDFS Vocabulary can be imported from rdflib.namespace, just like FOAF or RDF.

Consider the following Scenario: "University of California and University of Valencia are both Universities. All universities are higher education institutions (HEIs). Only persons can have an expertise, and what they have expertise in is always a subject. Only persons can graduate from a HEI. If you are a student, you are in fact a person as well. That a person is married to someone, means that they know them. Finally, if a person has a name, that name is also the label of that entity."

Create RDFS triples corresponding to the text above with RDFlib - if you can, try to build on your example from lab 2!

To create the graph in python, you can just use the g.add syntax as we have done previously, or you can use the following code sample to parse a file into a graph:

from rdflib import Graph, Namespace
import owlrl


# Create the graph
g = Graph()

# Parse input data into the graph, format is dependent on the file format. Here turtle (ttl). And location is the path to the local file
g.parse(location="input.ttl", format="turtle")


Using these three lines we can add automatically the inferred triples (like ex:University rdf:type ex:Higher_Education_Institute) :

rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
rdfs.closure()
rdfs.flush_stored_triples()

After you have done this, try to add the following scenario to you graph as well: "Having a degree from a HEI means that you have also graduated from that HEI. That a city is a capital of a country means that this city is located in that country. That someone was involved in a meeting, means that they have met the other participants. If someone partook in a meeting somewhere, means that they have visited that place" To do this, you will have to swap out the line

rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)

with

rdfs = owlrl.OWLRL.OWLRL_Semantics(g, False, False, False)

As last two sentences require more advanced reasoning with OWL. Or you can use what we already learned: g.query(), CONSTRUCT the relevant triples, and add them to the graph.

Check that simple inference works - make sure that your graph contains triples like these, even if you have not asserted them explicitly:

  • that University of California and Valencia are HEIs
  • that Cade, Emma, and Mary are all persons
  • that Cade and Emma have both graduated from some HEI
  • that Cade knows Mary

One way to check if the triples are there:

universities = g.query("""
PREFIX ex: <http://example.org/>
ASK {
    ex:University_of_California rdf:type ex:Higher_Education_Institution.
} 
""")
print(bool(universities))

Rewrite some of your existing code to use rdfs:label in a triple and add an rdfs:comment to the same resource.

If you have more time...

Create a new RDFS graph that wraps an empty graph. This graph contains only RDFS axioms. Write it out in Turtle and check that you understand the meaning and purpose of each axiom.

Create an RDF (not RDFS) graph that contains all the triples in your first graph (the one with all the people and universities). Subtract all the triples in the axiom graph from the people/university graph. Write it out to see that you are left with only the asserted and entailed triples and that none of the axioms remain.


Useful Readings