Lab: OWL 1: Difference between revisions

From info216
No edit summary
m (→‎Useful materials: Changed old lecture notes with the new one)
 
(63 intermediate revisions by 6 users not shown)
Line 1: Line 1:
=Lab 7: RDFS Plus=
==Topics==
==Topics==
RDF Plus sketching.
* Basic OWL ontology programming with RDFlib and owlrl.
WebVOWL visualisation.
* RDFS is relevant too.
Basic OWL ontology programming in Jena.
* WebVOWL visualisation.


==Tutorial==
==Useful materials==
Tutorial for the Jena-programming part: https://jena.apache.org/documentation/ontology/
Readings:
* [https://wiki.app.uib.no/info216/index.php?title=File:S08-OWL.pdf Lecture Notes]
* [https://wiki.uib.no/info216/index.php/Python_Examples#RDFS_Plus_.2F_OWL_inference_with_RDFLib Example page]
* [https://www.w3.org/TR/owl-ref/ OWL Documentation]
* [https://docs.google.com/presentation/d/1N8nN53I2-4Erp-SPoU0rI9yWjyfWaBKgwcRjr7R7c7w/ OWL 1 - Lab Presentation]


==Classes/interfaces (some in package org.apache.jena.ontology)==
Vocabularies and terms:
* ModelFactory (createOntologyModel),
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
* OntModel (createClass, createIndividual, createObjectProperty, CreateDatatypeProperty, createAllDifferent, createSymmetricProperty, createTransitiveProperty, createInverseFunctionalProperty),
* OWL (SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty, AllDifferent)
* Model (createList, write),
 
* OntClass, Individual, DatatypeProperty, ObjectProperty
==Tasks==
'''Task.'''
''Write OWL triples that corresponds to the following text.'' Try to continue your example from labs 1 and 2, or extend the triples at the bottom of this page. OWL terms can be imported from rdflib in the same way as RDF and RDFS terms.


Note that the OntModel interface extends InfModel and Model.
* Donald Trump and Robert Mueller are two different persons.
* Actually, all the names mentioned in connection with the Muelle investigation refer to different people.
* All these people are ''foaf:Person''s as well as ''schema:Person''s (they are http://xmlns.com/foaf/0.1/Person and http://schema.org/Person).
* Tax evation is a kind of bank and tax fraud.
* The Donald Trump involved in the Mueller investigation is ''dbpedia:Donald_Trump'' and not ''dbpedia:Donald_Trump_Jr.'' .
** ''Tip:'' rdflib's Turtle parser does not like URLs with punctuation marks written "prefix style" (''dbpedia:Donald_Trump_Jr.''), but it will accept the full URL written in angle brackets (''<http://dbpedia.org/resource/Donald_Trump_Jr.>'')
* Congress, FBI and the Mueller investigation are ''foaf:Organization''s.
* Nothing can be both a person and an organization.
* Leading an organization is a way of being involved in an organization.
* Being a campaign manager or an advisor for is a way of supporting someone.
* Donald Trump is a politician and a Republican.
* A  Republican politician is both a politician and a Republican.


==Tasks==
'''Task.'''
In earlier labs you modelled and programmed the following situations:
<syntaxhighlight>
g.add((ex.Paul_Manafort, ex.hasBusinessPartner, ex.Rick_Gates))
g.add((ex.Michael_Flynn, ex.adviserTo, ex.Donald_Trump))
g.add((ex.Rick_Gates_Lying, ex.wasLyingTo, ex.FBI))
g.add((ex.Donald_Trump, ex.presidentOf, ex.USA))
g.add((ex.USA, ex.hasPresident, ex.Donald_Trump))
</syntaxhighlight>
 
Look through the predicates (properties) above and add new triples for each one that describes them as any of the following: a reflexive, irreflexive, symmetric, asymmetric, transitive, functional, or an inverse functional property.
e.g
<syntaxhighlight>
g.add((ex.wasLyingTo, RDF.type, OWL.IrreflexiveProperty))
</syntaxhighlight>
 
'''Task.'''
Serialize the ontology and look at the results. Create an owlrl closure as below to infer additional triples and serialize it again. Can you spot the many inferences?
<syntaxhighlight>
DeductiveClosure(OWLRL_Semantics).expand(graph)
</syntaxhighlight>
 
'''Task.'''
Finally write the ontology to a XML file, and visualise it using http://vowl.visualdataweb.org/webvowl.html. The purpose of WebVOWL is to visualise classes and their properties, so the individuals may not show.
 
''Tip:'' When you save OWL files as XML, the extension ''.owl-xml'' can be used.
 
Most likely, your ontology is still quite disconnected. Add ''rdfs:subClassOf'', ''rdfs:domain'', and ''rdfs:range'' triples to turn it into a more connected graph that represents the domain. Calculate owlrl closures to see the effects of your triples as you add them.
 
===Triples you can use in the first task===
<syntaxhighlight>
@prefix ex: <http://example/org#> .
 
ex:Mueller_Investigation ex:involved ex:George_Papadopoulos,
        ex:Michael_Cohen,
        ex:Michael_Flynn,
        ex:Paul_Manafort,
        ex:Rick_Gates,
        ex:Roger_Stone ;
    ex:leadBy ex:Robert_Mueller .
 
ex:Michael_Cohen ex:attorneyFor ex:Donald_Trump ;
    ex:pleadedGuilty ex:Michael_Cohens_Lying .
 
ex:Michael_Cohens_Lying a ex:Lying ;
    ex:wasLyingAbout ex:Trump_RealEstateDeal ;
    ex:wasLyingTo ex:Congress .


In RDF: "Cade Tracy lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology
ex:Michael_Flynn ex:adviserTo ex:Donald_Trump ;
from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment,
    ex:negotiatedAgreement ex:PleaAgreement ;
photography and travelling. He has visited Canada and France. Emma Dominguez lives in Carrer de la Guardia
    ex:pleadedGuilty ex:Michael_Flynns_Lying .
Civil 20, 46020 Valencia, Spain. She has a M.Sc. in chemistry from the University of Valencia from 2015.
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.
ex:Michael_Flynns_Lying a ex:Lying ;
All universities are higher education instituttions (HEIs). Having a B.Sc. from a HEI and having a M.Sc.
    ex:wasLyingTo ex:FBI .
from a HEI are special cases of gradutating from that 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
about is always a subject."


Extend the RDF and RDFS graphs from earlier to account for the following situation:
ex:Paul_Manafort ex:campaignManager ex:Donald_Trump ;
Cade and Emma are two different persons. All the countries mentioned above are different. The country USA above is
    ex:chargedWith ex:ForeignLobbying,
the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames
        ex:MoneyLaundering,
resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Emma resources)
        ex:TaxEvasion ;
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,
    ex:convictedFor ex:BankAndTaxFraud ;
http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively. Nothing can be any two of a person, a university, or a city at the same time. The property
    ex:hasBusinessPartner ex:Rick_Gates ;
you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is
    ex:negotiatedAgreement ex:PleaAgreement ;
a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can
    ex:pleadedGuilty ex:Conspiracy ;
have the same postal code. The property you have used for Emma living in Valencia is the same property as FOAF's
    ex:sentencedTo ex:Prison .
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".)


Look through your graph and try to identify at least one of each: a reflexive and an irreflexive, a symmetric and
ex:Rick_Gates_Lying a ex:Lying ;
an asymmetric, as well as a transitive property.
    ex:wasLyingTo ex:FBI .


Create and output the OWL ontology in RDFlib. If you can, try to build on your example from labs 2 and 3!
ex:Rick_Gates ex:chargedWith ex:ForeignLobbying,
        ex:MoneyLaundering,
        ex:TaxEvasion ;
    ex:pleadedGuilty ex:Conspiracy,
        ex:Rick_Gates_Lying .
</syntaxhighlight>


Write the ontology to a TURTLE file, and try to visualise it using http://visualdataweb.de/webvowl/ . WebVOWL is oriented towards visualising classes and their properties, so the individuals may not show.
==If you have more time==
'''Task.'''
Inspect your ontology with [https://webprotege.stanford.edu/ Webprotégé].
* Register as a new user and log in.
* ''Create a new project'' and use ''Create from existing sources'' to upload your OWL/XML file.  
* Explore how to edit and extend your ontology using Protégé.


Use OntModel.writeAll() to write out the whole ontology, including OWL's built-in axioms (note that sending it to WebVOWL may not work.) Add a reasoner to your OntModel, for example ModelFactory.createOntology(OntModelSpec.OWL_MEM_RULE_INF), and writeAll() again. Can you spot any inferences?
You can also [https://protege.stanford.edu/products.php download Protégé] for free and run it on your local machine. The stand-alone version is even more powerful with lots of plug-ins.

Latest revision as of 14:53, 20 March 2023

Topics

  • Basic OWL ontology programming with RDFlib and owlrl.
  • RDFS is relevant too.
  • WebVOWL visualisation.

Useful materials

Readings:

Vocabularies and terms:

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

Tasks

Task. Write OWL triples that corresponds to the following text. Try to continue your example from labs 1 and 2, or extend the triples at the bottom of this page. OWL terms can be imported from rdflib in the same way as RDF and RDFS terms.

  • Donald Trump and Robert Mueller are two different persons.
  • Actually, all the names mentioned in connection with the Muelle investigation refer to different people.
  • All these people are foaf:Persons as well as schema:Persons (they are http://xmlns.com/foaf/0.1/Person and http://schema.org/Person).
  • Tax evation is a kind of bank and tax fraud.
  • The Donald Trump involved in the Mueller investigation is dbpedia:Donald_Trump and not dbpedia:Donald_Trump_Jr. .
    • Tip: rdflib's Turtle parser does not like URLs with punctuation marks written "prefix style" (dbpedia:Donald_Trump_Jr.), but it will accept the full URL written in angle brackets (<http://dbpedia.org/resource/Donald_Trump_Jr.>)
  • Congress, FBI and the Mueller investigation are foaf:Organizations.
  • Nothing can be both a person and an organization.
  • Leading an organization is a way of being involved in an organization.
  • Being a campaign manager or an advisor for is a way of supporting someone.
  • Donald Trump is a politician and a Republican.
  • A Republican politician is both a politician and a Republican.

Task.

g.add((ex.Paul_Manafort, ex.hasBusinessPartner, ex.Rick_Gates))
g.add((ex.Michael_Flynn, ex.adviserTo, ex.Donald_Trump))
g.add((ex.Rick_Gates_Lying, ex.wasLyingTo, ex.FBI))
g.add((ex.Donald_Trump, ex.presidentOf, ex.USA))
g.add((ex.USA, ex.hasPresident, ex.Donald_Trump))

Look through the predicates (properties) above and add new triples for each one that describes them as any of the following: a reflexive, irreflexive, symmetric, asymmetric, transitive, functional, or an inverse functional property. e.g

g.add((ex.wasLyingTo, RDF.type, OWL.IrreflexiveProperty))

Task. Serialize the ontology and look at the results. Create an owlrl closure as below to infer additional triples and serialize it again. Can you spot the many inferences?

DeductiveClosure(OWLRL_Semantics).expand(graph)

Task. Finally write the ontology to a XML file, and visualise it using http://vowl.visualdataweb.org/webvowl.html. The purpose of WebVOWL is to visualise classes and their properties, so the individuals may not show.

Tip: When you save OWL files as XML, the extension .owl-xml can be used.

Most likely, your ontology is still quite disconnected. Add rdfs:subClassOf, rdfs:domain, and rdfs:range triples to turn it into a more connected graph that represents the domain. Calculate owlrl closures to see the effects of your triples as you add them.

Triples you can use in the first task

@prefix ex: <http://example/org#> .

ex:Mueller_Investigation ex:involved ex:George_Papadopoulos,
        ex:Michael_Cohen,
        ex:Michael_Flynn,
        ex:Paul_Manafort,
        ex:Rick_Gates,
        ex:Roger_Stone ;
    ex:leadBy ex:Robert_Mueller .

ex:Michael_Cohen ex:attorneyFor ex:Donald_Trump ;
    ex:pleadedGuilty ex:Michael_Cohens_Lying .

ex:Michael_Cohens_Lying a ex:Lying ;
    ex:wasLyingAbout ex:Trump_RealEstateDeal ;
    ex:wasLyingTo ex:Congress .

ex:Michael_Flynn ex:adviserTo ex:Donald_Trump ;
    ex:negotiatedAgreement ex:PleaAgreement ;
    ex:pleadedGuilty ex:Michael_Flynns_Lying .

ex:Michael_Flynns_Lying a ex:Lying ;
    ex:wasLyingTo ex:FBI .

ex:Paul_Manafort ex:campaignManager ex:Donald_Trump ;
    ex:chargedWith ex:ForeignLobbying,
        ex:MoneyLaundering,
        ex:TaxEvasion ;
    ex:convictedFor ex:BankAndTaxFraud ;
    ex:hasBusinessPartner ex:Rick_Gates ;
    ex:negotiatedAgreement ex:PleaAgreement ;
    ex:pleadedGuilty ex:Conspiracy ;
    ex:sentencedTo ex:Prison .

ex:Rick_Gates_Lying a ex:Lying ;
    ex:wasLyingTo ex:FBI .

ex:Rick_Gates ex:chargedWith ex:ForeignLobbying,
        ex:MoneyLaundering,
        ex:TaxEvasion ;
    ex:pleadedGuilty ex:Conspiracy,
        ex:Rick_Gates_Lying .

If you have more time

Task. Inspect your ontology with Webprotégé.

  • Register as a new user and log in.
  • Create a new project and use Create from existing sources to upload your OWL/XML file.
  • Explore how to edit and extend your ontology using Protégé.

You can also download Protégé for free and run it on your local machine. The stand-alone version is even more powerful with lots of plug-ins.