Lab: RDFS: Difference between revisions

From info216
No edit summary
 
(10 intermediate revisions by 2 users not shown)
Line 8: Line 8:
* RDF (RDF.type)
* RDF (RDF.type)
* RDFS (RDFS.domain, RDFS.range, RDFS.subClassOf, RDFS.subPropertyOf)
* RDFS (RDFS.domain, RDFS.range, RDFS.subClassOf, RDFS.subPropertyOf)
* [https://docs.google.com/presentation/d/13fkzg7eM2pnKGqYlKpPMFIJwnOLKBkbT0A62s7OcnOs Lab Presentation of RDFS]


OWL-RL:
OWL-RL:
Line 19: Line 20:
'''Task:'''  
'''Task:'''  
Install OWL-RL into your virtual environment:
Install OWL-RL into your virtual environment:
  pip install owlrl.
  pip install owlrl


'''Task:'''  
'''Task:'''  
We will use simple examples from the Mueller investigation RDF graph you made in Exercise 1.
We will use simple RDF statements from the Mueller investigation RDF graph you create in Exercise 1. Create a new rdflib graph and add triples to represent that:  
 
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.
* Rick Gates was charged with money laundering and tax evasion.


Use RDFS to add these rules as triples:
Use RDFS terms to add these rules as triples:
* When one thing that is charged with another thing,
* When one thing that is charged with another thing,
** the first thing is a person under investigation and
** the first thing is a person under investigation and
** the second thing is an offense.
** the second thing is an offence.


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:
To add triples, you can use either:
* simple ''graph.add((s, p, o))'' statements or
* ''INSERT DATA {...}'' SPARQL updates.  
If you use SPARQL updates, you can define a namespace dictionary like this:
  EX = Namespace('http://example.org#')
  EX = Namespace('http://example.org#')
  NS = {
  NS = {
Line 44: Line 46:
     # when you provide an initNs-argument, you do not have  
     # when you provide an initNs-argument, you do not have  
     # to define PREFIX-es as part of the update (or query)
     # to define PREFIX-es as part of the update (or query)
     INSERT DATA {
     INSERT DATA {
         # your SPARQL update goes here
         # the triples you want to add go here,
        # you can use the prefixes defined in the NS-dict
     }
     }
  """, initNs=NS)
  """, initNs=NS)
Line 52: Line 56:
* Write a SPARQL query that checks the RDF type(s) of Rick Gates in your RDF graph.
* 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 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.
* 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.
* Re-run the SPARQL queries to check the types of Rick Gates and of money laundering again: have they changed?


You can compute the RDFS closure on a graph like this:
You can compute the RDFS closure on a graph ''g'' like this:
  engine = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
  import owlrl
  engine.closure()
   
engine.flush_stored_triples()
owlrl.DeductiveClosure(owlrl.RDFS_Semantics).expand(g)


'''Task:'''  
'''Task:'''  
Use RDFS to add this rule as a triple:
Use RDFS terms to add this rule as a triple:
* A person under investigation is a FOAF person.
* A person under investigation is a FOAF person.
* Like earlier, check the RDF types of Rick Gates before and after running RDFS reasoning.
* Like earlier, check the RDF types of Rick Gates before and after running RDFS reasoning. Do they change?


'''Task:'''  
'''Task:'''  
Line 69: Line 73:
* Paul Manafort was convicted for tax evasion.
* Paul Manafort was convicted for tax evasion.


Use RDFS to add these rules as triples:
Use RDFS terms to add these rules as triples:
* When one thing is ''convicted for'' another thing,
* When one thing is ''convicted for'' another thing,
** the first thing is also ''charged with'' the second thing.
** the first thing is also ''charged with'' the second thing.
Line 79: Line 83:


==If you have more time...==
==If you have more time...==
'''Task:'''
* Create a Turtle file with all the RDF and RDFS triples from the earlier tasks.
* Fire up GraphDB and create a new GraphDB Repository, ''this time with RDFS Ruleset'' for Inference and Validation.
* Load the graph from the Turtle file and go through each of the above queries to confirm that GraphDB has performed RDFS reasoning as you would expect.
You can list all the triples in the graph to see if anything has been added:
SELECT * WHERE { ?s ?p ?o }
'''Task:'''
* Create another GraphDB Repository, but with ''No inference''.
* Re-run the above tests and compare with the RDFS inference results.

Latest revision as of 13:18, 2 April 2024

Topics

  • Simple RDFS statements/triples
  • Basic RDFS programming in RDFlib
  • Basic RDFS reasoning with OWL-RL

Useful materials

rdflib classes/interfaces and attributes:

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 RDF statements from the Mueller investigation RDF graph you create in Exercise 1. Create a new rdflib graph and add triples to represent that:

  • Rick Gates was charged with money laundering and tax evasion.

Use RDFS terms 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 offence.

To add triples, you can use either:

  • simple graph.add((s, p, o)) statements or
  • 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 {
        # the triples you want to add go here,
        # you can use the prefixes defined in the NS-dict
    }
""", 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: have they changed?

You can compute the RDFS closure on a graph g like this:

import owlrl

owlrl.DeductiveClosure(owlrl.RDFS_Semantics).expand(g)

Task: Use RDFS terms 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. Do they change?

Task: Add in "plain RDF" as in Exercise 1:

  • Paul Manafort was convicted for tax evasion.

Use RDFS terms 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...

Task:

  • Create a Turtle file with all the RDF and RDFS triples from the earlier tasks.
  • Fire up GraphDB and create a new GraphDB Repository, this time with RDFS Ruleset for Inference and Validation.
  • Load the graph from the Turtle file and go through each of the above queries to confirm that GraphDB has performed RDFS reasoning as you would expect.

You can list all the triples in the graph to see if anything has been added:

SELECT * WHERE { ?s ?p ?o }

Task:

  • Create another GraphDB Repository, but with No inference.
  • Re-run the above tests and compare with the RDFS inference results.