Lab: RDF programming with RDFlib: Difference between revisions
No edit summary |
|||
Line 4: | Line 4: | ||
* Simple looping through graph | * Simple looping through graph | ||
==Useful rdflib | ==Useful Links== | ||
RDFlib documentation: | |||
* [https://rdflib.readthedocs.io/en/stable/intro_to_creating_rdf.html Creating Triples] | |||
* [https://rdflib.readthedocs.io/en/stable/intro_to_graphs.html Navigating Graphs] | |||
* [https://rdflib.readthedocs.io/en/stable/intro_to_parsing.html Parsing] | |||
from rdflib.namespace import RDF, FOAF, XSD | Useful classes/interfaces: | ||
* from rdflib import Graph, Namespace, URIRef, BNode, Literal | |||
* from rdflib.namespace import RDF, FOAF, XSD | |||
* from rdflib.collection import Collection | |||
Useful Graph methods: | |||
* add(), remove(), triples(), serialize(), parse(), bind() | |||
==Tasks== | ==Tasks== | ||
Line 52: | Line 44: | ||
Remove all triples about Mary using graph.remove(). (triples of Mary are from lab 1) | Remove all triples about Mary using graph.remove(). (triples of Mary are from lab 1) | ||
Revision as of 18:03, 13 January 2023
Topics
- Basic RDF graph programming with RDFlib.
- Simple reading/writing from/to file.
- Simple looping through graph
Useful Links
RDFlib documentation:
Useful classes/interfaces:
- from rdflib import Graph, Namespace, URIRef, BNode, Literal
- from rdflib.namespace import RDF, FOAF, XSD
- from rdflib.collection import Collection
Useful Graph methods:
- add(), remove(), triples(), serialize(), parse(), bind()
Tasks
Create a graph in RDFlib with triples corresponding to the text above. Build on the graph from lab 1. Use your own URIs when you need to (like "http://example.org/"), but try to use terms from vocabularies such as FOAF, RDF, XSD, and others.
Write out your graph to the console. This seems to be the cleanest way of printing the graph to me: print(g.serialize(format="turtle")) But try all the following formats: "turtle", "n3", "nt", "json-ld", "xml". How do they differ? What is the default?
Write your graph to a file. To do this, you can simply use the location parameter e.g: g.serialize(destination="triples.txt", format="turtle").
Look at the file and edit it so that Cade has also visited Germany and so that Emma is 26 years old.
Create a new program that reads your graph in again from the file and writes it to the console. e.g g.parse(location="triples.txt", format="turtle") Check that your new data is there!
Continuing with either your first or second program, write a loop that goes through all the triples in the graph and prints them to the console.
Change the loop so that (a) it only loops through triples about Emma (b) it only loops through triples involving the names of people.
Remove all triples about Mary using graph.remove(). (triples of Mary are from lab 1)