Lab: RDFS: Difference between revisions
(→Tasks) |
No edit summary |
||
Line 1: | Line 1: | ||
==Topics== | ==Topics== | ||
Basic RDFS | * Simple RDFS statements/triples | ||
* Basic RDFS programming in RDFlib | |||
* Basic RDFS reasoning with OWL-RL | |||
== | ==Useful materials== | ||
rdflib classes/interfaces and attributes/functions: | |||
* RDF (RDF.type) | |||
* RDFS (RDFS.domain, RDFS.range, RDFS.subClassOf, RDFS.subPropertyOf) | |||
OWL-RL: | |||
* [https://pypi.org/project/owlrl/ OWL-RL at PyPi] | |||
* [https://owl-rl.readthedocs.io/en/latest/ OWL-RL Documentation] | |||
OWL-RL classes/interfaces: | |||
* RDFSClosure, RDFS_Semantics | |||
==Tasks== | ==Tasks== | ||
'''Task:''' | |||
Install OWL-RL into your virtual environment: | |||
pip install owlrl. | |||
''' | |||
your | |||
'''Task:''' | |||
We will use simple examples from the Mueller investigation RDF graph you made in Exercise 1. | |||
Create a new rdflib graph and add in "plain RDF" like you did in Exercise 1: | |||
* Rick Gates was charged with money laundering and tax evasion. | |||
Use RDFS to add these rules as triples: | |||
* When one thing that is charged with another thing, | |||
** the first thing is a person under investigation and | |||
** the second thing is an offense. | |||
You can add triples using simple ''rdflib.add((s, p, o))'' or using ''INSERT DATA {...}'' SPARQL updates. If you use SPARQL updates, you can define a namespace dictionary like this: | |||
EX = Namespace('http://example.org#') | |||
NS = { | |||
'ex': EX, | |||
'rdf': RDF, | |||
'rdfs': RDFS, | |||
'foaf': FOAF, | |||
} | |||
rdfs | You can then give NS as an optional argument to graph.update() - or to graph.query() - like this: | ||
g.update(""" | |||
# when you provide an initNs-argument, you do not have | |||
# to define PREFIX-es as part of the update (or query) | |||
INSERT DATA { | |||
# your SPARQL update goes here | |||
} | |||
""", initNs=NS) | |||
'''Task:''' | |||
* Write a SPARQL query that checks the RDF type(s) of Rick Gates in your RDF graph. | |||
* that | * Write a similar SPARQL query that checks the RDF type(s) of money laundering in your RDF graph. | ||
* that | * Write a small function that ''computes the RDFS closure'' on your graph. | ||
* | * Re-run the SPARQL queries to check the types of Rick Gates and of money laundering again. | ||
You can compute the RDFS closure on a graph like this: | |||
engine = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False) | |||
engine.closure() | |||
engine.flush_stored_triples() | |||
'''Task:''' | |||
Use RDFS to add this rule as a triple: | |||
* A person under investigation is a FOAF person. | |||
* Like earlier, check the RDF types of Rick Gates before and after running RDFS reasoning. | |||
'''Task:''' | |||
Add in "plain RDF" as in Exercise 1: | |||
* Paul Manafort was convicted for tax evasion. | |||
Use RDFS to add these rules as triples: | |||
* When one thing is ''convicted for'' another thing, | |||
** the first thing is also ''charged with'' the second thing. | |||
''Note:'' we are dealing with a "timeless" graph here, that represents facts that has held at "some points in time", but not necessarily at the same time. | |||
* What are the RDF types of Paul Manafort and of tax evasion before and after RDFS reasoning? | |||
* Does the RDFS domain and range of the ''convicted for'' property change? | |||
== | ==If you have more time...= | ||
Revision as of 15:15, 18 February 2023
Topics
- Simple RDFS statements/triples
- Basic RDFS programming in RDFlib
- Basic RDFS reasoning with OWL-RL
Useful materials
rdflib classes/interfaces and attributes/functions:
- RDF (RDF.type)
- RDFS (RDFS.domain, RDFS.range, RDFS.subClassOf, RDFS.subPropertyOf)
OWL-RL:
OWL-RL classes/interfaces:
- RDFSClosure, RDFS_Semantics
Tasks
Task: Install OWL-RL into your virtual environment:
pip install owlrl.
Task: We will use simple examples from the Mueller investigation RDF graph you made in Exercise 1.
Create a new rdflib graph and add in "plain RDF" like you did in Exercise 1:
- Rick Gates was charged with money laundering and tax evasion.
Use RDFS to add these rules as triples:
- When one thing that is charged with another thing,
- the first thing is a person under investigation and
- the second thing is an offense.
You can add triples using simple rdflib.add((s, p, o)) or using INSERT DATA {...} SPARQL updates. If you use SPARQL updates, you can define a namespace dictionary like this:
EX = Namespace('http://example.org#') NS = { 'ex': EX, 'rdf': RDF, 'rdfs': RDFS, 'foaf': FOAF, }
You can then give NS as an optional argument to graph.update() - or to graph.query() - like this:
g.update(""" # when you provide an initNs-argument, you do not have # to define PREFIX-es as part of the update (or query) INSERT DATA { # your SPARQL update goes here } """, initNs=NS)
Task:
- Write a SPARQL query that checks the RDF type(s) of Rick Gates in your RDF graph.
- Write a similar SPARQL query that checks the RDF type(s) of money laundering in your RDF graph.
- Write a small function that computes the RDFS closure on your graph.
- Re-run the SPARQL queries to check the types of Rick Gates and of money laundering again.
You can compute the RDFS closure on a graph like this:
engine = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False) engine.closure() engine.flush_stored_triples()
Task: Use RDFS to add this rule as a triple:
- A person under investigation is a FOAF person.
- Like earlier, check the RDF types of Rick Gates before and after running RDFS reasoning.
Task: Add in "plain RDF" as in Exercise 1:
- Paul Manafort was convicted for tax evasion.
Use RDFS to add these rules as triples:
- When one thing is convicted for another thing,
- the first thing is also charged with the second thing.
Note: we are dealing with a "timeless" graph here, that represents facts that has held at "some points in time", but not necessarily at the same time.
- What are the RDF types of Paul Manafort and of tax evasion before and after RDFS reasoning?
- Does the RDFS domain and range of the convicted for property change?