Difference between revisions of "Lab: More OWL"

From Info216
Jump to: navigation, search
 
(7 intermediate revisions by the same user not shown)
Line 26: Line 26:
 
Most of the tasks today involve restrictions.  
 
Most of the tasks today involve restrictions.  
  
I Recommend refreshing your memories on restrictions from the [https://wiki.uib.no/info216/images/4/48/S11-OWL-15-utlagt.pdf lecture notes]
+
I Recommend refreshing your memories on restrictions and complex classes from the [https://wiki.uib.no/info216/images/4/48/S11-OWL-15-utlagt.pdf lecture notes]
 +
When solving the tasks look at the notes, find the relevant OWL term/s, and try to use the same principles in python code.
  
 
Down below is an example solution of the first task: "anyone who is a graduate has at least one degree".
 
Down below is an example solution of the first task: "anyone who is a graduate has at least one degree".
Line 73: Line 74:
 
import owlrl
 
import owlrl
 
from rdflib import Graph, Literal, Namespace, BNode
 
from rdflib import Graph, Literal, Namespace, BNode
from rdflib.namespace import RDF, OWL
+
from rdflib.namespace import RDF, OWL, RDFS
 
from rdflib.collection import Collection
 
from rdflib.collection import Collection
  
Line 99: Line 100:
  
 
==If You Have More Time==
 
==If You Have More Time==
In earlier labs we have modeled and programmed a scenario. You will find the descriptions at the end of this text.
+
Populate the ontology with data. E.g like:
 +
<syntaxhighlight>
 +
g.add((ex.Cade, RDF.type, ex.Graduate))
 +
g.add((ex.Cade, ex.grade, ex.A))
 +
 
 +
</syntaxhighlight>
 +
 
 +
Use owlrl like we did in lab 8 to infer additional triples.
 +
IMPORANT: It seems owlrl is unable to reason with OWL Restrictions and perhaps other concepts as well.
 +
There is a python library for better OWL reasoning called Owlready if you want to reason with restrictions.
 +
Below I print the ontology before and after the reasoning.
  
 +
What has changed about e.g Cade after using owlrl?
  
==Scenarios from previous labs==
+
<syntaxhighlight>
 +
# # Write owl file before any reasoned triples
 +
g.serialize(destination="owl1.ttl", format="turtle")
  
In RDF: "Cade Tracy lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology
+
# Infer additional triples
from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment,
+
owl_reasoner = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
photography and travelling. He has visited Canada and France. Ines Dominguez lives in Carrer de la Guardia
+
owl_reasoner.closure()
Civil 20, 46020 Valencia, Spain. She has a M.Sc. in chemistry from the University of Valencia from 2015.
+
owl_reasoner.flush_stored_triples()
Her areas of expertise include waste management, toxic waste, air pollution. Her interests include bike
 
riding, music and travelling. She has visited Portugal, Italy, France, Germany, Denmark and Sweden. Cade
 
knows Ines. They met in Paris in August 2014."
 
  
In RDFS: "University of California, Berkeley and University of Valencia are both Universities.
+
# Write owl file that includes reasoned triples
All universities are higher education instituttions (HEIs). Having a B.Sc. from a HEI and having a M.Sc.
+
g.serialize(destination="owl2.ttl", format="turtle")
from a HEI are special cases of gradutating from that HEI. That a person has a degree in a subject means
+
</syntaxhighlight>
that the person has expertise in that subject. Only persons can have expertise, and what they have expertise
 
about is always a subject."
 
  
In RDFS Plus: "Cade and Ines are two different persons. All the countries mentioned above are different. The country USA above is the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Ines resources) in your graph is the same as FOAF's, schema.org's and AKT's person classes (they are http://xmlns.com/foaf/0.1/Person, http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively, but on paper you can use prefixes). Nothing can be any two of a person, a university, a city, and a person at the same time. The property you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can have the same postal code. The property you have used for Ines living in Valencia is the same property as FOAF's based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia's hometown property (http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but "hometown" is perhaps the inverse of a subproperty of "based near".)
+
==Scenarios from previous labs==

Latest revision as of 11:25, 24 April 2020

Lab 13: More OWL

Topics

OWL ontology programming with RDFlib.

Tutorial

Classes and methods

In Lab 8 you have already used these OWL concepts:

  • (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
  • (ReflexiveProperty, IrreflexiveProperty, SymmetricProperty, AsymmetricProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty),


In this lab you will mostly use the following OWL terms:

  • (oneOf, unionOf, intersectionOf. complementOf)
  • (Restriction, onProperty)
  • (someValuesFrom, allValuesFrom, hasValue)
  • (cardinality, minCardinality, maxCardinality)
  • (qualifiedCardinality, minQualifiedCardinality, maxQualifiedCardinality, onClass)


OWL Restrictions

Most of the tasks today involve restrictions.

I Recommend refreshing your memories on restrictions and complex classes from the lecture notes When solving the tasks look at the notes, find the relevant OWL term/s, and try to use the same principles in python code.

Down below is an example solution of the first task: "anyone who is a graduate has at least one degree". It looks complicated, but once you understand it, the other tasks follow a similar pattern. In short: I create a blank node that is an OWL.restriction on the ex.degree property. The restriction in question is a minCardinaality restriction with the value 1. (e.g "at least one") Then I create another blank node that is for a List of all the criteria that makes a person a Graduate. In this case I say that a Graduate is the intersection of a Person and the restriction we creater earlier. Collection is essentially used to create lists and everything between [] is what is in the list (the intersections).

# anyone who is a graduate has at least one degree
 
br = BNode()
g.add((br, RDF.type, OWL.Restriction))
g.add((br, OWL.onProperty, ex.degree))
g.add((br, OWL.minCardinality, Literal(1)))
bi = BNode()
Collection(g, bi, [ex.Person, br])
g.add((ex.Graduate, OWL.intersectionOf, bi))


Tasks

Create or Extend a previous graph into an ontology that expresses the following using concepts from OWL (and some from RDF/RDFS):

  • anyone who is a graduate has at least one degree
  • anyone who is a university graduate has at least one degree from a university
  • a grade is either an A, B, C, D, E or F
  • a straight A student is a student that has only A grades
  • a graduate has no F grades
  • a student has a unique student number
  • each student has exactly one average grade
  • a course is either a bachelor, a master or a Ph.D course
  • a bachelor student takes only bachelor courses
  • a masters student takes only master courses and at most one bachelor course
  • a Ph.D student takes only Ph.D and at most two masters courses
  • a Ph.D. student cannot take a bachelor course

Write each of the above statements as python code using RDFlib and OWL.


Code to get started

import owlrl
from rdflib import Graph, Literal, Namespace, BNode
from rdflib.namespace import RDF, OWL, RDFS
from rdflib.collection import Collection

g = Graph()
ex = Namespace("http://example.org/")
g.bind("ex", ex)
g.bind("owl", OWL)

# anyone who is a graduate has at least one degree
br = BNode()
g.add((br, RDF.type, OWL.Restriction))
g.add((br, OWL.onProperty, ex.degree))
g.add((br, OWL.minCardinality, Literal(1)))
bi = BNode()
Collection(g, bi, [ex.Person, br])
g.add((ex.Graduate, OWL.intersectionOf, bi))

# Continue here with the other statements:

print(g.serialize(format="turtle").decode())


If You Have More Time

Populate the ontology with data. E.g like:

g.add((ex.Cade, RDF.type, ex.Graduate))
g.add((ex.Cade, ex.grade, ex.A))

Use owlrl like we did in lab 8 to infer additional triples. IMPORANT: It seems owlrl is unable to reason with OWL Restrictions and perhaps other concepts as well. There is a python library for better OWL reasoning called Owlready if you want to reason with restrictions. Below I print the ontology before and after the reasoning.

What has changed about e.g Cade after using owlrl?

# # Write owl file before any reasoned triples
g.serialize(destination="owl1.ttl", format="turtle")

# Infer additional triples
owl_reasoner = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
owl_reasoner.closure()
owl_reasoner.flush_stored_triples()

# Write owl file that includes reasoned triples
g.serialize(destination="owl2.ttl", format="turtle")

Scenarios from previous labs