Lab: OWL 1: Difference between revisions

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


WebVOWL visualisation.
==Useful materials==
 
Readings:
RDF and RDFS might be relevant too.
* [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/Vocabularies==
Vocabularies and terms:
 
Vocabulary:
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
* OWL (SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty)
* OWL (SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty, AllDifferent)


==Tasks==
==Tasks==
'''Task 1'''
'''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.


'''Write OWL triples that corresponds to the following text. '''.If you can, try to build on your example from labs 2 and 7, or extend the triples at the bottom of the page. OWL can be imported from rdflib.namespace.
* 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.


Cade and Emma are two different persons. <!-- All the countries mentioned above are different. --> The country USA above is
'''Task.'''
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 Emma 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. Nothing can be any two of a person, a university, or a city 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 Emma 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".)
 
 
'''Task 2'''
<syntaxhighlight>
<syntaxhighlight>
g.add((ex.Cade, ex.married, ex.Mary))
g.add((ex.Paul_Manafort, ex.hasBusinessPartner, ex.Rick_Gates))
g.add((ex.Cade, ex.livesWith, ex.Mary))
g.add((ex.Michael_Flynn, ex.adviserTo, ex.Donald_Trump))
g.add((ex.Cade, ex.sibling, ex.Andrew))
g.add((ex.Rick_Gates_Lying, ex.wasLyingTo, ex.FBI))
g.add((ex.Cade, ex.hasFather, ex.Bob))
g.add((ex.Donald_Trump, ex.presidentOf, ex.USA))
g.add((ex.Bob, ex.fatherOf, ex.Cade))
g.add((ex.USA, ex.hasPresident, ex.Donald_Trump))
</syntaxhighlight>
</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.
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
e.g
<syntaxhighlight>
<syntaxhighlight>
g.add((ex.married, RDF.type, OWL.SymmetricProperty))
g.add((ex.wasLyingTo, RDF.type, OWL.IrreflexiveProperty))
</syntaxhighlight>
</syntaxhighlight>


'''Task 3'''
'''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?
Print/Serialize the ontology. Then use owlrl like seen below to infer additional triples. Can you spot the many inferences?
 
<syntaxhighlight>
<syntaxhighlight>
# These three lines add inferred triples to the graph.
DeductiveClosure(OWLRL_Semantics).expand(graph)
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
owl.closure()
owl.flush_stored_triples()
</syntaxhighlight>
</syntaxhighlight>


Finally 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.
'''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.


==Useful Reading==
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.
* [https://wiki.uib.no/info216/index.php/File:S06-OWL-1.pdf Lecture Notes]


* [https://wiki.uib.no/info216/index.php/Python_Examples#RDFS_Plus_.2F_OWL_inference_with_RDFLib Example page]
===Triples you can use in the first task===
<syntaxhighlight>
@prefix ex: <http://example/org#> .


==Triples you can extend for the tasks==
ex:Mueller_Investigation ex:involved ex:George_Papadopoulos,
<syntaxhighlight>
        ex:Michael_Cohen,
        ex:Michael_Flynn,
        ex:Paul_Manafort,
        ex:Rick_Gates,
        ex:Roger_Stone ;
    ex:leadBy ex:Robert_Mueller .


import owlrl
ex:Michael_Cohen ex:attorneyFor ex:Donald_Trump ;
from rdflib import Graph, Namespace, Literal, URIRef
    ex:pleadedGuilty ex:Michael_Cohens_Lying .
from rdflib.namespace import RDF, RDFS, XSD, FOAF, OWL


g = Graph()
ex:Michael_Cohens_Lying a ex:Lying ;
    ex:wasLyingAbout ex:Trump_RealEstateDeal ;
    ex:wasLyingTo ex:Congress .


# Namespaces
ex:Michael_Flynn ex:adviserTo ex:Donald_Trump ;
ex = Namespace("http://example.org/")
    ex:negotiatedAgreement ex:PleaAgreement ;
dbp = Namespace("http://dbpedia.org/resource/")
    ex:pleadedGuilty ex:Michael_Flynns_Lying .
geo = Namespace("http://sws.geonames.org/")
schema = Namespace("https://schema.org/")
akt = Namespace("http://www.aktors.org/ontology/portal#")
vcard = Namespace("http://www.w3.org/2006/vcard/ns#")


g.bind("ex", ex)
ex:Michael_Flynns_Lying a ex:Lying ;
g.bind("owl", OWL)
    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 .


# RDFS Tasks from last time.
ex:Rick_Gates_Lying a ex:Lying ;
g.add((ex.Cade, ex.degreeFrom, ex.University_of_California))
    ex:wasLyingTo ex:FBI .
g.add((ex.Emma, ex.degreeFrom, ex.University_of_Valencia))
g.add((ex.Cade, ex.degreeSubject, ex.Biology))
g.add((ex.Emma, ex.degreeSubject, ex.Chemistry))
g.add((ex.University_of_California, RDF.type, ex.University))
g.add((ex.University_of_Valencia, RDF.type, ex.University))
g.add((ex.University, RDFS.subClassOf, ex.Higher_Education_Institution))
g.add((ex.expertise, RDFS.range, ex.Subject))
g.add((ex.expertise, RDFS.domain, FOAF.Person))
g.add((ex.degreeSubject, RDFS.subPropertyOf, ex.expertise))
g.add((ex.graduated, RDFS.range, ex.Higher_Education_Institution))
g.add((ex.graduated, RDFS.domain, FOAF.Person))
g.add((ex.degreeFrom, RDFS.subPropertyOf, ex.graduated))
g.add((ex.Biology, RDFS.label, Literal("Biology", lang="en")))
g.add((ex.Biology, RDFS.label, Literal("La Biologie", lang="fr")))
g.add((ex.Biology, RDFS.comment, Literal("Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy.")))
g.add((ex.Chemistry, RDFS.label, Literal("Chemistry", lang="en")))
g.add((ex.Chemistry, RDFS.label, Literal("La Chimie", lang="fr")))
g.add((ex.Chemistry, RDFS.comment, Literal("Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter.", lang="en")))


# Write OWL triples here
ex:Rick_Gates ex:chargedWith ex:ForeignLobbying,
        ex:MoneyLaundering,
        ex:TaxEvasion ;
    ex:pleadedGuilty ex:Conspiracy,
        ex:Rick_Gates_Lying .
</syntaxhighlight>
</syntaxhighlight>
==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é.
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.