Lab: RDF programming with RDFlib

From info216
Revision as of 07:26, 20 January 2020 by Say004 (talk | contribs)

Lab 2: RDF programming with RDFlib

Topics

  • RDF graph sketching.
  • Basic RDF graph programming with RDFlib.
  • Simple reading/writing from/to file.

Classes/interfaces

import rdflib:

  • Graph (add)
  • URIRef
  • Literal
  • NameSpace
  • Bnode
  • RDF, FOAF

Tasks

Consider the following situation: "Cade Tracy lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment, photography and travelling. He has visited Canada and France. Ines Dominguez lives in Carrer de la Guardia 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." (Make up your own terms when you need to, or even better: use terms you know from vocabularies such as FOAF and RDFS. But we are not so careful with full IRIs and prefix definitions in this exercise.)

Sketch this RDF graph on paper.

Create a Model object and create the graph in Jena using the RDF API. Try to use as many different methods as possible to create the triples.

Try to use createList to create the lists of interests and expertise.

Write out your model to the console in the following formats: TURTLE, N-TRIPLE, N3, JSON-LD, RDF/XML. How do they differ? What is the default? Check how the lists are serialised in TURTLE and JSON-LD.

Write your model to a file. (Remember to catch exceptions.)

Edit the file so that Case has the middle name Creighton and Ines' middle name is María.

Create a new program that reads your model in again from the file and writes it to the console. Check that the new middle names are there!

Continuing with either your first or second program, write a loop that goes through all the statements in the graph/model and prints them to the console.

Change the loop so that (a) it only loops through statements about Ines (b) it only loops through statements involving the names of people.

Remove all addresses from the graph/model.

Close the model (not important now, but it will be later when we use the TDB triple store.)

If you have more time...

Below are four lines of comma-separated values (csv - five lines with the headers) that could have been saved from a spreadsheet. Copy them into a file and write a program with a loop that reads each line from that file (except the initial header line) and adds it to your graph as triples:

 "Name","Gender","Country","Town","Expertise","Interests"
 "Regina Catherine Hall","F","Great Britain","Manchester","Ecology, zoology","Football, music travelling"
 "Achille Blaise","M","France","Nancy","","Chess, computer games"
 "Nyarai Awotwi Ihejirika","F","Kenya","Nairobi","Computers, semantic networks","Hiking, botany"
 "Xun He Zhang","M","China","Chengdu","Internet, mathematics, logistics","Dancing, music, trombone"

In the resulting graph, delete all information about Achille.

Have you used all the classes/intefaces listed at the beginning in your code, and all the variants of all the methods? If not, try to change your code to try them all!