Lab: RDFS: Difference between revisions

From info216
No edit summary
No edit summary
Line 21: Line 21:
'''Consider the following Scenario:'''
'''Consider the following Scenario:'''
"University of California and University of Valencia are both Universities.
"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. Having a degree from a HEI means that you have also graduated from that HEI. Only persons can graduate from a HEI. That a person is married to someone, means that they know them."
All universities are higher education institutions (HEIs). Only persons can have an expertise, and what they have expertise in is always a subject. Having a degree from a HEI means that you have also graduated from that HEI. 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."


'''Create RDFS triples corresponding to the text above with RDFlib''' - if you can, try to build on
'''Create RDFS triples corresponding to the text above with RDFlib''' - if you can, try to build on
Line 37: Line 37:
you have not asserted them explicitly:
you have not asserted them explicitly:
* that University of California and Valencia are HEIs
* that University of California and Valencia are HEIs
* that Cade and Emma are both persons
* 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 knows Mary
* that Cade knows Mary

Revision as of 08:47, 24 February 2021

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. Having a degree from a HEI means that you have also graduated from that HEI. 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."

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


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()

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