<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://info216.wiki.uib.no/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sas020</id>
	<title>info216 - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://info216.wiki.uib.no/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sas020"/>
	<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/Special:Contributions/Sas020"/>
	<updated>2026-04-19T22:16:37Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL&amp;diff=1609</id>
		<title>Lab: SPARQL</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL&amp;diff=1609"/>
		<updated>2021-05-10T11:33:51Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lab 3: SPARQL / Blazegraph=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
* Setting up the Blazegraph graph database. Previously we have only stored our triples in memory, which is not persistent. &lt;br /&gt;
* SPARQL queries and updates. We use SPARQL to retrieve of update triples in our databases/graphs of triples&lt;br /&gt;
&lt;br /&gt;
==Installing the Blazegraph database on your own computer==&lt;br /&gt;
Download Blazegraph (blazegraph.jar) from here: [https://blazegraph.com/ https://blazegraph.com/]&lt;br /&gt;
I recommend placing blazegraph.jar in the same folder of your python project for the labs. &lt;br /&gt;
Navigate to the folder of blazegraph.jar in your commandline/terminal using cd. (cd C:\Users\Martin\PycharmProjects\info216_labs for me as an example). Now run this command:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
java -server -Xmx4g -jar blazegraph.jar&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You might have to install java 8 64-bit JDK if you have problems running blazegraph. You can do it from  this link: &lt;br /&gt;
&amp;quot;https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html&amp;quot;&lt;br /&gt;
If you get an &amp;quot;Address already in use&amp;quot; error, this is likely because blazegraph has been terminated improperly. Either restart the terminal-session or try to run this command instead: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
java -server -Xmx4g -Djetty.port=19999 -jar blazegraph.jar &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This changes the port of the blazegraph server.&lt;br /&gt;
&lt;br /&gt;
If you have trouble installing Blazegraph you can use this link for now: &amp;quot;http://sandbox.i2s.uib.no/bigdata/&amp;quot;.&lt;br /&gt;
This is the same blazegraph interface, but its stored in the cloud and only be used on the UiB network. You may be able to access it without connecting to the UiB Network, but if you are unable to access the endpoint try connecting via the VPN. Instructions [https://hjelp.uib.no/tas/public/ssp/content/detail/service?unid=a566dafec92a4d35bba974f0733f3663 here].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If it works it should now display an url like: &amp;quot;http://10.0.0.13:9999/blazegraph/&amp;quot;. Open this in a browser. &lt;br /&gt;
You can now run SPARQL queries and updates and load RDF graphs from your file into Blazegraph.&lt;br /&gt;
In the update tab, load RDF data (select type below) and then paste the contents of your turtle/.txt file to add them all at once to the database. If you have not serialized your graph from lab 2 yet, you can use the triples on the bottom of the page instead. Just copy and paste them into the Update section.&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
Write the following SPARQL queries: &lt;br /&gt;
&lt;br /&gt;
* SELECT all triples in your graph. &lt;br /&gt;
* SELECT all the interests of Cade.&lt;br /&gt;
* SELECT the city and country of where Emma lives.&lt;br /&gt;
* SELECT only people who are older than 26.&lt;br /&gt;
* SELECT Everyone who graduated with a Bachelor Degree. &lt;br /&gt;
&lt;br /&gt;
Use SPARQL Update&#039;s DELETE DATA to delete that fact that Cade is interested in Photography. Run your SPARQL query again to check that the graph has changed.&lt;br /&gt;
&lt;br /&gt;
Use INSERT DATA to add information about Sergio Pastor, who lives in 4 Carrer del Serpis, 46021 Valencia, Spain. he has a M.Sc. in computer from the University of Valencia from 2008. His areas of expertise include big data, semantic technologies and machine learning.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL DELETE/INSERT update to change the name of &amp;quot;University of Valencia&amp;quot; to &amp;quot;Universidad de Valencia&amp;quot; whereever it occurs.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL DESCRIBE query to get basic information about Sergio.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL CONSTRUCT query that returns that: any city in an address is a cityOf the country of the same address.&lt;br /&gt;
&lt;br /&gt;
==If you have more time==&lt;br /&gt;
Redo all the above steps, this time writing a Python/RDFlib program. This will be the topic of lab 6.&lt;br /&gt;
You can look at the python example page to see how to connect to your blazegraph endpoint in Python and how to perform some basic queries.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Links==&lt;br /&gt;
[https://wiki.uib.no/info216/index.php/File:S03-SPARQL-13.pdf Lecture Notes]&lt;br /&gt;
&lt;br /&gt;
[https://www.w3.org/TR/sparql11-query/ SPARQL Query Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/sparql11-update/ SPARQL Update Documentation]&lt;br /&gt;
&lt;br /&gt;
==Triples that you can base your queries on: (turtle format)==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
@prefix ex: &amp;lt;http://example.org/&amp;gt; .&lt;br /&gt;
@prefix foaf: &amp;lt;http://xmlns.com/foaf/0.1/&amp;gt; .&lt;br /&gt;
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .&lt;br /&gt;
@prefix rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt; .&lt;br /&gt;
@prefix xml: &amp;lt;http://www.w3.org/XML/1998/namespace&amp;gt; .&lt;br /&gt;
@prefix xsd: &amp;lt;http://www.w3.org/2001/XMLSchema#&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
ex:Cade a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Berkeley ;&lt;br /&gt;
            ex:country ex:USA ;&lt;br /&gt;
            ex:postalCode &amp;quot;94709&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:state ex:California ;&lt;br /&gt;
            ex:street &amp;quot;1516_Henry_Street&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 27 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Biology ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Bachelor&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_California ;&lt;br /&gt;
            ex:year &amp;quot;2011-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:interest ex:Bird,&lt;br /&gt;
        ex:Ecology,&lt;br /&gt;
        ex:Environmentalism,&lt;br /&gt;
        ex:Photography,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:married ex:Mary ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ex:Canada,&lt;br /&gt;
        ex:France,&lt;br /&gt;
        ex:Germany ;&lt;br /&gt;
    foaf:knows ex:Emma ;&lt;br /&gt;
    foaf:name &amp;quot;Cade_Tracey&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Mary a ex:Student,&lt;br /&gt;
        foaf:Person ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:interest ex:Biology,&lt;br /&gt;
        ex:Chocolate,&lt;br /&gt;
        ex:Hiking .&lt;br /&gt;
&lt;br /&gt;
ex:Emma a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Valencia ;&lt;br /&gt;
            ex:country ex:Spain ;&lt;br /&gt;
            ex:postalCode &amp;quot;46020&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:street &amp;quot;Carrer_de_la Guardia_Civil_20&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Chemistry ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Master&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_Valencia ;&lt;br /&gt;
            ex:year &amp;quot;2015-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:expertise ex:Air_Pollution,&lt;br /&gt;
        ex:Toxic_Waste,&lt;br /&gt;
        ex:Waste_Management ;&lt;br /&gt;
    ex:interest ex:Bike_Riding,&lt;br /&gt;
        ex:Music,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ( ex:Portugal ex:Italy ex:France ex:Germany ex:Denmark ex:Sweden ) ;&lt;br /&gt;
    foaf:name &amp;quot;Emma_Dominguez&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Meeting1 a ex:Meeting ;&lt;br /&gt;
    ex:date &amp;quot;August, 2014&amp;quot;^^xsd:string ;&lt;br /&gt;
    ex:involved ex:Cade,&lt;br /&gt;
        ex:Emma ;&lt;br /&gt;
    ex:location ex:Paris .&lt;br /&gt;
&lt;br /&gt;
ex:Paris a ex:City ;&lt;br /&gt;
    ex:capitalOf ex:France ;&lt;br /&gt;
    ex:locatedIn ex:France .&lt;br /&gt;
&lt;br /&gt;
ex:France ex:capital ex:Paris .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_More_OWL&amp;diff=1600</id>
		<title>Lab: More OWL</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_More_OWL&amp;diff=1600"/>
		<updated>2021-04-21T17:25:59Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 12: More OWL=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
OWL ontology programming with RDFlib.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ==Tutorial== --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Classes and methods==&lt;br /&gt;
In an earlier lab, you have already used these OWL concepts:&lt;br /&gt;
* (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)&lt;br /&gt;
* (ReflexiveProperty, IrreflexiveProperty, SymmetricProperty, AsymmetricProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty),&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this lab you will mostly use the following OWL terms:&lt;br /&gt;
&lt;br /&gt;
* (oneOf, unionOf, intersectionOf. complementOf)&lt;br /&gt;
* (Restriction, onProperty)&lt;br /&gt;
* (someValuesFrom, allValuesFrom, hasValue)&lt;br /&gt;
* (cardinality, minCardinality, maxCardinality)&lt;br /&gt;
* (qualifiedCardinality, minQualifiedCardinality, maxQualifiedCardinality, onClass)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=OWL Restrictions=&lt;br /&gt;
Most of the tasks today involve restrictions. &lt;br /&gt;
&lt;br /&gt;
I Recommend refreshing your memories on restrictions and complex classes from the [https://wiki.uib.no/info216/images/6/69/S12-OWL-2.pdf lecture notes]&lt;br /&gt;
When solving the tasks look at the notes, find the relevant OWL term/s, and try to use the same principles in python code.&lt;br /&gt;
&lt;br /&gt;
Down below is an example solution of the first task: &amp;quot;anyone who is a graduate has at least one degree&amp;quot;.&lt;br /&gt;
It looks complicated, but once you understand it, the other tasks follow a similar pattern. &lt;br /&gt;
In short: I create a blank node that is an OWL.restriction on the ex.degree property.&lt;br /&gt;
The restriction in question is a minCardinality restriction with the value 1. (e.g &amp;quot;at least one&amp;quot;) &lt;br /&gt;
Then I create another blank node that is for a List of all the criteria that makes a person a Graduate.&lt;br /&gt;
In this case I say that a Graduate is the intersection of a Person and the restriction we created earlier.&lt;br /&gt;
Collection is essentially used to create lists and everything between [] is what is in the list (the intersections).  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# anyone who is a graduate has at least one degree&lt;br /&gt;
 &lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.degree))&lt;br /&gt;
g.add((br, OWL.minCardinality, Literal(1)))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Graduate, OWL.intersectionOf, bi))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
Create or Extend a previous graph into an ontology that expresses the following using concepts from OWL (and some from RDF/RDFS), you can do this either by creating the triples in Python using RDFLib or writing the triples using Turtle (or RDF/XML):&lt;br /&gt;
* anyone who is a graduate has at least one degree&lt;br /&gt;
* anyone who is a university graduate has at least one degree from a university&lt;br /&gt;
* a grade is either an A, B, C, D, E or F&lt;br /&gt;
* a straight A student is a student that has only A grades&lt;br /&gt;
* a graduate has no F grades&lt;br /&gt;
* a student has a unique student number&lt;br /&gt;
* each student has exactly one average grade&lt;br /&gt;
* a course is either a bachelor, a master or a Ph.D course&lt;br /&gt;
* a bachelor student takes only bachelor courses&lt;br /&gt;
* a masters student takes only master courses and at most one bachelor course&lt;br /&gt;
* a Ph.D student takes only Ph.D and at most two masters courses&lt;br /&gt;
* a Ph.D. student cannot take a bachelor course&lt;br /&gt;
&lt;br /&gt;
Write each of the above statements as python code using RDFlib and OWL.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code to get started==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import owlrl&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, BNode&lt;br /&gt;
from rdflib.namespace import RDF, OWL, RDFS&lt;br /&gt;
from rdflib.collection import Collection&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;owl&amp;quot;, OWL)&lt;br /&gt;
&lt;br /&gt;
# anyone who is a graduate has at least one degree&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.degree))&lt;br /&gt;
g.add((br, OWL.minCardinality, Literal(1)))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Graduate, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# Continue here with the other statements:&lt;br /&gt;
&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==If You Have More Time==&lt;br /&gt;
Populate the ontology with data. E.g like: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.Cade, RDF.type, ex.Graduate))&lt;br /&gt;
g.add((ex.Cade, ex.grade, ex.A))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use owlrl like we did in lab 8 to infer additional triples.&lt;br /&gt;
IMPORANT: It seems owlrl is unable to reason with OWL Restrictions and perhaps other concepts as well.&lt;br /&gt;
There is a python library for better OWL reasoning called Owlready if you want to reason with restrictions.  &lt;br /&gt;
Below I print the ontology before and after the reasoning.&lt;br /&gt;
&lt;br /&gt;
What has changed about e.g Cade after using owlrl?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# # Write owl file before any reasoned triples&lt;br /&gt;
g.serialize(destination=&amp;quot;owl1.ttl&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Infer additional triples&lt;br /&gt;
owl_reasoner = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
owl_reasoner.closure()&lt;br /&gt;
owl_reasoner.flush_stored_triples()&lt;br /&gt;
&lt;br /&gt;
# Write owl file that includes reasoned triples&lt;br /&gt;
g.serialize(destination=&amp;quot;owl2.ttl&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ==Scenarios from previous labs== --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful readings==&lt;br /&gt;
* [https://www.w3.org/TR/owl-primer/ OWL2 Primer]&lt;br /&gt;
* [https://www.w3.org/TR/2012/REC-owl2-quick-reference-20121211/ OWL2 Quick Reference Guide]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_More_OWL&amp;diff=1599</id>
		<title>Lab: More OWL</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_More_OWL&amp;diff=1599"/>
		<updated>2021-04-21T17:25:11Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 12: More OWL=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
OWL ontology programming with RDFlib.&lt;br /&gt;
&lt;br /&gt;
==Tutorial==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Classes and methods==&lt;br /&gt;
In an earlier lab, you have already used these OWL concepts:&lt;br /&gt;
* (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)&lt;br /&gt;
* (ReflexiveProperty, IrreflexiveProperty, SymmetricProperty, AsymmetricProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty),&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this lab you will mostly use the following OWL terms:&lt;br /&gt;
&lt;br /&gt;
* (oneOf, unionOf, intersectionOf. complementOf)&lt;br /&gt;
* (Restriction, onProperty)&lt;br /&gt;
* (someValuesFrom, allValuesFrom, hasValue)&lt;br /&gt;
* (cardinality, minCardinality, maxCardinality)&lt;br /&gt;
* (qualifiedCardinality, minQualifiedCardinality, maxQualifiedCardinality, onClass)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=OWL Restrictions=&lt;br /&gt;
Most of the tasks today involve restrictions. &lt;br /&gt;
&lt;br /&gt;
I Recommend refreshing your memories on restrictions and complex classes from the [https://wiki.uib.no/info216/images/6/69/S12-OWL-2.pdf lecture notes]&lt;br /&gt;
When solving the tasks look at the notes, find the relevant OWL term/s, and try to use the same principles in python code.&lt;br /&gt;
&lt;br /&gt;
Down below is an example solution of the first task: &amp;quot;anyone who is a graduate has at least one degree&amp;quot;.&lt;br /&gt;
It looks complicated, but once you understand it, the other tasks follow a similar pattern. &lt;br /&gt;
In short: I create a blank node that is an OWL.restriction on the ex.degree property.&lt;br /&gt;
The restriction in question is a minCardinality restriction with the value 1. (e.g &amp;quot;at least one&amp;quot;) &lt;br /&gt;
Then I create another blank node that is for a List of all the criteria that makes a person a Graduate.&lt;br /&gt;
In this case I say that a Graduate is the intersection of a Person and the restriction we created earlier.&lt;br /&gt;
Collection is essentially used to create lists and everything between [] is what is in the list (the intersections).  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# anyone who is a graduate has at least one degree&lt;br /&gt;
 &lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.degree))&lt;br /&gt;
g.add((br, OWL.minCardinality, Literal(1)))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Graduate, OWL.intersectionOf, bi))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
Create or Extend a previous graph into an ontology that expresses the following using concepts from OWL (and some from RDF/RDFS), you can do this either by creating the triples in Python using RDFLib or writing the triples using Turtle (or RDF/XML):&lt;br /&gt;
* anyone who is a graduate has at least one degree&lt;br /&gt;
* anyone who is a university graduate has at least one degree from a university&lt;br /&gt;
* a grade is either an A, B, C, D, E or F&lt;br /&gt;
* a straight A student is a student that has only A grades&lt;br /&gt;
* a graduate has no F grades&lt;br /&gt;
* a student has a unique student number&lt;br /&gt;
* each student has exactly one average grade&lt;br /&gt;
* a course is either a bachelor, a master or a Ph.D course&lt;br /&gt;
* a bachelor student takes only bachelor courses&lt;br /&gt;
* a masters student takes only master courses and at most one bachelor course&lt;br /&gt;
* a Ph.D student takes only Ph.D and at most two masters courses&lt;br /&gt;
* a Ph.D. student cannot take a bachelor course&lt;br /&gt;
&lt;br /&gt;
Write each of the above statements as python code using RDFlib and OWL.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code to get started==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import owlrl&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, BNode&lt;br /&gt;
from rdflib.namespace import RDF, OWL, RDFS&lt;br /&gt;
from rdflib.collection import Collection&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;owl&amp;quot;, OWL)&lt;br /&gt;
&lt;br /&gt;
# anyone who is a graduate has at least one degree&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.degree))&lt;br /&gt;
g.add((br, OWL.minCardinality, Literal(1)))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Graduate, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# Continue here with the other statements:&lt;br /&gt;
&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==If You Have More Time==&lt;br /&gt;
Populate the ontology with data. E.g like: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.Cade, RDF.type, ex.Graduate))&lt;br /&gt;
g.add((ex.Cade, ex.grade, ex.A))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use owlrl like we did in lab 8 to infer additional triples.&lt;br /&gt;
IMPORANT: It seems owlrl is unable to reason with OWL Restrictions and perhaps other concepts as well.&lt;br /&gt;
There is a python library for better OWL reasoning called Owlready if you want to reason with restrictions.  &lt;br /&gt;
Below I print the ontology before and after the reasoning.&lt;br /&gt;
&lt;br /&gt;
What has changed about e.g Cade after using owlrl?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# # Write owl file before any reasoned triples&lt;br /&gt;
g.serialize(destination=&amp;quot;owl1.ttl&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Infer additional triples&lt;br /&gt;
owl_reasoner = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
owl_reasoner.closure()&lt;br /&gt;
owl_reasoner.flush_stored_triples()&lt;br /&gt;
&lt;br /&gt;
# Write owl file that includes reasoned triples&lt;br /&gt;
g.serialize(destination=&amp;quot;owl2.ttl&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ==Scenarios from previous labs== --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful readings==&lt;br /&gt;
* [https://www.w3.org/TR/owl-primer/ OWL2 Primer]&lt;br /&gt;
* [https://www.w3.org/TR/2012/REC-owl2-quick-reference-20121211/ OWL2 Quick Reference Guide]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_More_OWL&amp;diff=1593</id>
		<title>Lab: More OWL</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_More_OWL&amp;diff=1593"/>
		<updated>2021-04-20T07:08:48Z</updated>

		<summary type="html">&lt;p&gt;Sas020: Updated link to lecture notes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 12: More OWL=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
OWL ontology programming with RDFlib.&lt;br /&gt;
&lt;br /&gt;
==Tutorial==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Classes and methods==&lt;br /&gt;
In an earlier lab, you have already used these OWL concepts:&lt;br /&gt;
* (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)&lt;br /&gt;
* (ReflexiveProperty, IrreflexiveProperty, SymmetricProperty, AsymmetricProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty),&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this lab you will mostly use the following OWL terms:&lt;br /&gt;
&lt;br /&gt;
* (oneOf, unionOf, intersectionOf. complementOf)&lt;br /&gt;
* (Restriction, onProperty)&lt;br /&gt;
* (someValuesFrom, allValuesFrom, hasValue)&lt;br /&gt;
* (cardinality, minCardinality, maxCardinality)&lt;br /&gt;
* (qualifiedCardinality, minQualifiedCardinality, maxQualifiedCardinality, onClass)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=OWL Restrictions=&lt;br /&gt;
Most of the tasks today involve restrictions. &lt;br /&gt;
&lt;br /&gt;
I Recommend refreshing your memories on restrictions and complex classes from the [https://wiki.uib.no/info216/images/6/69/S12-OWL-2.pdf lecture notes]&lt;br /&gt;
When solving the tasks look at the notes, find the relevant OWL term/s, and try to use the same principles in python code.&lt;br /&gt;
&lt;br /&gt;
Down below is an example solution of the first task: &amp;quot;anyone who is a graduate has at least one degree&amp;quot;.&lt;br /&gt;
It looks complicated, but once you understand it, the other tasks follow a similar pattern. &lt;br /&gt;
In short: I create a blank node that is an OWL.restriction on the ex.degree property.&lt;br /&gt;
The restriction in question is a minCardinality restriction with the value 1. (e.g &amp;quot;at least one&amp;quot;) &lt;br /&gt;
Then I create another blank node that is for a List of all the criteria that makes a person a Graduate.&lt;br /&gt;
In this case I say that a Graduate is the intersection of a Person and the restriction we created earlier.&lt;br /&gt;
Collection is essentially used to create lists and everything between [] is what is in the list (the intersections).  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# anyone who is a graduate has at least one degree&lt;br /&gt;
 &lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.degree))&lt;br /&gt;
g.add((br, OWL.minCardinality, Literal(1)))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Graduate, OWL.intersectionOf, bi))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
Create or Extend a previous graph into an ontology that expresses the following using concepts from OWL (and some from RDF/RDFS):&lt;br /&gt;
* anyone who is a graduate has at least one degree&lt;br /&gt;
* anyone who is a university graduate has at least one degree from a university&lt;br /&gt;
* a grade is either an A, B, C, D, E or F&lt;br /&gt;
* a straight A student is a student that has only A grades&lt;br /&gt;
* a graduate has no F grades&lt;br /&gt;
* a student has a unique student number&lt;br /&gt;
* each student has exactly one average grade&lt;br /&gt;
* a course is either a bachelor, a master or a Ph.D course&lt;br /&gt;
* a bachelor student takes only bachelor courses&lt;br /&gt;
* a masters student takes only master courses and at most one bachelor course&lt;br /&gt;
* a Ph.D student takes only Ph.D and at most two masters courses&lt;br /&gt;
* a Ph.D. student cannot take a bachelor course&lt;br /&gt;
&lt;br /&gt;
Write each of the above statements as python code using RDFlib and OWL.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code to get started==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import owlrl&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, BNode&lt;br /&gt;
from rdflib.namespace import RDF, OWL, RDFS&lt;br /&gt;
from rdflib.collection import Collection&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;owl&amp;quot;, OWL)&lt;br /&gt;
&lt;br /&gt;
# anyone who is a graduate has at least one degree&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.degree))&lt;br /&gt;
g.add((br, OWL.minCardinality, Literal(1)))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Graduate, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# Continue here with the other statements:&lt;br /&gt;
&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==If You Have More Time==&lt;br /&gt;
Populate the ontology with data. E.g like: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.Cade, RDF.type, ex.Graduate))&lt;br /&gt;
g.add((ex.Cade, ex.grade, ex.A))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use owlrl like we did in lab 8 to infer additional triples.&lt;br /&gt;
IMPORANT: It seems owlrl is unable to reason with OWL Restrictions and perhaps other concepts as well.&lt;br /&gt;
There is a python library for better OWL reasoning called Owlready if you want to reason with restrictions.  &lt;br /&gt;
Below I print the ontology before and after the reasoning.&lt;br /&gt;
&lt;br /&gt;
What has changed about e.g Cade after using owlrl?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# # Write owl file before any reasoned triples&lt;br /&gt;
g.serialize(destination=&amp;quot;owl1.ttl&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Infer additional triples&lt;br /&gt;
owl_reasoner = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
owl_reasoner.closure()&lt;br /&gt;
owl_reasoner.flush_stored_triples()&lt;br /&gt;
&lt;br /&gt;
# Write owl file that includes reasoned triples&lt;br /&gt;
g.serialize(destination=&amp;quot;owl2.ttl&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Scenarios from previous labs==&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Semantic_Lifting_-_XML&amp;diff=1571</id>
		<title>Lab: Semantic Lifting - XML</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Semantic_Lifting_-_XML&amp;diff=1571"/>
		<updated>2021-03-23T13:46:22Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 9: Semantic Lifting - XML=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
The first task for today will be finishing of [https://wiki.uib.no/info216/index.php/Lab:_Semantic_Lifting_-_CSV last weeks lab]. Both finishing parsing and lifting the file, and implementing DBpedia Spotlight on at least one of the columns.&lt;br /&gt;
&lt;br /&gt;
If you have completed that you can start working with lifting XML data in the task below.&lt;br /&gt;
XML stands for Extensible Markup Language and is still widely used for data storage/transfer, especially for websites.&lt;br /&gt;
&lt;br /&gt;
XML has a tree structure similar to HTML, consisting of a root element, children and parent elements, attributes and so on.&lt;br /&gt;
The goal is for you to learn an example of how we can convert unsemantic data into RDF. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Relevant Libraries/Functions==&lt;br /&gt;
&lt;br /&gt;
import requests&lt;br /&gt;
&lt;br /&gt;
import xml.etree.ElementTree as ET&lt;br /&gt;
&lt;br /&gt;
* ET.ElementTree()&lt;br /&gt;
* ET.parse(&#039;xmlfile.xml&#039;)&lt;br /&gt;
* ET.fromstring(&amp;quot;XML_data_as_string&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
All parts of the XML tree are considered &#039;&#039;&#039;Elements&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
* Element.getroot()&lt;br /&gt;
* Element.findall(&amp;quot;path_in_tree&amp;quot;)&lt;br /&gt;
* Element.find(&amp;quot;name_of_tag&amp;quot;)&lt;br /&gt;
* Element.text&lt;br /&gt;
* Element.attrib(&amp;quot;name_of_attribute&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lift the XML data from http://feeds.bbci.co.uk/news/rss.xml about news articles by BBC_News into RDF triples.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
You can look at the actual XML structure of the data by clicking ctrl + U when you have opend the link in browser.&lt;br /&gt;
&lt;br /&gt;
The actual data about the news articles are stored under the &amp;lt;item&amp;gt;&amp;lt;/item&amp;gt; tags&lt;br /&gt;
 &lt;br /&gt;
For instance a triple should be something of the form: news_paper_id - hasTitle - titleValue&lt;br /&gt;
&lt;br /&gt;
Do this by parsing the XML using ElementTree (see import above).&lt;br /&gt;
&lt;br /&gt;
I recommend starting with the code at the bottom of the page and continuing on it. This code retrieves the XML using a HTTPRequest and saves it to an XML_file, so that you can view and parse it easily. &lt;br /&gt;
&lt;br /&gt;
You can use this regex (string matcher) to get only the ID&#039;s from the full url that is in the &amp;lt;guid&amp;gt; data.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
news_id = re.findall(&#039;\d+$&#039;, news_id)[0]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Parse through the fictional XML data below and add the correct journalists as the writers of the news_articles from earlier. &lt;br /&gt;
This means that e.g if the news article is written on a Tuesday, Thomas Smith is the one who wrote it. &lt;br /&gt;
One way to do this is by checking if any of the days in the &amp;quot;whenWriting&amp;quot; attribute is contained in the news articles &amp;quot;pubDate&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;data&amp;gt;&lt;br /&gt;
    &amp;lt;news_publisher name=&amp;quot;BBC News&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Mon, Tue, Wed&amp;quot; &amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Thomas&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Smith&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Thu, Fri&amp;quot; &amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Joseph&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Olson&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Sat, Sun&amp;quot; &amp;gt;&lt;br /&gt;
             &amp;lt;firstname&amp;gt;Sophia&amp;lt;/firstname&amp;gt;&lt;br /&gt;
             &amp;lt;lastname&amp;gt;Cruise&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
    &amp;lt;/news_publisher&amp;gt;&lt;br /&gt;
&amp;lt;/data&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==If You have more Time==&lt;br /&gt;
Extend the graph using the PROV vocabulary to describe Agents and Entities.&lt;br /&gt;
For instance, we want to say that the news articles originates from BBC, &lt;br /&gt;
and that the journalists acts on behalf of BBC.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code to Get Started==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, XSD&lt;br /&gt;
import xml.etree.ElementTree as ET&lt;br /&gt;
import requests&lt;br /&gt;
import re&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
prov = Namespace(&amp;quot;http://www.w3.org/ns/prov#&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;prov&amp;quot;, prov)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# URL of xml data&lt;br /&gt;
url = &#039;http://feeds.bbci.co.uk/news/rss.xml&#039;&lt;br /&gt;
&lt;br /&gt;
# Retrieve the xml data from the web-url.&lt;br /&gt;
resp = requests.get(url)&lt;br /&gt;
&lt;br /&gt;
# Creating an ElementTree from the response content&lt;br /&gt;
tree = ET.ElementTree(ET.fromstring(resp.content))&lt;br /&gt;
&lt;br /&gt;
# Or saving the xml data to a .xml file and creating a tree from this&lt;br /&gt;
with open(&#039;news.xml&#039;, &#039;wb&#039;) as f:&lt;br /&gt;
    f.write(resp.content)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| role=&amp;quot;presentation&amp;quot; class=&amp;quot;wikitable mw-collapsible mw-collapsed&amp;quot;&lt;br /&gt;
| &amp;lt;strong&amp;gt;Hints&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://www.geeksforgeeks.org/xml-parsing-python/ XML-parsing-python by geeksforgeeks.org]&lt;br /&gt;
* [https://www.w3schools.com/xml/xml_whatis.asp XML information by w3schools.com]&lt;br /&gt;
* [https://www.w3.org/TR/prov-o/#description PROV vocabulary]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab_Exercises&amp;diff=1570</id>
		<title>Lab Exercises</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab_Exercises&amp;diff=1570"/>
		<updated>2021-03-21T16:08:17Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we will present new Python-based lab exercises each week. The old [[Java Labs | Java-based exercises]] are still available if you prefer.&lt;br /&gt;
&lt;br /&gt;
# [[Lab: Getting started with Pycharm, Python and RDFlib]] (week 4, from 26/1) &amp;lt;!-- After S1: KG, S2: RDF --&amp;gt;&lt;br /&gt;
# [[Lab: RDF programming with RDFlib]] (week 5, from 2/2) &amp;lt;!-- After S3: SPARQL --&amp;gt;&lt;br /&gt;
# [[Lab: SPARQL]] (week 6, from 9/2) &amp;lt;!-- After S4: Tools and services --&amp;gt;&lt;br /&gt;
# [[Lab: SPARQL Programming]] (week 7, from 16/2) &#039;&#039;Group project idea discussion&#039;&#039; &amp;lt;!-- After S5: RDFS--&amp;gt;&lt;br /&gt;
# [[Lab: RDFS]] (week 8, from 23/2) &amp;lt;!-- After S6: OWL1 --&amp;gt;&lt;br /&gt;
# [[Lab: OWL 1]] (week 9, from 2/3) &amp;lt;!-- After S7: Vocab and ont --&amp;gt;&lt;br /&gt;
# [[Lab: Web APIs and JSON-LD]] (week 10, from 9/3) &amp;lt;!-- After S8: --&amp;gt;&lt;br /&gt;
# [[Lab: Semantic Lifting - CSV]] (week 11, from 16/3) &amp;lt;!-- After S9: --&amp;gt;&lt;br /&gt;
# [[Lab: Semantic Lifting - XML]] (week 12, from 23/3) &#039;&#039;Group project presentations&#039;&#039;  &amp;lt;!-- After S10: --&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
# Week 13 is Easter&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
# [[Lab: Semantic Lifting - HTML]] (week 14, from 6/4) &#039;&#039;Half-way group project presentation&#039;&#039; &amp;lt;!-- After no new lecture --&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
# [[Lab: OWL 2]] (week 15, from 13/4) &amp;lt;!-- After S11: EKG--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
# # Week 17, from 27/4: Open &amp;lt;!-- After S5: --&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
# Week 17, from 27/4: &#039;&#039;Final group project presentation&#039;&#039;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&#039;&#039;&#039;Project presentations:&#039;&#039;&#039;&lt;br /&gt;
In weeks 7-8, 12-13, and 17-18 there will not be ordinary labs. Instead, we will have mandatory presentations of your group projects.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;div class=&amp;quot;credits&amp;quot; style=&amp;quot;text-align: right; direction: ltr; margin-left: 1em;&amp;quot;&amp;gt;&#039;&#039;INFO216, UiB, 2017-2021, Andreas L. Opdahl (c)&#039;&#039;&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Semantic_Lifting_-_XML&amp;diff=1569</id>
		<title>Lab: Semantic Lifting - XML</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Semantic_Lifting_-_XML&amp;diff=1569"/>
		<updated>2021-03-21T16:05:24Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 10: Semantic Lifting - XML=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
The first task for today will be finishing of [https://wiki.uib.no/info216/index.php/Lab:_Semantic_Lifting_-_CSV last weeks lab]. Both finishing parsing and lifting the file, and implementing DBpedia Spotlight on at least one of the columns.&lt;br /&gt;
&lt;br /&gt;
If you have completed that you can start working with lifting XML data in the task below.&lt;br /&gt;
XML stands for Extensible Markup Language and is still widely used for data storage/transfer, especially for websites.&lt;br /&gt;
&lt;br /&gt;
XML has a tree structure similar to HTML, consisting of a root element, children and parent elements, attributes and so on.&lt;br /&gt;
The goal is for you to learn an example of how we can convert unsemantic data into RDF. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Relevant Libraries/Functions==&lt;br /&gt;
&lt;br /&gt;
import requests&lt;br /&gt;
&lt;br /&gt;
import xml.etree.ElementTree as ET&lt;br /&gt;
&lt;br /&gt;
* ET.ElementTree()&lt;br /&gt;
* ET.parse(&#039;xmlfile.xml&#039;)&lt;br /&gt;
* ET.fromstring(&amp;quot;XML_data_as_string&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
All parts of the XML tree are considered &#039;&#039;&#039;Elements&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
* Element.getroot()&lt;br /&gt;
* Element.findall(&amp;quot;path_in_tree&amp;quot;)&lt;br /&gt;
* Element.find(&amp;quot;name_of_tag&amp;quot;)&lt;br /&gt;
* Element.text&lt;br /&gt;
* Element.attrib(&amp;quot;name_of_attribute&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lift the XML data from http://feeds.bbci.co.uk/news/rss.xml about news articles by BBC_News into RDF triples.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
You can look at the actual XML structure of the data by clicking ctrl + U when you have opend the link in browser.&lt;br /&gt;
&lt;br /&gt;
The actual data about the news articles are stored under the &amp;lt;item&amp;gt;&amp;lt;/item&amp;gt; tags&lt;br /&gt;
 &lt;br /&gt;
For instance a triple should be something of the form: news_paper_id - hasTitle - titleValue&lt;br /&gt;
&lt;br /&gt;
Do this by parsing the XML using ElementTree (see import above).&lt;br /&gt;
&lt;br /&gt;
I recommend starting with the code at the bottom of the page and continuing on it. This code retrieves the XML using a HTTPRequest and saves it to an XML_file, so that you can view and parse it easily. &lt;br /&gt;
&lt;br /&gt;
You can use this regex (string matcher) to get only the ID&#039;s from the full url that is in the &amp;lt;guid&amp;gt; data.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
news_id = re.findall(&#039;\d+$&#039;, news_id)[0]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Parse through the fictional XML data below and add the correct journalists as the writers of the news_articles from earlier. &lt;br /&gt;
This means that e.g if the news article is written on a Tuesday, Thomas Smith is the one who wrote it. &lt;br /&gt;
One way to do this is by checking if any of the days in the &amp;quot;whenWriting&amp;quot; attribute is contained in the news articles &amp;quot;pubDate&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;data&amp;gt;&lt;br /&gt;
    &amp;lt;news_publisher name=&amp;quot;BBC News&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Mon, Tue, Wed&amp;quot; &amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Thomas&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Smith&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Thu, Fri&amp;quot; &amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Joseph&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Olson&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Sat, Sun&amp;quot; &amp;gt;&lt;br /&gt;
             &amp;lt;firstname&amp;gt;Sophia&amp;lt;/firstname&amp;gt;&lt;br /&gt;
             &amp;lt;lastname&amp;gt;Cruise&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
    &amp;lt;/news_publisher&amp;gt;&lt;br /&gt;
&amp;lt;/data&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==If You have more Time==&lt;br /&gt;
Extend the graph using the PROV vocabulary to describe Agents and Entities.&lt;br /&gt;
For instance, we want to say that the news articles originates from BBC, &lt;br /&gt;
and that the journalists acts on behalf of BBC.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code to Get Started==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, XSD&lt;br /&gt;
import xml.etree.ElementTree as ET&lt;br /&gt;
import requests&lt;br /&gt;
import re&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
prov = Namespace(&amp;quot;http://www.w3.org/ns/prov#&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;prov&amp;quot;, prov)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# URL of xml data&lt;br /&gt;
url = &#039;http://feeds.bbci.co.uk/news/rss.xml&#039;&lt;br /&gt;
&lt;br /&gt;
# Retrieve the xml data from the web-url.&lt;br /&gt;
resp = requests.get(url)&lt;br /&gt;
&lt;br /&gt;
# Creating an ElementTree from the response content&lt;br /&gt;
tree = ET.ElementTree(ET.fromstring(resp.content))&lt;br /&gt;
&lt;br /&gt;
# Or saving the xml data to a .xml file and creating a tree from this&lt;br /&gt;
with open(&#039;news.xml&#039;, &#039;wb&#039;) as f:&lt;br /&gt;
    f.write(resp.content)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| role=&amp;quot;presentation&amp;quot; class=&amp;quot;wikitable mw-collapsible mw-collapsed&amp;quot;&lt;br /&gt;
| &amp;lt;strong&amp;gt;Hints&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://www.geeksforgeeks.org/xml-parsing-python/ XML-parsing-python by geeksforgeeks.org]&lt;br /&gt;
* [https://www.w3schools.com/xml/xml_whatis.asp XML information by w3schools.com]&lt;br /&gt;
* [https://www.w3.org/TR/prov-o/#description PROV vocabulary]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Semantic_Lifting_-_XML&amp;diff=1568</id>
		<title>Lab: Semantic Lifting - XML</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Semantic_Lifting_-_XML&amp;diff=1568"/>
		<updated>2021-03-16T13:28:01Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 10: Semantic Lifting - XML=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
Today&#039;s topic involves lifting data in XML format into RDF.&lt;br /&gt;
XML stands for Extensible Markup Language and is used to commonly for data storage/transfer, especially for websites.&lt;br /&gt;
&lt;br /&gt;
XML has a tree structure similar to HTML, consisting of a root element, children and parent elements, attributes and so on.&lt;br /&gt;
The goal is for you to learn an example of how we can convert unsemantic data into RDF. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Relevant Libraries/Functions==&lt;br /&gt;
&lt;br /&gt;
import requests&lt;br /&gt;
&lt;br /&gt;
import xml.etree.ElementTree as ET&lt;br /&gt;
&lt;br /&gt;
* ET.parse(&#039;xmlfile.xml&#039;)&lt;br /&gt;
&lt;br /&gt;
All parts of the XML tree are considered &#039;&#039;&#039;Elements&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
* Element.getroot()&lt;br /&gt;
* Element.findall(&amp;quot;path_in_tree&amp;quot;)&lt;br /&gt;
* Element.find(&amp;quot;name_of_tag&amp;quot;)&lt;br /&gt;
* Element.text&lt;br /&gt;
* Element.attrib(&amp;quot;name_of_attribute&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lift the XML data from http://feeds.bbci.co.uk/news/rss.xml about news articles by BBC_News into RDF triples.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
You can look at the actual XML structure of the data by clicking ctrl + U when you have opend the link in browser.&lt;br /&gt;
&lt;br /&gt;
The actual data about the news articles are stored under the &amp;lt;item&amp;gt;&amp;lt;/item&amp;gt; tags&lt;br /&gt;
 &lt;br /&gt;
For instance a triple should be something of the form: news_paper_id - hasTitle - titleValue&lt;br /&gt;
&lt;br /&gt;
Do this by parsing the XML using ElementTree (see import above).&lt;br /&gt;
&lt;br /&gt;
I recommend starting with the code at the bottom of the page and continuing on it. This code retrieves the XML using a HTTPRequest and saves it to an XML_file, so that you can view and parse it easily. &lt;br /&gt;
&lt;br /&gt;
You can use this regex (string matcher) to get only the ID&#039;s from the full url that is in the &amp;lt;guid&amp;gt; data.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
news_id = re.findall(&#039;\d+$&#039;, news_id)[0]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Parse through the fictional XML data below and add the correct journalists as the writers of the news_articles from earlier. &lt;br /&gt;
This means that e.g if the news article is written on a Tuesday, Thomas Smith is the one who wrote it. &lt;br /&gt;
One way to do this is by checking if any of the days in the &amp;quot;whenWriting&amp;quot; attribute is contained in the news articles &amp;quot;pubDate&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;data&amp;gt;&lt;br /&gt;
    &amp;lt;news_publisher name=&amp;quot;BBC News&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Mon, Tue, Wed&amp;quot; &amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Thomas&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Smith&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Thu, Fri&amp;quot; &amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Joseph&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Olson&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
        &amp;lt;journalist whenWriting=&amp;quot;Sat, Sun&amp;quot; &amp;gt;&lt;br /&gt;
             &amp;lt;firstname&amp;gt;Sophia&amp;lt;/firstname&amp;gt;&lt;br /&gt;
             &amp;lt;lastname&amp;gt;Cruise&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/journalist&amp;gt;&lt;br /&gt;
    &amp;lt;/news_publisher&amp;gt;&lt;br /&gt;
&amp;lt;/data&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==If You have more Time==&lt;br /&gt;
Extend the graph using the PROV vocabulary to describe Agents and Entities.&lt;br /&gt;
For instance, we want to say that the news articles originates from BBC, &lt;br /&gt;
and that the journalists acts on behalf of BBC.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code to Get Started==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, XSD&lt;br /&gt;
import xml.etree.ElementTree as ET&lt;br /&gt;
import requests&lt;br /&gt;
import re&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
prov = Namespace(&amp;quot;http://www.w3.org/ns/prov#&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;prov&amp;quot;, prov)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# URL of xml data&lt;br /&gt;
url = &#039;http://feeds.bbci.co.uk/news/rss.xml&#039;&lt;br /&gt;
&lt;br /&gt;
# Retrieve the xml data from the web-url.&lt;br /&gt;
resp = requests.get(url)&lt;br /&gt;
&lt;br /&gt;
# Saving the xml data to a .xml file&lt;br /&gt;
with open(&#039;news.xml&#039;, &#039;wb&#039;) as f:&lt;br /&gt;
    f.write(resp.content)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| role=&amp;quot;presentation&amp;quot; class=&amp;quot;wikitable mw-collapsible mw-collapsed&amp;quot;&lt;br /&gt;
| &amp;lt;strong&amp;gt;Hints&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://www.geeksforgeeks.org/xml-parsing-python/ XML-parsing-python by geeksforgeeks.org]&lt;br /&gt;
* [https://www.w3schools.com/xml/xml_whatis.asp XML information by w3schools.com]&lt;br /&gt;
* [https://www.w3.org/TR/prov-o/#description PROV vocabulary]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Semantic_Lifting_-_CSV&amp;diff=1567</id>
		<title>Lab: Semantic Lifting - CSV</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Semantic_Lifting_-_CSV&amp;diff=1567"/>
		<updated>2021-03-16T08:38:27Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 8: Semantic Lifting - CSV =&lt;br /&gt;
&lt;br /&gt;
== Topic ==&lt;br /&gt;
Today&#039;s topic involves lifting data in CSV format into RDF. The goal is for you to learn how we can convert non-semantic data into RDF as well as getting familiar with some common vocabularies.&lt;br /&gt;
&lt;br /&gt;
Fortunately, CSV is already structured in a way that makes the creation of triples relatively easy.&lt;br /&gt;
&lt;br /&gt;
We will also use Pandas Dataframes which will contain our CSV data in python code, and we&#039;ll do some basic data manipulation to improve our output data.&lt;br /&gt;
&lt;br /&gt;
== Relevant Libraries - Classes, Functions and Methods and Vocabularies==&lt;br /&gt;
=== Libraries ===&lt;br /&gt;
* RDFlib concepts from earlier (Graph, Namespace, URIRef, Literal, BNode)&lt;br /&gt;
* Pandas: DataFrame, apply, iterrows, astype&lt;br /&gt;
* DBpedia Spotlight&lt;br /&gt;
&lt;br /&gt;
=== Semantic Vocabularies ===&lt;br /&gt;
You do not have to use the same ones, but these should be well suited.&lt;br /&gt;
* RDF: type&lt;br /&gt;
* RDFS: label&lt;br /&gt;
* Simple Event Ontology (sem): Event, eventType, Actor, hasActor, hasActorType, hasBeginTimeStamp, EndTimeStamp, hasTime, hasSubEvent&lt;br /&gt;
* TimeLine Ontology (tl): durationInt&lt;br /&gt;
* An example-namespace to represent terms not found elsewhere (ex): IndictmentDays, Overturned, Pardoned&lt;br /&gt;
* DBpedia&lt;br /&gt;
&lt;br /&gt;
== Tasks ==&lt;br /&gt;
Today we will be working with FiveThirtyEight&#039;s russia-investigation dataset. It contains special investigations conducted by the United States since the Watergate-investigation with information about them to May 2017. If you found the last weeks exercice doable, I recommend trying to write this with object-oriented programming (OOP) structure, as this tends to make for cleaner code.&lt;br /&gt;
&lt;br /&gt;
It contains the following columns:&lt;br /&gt;
* investigation&lt;br /&gt;
* investigation-start&lt;br /&gt;
* investigation-end&lt;br /&gt;
* investigation-days&lt;br /&gt;
* name&lt;br /&gt;
* indictment-days&lt;br /&gt;
* type&lt;br /&gt;
* cp-date&lt;br /&gt;
* cp-days&lt;br /&gt;
* overturned&lt;br /&gt;
* pardoned&lt;br /&gt;
* american&lt;br /&gt;
* president&lt;br /&gt;
&lt;br /&gt;
More information about the columns and the dataset here: https://github.com/fivethirtyeight/data/tree/master/russia-investigation&lt;br /&gt;
&lt;br /&gt;
Our goal is to convert this non-semantic dataset into a semantic one. To do this we will go row-by-row through the dataset and extract the content of each column.&lt;br /&gt;
An investigation may have multiple rows in the dataset if it investigates multiple people, you can choose to represent these as one or multiple entities in the graph. Each investigation may also have a sub-event representing the result of the investigation, this could for instance be indictment or guilty-plea.&lt;br /&gt;
&lt;br /&gt;
For a row we will start by creating a resource representing the investigation. In this example we handle all investigations with the same name as the samme entity, and will therefore use the name of the investigation (&amp;quot;investigation&amp;quot;-column) to create the URI:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
name = row[&amp;quot;investigation&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
investigation = URIRef(ex + name)&lt;br /&gt;
g.add((investigation, RDF.type, sem.Event))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Further we will create a relation between the investigation and all its associated columns. For when the investigation started we&#039;ll use the &amp;quot;investigation-start&amp;quot;-column and we can use the property sem:hasBeginTimeStamp:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
investigation_start = row[&amp;quot;investigation-start&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
g.add((investigation, sem.hasBeginTimeStamp, Literal(investigation_start, datatype=XSD.datetime)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To represent the result of the investigation, if it has one, We can create another entity and connect it to the investigation using the sem:hasSubEvent. If so the following columns can be attributed to the sub-event:&lt;br /&gt;
* type&lt;br /&gt;
* indictment-days&lt;br /&gt;
* overturned&lt;br /&gt;
* pardon&lt;br /&gt;
* cp_date&lt;br /&gt;
* cp_days&lt;br /&gt;
* name (the name of the investigatee, not the name of the investigation)&lt;br /&gt;
&lt;br /&gt;
=== Code to get you started ===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import pandas as pd&lt;br /&gt;
import rdflib&lt;br /&gt;
&lt;br /&gt;
from rdflib import Graph, Namespace, URIRef, Literal, BNode&lt;br /&gt;
from rdflib.namespace import RDF, RDFS, XSD&lt;br /&gt;
&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
dbr = Namespace(&amp;quot;http://dbpedia.org/resource/&amp;quot;)&lt;br /&gt;
sem = Namespace(&amp;quot;http://semanticweb.cs.vu.nl/2009/11/sem/&amp;quot;)&lt;br /&gt;
tl = Namespace(&amp;quot;http://purl.org/NET/c4dm/timeline.owl#&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;dbr&amp;quot;, dbr)&lt;br /&gt;
g.bind(&amp;quot;sem&amp;quot;, sem)&lt;br /&gt;
g.bind(&amp;quot;tl&amp;quot;, tl)&lt;br /&gt;
&lt;br /&gt;
df = pd.read_csv(&amp;quot;data/investigations.csv&amp;quot;)&lt;br /&gt;
# We need to correct the type of the columns in the DataFrame, as Pandas assigns an incorrect type when it reads the file (for me at least). We use .astype(&amp;quot;str&amp;quot;) to convert the content of the columns to a string.&lt;br /&gt;
df[&amp;quot;name&amp;quot;] = df[&amp;quot;name&amp;quot;].astype(&amp;quot;str&amp;quot;)&lt;br /&gt;
df[&amp;quot;type&amp;quot;] = df[&amp;quot;type&amp;quot;].astype(&amp;quot;str&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# iterrows creates an iterable object (list of rows)&lt;br /&gt;
for row in df.iterrows():&lt;br /&gt;
	# Do something here to add the content of the row to the graph &lt;br /&gt;
	pass&lt;br /&gt;
&lt;br /&gt;
g.serialize(&amp;quot;output.ttl&amp;quot;, format=&amp;quot;ttl&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== If you have more time ==&lt;br /&gt;
If you have not already you should include some checks to assure that you don&#039;t add any empty columns to your graph.&lt;br /&gt;
&lt;br /&gt;
If you have more time you can implement DBpedia Spotlight to link the people mentioned in the dataset to DBpedia resources.&lt;br /&gt;
You can use the same code example as in the last lab, but you will need some error-handling for when DBpedia is unable to find a match. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# Parameter given to spotlight to filter out results with confidence lower than this value&lt;br /&gt;
CONFIDENCE = 0.5&lt;br /&gt;
&lt;br /&gt;
def annotate_entity(entity, filters={&amp;quot;types&amp;quot;:&amp;quot;DBpedia:Person&amp;quot;}):&lt;br /&gt;
	annotations = []&lt;br /&gt;
	try:&lt;br /&gt;
		annotations = spotlight.annotate(SERVER, entity, confidence=CONFIDENCE, filters=filters)&lt;br /&gt;
    # This catches errors thrown from Spotlight, including when no resource is found in DBpedia&lt;br /&gt;
	except SpotlightException as e:&lt;br /&gt;
		print(e)&lt;br /&gt;
		# Implement some error handling here&lt;br /&gt;
	return annotations&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we use the types-filter with DBpedia:Person, as we only want it to match with people. You can choose to only implement the URIs in the response, or the types as well. An issue here is that &lt;br /&gt;
&lt;br /&gt;
== Useful readings ==&lt;br /&gt;
* [https://github.com/fivethirtyeight/data/tree/master/russia-investigation Information about the dataset]&lt;br /&gt;
* [https://towardsdatascience.com/pandas-dataframe-playing-with-csv-files-944225d19ff Article about working with pandas.DataFrames and CSV]&lt;br /&gt;
* [https://pandas.pydata.org/pandas-docs/stable/reference/frame.html Pandas DataFrame documentation]&lt;br /&gt;
* [https://semanticweb.cs.vu.nl/2009/11/sem/#sem:eventType Simple Event Ontology Descripiton]&lt;br /&gt;
* [http://motools.sourceforge.net/timeline/timeline.html The TimeLine Ontology Description]&lt;br /&gt;
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1560</id>
		<title>Lab: Web APIs and JSON-LD</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1560"/>
		<updated>2021-03-13T10:30:52Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 7: Accessing and lifting Web APIs (RESTful web services)=&lt;br /&gt;
&lt;br /&gt;
==Topics== &lt;br /&gt;
Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD.&lt;br /&gt;
&lt;br /&gt;
We will use Web APIs to retrieve regular JSON data, parse them programmatically, where possible link the resources to established DBpedia ones and finally create a RDFLib graph with the data.&lt;br /&gt;
&lt;br /&gt;
==Imports==&lt;br /&gt;
* import json&lt;br /&gt;
* import rdflib&lt;br /&gt;
* import requests&lt;br /&gt;
* import spotlight&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
=== Task 1 ===&lt;br /&gt;
Write a small program that queries the Open Notify Astros API (link below) for the people currently in space. Create a graph from the response connecting each astronaut to the craft they are currently on, for instance using http://example.com/onCraft as a property. Also as the space station is not too big, it is safe to assume that two people who spent time on it at the same time know each other, so add this to the graph.&lt;br /&gt;
&lt;br /&gt;
* Astros API url: http://api.open-notify.org/astros.json&lt;br /&gt;
* Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/&lt;br /&gt;
&lt;br /&gt;
The response from the API follows the format&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;message&amp;quot;: &amp;quot;success&amp;quot;,&lt;br /&gt;
    &amp;quot;number&amp;quot;: 7,&lt;br /&gt;
    &amp;quot;people&amp;quot;: [&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Sergey Ryzhikov&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Kate Rubins&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        ...&lt;br /&gt;
    ]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We only need to think about whats inside the list of the &amp;quot;people&amp;quot;-value.&lt;br /&gt;
To create the graph you can iteratively extract the values of craft and name and add them. As none of the names or craft is a valid URI, they can be crated using the example-namespace.&lt;br /&gt;
&lt;br /&gt;
=== Task 2 ===&lt;br /&gt;
Serialise the graph to JSON-LD, set the context of the JSON-LD object to represent the properties for knows and onCraft.&lt;br /&gt;
&lt;br /&gt;
To do this you need to pip install the json-ld portion of rdflib if you have not already:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
pip install rdflib-jsonld&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== If you have more time ===&lt;br /&gt;
DBpedia Spotlight is a tool for automatically annotating mentions of DBpedia resources in text, providing a solution for linking unstructured information sources to the Linked Open Data cloud through DBpedia.&lt;br /&gt;
&lt;br /&gt;
Build upon the program using the DBpedia Spotlight API (example code below) to use a DBpedia-resource in your graph if one is available. You can add some simple error-handling for cases where no DBpedia resource is found - use an example-entity in stead. Keep in mind that some resources may represent other people with the same name, so try to change the types-parameter so you only get astronauts in return, the confidence-parameter might also help you with this.&lt;br /&gt;
&lt;br /&gt;
The response from DBpedia Spotlight is a list of dictionaries, where each dictionary contains the URI of the resource, its types and some other metadata we will not use now. Set the type of the resouce to the types listed in the response.&lt;br /&gt;
&lt;br /&gt;
==== Example code for DBpedia Spotlight query ====&lt;br /&gt;
First pip install &amp;lt;b&amp;gt;pyspotlight&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import spotlight&lt;br /&gt;
# Note that althoug we import spotlight in python, we need to pip install pyspotlight to get the correct package&lt;br /&gt;
&lt;br /&gt;
SERVER = &amp;quot;https://api.dbpedia-spotlight.org/en/annotate&amp;quot;&lt;br /&gt;
annotations = spotlight.annotate(SERVER, &amp;quot;str_to_be_annotated&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ Reading and writing with JSON - stackabuse.com]&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples Examples]&lt;br /&gt;
* [https://realpython.com/python-requests/ Requests - realpython.com]&lt;br /&gt;
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1559</id>
		<title>Lab: Web APIs and JSON-LD</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1559"/>
		<updated>2021-03-12T11:10:29Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 7: Accessing and lifting Web APIs (RESTful web services)=&lt;br /&gt;
&lt;br /&gt;
==Topics== &lt;br /&gt;
Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD.&lt;br /&gt;
&lt;br /&gt;
We will use Web APIs to retrieve regular JSON data, parse them programmatically, where possible link the resources to established DBpedia ones and finally create a RDFLib graph with the data.&lt;br /&gt;
&lt;br /&gt;
==Imports==&lt;br /&gt;
* import json&lt;br /&gt;
* import rdflib&lt;br /&gt;
* import requests&lt;br /&gt;
* import spotlight&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
=== Task 1 ===&lt;br /&gt;
Write a small program that queries the Open Notify Astros API (link below) for the people currently in space. Create a graph from the response connecting each astronaut to the craft they are currently on, for instance using http://example.com/onCraft as a property. Also as the space station is not too big, it is safe to assume that two people whom spent time on it at the same time know each other, so add this to the graph.&lt;br /&gt;
&lt;br /&gt;
* Astros API url: http://api.open-notify.org/astros.json&lt;br /&gt;
* Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/&lt;br /&gt;
&lt;br /&gt;
The response from the API follows the format&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;message&amp;quot;: &amp;quot;success&amp;quot;,&lt;br /&gt;
    &amp;quot;number&amp;quot;: 7,&lt;br /&gt;
    &amp;quot;people&amp;quot;: [&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Sergey Ryzhikov&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Kate Rubins&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        ...&lt;br /&gt;
    ]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We only need to think about whats inside the list of the &amp;quot;people&amp;quot;-value.&lt;br /&gt;
To create the graph you can iteratively extract the values of craft and name and add them. As none of the names or craft is a valid URI, they can be crated using the example-namespace.&lt;br /&gt;
&lt;br /&gt;
=== Task 2 ===&lt;br /&gt;
Serialise the graph to JSON-LD, set the context of the JSON-LD object to represent the properties for knows and onCraft.&lt;br /&gt;
&lt;br /&gt;
To do this you need to pip install the json-ld portion of rdflib if you have not already:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
pip install rdflib-jsonld&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== If you have more time ===&lt;br /&gt;
DBpedia Spotlight is a tool for automatically annotating mentions of DBpedia resources in text, providing a solution for linking unstructured information sources to the Linked Open Data cloud through DBpedia.&lt;br /&gt;
&lt;br /&gt;
Build upon the program using the DBpedia Spotlight API (example code below) to use a DBpedia-resource in your graph if one is available. You can add some simple error-handling for cases where no DBpedia resource is found - use an example-entity in stead. Keep in mind that some resources may represent other people with the same name, so try to change the types-parameter so you only get astronauts in return, the confidence-parameter might also help you with this.&lt;br /&gt;
&lt;br /&gt;
The response from DBpedia Spotlight is a list of dictionaries, where each dictionary contains the URI of the resource, its types and some other metadata we will not use now. Set the type of the resouce to the types listed in the response.&lt;br /&gt;
&lt;br /&gt;
==== Example code for DBpedia Spotlight query ====&lt;br /&gt;
First pip install &amp;lt;b&amp;gt;pyspotlight&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import spotlight&lt;br /&gt;
# Note that althoug we import spotlight in python, we need to pip install pyspotlight to get the correct package&lt;br /&gt;
&lt;br /&gt;
SERVER = &amp;quot;https://api.dbpedia-spotlight.org/en/annotate&amp;quot;&lt;br /&gt;
annotations = spotlight.annotate(SERVER, &amp;quot;str_to_be_annotated&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ Reading and writing with JSON - stackabuse.com]&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples Examples]&lt;br /&gt;
* [https://realpython.com/python-requests/ Requests - realpython.com]&lt;br /&gt;
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1558</id>
		<title>Lab: Web APIs and JSON-LD</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1558"/>
		<updated>2021-03-12T10:49:38Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 7: Accessing and lifting Web APIs (RESTful web services)=&lt;br /&gt;
&lt;br /&gt;
==Topics== &lt;br /&gt;
Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD.&lt;br /&gt;
&lt;br /&gt;
We will use Web APIs to retrieve regular JSON data, parse them programmatically, where possible link the resources to established DBpedia ones and finally create a RDFLib graph with the data.&lt;br /&gt;
&lt;br /&gt;
==Imports==&lt;br /&gt;
* import json&lt;br /&gt;
* import rdflib&lt;br /&gt;
* import requests&lt;br /&gt;
* import spotlight&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
=== Task 1 ===&lt;br /&gt;
Write a small program that queries the Open Notify Astros API (link below) for the people currently in space. Create a graph from the response connecting each astronaut to the craft they are currently on, for instance using http://example.com/onCraft as a property. Also as the space station is not too big, it is safe to assume that two people whom spent time on it at the same time know each other, so add this to the graph.&lt;br /&gt;
&lt;br /&gt;
* Astros API url: http://api.open-notify.org/astros.json&lt;br /&gt;
* Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/&lt;br /&gt;
&lt;br /&gt;
The response from the API follows the format&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;message&amp;quot;: &amp;quot;success&amp;quot;,&lt;br /&gt;
    &amp;quot;number&amp;quot;: 7,&lt;br /&gt;
    &amp;quot;people&amp;quot;: [&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Sergey Ryzhikov&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Kate Rubins&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        ...&lt;br /&gt;
    ]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We only need to think about whats inside the list of the &amp;quot;people&amp;quot;-value.&lt;br /&gt;
To create the graph you can iteratively extract the values of craft and name and add them. As none of the names or craft is a valid URI, they can be crated using the example-namespace.&lt;br /&gt;
&lt;br /&gt;
=== Task 2 ===&lt;br /&gt;
Serialise the graph to JSON-LD, set the context of the JSON-LD object to represent the properties for knows and onCraft.&lt;br /&gt;
&lt;br /&gt;
To do this you need to pip install the json-ld portion of rdflib if you have not already:&lt;br /&gt;
&lt;br /&gt;
pip install rdflib-jsonld&lt;br /&gt;
&lt;br /&gt;
=== If you have more time ===&lt;br /&gt;
DBpedia Spotlight is a tool for automatically annotating mentions of DBpedia resources in text, providing a solution for linking unstructured information sources to the Linked Open Data cloud through DBpedia.&lt;br /&gt;
&lt;br /&gt;
Build upon the program using the DBpedia Spotlight API (example code below) to use a DBpedia-resource in your graph if one is available. You can add some simple error-handling for cases where no DBpedia resource is found - use an example-entity in stead. Keep in mind that some resources may represent other people with the same name, so try to change the types-parameter so you only get astronauts in return, the confidence-parameter might also help you with this.&lt;br /&gt;
&lt;br /&gt;
The response from DBpedia Spotlight is a list of dictionaries, where each dictionary contains the URI of the resource, its types and some other metadata we will not use now. Set the type of the resouce to the types listed in the response.&lt;br /&gt;
&lt;br /&gt;
==== Example code for DBpedia Spotlight query ====&lt;br /&gt;
First pip install &amp;lt;b&amp;gt;pyspotlight&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import spotlight&lt;br /&gt;
# Note that althoug we import spotlight in python, we need to pip install pyspotlight to get the correct package&lt;br /&gt;
&lt;br /&gt;
SERVER = &amp;quot;https://api.dbpedia-spotlight.org/en/annotate&amp;quot;&lt;br /&gt;
annotations = spotlight.annotate(SERVER, &amp;quot;str_to_be_annotated&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ Reading and writing with JSON - stackabuse.com]&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples Examples]&lt;br /&gt;
* [https://realpython.com/python-requests/ Requests - realpython.com]&lt;br /&gt;
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1557</id>
		<title>Lab: Web APIs and JSON-LD</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1557"/>
		<updated>2021-03-12T10:48:39Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 7: Accessing and lifting Web APIs (RESTful web services)=&lt;br /&gt;
&lt;br /&gt;
==Topics== &lt;br /&gt;
Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD.&lt;br /&gt;
&lt;br /&gt;
We will use Web APIs to retrieve regular JSON data, parse them programmatically, where possible link the resources to established DBpedia ones and finally create a RDFLib graph with the data.&lt;br /&gt;
&lt;br /&gt;
==Imports==&lt;br /&gt;
* import json&lt;br /&gt;
* import rdflib&lt;br /&gt;
* import requests&lt;br /&gt;
* import spotlight&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
=== Task 1 ===&lt;br /&gt;
Write a small program that queries the Open Notify Astros API (link below) for the people currently in space. Create a graph from the response connecting each astronaut to the craft they are currently on, for instance using http://example.com/onCraft as a property. Also as the space station is not too big, it is safe to assume that two people whom spent time on it at the same time know each other, so add this to the graph.&lt;br /&gt;
&lt;br /&gt;
* Astros API url: http://api.open-notify.org/astros.json&lt;br /&gt;
* Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/&lt;br /&gt;
&lt;br /&gt;
The response from the API follows the format&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;message&amp;quot;: &amp;quot;success&amp;quot;,&lt;br /&gt;
    &amp;quot;number&amp;quot;: 7,&lt;br /&gt;
    &amp;quot;people&amp;quot;: [&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Sergey Ryzhikov&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Kate Rubins&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        ...&lt;br /&gt;
    ]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We only need to think about whats inside the list of the &amp;quot;people&amp;quot;-key.&lt;br /&gt;
To create the graph you can iteratively extract the values of craft and name and add them. As none of the names or craft is a valid URI, they can be crated using the example-namespace.&lt;br /&gt;
&lt;br /&gt;
=== Task 2 ===&lt;br /&gt;
Serialise the graph to JSON-LD, set the context of the JSON-LD object to represent the properties for knows and onCraft.&lt;br /&gt;
&lt;br /&gt;
To do this you need to pip install the json-ld portion of rdflib if you have not already:&lt;br /&gt;
&lt;br /&gt;
pip install rdflib-jsonld&lt;br /&gt;
&lt;br /&gt;
=== If you have more time ===&lt;br /&gt;
DBpedia Spotlight is a tool for automatically annotating mentions of DBpedia resources in text, providing a solution for linking unstructured information sources to the Linked Open Data cloud through DBpedia.&lt;br /&gt;
&lt;br /&gt;
Build upon the program using the DBpedia Spotlight API (example code below) to use a DBpedia-resource in your graph if one is available. You can add some simple error-handling for cases where no DBpedia resource is found - use an example-entity in stead. Keep in mind that some resources may represent other people with the same name, so try to change the types-parameter so you only get astronauts in return, the confidence-parameter might also help you with this.&lt;br /&gt;
&lt;br /&gt;
The response from DBpedia Spotlight is a list of dictionaries, where each dictionary contains the URI of the resource, its types and some other metadata we will not use now. Set the type of the resouce to the types listed in the response.&lt;br /&gt;
&lt;br /&gt;
==== Example code for DBpedia Spotlight query ====&lt;br /&gt;
First pip install &amp;lt;b&amp;gt;pyspotlight&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import spotlight&lt;br /&gt;
# Note that althoug we import spotlight in python, we need to pip install pyspotlight to get the correct package&lt;br /&gt;
&lt;br /&gt;
SERVER = &amp;quot;https://api.dbpedia-spotlight.org/en/annotate&amp;quot;&lt;br /&gt;
annotations = spotlight.annotate(SERVER, &amp;quot;str_to_be_annotated&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ Reading and writing with JSON - stackabuse.com]&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples Examples]&lt;br /&gt;
* [https://realpython.com/python-requests/ Requests - realpython.com]&lt;br /&gt;
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1556</id>
		<title>Lab: Web APIs and JSON-LD</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1556"/>
		<updated>2021-03-10T12:55:57Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 7: Accessing and lifting Web APIs (RESTful web services)=&lt;br /&gt;
&lt;br /&gt;
==Topics== &lt;br /&gt;
Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD.&lt;br /&gt;
&lt;br /&gt;
We will use Web APIs to retrieve regular JSON data, parse them programmatically, where possible link the resources to established DBpedia ones and finally create a RDFLib graph with the data.&lt;br /&gt;
&lt;br /&gt;
==Imports==&lt;br /&gt;
* import json&lt;br /&gt;
* import rdflib&lt;br /&gt;
* import requests&lt;br /&gt;
* import spotlight&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
=== Task 1 ===&lt;br /&gt;
Write a small program that queries the Open Notify Astros API (link below) for the people currently in space. Create a graph from the response connecting each astronaut to the craft they are currently on, for instance using http://example.com/onCraft as a property. Also as the space station is not too big, it is safe to assume that two people whom spent who time on it at the same time know each other, so add this to the graph.&lt;br /&gt;
&lt;br /&gt;
* Astros API url: http://api.open-notify.org/astros.json&lt;br /&gt;
* Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/&lt;br /&gt;
&lt;br /&gt;
The response from the API follows the format&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;message&amp;quot;: &amp;quot;success&amp;quot;,&lt;br /&gt;
    &amp;quot;number&amp;quot;: 7,&lt;br /&gt;
    &amp;quot;people&amp;quot;: [&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Sergey Ryzhikov&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Kate Rubins&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        ...&lt;br /&gt;
    ]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We only need to think about whats inside the list of the &amp;quot;people&amp;quot;-key.&lt;br /&gt;
To create the graph you can iteratively extract the values of craft and name and add them. As none of the names or craft is a valid URI, they can be crated using the example-namespace.&lt;br /&gt;
&lt;br /&gt;
=== Task 2 ===&lt;br /&gt;
Serialise the graph to JSON-LD, set the context of the JSON-LD object to represent the properties for knows and onCraft.&lt;br /&gt;
&lt;br /&gt;
To do this you need to pip install the json-ld portion of rdflib if you have not already:&lt;br /&gt;
&lt;br /&gt;
pip install rdflib-jsonld&lt;br /&gt;
&lt;br /&gt;
=== If you have more time ===&lt;br /&gt;
DBpedia Spotlight is a tool for automatically annotating mentions of DBpedia resources in text, providing a solution for linking unstructured information sources to the Linked Open Data cloud through DBpedia.&lt;br /&gt;
&lt;br /&gt;
Build upon the program using the DBpedia Spotlight API (example code below) to use a DBpedia-resource in your graph if one is available. You can add some simple error-handling for cases where no DBpedia resource is found - use an example-entity in stead. Keep in mind that some resources may represent other people with the same name, so try to change the types-parameter so you only get astronauts in return, the confidence-parameter might also help you with this.&lt;br /&gt;
&lt;br /&gt;
The response from DBpedia Spotlight is a list of dictionaries, where each dictionary contains the URI of the resource, its types and some other metadata we will not use now. Set the type of the resouce to the types listed in the response.&lt;br /&gt;
&lt;br /&gt;
==== Example code for DBpedia Spotlight query ====&lt;br /&gt;
First pip install &amp;lt;b&amp;gt;pyspotlight&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import spotlight&lt;br /&gt;
# Note that althoug we import spotlight in python, we need to pip install pyspotlight to get the correct package&lt;br /&gt;
&lt;br /&gt;
SERVER = &amp;quot;https://api.dbpedia-spotlight.org/en/annotate&amp;quot;&lt;br /&gt;
annotations = spotlight.annotate(SERVER, &amp;quot;str_to_be_annotated&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ Reading and writing with JSON - stackabuse.com]&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples Examples]&lt;br /&gt;
* [https://realpython.com/python-requests/ Requests - realpython.com]&lt;br /&gt;
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1555</id>
		<title>Lab: Web APIs and JSON-LD</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1555"/>
		<updated>2021-03-09T14:59:36Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 7: Accessing and lifting Web APIs (RESTful web services)=&lt;br /&gt;
&lt;br /&gt;
==Topics== &lt;br /&gt;
Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD.&lt;br /&gt;
&lt;br /&gt;
We will use Web APIs to retrieve regular JSON data, parse them programmatically, where possible link the resources to established DBpedia ones and finally create a RDFLib graph with the data.&lt;br /&gt;
&lt;br /&gt;
==Imports==&lt;br /&gt;
* import json&lt;br /&gt;
* import rdflib&lt;br /&gt;
* import requests&lt;br /&gt;
* import spotlight&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
=== Task 1 ===&lt;br /&gt;
Write a small program that queries the Open Notify Astros API (link below) for the people currently in space. Create a graph from the response connecting each astronaut to the craft they are currently on, for instance using http://example.com/onCraft as a property. Also as the space station is not too big, it is safe to assume that two people whom spent who time on it at the same time know each other, so add this to the graph.&lt;br /&gt;
&lt;br /&gt;
* Astros API url: http://api.open-notify.org/astros.json&lt;br /&gt;
* Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/&lt;br /&gt;
&lt;br /&gt;
The response from the API follows the format&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;message&amp;quot;: &amp;quot;success&amp;quot;,&lt;br /&gt;
    &amp;quot;number&amp;quot;: 7,&lt;br /&gt;
    &amp;quot;people&amp;quot;: [&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Sergey Ryzhikov&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;craft&amp;quot;: &amp;quot;ISS&amp;quot;,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Kate Rubins&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        ...&lt;br /&gt;
    ]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We only need to think about whats inside the list of the &amp;quot;people&amp;quot;-key.&lt;br /&gt;
To create the graph you can iteratively extract the values of craft and name and add them. As none of the names or craft is a valid URI, they can be crated using the example-namespace.&lt;br /&gt;
&lt;br /&gt;
=== Task 2 ===&lt;br /&gt;
Serialise the graph to JSON-LD, set the context of the JSON-LD object to represent the properties for knows and onCraft.&lt;br /&gt;
&lt;br /&gt;
To do this you need to pip install the json-ld portion of rdflib if you have not already:&lt;br /&gt;
&lt;br /&gt;
pip install rdflib-jsonld&lt;br /&gt;
&lt;br /&gt;
=== If you have more time ===&lt;br /&gt;
DBpedia Spotlight is a tool for automatically annotating mentions of DBpedia resources in text, providing a solution for linking unstructured information sources to the Linked Open Data cloud through DBpedia.&lt;br /&gt;
&lt;br /&gt;
Build upon the program using the DBpedia Spotlight API (example code below) to use a DBpedia-resource in your graph if one is available. You can add some simple error-handling for cases where no DBpedia resource is found - use an example-entity in stead. Keep in mind that some resources may represent other people with the same name, so try to change the types-parameter so you only get astronauts in return, the confidence-parameter might also help you with this.&lt;br /&gt;
&lt;br /&gt;
The response from DBpedia Spotlight is a list of dictionaries, where each dictionary contains the URI of the resource, its types and some other metadata we will not use now. Set the type of the resouce to the types listed in the response.&lt;br /&gt;
&lt;br /&gt;
==== Example code for DBpedia Spotlight query ====&lt;br /&gt;
First pip install pyspotlight&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import spotlight&lt;br /&gt;
&lt;br /&gt;
SERVER = &amp;quot;https://api.dbpedia-spotlight.org/en/annotate&amp;quot;&lt;br /&gt;
annotations = spotlight.annotate(SERVER, &amp;quot;str_to_be_annotated&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ Reading and writing with JSON - stackabuse.com]&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples Examples]&lt;br /&gt;
* [https://realpython.com/python-requests/ Requests - realpython.com]&lt;br /&gt;
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1554</id>
		<title>Lab: Web APIs and JSON-LD</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Web_APIs_and_JSON-LD&amp;diff=1554"/>
		<updated>2021-03-09T08:44:17Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 7: Accessing and lifting Web APIs (RESTful web services)=&lt;br /&gt;
&lt;br /&gt;
==Topics== &lt;br /&gt;
Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD.&lt;br /&gt;
&lt;br /&gt;
We will use Web APIs to retrieve regular JSON data, parse them programmatically, where possible link the resources to established DBpedia ones and finally create a RDFLib graph with the data.&lt;br /&gt;
&lt;br /&gt;
==Imports==&lt;br /&gt;
* import json&lt;br /&gt;
* import rdflib&lt;br /&gt;
* import requests&lt;br /&gt;
* import spotlight&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
=== Task 1 ===&lt;br /&gt;
Write a small program that queries the Open Notify Astros API (link below) for the people currently in space. Create a graph from the response connecting each astronaut to the craft they are currently on, for instance using http://example.com/onCraft as a property. Also as the space station is not too big, it is safe to assume that two people whom spent who time on it at the same time know each other, so add this to the graph.&lt;br /&gt;
&lt;br /&gt;
* Astros API url: http://api.open-notify.org/astros.json&lt;br /&gt;
* Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/&lt;br /&gt;
&lt;br /&gt;
=== Task 2 ===&lt;br /&gt;
Serialise the graph to JSON-LD, set the context of the JSON-LD object to represent the properties for knows and onCraft.&lt;br /&gt;
&lt;br /&gt;
To do this you need to pip install the json-ld portion of rdflib if you have not already:&lt;br /&gt;
&lt;br /&gt;
pip install rdflib-jsonld&lt;br /&gt;
&lt;br /&gt;
=== If you have more time ===&lt;br /&gt;
DBpedia Spotlight is a tool for automatically annotating mentions of DBpedia resources in text, providing a solution for linking unstructured information sources to the Linked Open Data cloud through DBpedia.&lt;br /&gt;
&lt;br /&gt;
Build upon the program using the DBpedia Spotlight API (example code below) to use a DBpedia-resource in your graph if one is available. You can add some simple error-handling for cases where no DBpedia resource is found - use an example-entity in stead. Keep in mind that some resources may represent other people with the same name, so try to change the types-parameter so you only get astronauts in return, the confidence-parameter might also help you with this.&lt;br /&gt;
&lt;br /&gt;
The response from DBpedia Spotlight is a list of dictionaries, where each dictionary contains the URI of the resource, its types and some other metadata we will not use now. Set the type of the resouce to the types listed in the response.&lt;br /&gt;
&lt;br /&gt;
==== Example code for DBpedia Spotlight query ====&lt;br /&gt;
First pip install pyspotlight&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import spotlight&lt;br /&gt;
&lt;br /&gt;
SERVER = &amp;quot;https://api.dbpedia-spotlight.org/en/annotate&amp;quot;&lt;br /&gt;
annotations = spotlight.annotate(SERVER, &amp;quot;str_to_be_annotated&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ Reading and writing with JSON - stackabuse.com]&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples Examples]&lt;br /&gt;
* [https://realpython.com/python-requests/ Requests - realpython.com]&lt;br /&gt;
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_OWL_1&amp;diff=1528</id>
		<title>Lab: OWL 1</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_OWL_1&amp;diff=1528"/>
		<updated>2021-03-04T15:47:49Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lab 6: OWL 1 (&amp;quot;RDFS Plus / Basic OWL&amp;quot;)=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
Basic OWL ontology programming with RDFlib and owlrl.&lt;br /&gt;
&lt;br /&gt;
WebVOWL visualisation.&lt;br /&gt;
&lt;br /&gt;
RDF and RDFS might be relevant too.&lt;br /&gt;
&lt;br /&gt;
==Classes/Vocabularies==&lt;br /&gt;
&lt;br /&gt;
Vocabulary:&lt;br /&gt;
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)&lt;br /&gt;
* OWL (SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty)&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&#039;&#039;&#039;Task 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Write OWL triples that corresponds to the following text. &#039;&#039;&#039;.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.&lt;br /&gt;
&lt;br /&gt;
Cade and Emma are two different persons. &amp;lt;!-- All the countries mentioned above are different. --&amp;gt; The country USA above is&lt;br /&gt;
the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames&lt;br /&gt;
resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Emma resources)&lt;br /&gt;
in your graph is the same as FOAF&#039;s, schema.org&#039;s and AKT&#039;s person classes (they are http://xmlns.com/foaf/0.1/Person,&lt;br /&gt;
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&lt;br /&gt;
you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is&lt;br /&gt;
a subproperty of VCard&#039;s postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can&lt;br /&gt;
have the same postal code. The property you have used for Emma living in Valencia is the same property as FOAF&#039;s&lt;br /&gt;
based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia&#039;s hometown property&lt;br /&gt;
(http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but &amp;quot;hometown&amp;quot; is perhaps the&lt;br /&gt;
inverse of a subproperty of &amp;quot;based near&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 2&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.Cade, ex.married, ex.Mary))&lt;br /&gt;
g.add((ex.Cade, ex.livesWith, ex.Mary))&lt;br /&gt;
g.add((ex.Cade, ex.sibling, ex.Andrew))&lt;br /&gt;
g.add((ex.Cade, ex.hasFather, ex.Bob))&lt;br /&gt;
g.add((ex.Bob, ex.fatherOf, ex.Cade))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
e.g&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.married, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Print/Serialize the ontology. Then use owlrl like seen below to infer additional triples. Can you spot the many inferences?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# These three lines add inferred triples to the graph.&lt;br /&gt;
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
owl.closure()&lt;br /&gt;
owl.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/File:S06-OWL-1.pdf Lecture Notes]&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples#RDFS_Plus_.2F_OWL_inference_with_RDFLib Example page]&lt;br /&gt;
&lt;br /&gt;
==Triples you can extend for the tasks==&lt;br /&gt;
&#039;&#039;&#039;Python&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import owlrl&lt;br /&gt;
from rdflib import Graph, Namespace, Literal, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, RDFS, XSD, FOAF, OWL&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
&lt;br /&gt;
# Namespaces&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
dbp = Namespace(&amp;quot;http://dbpedia.org/resource/&amp;quot;)&lt;br /&gt;
geo = Namespace(&amp;quot;http://sws.geonames.org/&amp;quot;)&lt;br /&gt;
schema = Namespace(&amp;quot;https://schema.org/&amp;quot;)&lt;br /&gt;
akt = Namespace(&amp;quot;http://www.aktors.org/ontology/portal#&amp;quot;)&lt;br /&gt;
vcard = Namespace(&amp;quot;http://www.w3.org/2006/vcard/ns#&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;owl&amp;quot;, OWL)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# RDFS Tasks from last time.&lt;br /&gt;
g.add((ex.Cade, ex.degreeFrom, ex.University_of_California))&lt;br /&gt;
g.add((ex.Emma, ex.degreeFrom, ex.University_of_Valencia))&lt;br /&gt;
g.add((ex.Cade, ex.degreeSubject, ex.Biology))&lt;br /&gt;
g.add((ex.Emma, ex.degreeSubject, ex.Chemistry))&lt;br /&gt;
g.add((ex.University_of_California, RDF.type, ex.University))&lt;br /&gt;
g.add((ex.University_of_Valencia, RDF.type, ex.University))&lt;br /&gt;
g.add((ex.University, RDFS.subClassOf, ex.Higher_Education_Institution))&lt;br /&gt;
g.add((ex.expertise, RDFS.range, ex.Subject))&lt;br /&gt;
g.add((ex.expertise, RDFS.domain, FOAF.Person))&lt;br /&gt;
g.add((ex.degreeSubject, RDFS.subPropertyOf, ex.expertise))&lt;br /&gt;
g.add((ex.graduated, RDFS.range, ex.Higher_Education_Institution))&lt;br /&gt;
g.add((ex.graduated, RDFS.domain, FOAF.Person))&lt;br /&gt;
g.add((ex.degreeFrom, RDFS.subPropertyOf, ex.graduated))&lt;br /&gt;
g.add((ex.Biology, RDFS.label, Literal(&amp;quot;Biology&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
g.add((ex.Biology, RDFS.label, Literal(&amp;quot;La Biologie&amp;quot;, lang=&amp;quot;fr&amp;quot;)))&lt;br /&gt;
g.add((ex.Biology, RDFS.comment, Literal(&amp;quot;Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy.&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.label, Literal(&amp;quot;Chemistry&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.label, Literal(&amp;quot;La Chimie&amp;quot;, lang=&amp;quot;fr&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.comment, Literal(&amp;quot;Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter.&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
# Write OWL triples here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Turtle&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
@prefix ex: &amp;lt;http://example.org/&amp;gt; .&lt;br /&gt;
@prefix rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
ex:Cade ex:degreeFrom ex:University_of_California ;&lt;br /&gt;
    ex:degreeSubject ex:Biology .&lt;br /&gt;
&lt;br /&gt;
ex:Emma ex:degreeFrom ex:University_of_Valencia ;&lt;br /&gt;
    ex:degreeSubject ex:Chemistry .&lt;br /&gt;
&lt;br /&gt;
ex:degreeFrom rdfs:subPropertyOf ex:graduated .&lt;br /&gt;
&lt;br /&gt;
ex:degreeSubject rdfs:subPropertyOf ex:expertise .&lt;br /&gt;
&lt;br /&gt;
ex:Biology rdfs:label &amp;quot;Biology&amp;quot;@en,&lt;br /&gt;
        &amp;quot;La Biologie&amp;quot;@fr ;&lt;br /&gt;
    rdfs:comment &amp;quot;Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy.&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
ex:Chemistry rdfs:label &amp;quot;Chemistry&amp;quot;@en,&lt;br /&gt;
        &amp;quot;La Chimie&amp;quot;@fr ;&lt;br /&gt;
    rdfs:comment &amp;quot;Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter.&amp;quot;@en .&lt;br /&gt;
&lt;br /&gt;
ex:University_of_California a ex:University .&lt;br /&gt;
&lt;br /&gt;
ex:University_of_Valencia a ex:University .&lt;br /&gt;
&lt;br /&gt;
ex:expertise rdfs:domain &amp;lt;http://xmlns.com/foaf/0.1/Person&amp;gt; ;&lt;br /&gt;
    rdfs:range ex:Subject .&lt;br /&gt;
&lt;br /&gt;
ex:graduated rdfs:domain &amp;lt;http://xmlns.com/foaf/0.1/Person&amp;gt; ;&lt;br /&gt;
    rdfs:range ex:Higher_Education_Institution .&lt;br /&gt;
&lt;br /&gt;
ex:University rdfs:subClassOf ex:Higher_Education_Institution .&lt;br /&gt;
&lt;br /&gt;
ex:Cade a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Berkeley ;&lt;br /&gt;
            ex:country ex:USA ;&lt;br /&gt;
            ex:postalCode &amp;quot;94709&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:state ex:California ;&lt;br /&gt;
            ex:street &amp;quot;1516_Henry_Street&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 27 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Biology ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Bachelor&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_California ;&lt;br /&gt;
            ex:year &amp;quot;2011-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:interest ex:Bird,&lt;br /&gt;
        ex:Ecology,&lt;br /&gt;
        ex:Environmentalism,&lt;br /&gt;
        ex:Photography,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:married ex:Mary ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ex:Canada,&lt;br /&gt;
        ex:France,&lt;br /&gt;
        ex:Germany ;&lt;br /&gt;
    foaf:knows ex:Emma ;&lt;br /&gt;
    ex:name &amp;quot;Cade_Tracey&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Mary a ex:Student;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:interest ex:Biology,&lt;br /&gt;
        ex:Chocolate,&lt;br /&gt;
        ex:Hiking .&lt;br /&gt;
&lt;br /&gt;
ex:Emma a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Valencia ;&lt;br /&gt;
            ex:country ex:Spain ;&lt;br /&gt;
            ex:postalCode &amp;quot;46020&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:street &amp;quot;Carrer_de_la Guardia_Civil_20&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Chemistry ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Master&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_Valencia ;&lt;br /&gt;
            ex:year &amp;quot;2015-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:expertise ex:Air_Pollution,&lt;br /&gt;
        ex:Toxic_Waste,&lt;br /&gt;
        ex:Waste_Management,&lt;br /&gt;
        ex:Bananas ;&lt;br /&gt;
    ex:interest ex:Bike_Riding,&lt;br /&gt;
        ex:Music,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ( ex:Portugal ex:Italy ex:France ex:Germany ex:Denmark ex:Sweden ) ;&lt;br /&gt;
    ex:name &amp;quot;Emma_Dominguez&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Meeting1 a ex:Meeting ;&lt;br /&gt;
    ex:date &amp;quot;August, 2014&amp;quot;^^xsd:string ;&lt;br /&gt;
    ex:involved ex:Cade,&lt;br /&gt;
        ex:Emma ;&lt;br /&gt;
    ex:location ex:Paris .&lt;br /&gt;
&lt;br /&gt;
ex:Paris a ex:City ;&lt;br /&gt;
    ex:capitalOf ex:France ;&lt;br /&gt;
    ex:locatedIn ex:France .&lt;br /&gt;
&lt;br /&gt;
ex:France ex:capital ex:Paris .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_OWL_1&amp;diff=1527</id>
		<title>Lab: OWL 1</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_OWL_1&amp;diff=1527"/>
		<updated>2021-03-04T14:57:01Z</updated>

		<summary type="html">&lt;p&gt;Sas020: Add triples for tasks as turtle&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lab 6: OWL 1 (&amp;quot;RDFS Plus / Basic OWL&amp;quot;)=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
Basic OWL ontology programming with RDFlib and owlrl.&lt;br /&gt;
&lt;br /&gt;
WebVOWL visualisation.&lt;br /&gt;
&lt;br /&gt;
RDF and RDFS might be relevant too.&lt;br /&gt;
&lt;br /&gt;
==Classes/Vocabularies==&lt;br /&gt;
&lt;br /&gt;
Vocabulary:&lt;br /&gt;
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)&lt;br /&gt;
* OWL (SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty)&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&#039;&#039;&#039;Task 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Write OWL triples that corresponds to the following text. &#039;&#039;&#039;.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.&lt;br /&gt;
&lt;br /&gt;
Cade and Emma are two different persons. &amp;lt;!-- All the countries mentioned above are different. --&amp;gt; The country USA above is&lt;br /&gt;
the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames&lt;br /&gt;
resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Emma resources)&lt;br /&gt;
in your graph is the same as FOAF&#039;s, schema.org&#039;s and AKT&#039;s person classes (they are http://xmlns.com/foaf/0.1/Person,&lt;br /&gt;
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&lt;br /&gt;
you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is&lt;br /&gt;
a subproperty of VCard&#039;s postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can&lt;br /&gt;
have the same postal code. The property you have used for Emma living in Valencia is the same property as FOAF&#039;s&lt;br /&gt;
based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia&#039;s hometown property&lt;br /&gt;
(http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but &amp;quot;hometown&amp;quot; is perhaps the&lt;br /&gt;
inverse of a subproperty of &amp;quot;based near&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 2&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.Cade, ex.married, ex.Mary))&lt;br /&gt;
g.add((ex.Cade, ex.livesWith, ex.Mary))&lt;br /&gt;
g.add((ex.Cade, ex.sibling, ex.Andrew))&lt;br /&gt;
g.add((ex.Cade, ex.hasFather, ex.Bob))&lt;br /&gt;
g.add((ex.Bob, ex.fatherOf, ex.Cade))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
e.g&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.married, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Print/Serialize the ontology. Then use owlrl like seen below to infer additional triples. Can you spot the many inferences?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# These three lines add inferred triples to the graph.&lt;br /&gt;
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
owl.closure()&lt;br /&gt;
owl.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/File:S06-OWL-1.pdf Lecture Notes]&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples#RDFS_Plus_.2F_OWL_inference_with_RDFLib Example page]&lt;br /&gt;
&lt;br /&gt;
==Triples you can extend for the tasks==&lt;br /&gt;
&#039;&#039;&#039;Python&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import owlrl&lt;br /&gt;
from rdflib import Graph, Namespace, Literal, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, RDFS, XSD, FOAF, OWL&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
&lt;br /&gt;
# Namespaces&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
dbp = Namespace(&amp;quot;http://dbpedia.org/resource/&amp;quot;)&lt;br /&gt;
geo = Namespace(&amp;quot;http://sws.geonames.org/&amp;quot;)&lt;br /&gt;
schema = Namespace(&amp;quot;https://schema.org/&amp;quot;)&lt;br /&gt;
akt = Namespace(&amp;quot;http://www.aktors.org/ontology/portal#&amp;quot;)&lt;br /&gt;
vcard = Namespace(&amp;quot;http://www.w3.org/2006/vcard/ns#&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;owl&amp;quot;, OWL)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# RDFS Tasks from last time.&lt;br /&gt;
g.add((ex.Cade, ex.degreeFrom, ex.University_of_California))&lt;br /&gt;
g.add((ex.Emma, ex.degreeFrom, ex.University_of_Valencia))&lt;br /&gt;
g.add((ex.Cade, ex.degreeSubject, ex.Biology))&lt;br /&gt;
g.add((ex.Emma, ex.degreeSubject, ex.Chemistry))&lt;br /&gt;
g.add((ex.University_of_California, RDF.type, ex.University))&lt;br /&gt;
g.add((ex.University_of_Valencia, RDF.type, ex.University))&lt;br /&gt;
g.add((ex.University, RDFS.subClassOf, ex.Higher_Education_Institution))&lt;br /&gt;
g.add((ex.expertise, RDFS.range, ex.Subject))&lt;br /&gt;
g.add((ex.expertise, RDFS.domain, FOAF.Person))&lt;br /&gt;
g.add((ex.degreeSubject, RDFS.subPropertyOf, ex.expertise))&lt;br /&gt;
g.add((ex.graduated, RDFS.range, ex.Higher_Education_Institution))&lt;br /&gt;
g.add((ex.graduated, RDFS.domain, FOAF.Person))&lt;br /&gt;
g.add((ex.degreeFrom, RDFS.subPropertyOf, ex.graduated))&lt;br /&gt;
g.add((ex.Biology, RDFS.label, Literal(&amp;quot;Biology&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
g.add((ex.Biology, RDFS.label, Literal(&amp;quot;La Biologie&amp;quot;, lang=&amp;quot;fr&amp;quot;)))&lt;br /&gt;
g.add((ex.Biology, RDFS.comment, Literal(&amp;quot;Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy.&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.label, Literal(&amp;quot;Chemistry&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.label, Literal(&amp;quot;La Chimie&amp;quot;, lang=&amp;quot;fr&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.comment, Literal(&amp;quot;Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter.&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
# Write OWL triples here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Turtle&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
@prefix ex: &amp;lt;http://example.org/&amp;gt; .&lt;br /&gt;
@prefix rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
ex:Cade ex:degreeFrom ex:University_of_California ;&lt;br /&gt;
    ex:degreeSubject ex:Biology .&lt;br /&gt;
&lt;br /&gt;
ex:Emma ex:degreeFrom ex:University_of_Valencia ;&lt;br /&gt;
    ex:degreeSubject ex:Chemistry .&lt;br /&gt;
&lt;br /&gt;
ex:degreeFrom rdfs:subPropertyOf ex:graduated .&lt;br /&gt;
&lt;br /&gt;
ex:degreeSubject rdfs:subPropertyOf ex:expertise .&lt;br /&gt;
&lt;br /&gt;
ex:Biology rdfs:label &amp;quot;Biology&amp;quot;@en,&lt;br /&gt;
        &amp;quot;La Biologie&amp;quot;@fr ;&lt;br /&gt;
    rdfs:comment &amp;quot;Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy.&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
ex:Chemistry rdfs:label &amp;quot;Chemistry&amp;quot;@en,&lt;br /&gt;
        &amp;quot;La Chimie&amp;quot;@fr ;&lt;br /&gt;
    rdfs:comment &amp;quot;Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter.&amp;quot;@en .&lt;br /&gt;
&lt;br /&gt;
ex:University_of_California a ex:University .&lt;br /&gt;
&lt;br /&gt;
ex:University_of_Valencia a ex:University .&lt;br /&gt;
&lt;br /&gt;
ex:expertise rdfs:domain &amp;lt;http://xmlns.com/foaf/0.1/Person&amp;gt; ;&lt;br /&gt;
    rdfs:range ex:Subject .&lt;br /&gt;
&lt;br /&gt;
ex:graduated rdfs:domain &amp;lt;http://xmlns.com/foaf/0.1/Person&amp;gt; ;&lt;br /&gt;
    rdfs:range ex:Higher_Education_Institution .&lt;br /&gt;
&lt;br /&gt;
ex:University rdfs:subClassOf ex:Higher_Education_Institution .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_RDFS&amp;diff=1526</id>
		<title>Lab: RDFS</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_RDFS&amp;diff=1526"/>
		<updated>2021-03-02T13:58:53Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 5: RDFS Programming with rdflib and owlrl=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
Basic RDFS graph programming in RDFlib.&lt;br /&gt;
Entailments and axioms with owlrl.&lt;br /&gt;
&lt;br /&gt;
==Classes/Methods/Vocabularies==&lt;br /&gt;
owlrl.RDFSClosure (RDFS_Semantics, closure, flush_stored_triples)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Vocabularies: &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
RDF.type&lt;br /&gt;
&lt;br /&gt;
RDFS.subClassOf, RDFS.subPropertyOf, RDFS.domain, RDFS.range, RDFS.label, RDFS.comment, &lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
First, pip install owlrl.&lt;br /&gt;
The RDFS Vocabulary can be imported from rdflib.namespace, just like FOAF or RDF.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Consider the following Scenario:&#039;&#039;&#039;&lt;br /&gt;
&amp;quot;University of California and University of Valencia are both Universities.&lt;br /&gt;
All universities are higher education institutions (HEIs). Only persons can have an expertise, and what they have expertise in is always a subject. Only persons can graduate from a HEI. If you are a student, you are in fact a person as well. That a person is married to someone, means that they know them. Finally, if a person has a name, that name is also the label of that entity.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create RDFS triples corresponding to the text above with RDFlib&#039;&#039;&#039; - if you can, try to build on&lt;br /&gt;
your example from lab 2! &lt;br /&gt;
&lt;br /&gt;
To create the graph in python, you can just use the g.add syntax as we have done previously, or you can use the following code sample to parse a file into a graph:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace&lt;br /&gt;
import owlrl&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Create the graph&lt;br /&gt;
g = Graph()&lt;br /&gt;
&lt;br /&gt;
# Parse input data into the graph, format is dependent on the file format. Here turtle (ttl). And location is the path to the local file&lt;br /&gt;
g.parse(location=&amp;quot;input.ttl&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Using these three lines we can add automatically the inferred triples (like ex:University rdf:type ex:Higher_Education_Institute) :&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
rdfs.closure()&lt;br /&gt;
rdfs.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you have done this, try to add the following scenario to you graph as well:&lt;br /&gt;
&amp;quot;Having a degree from a HEI means that you have also graduated from that HEI. That a city is a capital of a country means that this city is located in that country. That someone was involved in a meeting, means that they have met the other participants. If someone partook in a meeting somewhere, means that they have visited that place&amp;quot;&lt;br /&gt;
To do this, you will have to swap out the line&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
with&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
rdfs = owlrl.OWLRL.OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As some of these triples require more advanced reasoning.&lt;br /&gt;
&lt;br /&gt;
Check that simple inference works -  make sure that your graph contains triples like these, even if&lt;br /&gt;
you have not asserted them explicitly:&lt;br /&gt;
* that University of California and Valencia are HEIs&lt;br /&gt;
* that Cade, Emma, and Mary are all persons&lt;br /&gt;
* that Cade and Emma have both graduated from some HEI&lt;br /&gt;
* that Cade knows Mary&lt;br /&gt;
&lt;br /&gt;
One way to check if the triples are there:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
universities = g.query(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
    ex:University_of_California rdf:type ex:Higher_Education_Institution.&lt;br /&gt;
} &lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
print(bool(universities))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rewrite some of your existing code to use rdfs:label in a triple and add an rdfs:comment to the same resource.&lt;br /&gt;
&lt;br /&gt;
==If you have more time...==&lt;br /&gt;
Create a new RDFS graph that wraps an empty graph. This graph contains only RDFS axioms. Write it out in Turtle and check that you understand  the meaning and purpose of each axiom.&lt;br /&gt;
&lt;br /&gt;
Create an RDF (not RDFS) graph that contains all the triples in your first graph (the one with all the people and universities). Subtract all the triples in the axiom graph from the people/university graph. Write it out to see that you are left with only the asserted and entailed triples and that none of the axioms remain.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Download the SKOS vocabulary from https://www.w3.org/2009/08/skos-reference/skos.rdf and save it to a file called, e.g., SKOS.rdf .&lt;br /&gt;
Use the schemagen tool (it is inside your Jena folders, for example under apache-jena-3.1.1/bin) to generate a Java class for the SKOS vocabulary. &lt;br /&gt;
You need to do this from a console window, using a command like &amp;quot;&amp;lt;path&amp;gt;/schemagen -i &amp;lt;infile.rdf&amp;gt; -o &amp;lt;outfile.java&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Copy the SKOS.java file into your project in the same package as your other Java files,  and try to use SKOS properties &lt;br /&gt;
where they fit, for example to organise the keywords for interests and expertise.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Readings==&lt;br /&gt;
*[https://wiki.uib.no/info216/index.php/File:S05-RDFS-11.pdf Lecture Notes]&lt;br /&gt;
*[https://wiki.uib.no/info216/index.php/Python_Examples Example page]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_OWL_1&amp;diff=1525</id>
		<title>Lab: OWL 1</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_OWL_1&amp;diff=1525"/>
		<updated>2021-03-02T12:37:26Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lab 6: OWL 1 (&amp;quot;RDFS Plus / Basic OWL&amp;quot;)=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
Basic OWL ontology programming with RDFlib and owlrl.&lt;br /&gt;
&lt;br /&gt;
WebVOWL visualisation.&lt;br /&gt;
&lt;br /&gt;
RDF and RDFS might be relevant too.&lt;br /&gt;
&lt;br /&gt;
==Classes/Vocabularies==&lt;br /&gt;
&lt;br /&gt;
Vocabulary:&lt;br /&gt;
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)&lt;br /&gt;
* OWL (SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty)&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&#039;&#039;&#039;Task 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Write OWL triples that corresponds to the following text. &#039;&#039;&#039;.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.&lt;br /&gt;
&lt;br /&gt;
Cade and Emma are two different persons. &amp;lt;!-- All the countries mentioned above are different. --&amp;gt; The country USA above is&lt;br /&gt;
the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames&lt;br /&gt;
resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Emma resources)&lt;br /&gt;
in your graph is the same as FOAF&#039;s, schema.org&#039;s and AKT&#039;s person classes (they are http://xmlns.com/foaf/0.1/Person,&lt;br /&gt;
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&lt;br /&gt;
you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is&lt;br /&gt;
a subproperty of VCard&#039;s postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can&lt;br /&gt;
have the same postal code. The property you have used for Emma living in Valencia is the same property as FOAF&#039;s&lt;br /&gt;
based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia&#039;s hometown property&lt;br /&gt;
(http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but &amp;quot;hometown&amp;quot; is perhaps the&lt;br /&gt;
inverse of a subproperty of &amp;quot;based near&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 2&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.Cade, ex.married, ex.Mary))&lt;br /&gt;
g.add((ex.Cade, ex.livesWith, ex.Mary))&lt;br /&gt;
g.add((ex.Cade, ex.sibling, ex.Andrew))&lt;br /&gt;
g.add((ex.Cade, ex.hasFather, ex.Bob))&lt;br /&gt;
g.add((ex.Bob, ex.fatherOf, ex.Cade))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
e.g&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.married, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Task 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Print/Serialize the ontology. Then use owlrl like seen below to infer additional triples. Can you spot the many inferences?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# These three lines add inferred triples to the graph.&lt;br /&gt;
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
owl.closure()&lt;br /&gt;
owl.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Reading==&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/File:S06-OWL-1.pdf Lecture Notes]&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.uib.no/info216/index.php/Python_Examples#RDFS_Plus_.2F_OWL_inference_with_RDFLib Example page]&lt;br /&gt;
&lt;br /&gt;
==Triples you can extend for the tasks==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import owlrl&lt;br /&gt;
from rdflib import Graph, Namespace, Literal, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, RDFS, XSD, FOAF, OWL&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
&lt;br /&gt;
# Namespaces&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
dbp = Namespace(&amp;quot;http://dbpedia.org/resource/&amp;quot;)&lt;br /&gt;
geo = Namespace(&amp;quot;http://sws.geonames.org/&amp;quot;)&lt;br /&gt;
schema = Namespace(&amp;quot;https://schema.org/&amp;quot;)&lt;br /&gt;
akt = Namespace(&amp;quot;http://www.aktors.org/ontology/portal#&amp;quot;)&lt;br /&gt;
vcard = Namespace(&amp;quot;http://www.w3.org/2006/vcard/ns#&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;owl&amp;quot;, OWL)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# RDFS Tasks from last time.&lt;br /&gt;
g.add((ex.Cade, ex.degreeFrom, ex.University_of_California))&lt;br /&gt;
g.add((ex.Emma, ex.degreeFrom, ex.University_of_Valencia))&lt;br /&gt;
g.add((ex.Cade, ex.degreeSubject, ex.Biology))&lt;br /&gt;
g.add((ex.Emma, ex.degreeSubject, ex.Chemistry))&lt;br /&gt;
g.add((ex.University_of_California, RDF.type, ex.University))&lt;br /&gt;
g.add((ex.University_of_Valencia, RDF.type, ex.University))&lt;br /&gt;
g.add((ex.University, RDFS.subClassOf, ex.Higher_Education_Institution))&lt;br /&gt;
g.add((ex.expertise, RDFS.range, ex.Subject))&lt;br /&gt;
g.add((ex.expertise, RDFS.domain, FOAF.Person))&lt;br /&gt;
g.add((ex.degreeSubject, RDFS.subPropertyOf, ex.expertise))&lt;br /&gt;
g.add((ex.graduated, RDFS.range, ex.Higher_Education_Institution))&lt;br /&gt;
g.add((ex.graduated, RDFS.domain, FOAF.Person))&lt;br /&gt;
g.add((ex.degreeFrom, RDFS.subPropertyOf, ex.graduated))&lt;br /&gt;
g.add((ex.Biology, RDFS.label, Literal(&amp;quot;Biology&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
g.add((ex.Biology, RDFS.label, Literal(&amp;quot;La Biologie&amp;quot;, lang=&amp;quot;fr&amp;quot;)))&lt;br /&gt;
g.add((ex.Biology, RDFS.comment, Literal(&amp;quot;Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy.&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.label, Literal(&amp;quot;Chemistry&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.label, Literal(&amp;quot;La Chimie&amp;quot;, lang=&amp;quot;fr&amp;quot;)))&lt;br /&gt;
g.add((ex.Chemistry, RDFS.comment, Literal(&amp;quot;Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter.&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
# Write OWL triples here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_RDFS&amp;diff=1517</id>
		<title>Lab: RDFS</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_RDFS&amp;diff=1517"/>
		<updated>2021-02-26T11:43:18Z</updated>

		<summary type="html">&lt;p&gt;Sas020: Found a solution to the chaining issue using owlrl semantics, re-added removed tasks for this.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 5: RDFS Programming with rdflib and owlrl=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
Basic RDFS graph programming in RDFlib.&lt;br /&gt;
Entailments and axioms with owlrl.&lt;br /&gt;
&lt;br /&gt;
==Classes/Methods/Vocabularies==&lt;br /&gt;
owlrl.RDFSClosure (RDFS_Semantics, closure, flush_stored_triples)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Vocabularies: &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
RDF.type&lt;br /&gt;
&lt;br /&gt;
RDFS.subClassOf, RDFS.subPropertyOf, RDFS.domain, RDFS.range, RDFS.label, RDFS.comment, &lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
First, pip install owlrl.&lt;br /&gt;
The RDFS Vocabulary can be imported from rdflib.namespace, just like FOAF or RDF.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Consider the following Scenario:&#039;&#039;&#039;&lt;br /&gt;
&amp;quot;University of California and University of Valencia are both Universities.&lt;br /&gt;
All universities are higher education institutions (HEIs). Only persons can have an expertise, and what they have expertise in is always a subject. Only persons can graduate from a HEI. If you are a student, you are in fact a person as well. That a person is married to someone, means that they know them. Finally, if a person has a name, that name is also the label of that entity.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create RDFS triples corresponding to the text above with RDFlib&#039;&#039;&#039; - if you can, try to build on&lt;br /&gt;
your example from lab 2! &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Using these three lines we can add automatically the inferred triples (like ex:University rdf:type ex:Higher_Education_Institute) :&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
rdfs.closure()&lt;br /&gt;
rdfs.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you have done this, try to add the following scenario to you graph as well:&lt;br /&gt;
&amp;quot;Having a degree from a HEI means that you have also graduated from that HEI. That a city is a capital of a country means that this city is located in that country. That someone was involved in a meeting, means that they have met the other participants. If someone partook in a meeting somewhere, means that they have visited that place&amp;quot;&lt;br /&gt;
To do this, you will have to swap out the line&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
with&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
rdfs = owlrl.OWLRL.OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As some of these triples require more advanced reasoning.&lt;br /&gt;
&lt;br /&gt;
Check that simple inference works -  make sure that your graph contains triples like these, even if&lt;br /&gt;
you have not asserted them explicitly:&lt;br /&gt;
* that University of California and Valencia are HEIs&lt;br /&gt;
* that Cade, Emma, and Mary are all persons&lt;br /&gt;
* that Cade and Emma have both graduated from some HEI&lt;br /&gt;
* that Cade knows Mary&lt;br /&gt;
&lt;br /&gt;
One way to check if the triples are there:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
universities = g.query(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
    ex:University_of_California rdf:type ex:Higher_Education_Institution.&lt;br /&gt;
} &lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
print(bool(universities))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rewrite some of your existing code to use rdfs:label in a triple and add an rdfs:comment to the same resource.&lt;br /&gt;
&lt;br /&gt;
==If you have more time...==&lt;br /&gt;
Create a new RDFS graph that wraps an empty graph. This graph contains only RDFS axioms. Write it out in Turtle and check that you understand  the meaning and purpose of each axiom.&lt;br /&gt;
&lt;br /&gt;
Create an RDF (not RDFS) graph that contains all the triples in your first graph (the one with all the people and universities). Subtract all the triples in the axiom graph from the people/university graph. Write it out to see that you are left with only the asserted and entailed triples and that none of the axioms remain.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Download the SKOS vocabulary from https://www.w3.org/2009/08/skos-reference/skos.rdf and save it to a file called, e.g., SKOS.rdf .&lt;br /&gt;
Use the schemagen tool (it is inside your Jena folders, for example under apache-jena-3.1.1/bin) to generate a Java class for the SKOS vocabulary. &lt;br /&gt;
You need to do this from a console window, using a command like &amp;quot;&amp;lt;path&amp;gt;/schemagen -i &amp;lt;infile.rdf&amp;gt; -o &amp;lt;outfile.java&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Copy the SKOS.java file into your project in the same package as your other Java files,  and try to use SKOS properties &lt;br /&gt;
where they fit, for example to organise the keywords for interests and expertise.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Readings==&lt;br /&gt;
*[https://wiki.uib.no/info216/index.php/File:S05-RDFS-11.pdf Lecture Notes]&lt;br /&gt;
*[https://wiki.uib.no/info216/index.php/Python_Examples Example page]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab_Solutions&amp;diff=1516</id>
		<title>Lab Solutions</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab_Solutions&amp;diff=1516"/>
		<updated>2021-02-26T10:45:58Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page will be updated with Python examples related to the lectures and labs. We will add more examples after each lab has ended. The first examples will use Python&#039;s RDFlib. We will introduce other relevant libraries later.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Lecture 1: Python, RDFlib, and PyCharm==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Printing the triples of the Graph in a readable way===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# The turtle format has the purpose of being more readable for humans. &lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Coding Tasks Lab 1===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace, URIRef, BNode, Literal&lt;br /&gt;
from rdflib.namespace import RDF, FOAF, XSD&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade, ex.married, ex.Mary))&lt;br /&gt;
g.add((ex.France, ex.capital, ex.Paris))&lt;br /&gt;
g.add((ex.Cade, ex.age, Literal(&amp;quot;27&amp;quot;, datatype=XSD.integer)))&lt;br /&gt;
g.add((ex.Mary, ex.age, Literal(&amp;quot;26&amp;quot;, datatype=XSD.integer)))&lt;br /&gt;
g.add((ex.Mary, ex.interest, ex.Hiking))&lt;br /&gt;
g.add((ex.Mary, ex.interest, ex.Chocolate))&lt;br /&gt;
g.add((ex.Mary, ex.interest, ex.Biology))&lt;br /&gt;
g.add((ex.Mary, RDF.type, ex.Student))&lt;br /&gt;
g.add((ex.Paris, RDF.type, ex.City))&lt;br /&gt;
g.add((ex.Paris, ex.locatedIn, ex.France))&lt;br /&gt;
g.add((ex.Cade, ex.characteristic, ex.Kind))&lt;br /&gt;
g.add((ex.Mary, ex.characteristic, ex.Kind))&lt;br /&gt;
g.add((ex.Mary, RDF.type, FOAF.Person))&lt;br /&gt;
g.add((ex.Cade, RDF.type, FOAF.Person))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Lecture 2: RDF programming==&lt;br /&gt;
&lt;br /&gt;
===Different ways to create an address===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
from rdflib import Graph, Namespace, URIRef, BNode, Literal&lt;br /&gt;
from rdflib.namespace import RDF, FOAF, XSD&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# How to represent the address of Cade Tracey. From probably the worst solution to the best.&lt;br /&gt;
&lt;br /&gt;
# Solution 1 -&lt;br /&gt;
# Make the entire address into one Literal. However, Generally we want to separate each part of an address into their own triples. This is useful for instance if we want to find only the streets where people live. &lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_Tracey, ex.livesIn, Literal(&amp;quot;1516_Henry_Street, Berkeley, California 94709, USA&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Solution 2 - &lt;br /&gt;
# Seperate the different pieces information into their own triples&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_tracey, ex.street, Literal(&amp;quot;1516_Henry_Street&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.city, Literal(&amp;quot;Berkeley&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.state, Literal(&amp;quot;California&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.zipcode, Literal(&amp;quot;94709&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.country, Literal(&amp;quot;USA&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Solution 3 - Some parts of the addresses can make more sense to be resources than Literals.&lt;br /&gt;
# Larger concepts like a city or state are typically represented as resources rather than Literals, but this is not necesarilly a requirement in the case that you don&#039;t intend to say more about them. &lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_tracey, ex.street, Literal(&amp;quot;1516_Henry_Street&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.city, ex.Berkeley))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.state, ex.California))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.zipcode, Literal(&amp;quot;94709&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.country, ex.USA))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Solution 4 &lt;br /&gt;
# Grouping of the information into an Address. We can Represent the address concept with its own URI OR with a Blank Node. &lt;br /&gt;
# One advantage of this is that we can easily remove the entire address, instead of removing each individual part of the address. &lt;br /&gt;
# Solution 4 or 5 is how I would recommend to make addresses. Here, ex.CadeAddress could also be called something like ex.address1 or so on, if you want to give each address a unique ID. &lt;br /&gt;
&lt;br /&gt;
# Address URI - CadeAdress&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_Tracey, ex.address, ex.CadeAddress))&lt;br /&gt;
g.add((ex.CadeAddress, RDF.type, ex.Address))&lt;br /&gt;
g.add((ex.CadeAddress, ex.street, Literal(&amp;quot;1516 Henry Street&amp;quot;)))&lt;br /&gt;
g.add((ex.CadeAddress, ex.city, ex.Berkeley))&lt;br /&gt;
g.add((ex.CadeAddress, ex.state, ex.California))&lt;br /&gt;
g.add((ex.CadeAddress, ex.postalCode, Literal(&amp;quot;94709&amp;quot;)))&lt;br /&gt;
g.add((ex.CadeAddress, ex.country, ex.USA))&lt;br /&gt;
&lt;br /&gt;
# OR&lt;br /&gt;
&lt;br /&gt;
# Blank node for Address.  &lt;br /&gt;
address = BNode()&lt;br /&gt;
g.add((ex.Cade_Tracey, ex.address, address))&lt;br /&gt;
g.add((address, RDF.type, ex.Address))&lt;br /&gt;
g.add((address, ex.street, Literal(&amp;quot;1516 Henry Street&amp;quot;, datatype=XSD.string)))&lt;br /&gt;
g.add((address, ex.city, ex.Berkeley))&lt;br /&gt;
g.add((address, ex.state, ex.California))&lt;br /&gt;
g.add((address, ex.postalCode, Literal(&amp;quot;94709&amp;quot;, datatype=XSD.string)))&lt;br /&gt;
g.add((address, ex.country, ex.USA))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Solution 5 using existing vocabularies for address &lt;br /&gt;
&lt;br /&gt;
# (in this case https://schema.org/PostalAddress from schema.org). &lt;br /&gt;
# Also using existing ontology for places like California. (like http://dbpedia.org/resource/California from dbpedia.org)&lt;br /&gt;
&lt;br /&gt;
schema = Namespace(&amp;quot;https://schema.org/&amp;quot;)&lt;br /&gt;
dbp = Namespace(&amp;quot;https://dpbedia.org/resource/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_Tracey, schema.address, ex.CadeAddress))&lt;br /&gt;
g.add((ex.CadeAddress, RDF.type, schema.PostalAddress))&lt;br /&gt;
g.add((ex.CadeAddress, schema.streetAddress, Literal(&amp;quot;1516 Henry Street&amp;quot;)))&lt;br /&gt;
g.add((ex.CadeAddress, schema.addresCity, dbp.Berkeley))&lt;br /&gt;
g.add((ex.CadeAddress, schema.addressRegion, dbp.California))&lt;br /&gt;
g.add((ex.CadeAddress, schema.postalCode, Literal(&amp;quot;94709&amp;quot;)))&lt;br /&gt;
g.add((ex.CadeAddress, schema.addressCountry, dbp.United_States))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Typed Literals===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace&lt;br /&gt;
from rdflib.namespace import XSD&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade, ex.age, Literal(27, datatype=XSD.integer)))&lt;br /&gt;
g.add((ex.Cade, ex.gpa, Literal(3.3, datatype=XSD.float)))&lt;br /&gt;
g.add((ex.Cade, FOAF.name, Literal(&amp;quot;Cade Tracey&amp;quot;, datatype=XSD.string)))&lt;br /&gt;
g.add((ex.Cade, ex.birthday, Literal(&amp;quot;2006-01-01&amp;quot;, datatype=XSD.date)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Writing and reading graphs/files===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
   # Writing the graph to a file on your system. Possible formats = turtle, n3, xml, nt.&lt;br /&gt;
g.serialize(destination=&amp;quot;triples.txt&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
   # Parsing a local file&lt;br /&gt;
parsed_graph = g.parse(location=&amp;quot;triples.txt&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
   # Parsing a remote endpoint like Dbpedia&lt;br /&gt;
dbpedia_graph = g.parse(&amp;quot;http://dbpedia.org/resource/Pluto&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Graph Binding===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
#Graph Binding is useful for at least two reasons:&lt;br /&gt;
#(1) We no longer need to specify prefixes with SPARQL queries if they are already binded to the graph.&lt;br /&gt;
#(2) When serializing the graph, the serialization will show the correct expected prefix &lt;br /&gt;
# instead of default namespace names ns1, ns2 etc.&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
dbp = Namespace(&amp;quot;http://dbpedia.org/resource/&amp;quot;)&lt;br /&gt;
schema = Namespace(&amp;quot;https://schema.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;dbp&amp;quot;, dbp)&lt;br /&gt;
g.bind(&amp;quot;schema&amp;quot;, schema)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Collection Example===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace&lt;br /&gt;
from rdflib.collection import Collection&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Sometimes we want to add many objects or subjects for the same predicate at once. &lt;br /&gt;
# In these cases we can use Collection() to save some time.&lt;br /&gt;
# In this case I want to add all countries that Emma has visited at once.&lt;br /&gt;
&lt;br /&gt;
b = BNode()&lt;br /&gt;
g.add((ex.Emma, ex.visit, b))&lt;br /&gt;
Collection(g, b,&lt;br /&gt;
    [ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])&lt;br /&gt;
&lt;br /&gt;
# OR&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Emma, ex.visit, ex.EmmaVisits))&lt;br /&gt;
Collection(g, ex.EmmaVisits,&lt;br /&gt;
    [ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Lecture 3: SPARQL==&lt;br /&gt;
&lt;br /&gt;
See the [[SPARQL Examples]] page!&lt;br /&gt;
&lt;br /&gt;
===Querying a local (&amp;quot;in memory&amp;quot;) graph===&lt;br /&gt;
&lt;br /&gt;
Example contents of the file family.ttl:&lt;br /&gt;
 @prefix rex: &amp;lt;http://example.org/royal#&amp;gt; .&lt;br /&gt;
 @prefix fam: &amp;lt;http://example.org/family#&amp;gt; .&lt;br /&gt;
 &lt;br /&gt;
 rex:IngridAlexandra fam:hasParent rex:HaakonMagnus .&lt;br /&gt;
 rex:SverreMagnus fam:hasParent rex:HaakonMagnus .&lt;br /&gt;
 rex:HaakonMagnus fam:hasParent rex:Harald .&lt;br /&gt;
 rex:MarthaLouise fam:hasParent rex:Harald .&lt;br /&gt;
 rex:HaakonMagnus fam:hasSister rex:MarthaLouise .&lt;br /&gt;
&lt;br /&gt;
 import rdflib&lt;br /&gt;
 &lt;br /&gt;
 g = rdflib.Graph()&lt;br /&gt;
 g.parse(&amp;quot;family.ttl&amp;quot;, format=&#039;ttl&#039;)&lt;br /&gt;
 &lt;br /&gt;
 qres = g.query(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
 PREFIX fam: &amp;lt;http://example.org/family#&amp;gt;&lt;br /&gt;
     SELECT ?child ?sister WHERE {&lt;br /&gt;
         ?child fam:hasParent ?parent .	&lt;br /&gt;
         ?parent fam:hasSister ?sister .&lt;br /&gt;
     }&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
 for row in qres:&lt;br /&gt;
     print(&amp;quot;%s has aunt %s&amp;quot; % row)&lt;br /&gt;
&lt;br /&gt;
With a prepared query, you can write the query once, and then bind some of the variables each time you use it:&lt;br /&gt;
 import rdflib&lt;br /&gt;
 &lt;br /&gt;
 g = rdflib.Graph()&lt;br /&gt;
 g.parse(&amp;quot;family.ttl&amp;quot;, format=&#039;ttl&#039;)&lt;br /&gt;
 &lt;br /&gt;
 q = rdflib.plugins.sparql.prepareQuery(&lt;br /&gt;
         &amp;quot;&amp;quot;&amp;quot;SELECT ?child ?sister WHERE {&lt;br /&gt;
                   ?child fam:hasParent ?parent .&lt;br /&gt;
                   ?parent fam:hasSister ?sister .&lt;br /&gt;
        }&amp;quot;&amp;quot;&amp;quot;,&lt;br /&gt;
        initNs = { &amp;quot;fam&amp;quot;: &amp;quot;http://example.org/family#&amp;quot;})&lt;br /&gt;
 &lt;br /&gt;
 sm = rdflib.URIRef(&amp;quot;http://example.org/royal#SverreMagnus&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 for row in g.query(q, initBindings={&#039;child&#039;: sm}):&lt;br /&gt;
         print(row)&lt;br /&gt;
&lt;br /&gt;
===Select all contents of lists (rdfllib.Collection)===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# rdflib.Collection has a different interntal structure so it requires a slightly more advance query. Here I am selecting all places that Emma has visited.&lt;br /&gt;
&lt;br /&gt;
PREFIX ex:   &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
PREFIX rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SELECT ?visit&lt;br /&gt;
WHERE {&lt;br /&gt;
  ex:Emma ex:visit/rdf:rest*/rdf:first ?visit&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Lecture 4- SPARQL PROGRAMMING==&lt;br /&gt;
&lt;br /&gt;
===Using parameters/variables in rdflib queries===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace, URIRef&lt;br /&gt;
from rdflib.plugins.sparql import prepareQuery&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade, ex.livesIn, ex.France))&lt;br /&gt;
g.add((ex.Anne, ex.livesIn, ex.Norway))&lt;br /&gt;
g.add((ex.Sofie, ex.livesIn, ex.Sweden))&lt;br /&gt;
g.add((ex.Per, ex.livesIn, ex.Norway))&lt;br /&gt;
g.add((ex.John, ex.livesIn, ex.USA))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def find_people_from_country(country):&lt;br /&gt;
        country = URIRef(ex + country)&lt;br /&gt;
        q = prepareQuery(&lt;br /&gt;
         &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
         PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
         SELECT ?person WHERE { &lt;br /&gt;
         ?person ex:livesIn ?country.&lt;br /&gt;
         }&lt;br /&gt;
         &amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
        capital_result = g.query(q, initBindings={&#039;country&#039;: country})&lt;br /&gt;
&lt;br /&gt;
        for row in capital_result:&lt;br /&gt;
            print(row)&lt;br /&gt;
&lt;br /&gt;
find_people_from_country(&amp;quot;Norway&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===SELECTING data from Blazegraph via Python===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, JSON&lt;br /&gt;
&lt;br /&gt;
# This creates a server connection to the same URL that contains the graphic interface for Blazegraph. &lt;br /&gt;
# You also need to add &amp;quot;sparql&amp;quot; to end of the URL like below.&lt;br /&gt;
&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;http://localhost:9999/blazegraph/sparql&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# SELECT all triples in the database.&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    SELECT DISTINCT ?p WHERE {&lt;br /&gt;
    ?s ?p ?o.&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;p&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&lt;br /&gt;
# SELECT all interests of Cade&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
    SELECT DISTINCT ?interest WHERE {&lt;br /&gt;
    ex:Cade ex:interest ?interest.&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;interest&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Updating data from Blazegraph via Python===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, POST, DIGEST&lt;br /&gt;
&lt;br /&gt;
namespace = &amp;quot;kb&amp;quot;&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;http://localhost:9999/blazegraph/namespace/&amp;quot;+ namespace + &amp;quot;/sparql&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setMethod(POST)&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
    INSERT DATA{&lt;br /&gt;
    ex:Cade ex:interest ex:Mathematics.&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
results = sparql.query()&lt;br /&gt;
print(results.response.read())&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===Retrieving data from Wikidata with SparqlWrapper===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, JSON&lt;br /&gt;
&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;https://query.wikidata.org/sparql&amp;quot;)&lt;br /&gt;
# In the query I want to select all the Vitamins in wikidata.&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    SELECT ?nutrient ?nutrientLabel WHERE&lt;br /&gt;
{&lt;br /&gt;
  ?nutrient wdt:P279 wd:Q34956.&lt;br /&gt;
  SERVICE wikibase:label { bd:serviceParam wikibase:language &amp;quot;[AUTO_LANGUAGE],en&amp;quot;. }&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;nutrient&amp;quot;][&amp;quot;value&amp;quot;], &amp;quot;   &amp;quot;, result[&amp;quot;nutrientLabel&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More examples can be found in the example section on the official query service here: https://query.wikidata.org/.&lt;br /&gt;
&lt;br /&gt;
===Download from BlazeGraph===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
Dumps a database to a local RDF file.&lt;br /&gt;
You need to install the SPARQLWrapper package first...&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
import datetime&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, RDFXML&lt;br /&gt;
&lt;br /&gt;
# your namespace, the default is &#039;kb&#039;&lt;br /&gt;
ns = &#039;kb&#039;&lt;br /&gt;
&lt;br /&gt;
# the SPARQL endpoint&lt;br /&gt;
endpoint = &#039;http://info216.i2s.uib.no/bigdata/namespace/&#039; + ns + &#039;/sparql&#039;&lt;br /&gt;
&lt;br /&gt;
# - the endpoint just moved, the old one was:&lt;br /&gt;
# endpoint = &#039;http://i2s.uib.no:8888/bigdata/namespace/&#039; + ns + &#039;/sparql&#039;&lt;br /&gt;
&lt;br /&gt;
# create wrapper&lt;br /&gt;
wrapper = SPARQLWrapper(endpoint)&lt;br /&gt;
&lt;br /&gt;
# prepare the SPARQL update&lt;br /&gt;
wrapper.setQuery(&#039;CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }&#039;)&lt;br /&gt;
wrapper.setReturnFormat(RDFXML)&lt;br /&gt;
&lt;br /&gt;
# execute the SPARQL update and convert the result to an rdflib.Graph &lt;br /&gt;
graph = wrapper.query().convert()&lt;br /&gt;
&lt;br /&gt;
# the destination file, with code to make it timestamped&lt;br /&gt;
destfile = &#039;rdf_dumps/slr-kg4news-&#039; + datetime.datetime.now().strftime(&#039;%Y%m%d-%H%M&#039;) + &#039;.rdf&#039;&lt;br /&gt;
&lt;br /&gt;
# serialize the result to file&lt;br /&gt;
graph.serialize(destination=destfile, format=&#039;ttl&#039;)&lt;br /&gt;
&lt;br /&gt;
# report and quit&lt;br /&gt;
print(&#039;Wrote %u triples to file %s .&#039; %&lt;br /&gt;
      (len(res), destfile))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Query Dbpedia with SparqlWrapper===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, JSON&lt;br /&gt;
&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;http://dbpedia.org/sparql&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    PREFIX dbr: &amp;lt;http://dbpedia.org/resource/&amp;gt;&lt;br /&gt;
    PREFIX dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt;&lt;br /&gt;
    PREFIX rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt;&lt;br /&gt;
    SELECT ?comment&lt;br /&gt;
    WHERE {&lt;br /&gt;
    dbr:Barack_Obama rdfs:comment ?comment.&lt;br /&gt;
    FILTER (langMatches(lang(?comment),&amp;quot;en&amp;quot;))&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;comment&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lecture 5: RDFS==&lt;br /&gt;
&lt;br /&gt;
===RDFS-plus (OWL) Properties===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.married, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
g.add((ex.married, RDF.type, OWL.IrreflexiveProperty))&lt;br /&gt;
g.add((ex.livesWith, RDF.type, OWL.ReflexiveProperty))&lt;br /&gt;
g.add((ex.livesWith, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
g.add((ex.sibling, RDF.type, OWL.TransitiveProperty))&lt;br /&gt;
g.add((ex.sibling, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
g.add((ex.sibling, RDF.type, OWL.IrreflexiveProperty))&lt;br /&gt;
g.add((ex.hasFather, RDF.type, OWL.FunctionalProperty))&lt;br /&gt;
g.add((ex.hasFather, RDF.type, OWL.AsymmetricProperty))&lt;br /&gt;
g.add((ex.hasFather, RDF.type, OWL.IrreflexiveProperty))&lt;br /&gt;
g.add((ex.fatherOf, RDF.type, OWL.AsymmetricProperty))&lt;br /&gt;
g.add((ex.fatherOf, RDF.type, OWL.IrreflexiveProperty))&lt;br /&gt;
&lt;br /&gt;
# Sometimes there is no definite answer, and it comes down to how we want to model our properties&lt;br /&gt;
# e.g is livesWith a transitive property? Usually yes, but we can also want to specify that a child lives with both of her divorced parents.&lt;br /&gt;
# which means that: (mother livesWith child % child livesWith father) != mother livesWith father. Which makes it non-transitive.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RDFS inference with RDFLib===&lt;br /&gt;
You can use the OWL-RL package to add inference capabilities to RDFLib. It can be installed using the pip install command:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
pip install owlrl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Or download it from [https://github.com/RDFLib/OWL-RL GitHub] and copy the &#039;&#039;owlrl&#039;&#039; subfolder into your project folder next to your Python files.&lt;br /&gt;
&lt;br /&gt;
[https://owl-rl.readthedocs.io/en/latest/owlrl.html OWL-RL documentation.]&lt;br /&gt;
&lt;br /&gt;
Example program to get you started. In this example we are creating the graph using sparql.update, but it is also possible to parse the data from a file.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import rdflib.plugins.sparql.update&lt;br /&gt;
import owlrl.RDFSClosure&lt;br /&gt;
&lt;br /&gt;
g = rdflib.Graph()&lt;br /&gt;
&lt;br /&gt;
ex = rdflib.Namespace(&#039;http://example.org#&#039;)&lt;br /&gt;
g.bind(&#039;&#039;, ex)&lt;br /&gt;
&lt;br /&gt;
g.update(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org#&amp;gt;&lt;br /&gt;
PREFIX owl: &amp;lt;http://www.w3.org/2002/07/owl#&amp;gt;&lt;br /&gt;
INSERT DATA {&lt;br /&gt;
    ex:Socrates rdf:type ex:Man .&lt;br /&gt;
    ex:Man rdfs:subClassOf ex:Mortal .&lt;br /&gt;
}&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
# RDF_Semantics parameters:	&lt;br /&gt;
# - graph (rdflib.Graph) – The RDF graph to be extended.&lt;br /&gt;
# - axioms (bool) – Whether (non-datatype) axiomatic triples should be added or not.&lt;br /&gt;
# - daxioms (bool) – Whether datatype axiomatic triples should be added or not.&lt;br /&gt;
# - rdfs (bool) – Whether RDFS inference is also done (used in subclassed only).&lt;br /&gt;
# For now, you will in most cases use all False in RDFS_Semtantics.&lt;br /&gt;
&lt;br /&gt;
# Generates the closure of the graph - generates the new entailed triples, but does not add them to the graph.&lt;br /&gt;
rdfs.closure()&lt;br /&gt;
# Adds the new triples to the graph and empties the RDFS triple-container.&lt;br /&gt;
rdfs.flush_stored_triples()&lt;br /&gt;
&lt;br /&gt;
# Ask-query to check whether a new triple has been generated from the entailment.&lt;br /&gt;
b = g.query(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org#&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
    ex:Socrates rdf:type ex:Mortal .&lt;br /&gt;
} &lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
print(&#039;Result: &#039; + bool(b))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Languaged tagged RDFS labels=== &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace, Literal&lt;br /&gt;
from rdflib.namespace import RDFS&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.France, RDFS.label, Literal(&amp;quot;Frankrike&amp;quot;, lang=&amp;quot;no&amp;quot;)))&lt;br /&gt;
g.add((ex.France, RDFS.label, Literal(&amp;quot;France&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
g.add((ex.France, RDFS.label, Literal(&amp;quot;Francia&amp;quot;, lang=&amp;quot;es&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lecture 6: RDFS Plus / OWL == &lt;br /&gt;
===RDFS Plus / OWL inference with RDFLib=== &lt;br /&gt;
&lt;br /&gt;
You can use the OWL-RL package again as for Lecture 5.&lt;br /&gt;
&lt;br /&gt;
Instead of: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# The next three lines add inferred triples to g.&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
rdfs.closure()&lt;br /&gt;
rdfs.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
you can write this to get both RDFS and basic RDFS Plus / OWL inference:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# The next three lines add inferred triples to g.&lt;br /&gt;
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
owl.closure()&lt;br /&gt;
owl.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example updates and queries:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
PREFIX rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt;&lt;br /&gt;
PREFIX owl: &amp;lt;http://www.w3.org/2002/07/owl#&amp;gt;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org#&amp;gt;&lt;br /&gt;
&lt;br /&gt;
INSERT DATA {&lt;br /&gt;
    ex:Socrates ex:hasWife ex:Xanthippe .&lt;br /&gt;
    ex:hasHusband owl:inverseOf ex:hasWife .&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
   ex:Xanthippe ex:hasHusband ex:Socrates .&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
   ex:Socrates ^ex:hasHusband ex:Xanthippe .&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
INSERT DATA {&lt;br /&gt;
    ex:hasWife rdfs:subPropertyOf ex:hasSpouse .&lt;br /&gt;
    ex:hasSpouse rdf:type owl:SymmetricProperty . &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
   ex:Socrates ex:hasSpouse ex:Xanthippe .&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
   ex:Socrates ^ex:hasSpouse ex:Xanthippe .&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Semantic Lifting - CSV==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, FOAF, RDFS, OWL&lt;br /&gt;
import pandas as pd&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
&lt;br /&gt;
# Load the CSV data as a pandas Dataframe.&lt;br /&gt;
csv_data = pd.read_csv(&amp;quot;task1.csv&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Here I deal with spaces (&amp;quot; &amp;quot;) in the data. I replace them with &amp;quot;_&amp;quot; so that URI&#039;s become valid.&lt;br /&gt;
csv_data = csv_data.replace(to_replace=&amp;quot; &amp;quot;, value=&amp;quot;_&amp;quot;, regex=True)&lt;br /&gt;
&lt;br /&gt;
# Here I mark all missing/empty data as &amp;quot;unknown&amp;quot;. This makes it easy to delete triples containing this later.&lt;br /&gt;
csv_data = csv_data.fillna(&amp;quot;unknown&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Loop through the CSV data, and then make RDF triples.&lt;br /&gt;
for index, row in csv_data.iterrows():&lt;br /&gt;
    # The names of the people act as subjects.&lt;br /&gt;
    subject = row[&#039;Name&#039;]&lt;br /&gt;
    # Create triples: e.g. &amp;quot;Cade_Tracey - age - 27&amp;quot;&lt;br /&gt;
    g.add((URIRef(ex + subject), URIRef(ex + &amp;quot;age&amp;quot;), Literal(row[&amp;quot;Age&amp;quot;])))&lt;br /&gt;
    g.add((URIRef(ex + subject), URIRef(ex + &amp;quot;married&amp;quot;), URIRef(ex + row[&amp;quot;Spouse&amp;quot;])))&lt;br /&gt;
    g.add((URIRef(ex + subject), URIRef(ex + &amp;quot;country&amp;quot;), URIRef(ex + row[&amp;quot;Country&amp;quot;])))&lt;br /&gt;
&lt;br /&gt;
    # If We want can add additional RDF/RDFS/OWL information e.g&lt;br /&gt;
    g.add((URIRef(ex + subject), RDF.type, FOAF.Person))&lt;br /&gt;
&lt;br /&gt;
# I remove triples that I marked as unknown earlier.&lt;br /&gt;
g.remove((None, None, URIRef(&amp;quot;http://example.org/unknown&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
# Clean printing of the graph.&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===CSV file for above example===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;quot;Name&amp;quot;,&amp;quot;Age&amp;quot;,&amp;quot;Spouse&amp;quot;,&amp;quot;Country&amp;quot;&lt;br /&gt;
&amp;quot;Cade Tracey&amp;quot;,&amp;quot;26&amp;quot;,&amp;quot;Mary Jackson&amp;quot;,&amp;quot;US&amp;quot;&lt;br /&gt;
&amp;quot;Bob Johnson&amp;quot;,&amp;quot;21&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;Canada&amp;quot;&lt;br /&gt;
&amp;quot;Mary Jackson&amp;quot;,&amp;quot;25&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;France&amp;quot;&lt;br /&gt;
&amp;quot;Phil Philips&amp;quot;,&amp;quot;32&amp;quot;,&amp;quot;Catherine Smith&amp;quot;,&amp;quot;Japan&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Semantic Lifting - XML==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, XSD, RDFS&lt;br /&gt;
import xml.etree.ElementTree as ET&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/TV/&amp;quot;)&lt;br /&gt;
prov = Namespace(&amp;quot;http://www.w3.org/ns/prov#&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;prov&amp;quot;, prov)&lt;br /&gt;
&lt;br /&gt;
tree = ET.parse(&amp;quot;tv_shows.xml&amp;quot;)&lt;br /&gt;
root = tree.getroot()&lt;br /&gt;
&lt;br /&gt;
for tv_show in root.findall(&#039;tv_show&#039;):&lt;br /&gt;
    show_id = tv_show.attrib[&amp;quot;id&amp;quot;]&lt;br /&gt;
    title = tv_show.find(&amp;quot;title&amp;quot;).text&lt;br /&gt;
&lt;br /&gt;
    g.add((URIRef(ex + show_id), ex.title, Literal(title, datatype=XSD.string)))&lt;br /&gt;
    g.add((URIRef(ex + show_id), RDF.type, ex.TV_Show))&lt;br /&gt;
&lt;br /&gt;
    for actor in tv_show.findall(&amp;quot;actor&amp;quot;):&lt;br /&gt;
        first_name = actor.find(&amp;quot;firstname&amp;quot;).text&lt;br /&gt;
        last_name = actor.find(&amp;quot;lastname&amp;quot;).text&lt;br /&gt;
        full_name = first_name + &amp;quot;_&amp;quot; + last_name&lt;br /&gt;
        &lt;br /&gt;
        g.add((URIRef(ex + show_id), ex.stars, URIRef(ex + full_name)))&lt;br /&gt;
        g.add((URIRef(ex + full_name), ex.starsIn, URIRef(title)))&lt;br /&gt;
        g.add((URIRef(ex + full_name), RDF.type, ex.Actor))&lt;br /&gt;
&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===XML Data for above example===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;data&amp;gt;&lt;br /&gt;
    &amp;lt;tv_show id=&amp;quot;1050&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;title&amp;gt;The_Sopranos&amp;lt;/title&amp;gt;&lt;br /&gt;
        &amp;lt;actor&amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;James&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Gandolfini&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/actor&amp;gt;&lt;br /&gt;
    &amp;lt;/tv_show&amp;gt;&lt;br /&gt;
    &amp;lt;tv_show id=&amp;quot;1066&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;title&amp;gt;Seinfeld&amp;lt;/title&amp;gt;&lt;br /&gt;
        &amp;lt;actor&amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Jerry&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Seinfeld&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/actor&amp;gt;&lt;br /&gt;
        &amp;lt;actor&amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Julia&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Louis-dreyfus&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/actor&amp;gt;&lt;br /&gt;
        &amp;lt;actor&amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Jason&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Alexander&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/actor&amp;gt;&lt;br /&gt;
    &amp;lt;/tv_show&amp;gt;&lt;br /&gt;
&amp;lt;/data&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Semantic Lifting - HTML==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from bs4 import BeautifulSoup as bs, NavigableString&lt;br /&gt;
from rdflib import Graph, URIRef, Namespace&lt;br /&gt;
from rdflib.namespace import RDF&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
&lt;br /&gt;
html = open(&amp;quot;tv_shows.html&amp;quot;).read()&lt;br /&gt;
html = bs(html, features=&amp;quot;html.parser&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
shows = html.find_all(&#039;li&#039;, attrs={&#039;class&#039;: &#039;show&#039;})&lt;br /&gt;
for show in shows:&lt;br /&gt;
    title = show.find(&amp;quot;h3&amp;quot;).text&lt;br /&gt;
    actors = show.find(&#039;ul&#039;, attrs={&#039;class&#039;: &#039;actor_list&#039;})&lt;br /&gt;
    for actor in actors:&lt;br /&gt;
        if isinstance(actor, NavigableString):&lt;br /&gt;
            continue&lt;br /&gt;
        else:&lt;br /&gt;
            actor = actor.text.replace(&amp;quot; &amp;quot;, &amp;quot;_&amp;quot;)&lt;br /&gt;
            g.add((URIRef(ex + title), ex.stars, URIRef(ex + actor)))&lt;br /&gt;
            g.add((URIRef(ex + actor), RDF.type, ex.Actor))&lt;br /&gt;
&lt;br /&gt;
    g.add((URIRef(ex + title), RDF.type, ex.TV_Show))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTML code for the example above===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE html&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
    &amp;lt;meta charset=&amp;quot;utf-8&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
    &amp;lt;div class=&amp;quot;tv_shows&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;ul&amp;gt;&lt;br /&gt;
            &amp;lt;li class=&amp;quot;show&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;h3&amp;gt;The_Sopranos&amp;lt;/h3&amp;gt;&lt;br /&gt;
                &amp;lt;div class=&amp;quot;irrelevant_data&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
                &amp;lt;ul class=&amp;quot;actor_list&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;li&amp;gt;James Gandolfini&amp;lt;/li&amp;gt;&lt;br /&gt;
                &amp;lt;/ul&amp;gt;&lt;br /&gt;
            &amp;lt;/li&amp;gt;&lt;br /&gt;
            &amp;lt;li class=&amp;quot;show&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;h3&amp;gt;Seinfeld&amp;lt;/h3&amp;gt;&lt;br /&gt;
                &amp;lt;div class=&amp;quot;irrelevant_data&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
                &amp;lt;ul class=&amp;quot;actor_list&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;li &amp;gt;Jerry Seinfeld&amp;lt;/li&amp;gt;&lt;br /&gt;
                    &amp;lt;li&amp;gt;Jason Alexander&amp;lt;/li&amp;gt;&lt;br /&gt;
                    &amp;lt;li&amp;gt;Julia Louis-Dreyfus&amp;lt;/li&amp;gt;&lt;br /&gt;
                &amp;lt;/ul&amp;gt;&lt;br /&gt;
            &amp;lt;/li&amp;gt;&lt;br /&gt;
        &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==WEB API Calls (In this case JSON)==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import requests&lt;br /&gt;
import json&lt;br /&gt;
import pprint&lt;br /&gt;
&lt;br /&gt;
# Retrieve JSON data from API service URL. Then load it with the json library as a json object.&lt;br /&gt;
url = &amp;quot;http://api.geonames.org/postalCodeLookupJSON?postalcode=46020&amp;amp;#country=ES&amp;amp;username=demo&amp;quot;&lt;br /&gt;
data = requests.get(url).content.decode(&amp;quot;utf-8&amp;quot;)&lt;br /&gt;
data = json.loads(data)&lt;br /&gt;
pprint.pprint(data)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==JSON-LD==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import rdflib&lt;br /&gt;
&lt;br /&gt;
g = rdflib.Graph()&lt;br /&gt;
&lt;br /&gt;
example = &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;@context&amp;quot;: {&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;http://xmlns.com/foaf/0.1/name&amp;quot;,&lt;br /&gt;
    &amp;quot;homepage&amp;quot;: {&lt;br /&gt;
      &amp;quot;@id&amp;quot;: &amp;quot;http://xmlns.com/foaf/0.1/homepage&amp;quot;,&lt;br /&gt;
      &amp;quot;@type&amp;quot;: &amp;quot;@id&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;@id&amp;quot;: &amp;quot;http://me.markus-lanthaler.com/&amp;quot;,&lt;br /&gt;
  &amp;quot;name&amp;quot;: &amp;quot;Markus Lanthaler&amp;quot;,&lt;br /&gt;
  &amp;quot;homepage&amp;quot;: &amp;quot;http://www.markus-lanthaler.com/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# json-ld parsing automatically deals with @contexts&lt;br /&gt;
g.parse(data=example, format=&#039;json-ld&#039;)&lt;br /&gt;
&lt;br /&gt;
# serialisation does expansion by default&lt;br /&gt;
for line in g.serialize(format=&#039;json-ld&#039;).decode().splitlines():&lt;br /&gt;
    print(line)&lt;br /&gt;
&lt;br /&gt;
# by supplying a context object, serialisation can do compaction&lt;br /&gt;
context = {&lt;br /&gt;
    &amp;quot;foaf&amp;quot;: &amp;quot;http://xmlns.com/foaf/0.1/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
for line in g.serialize(format=&#039;json-ld&#039;, context=context).decode().splitlines():&lt;br /&gt;
    print(line)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;credits&amp;quot; style=&amp;quot;text-align: right; direction: ltr; margin-left: 1em;&amp;quot;&amp;gt;&#039;&#039;INFO216, UiB, 2017-2020. All code examples are [https://creativecommons.org/choose/zero/ CC0].&#039;&#039; &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==OWL - Complex Classes and Restrictions==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import owlrl&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, BNode&lt;br /&gt;
from rdflib.namespace import RDF, OWL, RDFS&lt;br /&gt;
from rdflib.collection import Collection&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;owl&amp;quot;, OWL)&lt;br /&gt;
&lt;br /&gt;
# a Season is either Autumn, Winter, Spring, Summer&lt;br /&gt;
seasons = BNode()&lt;br /&gt;
Collection(g, seasons, [ex.Winter, ex.Autumn, ex.Spring, ex.Summer])&lt;br /&gt;
g.add((ex.Season, OWL.oneOf, seasons))&lt;br /&gt;
&lt;br /&gt;
# A Parent is a Father or Mother&lt;br /&gt;
b = BNode()&lt;br /&gt;
Collection(g, b, [ex.Father, ex.Mother])&lt;br /&gt;
g.add((ex.Parent, OWL.unionOf, b))&lt;br /&gt;
&lt;br /&gt;
# A Woman is a person who has the &amp;quot;female&amp;quot; gender&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.gender))&lt;br /&gt;
g.add((br, OWL.hasValue, ex.Female))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Woman, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# A vegetarian is a Person who only eats vegetarian food&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.eats))&lt;br /&gt;
g.add((br, OWL.allValuesFrom, ex.VeganFood))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Vegetarian, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# A vegetarian is a Person who can not eat meat.&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.eats))&lt;br /&gt;
g.add((br, OWL.QualifiedCardinality, Literal(0)))&lt;br /&gt;
g.add((br, OWL.onClass, ex.Meat))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Vegetarian, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# A Worried Parent is a parent who has at least one sick child&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.hasChild))&lt;br /&gt;
g.add((br, OWL.QualifiedMinCardinality, Literal(1)))&lt;br /&gt;
g.add((br, OWL.onClass, ex.Sick))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Parent, br])&lt;br /&gt;
g.add((ex.WorriedParent, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# using the restriction above, If we now write...: &lt;br /&gt;
g.add((ex.Bob, RDF.type, ex.Parent))&lt;br /&gt;
g.add((ex.Bob, ex.hasChild, ex.John))&lt;br /&gt;
g.add((ex.John, RDF.type, ex.Sick))&lt;br /&gt;
# ...we can infer with owl reasoning that Bob is a worried Parent even though we didn&#039;t specify it ourselves because Bob fullfills the restriction and Parent requirements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_RDFS&amp;diff=1515</id>
		<title>Lab: RDFS</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_RDFS&amp;diff=1515"/>
		<updated>2021-02-25T11:55:40Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Lab 5: RDFS Programming with rdflib and owlrl=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
Basic RDFS graph programming in RDFlib.&lt;br /&gt;
Entailments and axioms with owlrl.&lt;br /&gt;
&lt;br /&gt;
==Classes/Methods/Vocabularies==&lt;br /&gt;
owlrl.RDFSClosure (RDFS_Semantics, closure, flush_stored_triples)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Vocabularies: &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
RDF.type&lt;br /&gt;
&lt;br /&gt;
RDFS.subClassOf, RDFS.subPropertyOf, RDFS.domain, RDFS.range, RDFS.label, RDFS.comment, &lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
First, pip install owlrl.&lt;br /&gt;
The RDFS Vocabulary can be imported from rdflib.namespace, just like FOAF or RDF.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Consider the following Scenario:&#039;&#039;&#039;&lt;br /&gt;
&amp;quot;University of California and University of Valencia are both Universities.&lt;br /&gt;
All universities are higher education institutions (HEIs). Only persons can have an expertise, and what they have expertise in is always a subject. Only persons can graduate from a HEI. If you are a student, you are in fact a person as well. That a person is married to someone, means that they know them. Finally, if a person has a name, that name is also the label of that entity.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create RDFS triples corresponding to the text above with RDFlib&#039;&#039;&#039; - if you can, try to build on&lt;br /&gt;
your example from lab 2! &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Using these three lines we can add automatically the inferred triples (like ex:University rdf:type ex:Higher_Education_Institute) :&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
rdfs.closure()&lt;br /&gt;
rdfs.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Check that simple inference works -  make sure that your graph contains triples like these, even if&lt;br /&gt;
you have not asserted them explicitly:&lt;br /&gt;
* that University of California and Valencia are HEIs&lt;br /&gt;
* that Cade, Emma, and Mary are all persons&lt;br /&gt;
* that Cade and Emma have both graduated from some HEI&lt;br /&gt;
* that Cade knows Mary&lt;br /&gt;
&lt;br /&gt;
One way to check if the triples are there:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
universities = g.query(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
    ex:University_of_California rdf:type ex:Higher_Education_Institution.&lt;br /&gt;
} &lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
print(bool(universities))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rewrite some of your existing code to use rdfs:label in a triple and add an rdfs:comment to the same resource.&lt;br /&gt;
&lt;br /&gt;
==If you have more time...==&lt;br /&gt;
Create a new RDFS graph that wraps an empty graph. This graph contains only RDFS axioms. Write it out in Turtle and check that you understand  the meaning and purpose of each axiom.&lt;br /&gt;
&lt;br /&gt;
Create an RDF (not RDFS) graph that contains all the triples in your first graph (the one with all the people and universities). Subtract all the triples in the axiom graph from the people/university graph. Write it out to see that you are left with only the asserted and entailed triples and that none of the axioms remain.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Download the SKOS vocabulary from https://www.w3.org/2009/08/skos-reference/skos.rdf and save it to a file called, e.g., SKOS.rdf .&lt;br /&gt;
Use the schemagen tool (it is inside your Jena folders, for example under apache-jena-3.1.1/bin) to generate a Java class for the SKOS vocabulary. &lt;br /&gt;
You need to do this from a console window, using a command like &amp;quot;&amp;lt;path&amp;gt;/schemagen -i &amp;lt;infile.rdf&amp;gt; -o &amp;lt;outfile.java&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Copy the SKOS.java file into your project in the same package as your other Java files,  and try to use SKOS properties &lt;br /&gt;
where they fit, for example to organise the keywords for interests and expertise.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Useful Readings==&lt;br /&gt;
*[https://wiki.uib.no/info216/index.php/File:S05-RDFS-11.pdf Lecture Notes]&lt;br /&gt;
*[https://wiki.uib.no/info216/index.php/Python_Examples Example page]&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL_Programming&amp;diff=1500</id>
		<title>Lab: SPARQL Programming</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL_Programming&amp;diff=1500"/>
		<updated>2021-02-16T13:12:40Z</updated>

		<summary type="html">&lt;p&gt;Sas020: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Lab 4 - SPARQL PROGRAMMING ==&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
SPARQL programming in python with SPARQLWrapper and Blazegraph, or alternatively RDFlib.&lt;br /&gt;
These tasks are about programming SPARQL queries and inserts in a python program. &lt;br /&gt;
&lt;br /&gt;
Last week we added triples manually from the web interface. &lt;br /&gt;
&lt;br /&gt;
However, in the majority of cases, we want to program the insertion or updates of triples for our graphs/databases, for instance to handle automatic or scheduled updates. &lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
Remember, before you can interact with Blazegraph you have to make sure its running like we did in [https://wiki.uib.no/info216/index.php/Lab:_SPARQL Lab 4].&lt;br /&gt;
*&#039;&#039;&#039;Make a new blazegraph namespace from the blazegraph web-interface and add all the triples that are on the bottom of the page like we did in [https://wiki.uib.no/info216/index.php/Lab:_SPARQL Lab 4]&#039;&#039;&#039;&lt;br /&gt;
Alternatively you can use your own triples if you have them. &lt;br /&gt;
&lt;br /&gt;
The default namespace for blazegraph is &amp;quot;kb&amp;quot;. If you want to add other namespaces you can do it from the web-interface of Blazegraph, from the &amp;quot;Namespace&amp;quot; Tab. Remember to click &amp;quot;Use&amp;quot; on the namespace after you have created it.&lt;br /&gt;
&lt;br /&gt;
The different namespaces for blazegraph acts as seperate graphs/databases. This is especially useful if you are using the UiB link to blazegraph: &amp;quot;i2s.uib.no:8888/bigdata/#splash&amp;quot;, because with your own namespace, only you can select and update your data. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*&#039;&#039;&#039;Redo all the SPARQL queries and updates from [https://wiki.uib.no/info216/index.php/Lab:_SPARQL Lab 4], this time writing a Python program that uses SPARQLWrapper to handle them.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* SELECT all triples in your graph.&lt;br /&gt;
* SELECT all the interests of Cade.&lt;br /&gt;
* SELECT the city and country of where Emma lives.&lt;br /&gt;
* SELECT only people who are older than 26.&lt;br /&gt;
* SELECT Everyone who graduated with a Bachelor Degree.&lt;br /&gt;
* Use SPARQL Update&#039;s DELETE DATA to delete that fact that Cade is interested in Photography. Run your SPARQL query again to check that the graph has changed.&lt;br /&gt;
&lt;br /&gt;
* Use INSERT DATA to add information about Sergio Pastor, who lives in 4 Carrer del Serpis, 46021 Valencia, Spain. he has a M.Sc. in computer from the University of Valencia from 2008. His areas of expertise include big data, semantic technologies and machine learning.&lt;br /&gt;
&lt;br /&gt;
* Write a SPARQL DELETE/INSERT update to change the name of &amp;quot;University of Valencia&amp;quot; to &amp;quot;Universidad de Valencia&amp;quot; whereever it occurs.&lt;br /&gt;
&lt;br /&gt;
* Write a SPARQL DESCRIBE query to get basic information about Sergio.&lt;br /&gt;
&lt;br /&gt;
* Write a SPARQL CONSTRUCT query that returns that: any city in an address is a cityOf the country of the same address. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==With Blazegraph== &lt;br /&gt;
First, pip install SPARQLWrapper.&lt;br /&gt;
The most important part is that we need to import a SPARQLWrapper in order to connect to the SPARQL endpoint of Blazegraph.&lt;br /&gt;
 &lt;br /&gt;
When it comes to how to do some queries and updates I recommend scrolling down on this page for help: https://github.com/RDFLib/sparqlwrapper. There are also some examples on our example page.&lt;br /&gt;
&lt;br /&gt;
Remember, before you can program with Blazegraph you have to make sure its running like we did in  [https://wiki.uib.no/info216/index.php/Lab:_SPARQL Lab 4]. Make sure that the URL you use with SPARQLWrapper has the same address and port as the one you get from running it. &lt;br /&gt;
Now you will be able to program queries and updates.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# How to establish connection to Blazegraph endpoint. Also a quick select example.&lt;br /&gt;
&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, JSON&lt;br /&gt;
&lt;br /&gt;
namespace = &amp;quot;kb&amp;quot;&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;http://localhost:9999/blazegraph/namespace/&amp;quot;+ namespace + &amp;quot;/sparql&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
    SELECT * WHERE {&lt;br /&gt;
    ex:Cade ex:interest ?interest.&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;interest&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The different types of queries requires different return formats: &lt;br /&gt;
* SELECT and ASK: a SPARQL Results Document in XML, JSON, or CSV/TSV format.&lt;br /&gt;
* DESCRIBE and CONSTRUCT: an RDF graph serialized, for example, in the TURTLE or RDF/XML syntax, or an equivalent RDF graph serialization.&lt;br /&gt;
Remember to make sure that you can see the changes that take place after your inserts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Without Blazegraph==&lt;br /&gt;
If you have not been able to run Blazegraph on your own computer yet, you can use the UiB blazegraph service: i2s.uib.no:8888/bigdata/#splash.&lt;br /&gt;
Remember to create your own namespace like said above in the web-interface. &lt;br /&gt;
&lt;br /&gt;
Alternatively, you can&lt;br /&gt;
instead program SPARQL queries directly with RDFlib. &lt;br /&gt;
&lt;br /&gt;
For help, look at the link below: &lt;br /&gt;
&lt;br /&gt;
[https://rdflib.readthedocs.io/en/4.2.0/intro_to_sparql.html Querying with Sparql]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Readings==&lt;br /&gt;
*[https://github.com/RDFLib/sparqlwrapper SPARQLWrapper]&lt;br /&gt;
*[https://rdflib.readthedocs.io/en/4.2.0/intro_to_sparql.html RDFlib - Querying with Sparql]&lt;br /&gt;
&lt;br /&gt;
==SPARQL Queries you can use for tasks==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# SPARQL Queries&lt;br /&gt;
&lt;br /&gt;
prefix ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# SELECT Every triple&lt;br /&gt;
SELECT * WHERE {?s ?p ?o}&lt;br /&gt;
&lt;br /&gt;
# Select the interests of Cade&lt;br /&gt;
SELECT ?interest WHERE {ex:Cade ex:interest ?interest}&lt;br /&gt;
&lt;br /&gt;
# SELECT only people who are older than 26&lt;br /&gt;
SELECT ?person ?age WHERE {?person ex:age ?age. FILTER(?age &amp;gt; 26)}&lt;br /&gt;
&lt;br /&gt;
# SELECT The City and country of Cade&lt;br /&gt;
SELECT ?country ?city WHERE {ex:Cade ex:address ?address. ?address ex:country ?country. ?address ex:city ?city.}&lt;br /&gt;
&lt;br /&gt;
# SELECT Everyone who graduated with a Bachelor Degree.&lt;br /&gt;
SELECT ?person ?level WHERE {?person ex:degree ?degree. ?degree ex:degreeLevel ?level. FILTER(?level=&amp;quot;Bachelor&amp;quot;)}&lt;br /&gt;
&lt;br /&gt;
# DELETE Photography&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
DELETE DATA&lt;br /&gt;
&lt;br /&gt;
{&lt;br /&gt;
 ex:Cade ex:interest ex:Photography.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# INSERT Sergio&lt;br /&gt;
&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
INSERT DATA&lt;br /&gt;
{&lt;br /&gt;
 ex:Sergio ex:address ex:SergioAddress.&lt;br /&gt;
 ex:SergioAddress ex:city ex:Valencia.&lt;br /&gt;
 ex:SergioAddress ex:street &amp;quot;4 Carrer del Serpis&amp;quot;.&lt;br /&gt;
 ex:SergioAddress ex:postalCode &amp;quot;46021&amp;quot;.&lt;br /&gt;
 ex:SergioAddress ex:country ex:Spain.&lt;br /&gt;
 ex:Sergio ex:address ex:SergioDegree.&lt;br /&gt;
 ex:SergioDegree ex:degreeLevel &amp;quot;Master&amp;quot;.&lt;br /&gt;
 ex:SergioDegree ex:degreeField ex:Computer_Science.&lt;br /&gt;
 ex:SergioDegree ex:degreeYear &amp;quot;2008&amp;quot;.&lt;br /&gt;
 ex:SergioDegree ex:degreeSource ex:University_of_Valencia.&lt;br /&gt;
 ex:Sergio ex:expertise ex:Big_Data.&lt;br /&gt;
 ex:Sergio ex:expertise ex:Semantic_Technologies.&lt;br /&gt;
 ex:Sergio ex:expertise ex:Machine_Learning.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# DELETE Photography&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
DELETE DATA&lt;br /&gt;
&lt;br /&gt;
{&lt;br /&gt;
 ex:Cade ex:interest ex:Photography.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# DELETE/INSERT University&lt;br /&gt;
&lt;br /&gt;
prefix ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
DELETE {&lt;br /&gt;
  ?s ?p ex:University_of_Valencia.&lt;br /&gt;
}&lt;br /&gt;
INSERT {?s ?p ex:Universidad_de_Valencia.}&lt;br /&gt;
&lt;br /&gt;
WHERE {&lt;br /&gt;
?s ?p ex:University_of_Valencia.}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Construct&lt;br /&gt;
&lt;br /&gt;
prefix ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
CONSTRUCT {?city ex:cityOf ?country}&lt;br /&gt;
WHERE {?address ex:city ?city. ?address ex:country ?country} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Triples that you can base your queries on: (turtle format)==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
@prefix ex: &amp;lt;http://example.org/&amp;gt; .&lt;br /&gt;
@prefix foaf: &amp;lt;http://xmlns.com/foaf/0.1/&amp;gt; .&lt;br /&gt;
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .&lt;br /&gt;
@prefix rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt; .&lt;br /&gt;
@prefix xml: &amp;lt;http://www.w3.org/XML/1998/namespace&amp;gt; .&lt;br /&gt;
@prefix xsd: &amp;lt;http://www.w3.org/2001/XMLSchema#&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
ex:Cade a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Berkeley ;&lt;br /&gt;
            ex:country ex:USA ;&lt;br /&gt;
            ex:postalCode &amp;quot;94709&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:state ex:California ;&lt;br /&gt;
            ex:street &amp;quot;1516_Henry_Street&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 27 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Biology ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Bachelor&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_California ;&lt;br /&gt;
            ex:year &amp;quot;2011-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:interest ex:Bird,&lt;br /&gt;
        ex:Ecology,&lt;br /&gt;
        ex:Environmentalism,&lt;br /&gt;
        ex:Photography,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:married ex:Mary ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ex:Canada,&lt;br /&gt;
        ex:France,&lt;br /&gt;
        ex:Germany ;&lt;br /&gt;
    foaf:knows ex:Emma ;&lt;br /&gt;
    foaf:name &amp;quot;Cade_Tracey&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Mary a ex:Student,&lt;br /&gt;
        foaf:Person ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:interest ex:Biology,&lt;br /&gt;
        ex:Chocolate,&lt;br /&gt;
        ex:Hiking .&lt;br /&gt;
&lt;br /&gt;
ex:Emma a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Valencia ;&lt;br /&gt;
            ex:country ex:Spain ;&lt;br /&gt;
            ex:postalCode &amp;quot;46020&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:street &amp;quot;Carrer_de_la Guardia_Civil_20&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Chemistry ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Master&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_Valencia ;&lt;br /&gt;
            ex:year &amp;quot;2015-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:expertise ex:Air_Pollution,&lt;br /&gt;
        ex:Toxic_Waste,&lt;br /&gt;
        ex:Waste_Management ;&lt;br /&gt;
    ex:interest ex:Bike_Riding,&lt;br /&gt;
        ex:Music,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ( ex:Portugal ex:Italy ex:France ex:Germany ex:Denmark ex:Sweden ) ;&lt;br /&gt;
    foaf:name &amp;quot;Emma_Dominguez&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Meeting1 a ex:Meeting ;&lt;br /&gt;
    ex:date &amp;quot;August, 2014&amp;quot;^^xsd:string ;&lt;br /&gt;
    ex:involved ex:Cade,&lt;br /&gt;
        ex:Emma ;&lt;br /&gt;
    ex:location ex:Paris .&lt;br /&gt;
&lt;br /&gt;
ex:Paris a ex:City ;&lt;br /&gt;
    ex:capitalOf ex:France ;&lt;br /&gt;
    ex:locatedIn ex:France .&lt;br /&gt;
&lt;br /&gt;
ex:France ex:capital ex:Paris .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL&amp;diff=1499</id>
		<title>Lab: SPARQL</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL&amp;diff=1499"/>
		<updated>2021-02-16T09:59:17Z</updated>

		<summary type="html">&lt;p&gt;Sas020: Update link to internal Blazegraph.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lab 4: SPARQL / Blazegraph=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
* Setting up the Blazegraph graph database. Previously we have only stored our triples in memory, which is not persistent. &lt;br /&gt;
* SPARQL queries and updates. We use SPARQL to retrieve of update triples in our databases/graphs of triples&lt;br /&gt;
&lt;br /&gt;
==Installing the Blazegraph database on your own computer==&lt;br /&gt;
Download Blazegraph (blazegraph.jar) from here: [https://blazegraph.com/ https://blazegraph.com/]&lt;br /&gt;
I recommend placing blazegraph.jar in the same folder of your python project for the labs. &lt;br /&gt;
Navigate to the folder of blazegraph.jar in your commandline/terminal using cd. (cd C:\Users\Martin\PycharmProjects\info216_labs for me as an example). Now run this command:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
java -server -Xmx4g -jar blazegraph.jar&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You might have to install java 8 64-bit JDK if you have problems running blazegraph. You can do it from  this link: &lt;br /&gt;
&amp;quot;https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html&amp;quot;&lt;br /&gt;
If you get an &amp;quot;Address already in use&amp;quot; error, this is likely because blazegraph has been terminated improperly. Either restart the terminal-session or try to run this command instead: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
java -server -Xmx4g -Djetty.port=19999 -jar blazegraph.jar &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This changes the port of the blazegraph server.&lt;br /&gt;
&lt;br /&gt;
If you have trouble installing Blazegraph you can use this link for now: &amp;quot;http://sandbox.i2s.uib.no/bigdata/&amp;quot;.&lt;br /&gt;
This is the same blazegraph interface, but its stored in the cloud and only be used on the UiB network. You may be able to access it without connecting to the UiB Network, but if you are unable to access the endpoint try connecting via the VPN. Instructions [https://hjelp.uib.no/tas/public/ssp/content/detail/service?unid=a566dafec92a4d35bba974f0733f3663 here].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If it works it should now display an url like: &amp;quot;http://10.0.0.13:9999/blazegraph/&amp;quot;. Open this in a browser. &lt;br /&gt;
You can now run SPARQL queries and updates and load RDF graphs from your file into Blazegraph.&lt;br /&gt;
In the update tab, load RDF data (select type below) and then paste the contents of your turtle/.txt file to add them all at once to the database. If you have not serialized your graph from lab 2 yet, you can use the triples on the bottom of the page instead. Just copy and paste them into the Update section.&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
Write the following SPARQL queries: &lt;br /&gt;
&lt;br /&gt;
* SELECT all triples in your graph. &lt;br /&gt;
* SELECT all the interests of Cade.&lt;br /&gt;
* SELECT the city and country of where Emma lives.&lt;br /&gt;
* SELECT only people who are older than 26.&lt;br /&gt;
* SELECT Everyone who graduated with a Bachelor Degree. &lt;br /&gt;
&lt;br /&gt;
Use SPARQL Update&#039;s DELETE DATA to delete that fact that Cade is interested in Photography. Run your SPARQL query again to check that the graph has changed.&lt;br /&gt;
&lt;br /&gt;
Use INSERT DATA to add information about Sergio Pastor, who lives in 4 Carrer del Serpis, 46021 Valencia, Spain. he has a M.Sc. in computer from the University of Valencia from 2008. His areas of expertise include big data, semantic technologies and machine learning.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL DELETE/INSERT update to change the name of &amp;quot;University of Valencia&amp;quot; to &amp;quot;Universidad de Valencia&amp;quot; whereever it occurs.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL DESCRIBE query to get basic information about Sergio.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL CONSTRUCT query that returns that: any city in an address is a cityOf the country of the same address.&lt;br /&gt;
&lt;br /&gt;
==If you have more time==&lt;br /&gt;
Redo all the above steps, this time writing a Python/RDFlib program. This will be the topic of lab 6.&lt;br /&gt;
You can look at the python example page to see how to connect to your blazegraph endpoint in Python and how to perform some basic queries.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Links==&lt;br /&gt;
[https://wiki.uib.no/info216/index.php/File:S03-SPARQL-13.pdf Lecture Notes]&lt;br /&gt;
&lt;br /&gt;
[https://www.w3.org/TR/sparql11-query/ SPARQL Query Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/sparql11-update/ SPARQL Update Documentation]&lt;br /&gt;
&lt;br /&gt;
==Triples that you can base your queries on: (turtle format)==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
@prefix ex: &amp;lt;http://example.org/&amp;gt; .&lt;br /&gt;
@prefix foaf: &amp;lt;http://xmlns.com/foaf/0.1/&amp;gt; .&lt;br /&gt;
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .&lt;br /&gt;
@prefix rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt; .&lt;br /&gt;
@prefix xml: &amp;lt;http://www.w3.org/XML/1998/namespace&amp;gt; .&lt;br /&gt;
@prefix xsd: &amp;lt;http://www.w3.org/2001/XMLSchema#&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
ex:Cade a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Berkeley ;&lt;br /&gt;
            ex:country ex:USA ;&lt;br /&gt;
            ex:postalCode &amp;quot;94709&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:state ex:California ;&lt;br /&gt;
            ex:street &amp;quot;1516_Henry_Street&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 27 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Biology ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Bachelor&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_California ;&lt;br /&gt;
            ex:year &amp;quot;2011-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:interest ex:Bird,&lt;br /&gt;
        ex:Ecology,&lt;br /&gt;
        ex:Environmentalism,&lt;br /&gt;
        ex:Photography,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:married ex:Mary ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ex:Canada,&lt;br /&gt;
        ex:France,&lt;br /&gt;
        ex:Germany ;&lt;br /&gt;
    foaf:knows ex:Emma ;&lt;br /&gt;
    foaf:name &amp;quot;Cade_Tracey&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Mary a ex:Student,&lt;br /&gt;
        foaf:Person ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:interest ex:Biology,&lt;br /&gt;
        ex:Chocolate,&lt;br /&gt;
        ex:Hiking .&lt;br /&gt;
&lt;br /&gt;
ex:Emma a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Valencia ;&lt;br /&gt;
            ex:country ex:Spain ;&lt;br /&gt;
            ex:postalCode &amp;quot;46020&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:street &amp;quot;Carrer_de_la Guardia_Civil_20&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Chemistry ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Master&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_Valencia ;&lt;br /&gt;
            ex:year &amp;quot;2015-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:expertise ex:Air_Pollution,&lt;br /&gt;
        ex:Toxic_Waste,&lt;br /&gt;
        ex:Waste_Management ;&lt;br /&gt;
    ex:interest ex:Bike_Riding,&lt;br /&gt;
        ex:Music,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ( ex:Portugal ex:Italy ex:France ex:Germany ex:Denmark ex:Sweden ) ;&lt;br /&gt;
    foaf:name &amp;quot;Emma_Dominguez&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Meeting1 a ex:Meeting ;&lt;br /&gt;
    ex:date &amp;quot;August, 2014&amp;quot;^^xsd:string ;&lt;br /&gt;
    ex:involved ex:Cade,&lt;br /&gt;
        ex:Emma ;&lt;br /&gt;
    ex:location ex:Paris .&lt;br /&gt;
&lt;br /&gt;
ex:Paris a ex:City ;&lt;br /&gt;
    ex:capitalOf ex:France ;&lt;br /&gt;
    ex:locatedIn ex:France .&lt;br /&gt;
&lt;br /&gt;
ex:France ex:capital ex:Paris .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL&amp;diff=1486</id>
		<title>Lab: SPARQL</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL&amp;diff=1486"/>
		<updated>2021-02-11T17:10:45Z</updated>

		<summary type="html">&lt;p&gt;Sas020: Minor: Add SPARQL query and update documentation to useful links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lab 4: SPARQL / Blazegraph=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
* Setting up the Blazegraph graph database. Previously we have only stored our triples in memory, which is not persistent. &lt;br /&gt;
* SPARQL queries and updates. We use SPARQL to retrieve of update triples in our databases/graphs of triples&lt;br /&gt;
&lt;br /&gt;
==Installing the Blazegraph database on your own computer==&lt;br /&gt;
Download Blazegraph (blazegraph.jar) from here: [https://blazegraph.com/ https://blazegraph.com/]&lt;br /&gt;
I recommend placing blazegraph.jar in the same folder of your python project for the labs. &lt;br /&gt;
Navigate to the folder of blazegraph.jar in your commandline/terminal using cd. (cd C:\Users\Martin\PycharmProjects\info216_labs for me as an example). Now run this command:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
java -server -Xmx4g -jar blazegraph.jar&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You might have to install java 8 64-bit JDK if you have problems running blazegraph. You can do it from  this link: &lt;br /&gt;
&amp;quot;https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html&amp;quot;&lt;br /&gt;
If you get an &amp;quot;Address already in use&amp;quot; error, this is likely because blazegraph has been terminated improperly. Either restart the terminal-session or try to run this command instead: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
java -server -Xmx4g -Djetty.port=19999 -jar blazegraph.jar &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This changes the port of the blazegraph server.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--If you have trouble installing Blazegraph you can use this link for now: &amp;quot;i2s.uib.no:8888/bigdata/#splash&amp;quot;.&lt;br /&gt;
This is the same blazegraph interface, but its stored in the cloud and only be used on the UiB network.--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If it works it should now display an url like: &amp;quot;http://10.0.0.13:9999/blazegraph/&amp;quot;. Open this in a browser. &lt;br /&gt;
You can now run SPARQL queries and updates and load RDF graphs from your file into Blazegraph.&lt;br /&gt;
In the update tab, load RDF data (select type below) and then paste the contents of your turtle/.txt file to add them all at once to the database. If you have not serialized your graph from lab 2 yet, you can use the triples on the bottom of the page instead. Just copy and paste them into the Update section.&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
Write the following SPARQL queries: &lt;br /&gt;
&lt;br /&gt;
* SELECT all triples in your graph. &lt;br /&gt;
* SELECT all the interests of Cade.&lt;br /&gt;
* SELECT the city and country of where Emma lives.&lt;br /&gt;
* SELECT only people who are older than 26.&lt;br /&gt;
* SELECT Everyone who graduated with a Bachelor Degree. &lt;br /&gt;
&lt;br /&gt;
Use SPARQL Update&#039;s DELETE DATA to delete that fact that Cade is interested in Photography. Run your SPARQL query again to check that the graph has changed.&lt;br /&gt;
&lt;br /&gt;
Use INSERT DATA to add information about Sergio Pastor, who lives in 4 Carrer del Serpis, 46021 Valencia, Spain. he has a M.Sc. in computer from the University of Valencia from 2008. His areas of expertise include big data, semantic technologies and machine learning.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL DELETE/INSERT update to change the name of &amp;quot;University of Valencia&amp;quot; to &amp;quot;Universidad de Valencia&amp;quot; whereever it occurs.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL DESCRIBE query to get basic information about Sergio.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL CONSTRUCT query that returns that: any city in an address is a cityOf the country of the same address.&lt;br /&gt;
&lt;br /&gt;
==If you have more time==&lt;br /&gt;
Redo all the above steps, this time writing a Python/RDFlib program. This will be the topic of lab 6.&lt;br /&gt;
You can look at the python example page to see how to connect to your blazegraph endpoint in Python and how to perform some basic queries.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Links==&lt;br /&gt;
[https://wiki.uib.no/info216/index.php/File:S03-SPARQL-13.pdf Lecture Notes]&lt;br /&gt;
&lt;br /&gt;
[https://www.w3.org/TR/sparql11-query/ SPARQL Query Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/sparql11-update/ SPARQL Update Documentation]&lt;br /&gt;
&lt;br /&gt;
==Triples that you can base your queries on: (turtle format)==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
@prefix ex: &amp;lt;http://example.org/&amp;gt; .&lt;br /&gt;
@prefix foaf: &amp;lt;http://xmlns.com/foaf/0.1/&amp;gt; .&lt;br /&gt;
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .&lt;br /&gt;
@prefix rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt; .&lt;br /&gt;
@prefix xml: &amp;lt;http://www.w3.org/XML/1998/namespace&amp;gt; .&lt;br /&gt;
@prefix xsd: &amp;lt;http://www.w3.org/2001/XMLSchema#&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
ex:Cade a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Berkeley ;&lt;br /&gt;
            ex:country ex:USA ;&lt;br /&gt;
            ex:postalCode &amp;quot;94709&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:state ex:California ;&lt;br /&gt;
            ex:street &amp;quot;1516_Henry_Street&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 27 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Biology ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Bachelor&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_California ;&lt;br /&gt;
            ex:year &amp;quot;2011-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:interest ex:Bird,&lt;br /&gt;
        ex:Ecology,&lt;br /&gt;
        ex:Environmentalism,&lt;br /&gt;
        ex:Photography,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:married ex:Mary ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ex:Canada,&lt;br /&gt;
        ex:France,&lt;br /&gt;
        ex:Germany ;&lt;br /&gt;
    foaf:knows ex:Emma ;&lt;br /&gt;
    foaf:name &amp;quot;Cade_Tracey&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Mary a ex:Student,&lt;br /&gt;
        foaf:Person ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:interest ex:Biology,&lt;br /&gt;
        ex:Chocolate,&lt;br /&gt;
        ex:Hiking .&lt;br /&gt;
&lt;br /&gt;
ex:Emma a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Valencia ;&lt;br /&gt;
            ex:country ex:Spain ;&lt;br /&gt;
            ex:postalCode &amp;quot;46020&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:street &amp;quot;Carrer_de_la Guardia_Civil_20&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Chemistry ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Master&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_Valencia ;&lt;br /&gt;
            ex:year &amp;quot;2015-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:expertise ex:Air_Pollution,&lt;br /&gt;
        ex:Toxic_Waste,&lt;br /&gt;
        ex:Waste_Management ;&lt;br /&gt;
    ex:interest ex:Bike_Riding,&lt;br /&gt;
        ex:Music,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ( ex:Portugal ex:Italy ex:France ex:Germany ex:Denmark ex:Sweden ) ;&lt;br /&gt;
    foaf:name &amp;quot;Emma_Dominguez&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Meeting1 a ex:Meeting ;&lt;br /&gt;
    ex:date &amp;quot;August, 2014&amp;quot;^^xsd:string ;&lt;br /&gt;
    ex:involved ex:Cade,&lt;br /&gt;
        ex:Emma ;&lt;br /&gt;
    ex:location ex:Paris .&lt;br /&gt;
&lt;br /&gt;
ex:Paris a ex:City ;&lt;br /&gt;
    ex:capitalOf ex:France ;&lt;br /&gt;
    ex:locatedIn ex:France .&lt;br /&gt;
&lt;br /&gt;
ex:France ex:capital ex:Paris .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL&amp;diff=1484</id>
		<title>Lab: SPARQL</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_SPARQL&amp;diff=1484"/>
		<updated>2021-02-09T10:44:00Z</updated>

		<summary type="html">&lt;p&gt;Sas020: Minor: add syntax highlighting to command with Djetty.port-flag.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lab 4: SPARQL / Blazegraph=&lt;br /&gt;
&lt;br /&gt;
==Topics==&lt;br /&gt;
* Setting up the Blazegraph graph database. Previously we have only stored our triples in memory, which is not persistent. &lt;br /&gt;
* SPARQL queries and updates. We use SPARQL to retrieve of update triples in our databases/graphs of triples&lt;br /&gt;
&lt;br /&gt;
==Installing the Blazegraph database on your own computer==&lt;br /&gt;
Download Blazegraph (blazegraph.jar) from here: [https://blazegraph.com/ https://blazegraph.com/]&lt;br /&gt;
I recommend placing blazegraph.jar in the same folder of your python project for the labs. &lt;br /&gt;
Navigate to the folder of blazegraph.jar in your commandline/terminal using cd. (cd C:\Users\Martin\PycharmProjects\info216_labs for me as an example). Now run this command:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
java -server -Xmx4g -jar blazegraph.jar&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You might have to install java 8 64-bit JDK if you have problems running blazegraph. You can do it from  this link: &lt;br /&gt;
&amp;quot;https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html&amp;quot;&lt;br /&gt;
If you get an &amp;quot;Address already in use&amp;quot; error, this is likely because blazegraph has been terminated improperly. Either restart the terminal-session or try to run this command instead: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
java -server -Xmx4g -Djetty.port=19999 -jar blazegraph.jar &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This changes the port of the blazegraph server.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--If you have trouble installing Blazegraph you can use this link for now: &amp;quot;i2s.uib.no:8888/bigdata/#splash&amp;quot;.&lt;br /&gt;
This is the same blazegraph interface, but its stored in the cloud and only be used on the UiB network.--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If it works it should now display an url like: &amp;quot;http://10.0.0.13:9999/blazegraph/&amp;quot;. Open this in a browser. &lt;br /&gt;
You can now run SPARQL queries and updates and load RDF graphs from your file into Blazegraph.&lt;br /&gt;
In the update tab, load RDF data (select type below) and then paste the contents of your turtle/.txt file to add them all at once to the database. If you have not serialized your graph from lab 2 yet, you can use the triples on the bottom of the page instead. Just copy and paste them into the Update section.&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
Write the following SPARQL queries: &lt;br /&gt;
&lt;br /&gt;
* SELECT all triples in your graph. &lt;br /&gt;
* SELECT all the interests of Cade.&lt;br /&gt;
* SELECT the city and country of where Emma lives.&lt;br /&gt;
* SELECT only people who are older than 26.&lt;br /&gt;
* SELECT Everyone who graduated with a Bachelor Degree. &lt;br /&gt;
&lt;br /&gt;
Use SPARQL Update&#039;s DELETE DATA to delete that fact that Cade is interested in Photography. Run your SPARQL query again to check that the graph has changed.&lt;br /&gt;
&lt;br /&gt;
Use INSERT DATA to add information about Sergio Pastor, who lives in 4 Carrer del Serpis, 46021 Valencia, Spain. he has a M.Sc. in computer from the University of Valencia from 2008. His areas of expertise include big data, semantic technologies and machine learning.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL DELETE/INSERT update to change the name of &amp;quot;University of Valencia&amp;quot; to &amp;quot;Universidad de Valencia&amp;quot; whereever it occurs.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL DESCRIBE query to get basic information about Sergio.&lt;br /&gt;
&lt;br /&gt;
Write a SPARQL CONSTRUCT query that returns that: any city in an address is a cityOf the country of the same address.&lt;br /&gt;
&lt;br /&gt;
==If you have more time==&lt;br /&gt;
Redo all the above steps, this time writing a Python/RDFlib program. This will be the topic of lab 6.&lt;br /&gt;
You can look at the python example page to see how to connect to your blazegraph endpoint in Python and how to perform some basic queries.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Useful Links==&lt;br /&gt;
[https://wiki.uib.no/info216/index.php/File:S03-SPARQL-13.pdf Lecture Notes]&lt;br /&gt;
&lt;br /&gt;
==Triples that you can base your queries on: (turtle format)==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
@prefix ex: &amp;lt;http://example.org/&amp;gt; .&lt;br /&gt;
@prefix foaf: &amp;lt;http://xmlns.com/foaf/0.1/&amp;gt; .&lt;br /&gt;
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .&lt;br /&gt;
@prefix rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt; .&lt;br /&gt;
@prefix xml: &amp;lt;http://www.w3.org/XML/1998/namespace&amp;gt; .&lt;br /&gt;
@prefix xsd: &amp;lt;http://www.w3.org/2001/XMLSchema#&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
ex:Cade a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Berkeley ;&lt;br /&gt;
            ex:country ex:USA ;&lt;br /&gt;
            ex:postalCode &amp;quot;94709&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:state ex:California ;&lt;br /&gt;
            ex:street &amp;quot;1516_Henry_Street&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 27 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Biology ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Bachelor&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_California ;&lt;br /&gt;
            ex:year &amp;quot;2011-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:interest ex:Bird,&lt;br /&gt;
        ex:Ecology,&lt;br /&gt;
        ex:Environmentalism,&lt;br /&gt;
        ex:Photography,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:married ex:Mary ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ex:Canada,&lt;br /&gt;
        ex:France,&lt;br /&gt;
        ex:Germany ;&lt;br /&gt;
    foaf:knows ex:Emma ;&lt;br /&gt;
    foaf:name &amp;quot;Cade_Tracey&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Mary a ex:Student,&lt;br /&gt;
        foaf:Person ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:characteristic ex:Kind ;&lt;br /&gt;
    ex:interest ex:Biology,&lt;br /&gt;
        ex:Chocolate,&lt;br /&gt;
        ex:Hiking .&lt;br /&gt;
&lt;br /&gt;
ex:Emma a foaf:Person ;&lt;br /&gt;
    ex:address [ a ex:Address ;&lt;br /&gt;
            ex:city ex:Valencia ;&lt;br /&gt;
            ex:country ex:Spain ;&lt;br /&gt;
            ex:postalCode &amp;quot;46020&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:street &amp;quot;Carrer_de_la Guardia_Civil_20&amp;quot;^^xsd:string ] ;&lt;br /&gt;
    ex:age 26 ;&lt;br /&gt;
    ex:degree [ ex:degreeField ex:Chemistry ;&lt;br /&gt;
            ex:degreeLevel &amp;quot;Master&amp;quot;^^xsd:string ;&lt;br /&gt;
            ex:degreeSource ex:University_of_Valencia ;&lt;br /&gt;
            ex:year &amp;quot;2015-01-01&amp;quot;^^xsd:gYear ] ;&lt;br /&gt;
    ex:expertise ex:Air_Pollution,&lt;br /&gt;
        ex:Toxic_Waste,&lt;br /&gt;
        ex:Waste_Management ;&lt;br /&gt;
    ex:interest ex:Bike_Riding,&lt;br /&gt;
        ex:Music,&lt;br /&gt;
        ex:Travelling ;&lt;br /&gt;
    ex:meeting ex:Meeting1 ;&lt;br /&gt;
    ex:visit ( ex:Portugal ex:Italy ex:France ex:Germany ex:Denmark ex:Sweden ) ;&lt;br /&gt;
    foaf:name &amp;quot;Emma_Dominguez&amp;quot;^^xsd:string .&lt;br /&gt;
&lt;br /&gt;
ex:Meeting1 a ex:Meeting ;&lt;br /&gt;
    ex:date &amp;quot;August, 2014&amp;quot;^^xsd:string ;&lt;br /&gt;
    ex:involved ex:Cade,&lt;br /&gt;
        ex:Emma ;&lt;br /&gt;
    ex:location ex:Paris .&lt;br /&gt;
&lt;br /&gt;
ex:Paris a ex:City ;&lt;br /&gt;
    ex:capitalOf ex:France ;&lt;br /&gt;
    ex:locatedIn ex:France .&lt;br /&gt;
&lt;br /&gt;
ex:France ex:capital ex:Paris .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab_Solutions&amp;diff=1471</id>
		<title>Lab Solutions</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab_Solutions&amp;diff=1471"/>
		<updated>2021-02-05T11:49:51Z</updated>

		<summary type="html">&lt;p&gt;Sas020: Minor: Convert schema and dbp namespace-strings to Namespace objects (lecture2 code).&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page will be updated with Python examples related to the lectures and labs. We will add more examples after each lab has ended. The first examples will use Python&#039;s RDFlib. We will introduce other relevant libraries later.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Lecture 1: Python, RDFlib, and PyCharm==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Printing the triples of the Graph in a readable way===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# The turtle format has the purpose of being more readable for humans. &lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Coding Tasks Lab 1===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace, URIRef, BNode, Literal&lt;br /&gt;
from rdflib.namespace import RDF, FOAF, XSD&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade, ex.married, ex.Mary))&lt;br /&gt;
g.add((ex.France, ex.capital, ex.Paris))&lt;br /&gt;
g.add((ex.Cade, ex.age, Literal(&amp;quot;27&amp;quot;, datatype=XSD.integer)))&lt;br /&gt;
g.add((ex.Mary, ex.age, Literal(&amp;quot;26&amp;quot;, datatype=XSD.integer)))&lt;br /&gt;
g.add((ex.Mary, ex.interest, ex.Hiking))&lt;br /&gt;
g.add((ex.Mary, ex.interest, ex.Chocolate))&lt;br /&gt;
g.add((ex.Mary, ex.interest, ex.Biology))&lt;br /&gt;
g.add((ex.Mary, RDF.type, ex.Student))&lt;br /&gt;
g.add((ex.Paris, RDF.type, ex.City))&lt;br /&gt;
g.add((ex.Paris, ex.locatedIn, ex.France))&lt;br /&gt;
g.add((ex.Cade, ex.characteristic, ex.Kind))&lt;br /&gt;
g.add((ex.Mary, ex.characteristic, ex.Kind))&lt;br /&gt;
g.add((ex.Mary, RDF.type, FOAF.Person))&lt;br /&gt;
g.add((ex.Cade, RDF.type, FOAF.Person))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Lecture 2: RDF programming==&lt;br /&gt;
&lt;br /&gt;
===Different ways to create an address===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
from rdflib import Graph, Namespace, URIRef, BNode, Literal&lt;br /&gt;
from rdflib.namespace import RDF, FOAF, XSD&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# How to represent the address of Cade Tracey. From probably the worst solution to the best.&lt;br /&gt;
&lt;br /&gt;
# Solution 1 -&lt;br /&gt;
# Make the entire address into one Literal. However, Generally we want to separate each part of an address into their own triples. This is useful for instance if we want to find only the streets where people live. &lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_Tracey, ex.livesIn, Literal(&amp;quot;1516_Henry_Street, Berkeley, California 94709, USA&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Solution 2 - &lt;br /&gt;
# Seperate the different pieces information into their own triples&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_tracey, ex.street, Literal(&amp;quot;1516_Henry_Street&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.city, Literal(&amp;quot;Berkeley&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.state, Literal(&amp;quot;California&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.zipcode, Literal(&amp;quot;94709&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.country, Literal(&amp;quot;USA&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Solution 3 - Some parts of the addresses can make more sense to be resources than Literals.&lt;br /&gt;
# Larger concepts like a city or state are typically represented as resources rather than Literals, but this is not necesarilly a requirement in the case that you don&#039;t intend to say more about them. &lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_tracey, ex.street, Literal(&amp;quot;1516_Henry_Street&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.city, ex.Berkeley))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.state, ex.California))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.zipcode, Literal(&amp;quot;94709&amp;quot;)))&lt;br /&gt;
g.add((ex.Cade_tracey, ex.country, ex.USA))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Solution 4 &lt;br /&gt;
# Grouping of the information into an Address. We can Represent the address concept with its own URI OR with a Blank Node. &lt;br /&gt;
# One advantage of this is that we can easily remove the entire address, instead of removing each individual part of the address. &lt;br /&gt;
# Solution 4 or 5 is how I would recommend to make addresses. Here, ex.CadeAddress could also be called something like ex.address1 or so on, if you want to give each address a unique ID. &lt;br /&gt;
&lt;br /&gt;
# Address URI - CadeAdress&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_Tracey, ex.address, ex.CadeAddress))&lt;br /&gt;
g.add((ex.CadeAddress, RDF.type, ex.Address))&lt;br /&gt;
g.add((ex.CadeAddress, ex.street, Literal(&amp;quot;1516 Henry Street&amp;quot;)))&lt;br /&gt;
g.add((ex.CadeAddress, ex.city, ex.Berkeley))&lt;br /&gt;
g.add((ex.CadeAddress, ex.state, ex.California))&lt;br /&gt;
g.add((ex.CadeAddress, ex.postalCode, Literal(&amp;quot;94709&amp;quot;)))&lt;br /&gt;
g.add((ex.CadeAddress, ex.country, ex.USA))&lt;br /&gt;
&lt;br /&gt;
# OR&lt;br /&gt;
&lt;br /&gt;
# Blank node for Address.  &lt;br /&gt;
address = BNode()&lt;br /&gt;
g.add((ex.Cade_Tracey, ex.address, address))&lt;br /&gt;
g.add((address, RDF.type, ex.Address))&lt;br /&gt;
g.add((address, ex.street, Literal(&amp;quot;1516 Henry Street&amp;quot;, datatype=XSD.string)))&lt;br /&gt;
g.add((address, ex.city, ex.Berkeley))&lt;br /&gt;
g.add((address, ex.state, ex.California))&lt;br /&gt;
g.add((address, ex.postalCode, Literal(&amp;quot;94709&amp;quot;, datatype=XSD.string)))&lt;br /&gt;
g.add((address, ex.country, ex.USA))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Solution 5 using existing vocabularies for address &lt;br /&gt;
&lt;br /&gt;
# (in this case https://schema.org/PostalAddress from schema.org). &lt;br /&gt;
# Also using existing ontology for places like California. (like http://dbpedia.org/resource/California from dbpedia.org)&lt;br /&gt;
&lt;br /&gt;
schema = Namespace(&amp;quot;https://schema.org/&amp;quot;)&lt;br /&gt;
dbp = Namespace(&amp;quot;https://dpbedia.org/resource/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade_Tracey, schema.address, ex.CadeAddress))&lt;br /&gt;
g.add((ex.CadeAddress, RDF.type, schema.PostalAddress))&lt;br /&gt;
g.add((ex.CadeAddress, schema.streetAddress, Literal(&amp;quot;1516 Henry Street&amp;quot;)))&lt;br /&gt;
g.add((ex.CadeAddress, schema.addresCity, dbp.Berkeley))&lt;br /&gt;
g.add((ex.CadeAddress, schema.addressRegion, dbp.California))&lt;br /&gt;
g.add((ex.CadeAddress, schema.postalCode, Literal(&amp;quot;94709&amp;quot;)))&lt;br /&gt;
g.add((ex.CadeAddress, schema.addressCountry, dbp.United_States))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Typed Literals===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace&lt;br /&gt;
from rdflib.namespace import XSD&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade, ex.age, Literal(27, datatype=XSD.integer)))&lt;br /&gt;
g.add((ex.Cade, ex.gpa, Literal(3.3, datatype=XSD.float)))&lt;br /&gt;
g.add((ex.Cade, FOAF.name, Literal(&amp;quot;Cade Tracey&amp;quot;, datatype=XSD.string)))&lt;br /&gt;
g.add((ex.Cade, ex.birthday, Literal(&amp;quot;2006-01-01&amp;quot;, datatype=XSD.date)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Writing and reading graphs/files===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
   # Writing the graph to a file on your system. Possible formats = turtle, n3, xml, nt.&lt;br /&gt;
g.serialize(destination=&amp;quot;triples.txt&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
   # Parsing a local file&lt;br /&gt;
parsed_graph = g.parse(location=&amp;quot;triples.txt&amp;quot;, format=&amp;quot;turtle&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
   # Parsing a remote endpoint like Dbpedia&lt;br /&gt;
dbpedia_graph = g.parse(&amp;quot;http://dbpedia.org/resource/Pluto&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Graph Binding===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
#Graph Binding is useful for at least two reasons:&lt;br /&gt;
#(1) We no longer need to specify prefixes with SPARQL queries if they are already binded to the graph.&lt;br /&gt;
#(2) When serializing the graph, the serialization will show the correct expected prefix &lt;br /&gt;
# instead of default namespace names ns1, ns2 etc.&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
dbp = Namespace(&amp;quot;http://dbpedia.org/resource/&amp;quot;)&lt;br /&gt;
schema = Namespace(&amp;quot;https://schema.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;dbp&amp;quot;, dbp)&lt;br /&gt;
g.bind(&amp;quot;schema&amp;quot;, schema)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Collection Example===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace&lt;br /&gt;
from rdflib.collection import Collection&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Sometimes we want to add many objects or subjects for the same predicate at once. &lt;br /&gt;
# In these cases we can use Collection() to save some time.&lt;br /&gt;
# In this case I want to add all countries that Emma has visited at once.&lt;br /&gt;
&lt;br /&gt;
b = BNode()&lt;br /&gt;
g.add((ex.Emma, ex.visit, b))&lt;br /&gt;
Collection(g, b,&lt;br /&gt;
    [ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])&lt;br /&gt;
&lt;br /&gt;
# OR&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Emma, ex.visit, ex.EmmaVisits))&lt;br /&gt;
Collection(g, ex.EmmaVisits,&lt;br /&gt;
    [ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Lecture 3: SPARQL==&lt;br /&gt;
&lt;br /&gt;
See the [[SPARQL Examples]] page!&lt;br /&gt;
&lt;br /&gt;
===Querying a local (&amp;quot;in memory&amp;quot;) graph===&lt;br /&gt;
&lt;br /&gt;
Example contents of the file family.ttl:&lt;br /&gt;
 @prefix rex: &amp;lt;http://example.org/royal#&amp;gt; .&lt;br /&gt;
 @prefix fam: &amp;lt;http://example.org/family#&amp;gt; .&lt;br /&gt;
 &lt;br /&gt;
 rex:IngridAlexandra fam:hasParent rex:HaakonMagnus .&lt;br /&gt;
 rex:SverreMagnus fam:hasParent rex:HaakonMagnus .&lt;br /&gt;
 rex:HaakonMagnus fam:hasParent rex:Harald .&lt;br /&gt;
 rex:MarthaLouise fam:hasParent rex:Harald .&lt;br /&gt;
 rex:HaakonMagnus fam:hasSister rex:MarthaLouise .&lt;br /&gt;
&lt;br /&gt;
 import rdflib&lt;br /&gt;
 &lt;br /&gt;
 g = rdflib.Graph()&lt;br /&gt;
 g.parse(&amp;quot;family.ttl&amp;quot;, format=&#039;ttl&#039;)&lt;br /&gt;
 &lt;br /&gt;
 qres = g.query(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
 PREFIX fam: &amp;lt;http://example.org/family#&amp;gt;&lt;br /&gt;
     SELECT ?child ?sister WHERE {&lt;br /&gt;
         ?child fam:hasParent ?parent .	&lt;br /&gt;
         ?parent fam:hasSister ?sister .&lt;br /&gt;
     }&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
 for row in qres:&lt;br /&gt;
     print(&amp;quot;%s has aunt %s&amp;quot; % row)&lt;br /&gt;
&lt;br /&gt;
With a prepared query, you can write the query once, and then bind some of the variables each time you use it:&lt;br /&gt;
 import rdflib&lt;br /&gt;
 &lt;br /&gt;
 g = rdflib.Graph()&lt;br /&gt;
 g.parse(&amp;quot;family.ttl&amp;quot;, format=&#039;ttl&#039;)&lt;br /&gt;
 &lt;br /&gt;
 q = rdflib.plugins.sparql.prepareQuery(&lt;br /&gt;
         &amp;quot;&amp;quot;&amp;quot;SELECT ?child ?sister WHERE {&lt;br /&gt;
                   ?child fam:hasParent ?parent .&lt;br /&gt;
                   ?parent fam:hasSister ?sister .&lt;br /&gt;
        }&amp;quot;&amp;quot;&amp;quot;,&lt;br /&gt;
        initNs = { &amp;quot;fam&amp;quot;: &amp;quot;http://example.org/family#&amp;quot;})&lt;br /&gt;
 &lt;br /&gt;
 sm = rdflib.URIRef(&amp;quot;http://example.org/royal#SverreMagnus&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 for row in g.query(q, initBindings={&#039;child&#039;: sm}):&lt;br /&gt;
         print(row)&lt;br /&gt;
&lt;br /&gt;
===Select all contents of lists (rdfllib.Collection)===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# rdflib.Collection has a different interntal structure so it requires a slightly more advance query. Here I am selecting all places that Emma has visited.&lt;br /&gt;
&lt;br /&gt;
PREFIX ex:   &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
PREFIX rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SELECT ?visit&lt;br /&gt;
WHERE {&lt;br /&gt;
  ex:Emma ex:visit/rdf:rest*/rdf:first ?visit&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Lecture 4- SPARQL PROGRAMMING==&lt;br /&gt;
&lt;br /&gt;
===Using parameters/variables in rdflib queries===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace, URIRef&lt;br /&gt;
from rdflib.plugins.sparql import prepareQuery&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.Cade, ex.livesIn, ex.France))&lt;br /&gt;
g.add((ex.Anne, ex.livesIn, ex.Norway))&lt;br /&gt;
g.add((ex.Sofie, ex.livesIn, ex.Sweden))&lt;br /&gt;
g.add((ex.Per, ex.livesIn, ex.Norway))&lt;br /&gt;
g.add((ex.John, ex.livesIn, ex.USA))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def find_people_from_country(country):&lt;br /&gt;
        country = URIRef(ex + country)&lt;br /&gt;
        q = prepareQuery(&lt;br /&gt;
         &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
         PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
         SELECT ?person WHERE { &lt;br /&gt;
         ?person ex:livesIn ?country.&lt;br /&gt;
         }&lt;br /&gt;
         &amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
        capital_result = g.query(q, initBindings={&#039;country&#039;: country})&lt;br /&gt;
&lt;br /&gt;
        for row in capital_result:&lt;br /&gt;
            print(row)&lt;br /&gt;
&lt;br /&gt;
find_people_from_country(&amp;quot;Norway&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===SELECTING data from Blazegraph via Python===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, JSON&lt;br /&gt;
&lt;br /&gt;
# This creates a server connection to the same URL that contains the graphic interface for Blazegraph. &lt;br /&gt;
# You also need to add &amp;quot;sparql&amp;quot; to end of the URL like below.&lt;br /&gt;
&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;http://localhost:9999/blazegraph/sparql&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# SELECT all triples in the database.&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    SELECT DISTINCT ?p WHERE {&lt;br /&gt;
    ?s ?p ?o.&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;p&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&lt;br /&gt;
# SELECT all interests of Cade&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
    SELECT DISTINCT ?interest WHERE {&lt;br /&gt;
    ex:Cade ex:interest ?interest.&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;interest&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Updating data from Blazegraph via Python===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, POST, DIGEST&lt;br /&gt;
&lt;br /&gt;
namespace = &amp;quot;kb&amp;quot;&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;http://localhost:9999/blazegraph/namespace/&amp;quot;+ namespace + &amp;quot;/sparql&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setMethod(POST)&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    PREFIX ex: &amp;lt;http://example.org/&amp;gt;&lt;br /&gt;
    INSERT DATA{&lt;br /&gt;
    ex:Cade ex:interest ex:Mathematics.&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
results = sparql.query()&lt;br /&gt;
print(results.response.read())&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===Retrieving data from Wikidata with SparqlWrapper===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, JSON&lt;br /&gt;
&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;https://query.wikidata.org/sparql&amp;quot;)&lt;br /&gt;
# In the query I want to select all the Vitamins in wikidata.&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    SELECT ?nutrient ?nutrientLabel WHERE&lt;br /&gt;
{&lt;br /&gt;
  ?nutrient wdt:P279 wd:Q34956.&lt;br /&gt;
  SERVICE wikibase:label { bd:serviceParam wikibase:language &amp;quot;[AUTO_LANGUAGE],en&amp;quot;. }&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;nutrient&amp;quot;][&amp;quot;value&amp;quot;], &amp;quot;   &amp;quot;, result[&amp;quot;nutrientLabel&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More examples can be found in the example section on the official query service here: https://query.wikidata.org/.&lt;br /&gt;
&lt;br /&gt;
===Download from BlazeGraph===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
Dumps a database to a local RDF file.&lt;br /&gt;
You need to install the SPARQLWrapper package first...&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
import datetime&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, RDFXML&lt;br /&gt;
&lt;br /&gt;
# your namespace, the default is &#039;kb&#039;&lt;br /&gt;
ns = &#039;kb&#039;&lt;br /&gt;
&lt;br /&gt;
# the SPARQL endpoint&lt;br /&gt;
endpoint = &#039;http://info216.i2s.uib.no/bigdata/namespace/&#039; + ns + &#039;/sparql&#039;&lt;br /&gt;
&lt;br /&gt;
# - the endpoint just moved, the old one was:&lt;br /&gt;
# endpoint = &#039;http://i2s.uib.no:8888/bigdata/namespace/&#039; + ns + &#039;/sparql&#039;&lt;br /&gt;
&lt;br /&gt;
# create wrapper&lt;br /&gt;
wrapper = SPARQLWrapper(endpoint)&lt;br /&gt;
&lt;br /&gt;
# prepare the SPARQL update&lt;br /&gt;
wrapper.setQuery(&#039;CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }&#039;)&lt;br /&gt;
wrapper.setReturnFormat(RDFXML)&lt;br /&gt;
&lt;br /&gt;
# execute the SPARQL update and convert the result to an rdflib.Graph &lt;br /&gt;
graph = wrapper.query().convert()&lt;br /&gt;
&lt;br /&gt;
# the destination file, with code to make it timestamped&lt;br /&gt;
destfile = &#039;rdf_dumps/slr-kg4news-&#039; + datetime.datetime.now().strftime(&#039;%Y%m%d-%H%M&#039;) + &#039;.rdf&#039;&lt;br /&gt;
&lt;br /&gt;
# serialize the result to file&lt;br /&gt;
graph.serialize(destination=destfile, format=&#039;ttl&#039;)&lt;br /&gt;
&lt;br /&gt;
# report and quit&lt;br /&gt;
print(&#039;Wrote %u triples to file %s .&#039; %&lt;br /&gt;
      (len(res), destfile))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Query Dbpedia with SparqlWrapper===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from SPARQLWrapper import SPARQLWrapper, JSON&lt;br /&gt;
&lt;br /&gt;
sparql = SPARQLWrapper(&amp;quot;http://dbpedia.org/sparql&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setQuery(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    PREFIX dbr: &amp;lt;http://dbpedia.org/resource/&amp;gt;&lt;br /&gt;
    PREFIX dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt;&lt;br /&gt;
    PREFIX rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt;&lt;br /&gt;
    SELECT ?comment&lt;br /&gt;
    WHERE {&lt;br /&gt;
    dbr:Barack_Obama rdfs:comment ?comment.&lt;br /&gt;
    FILTER (langMatches(lang(?comment),&amp;quot;en&amp;quot;))&lt;br /&gt;
    }&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sparql.setReturnFormat(JSON)&lt;br /&gt;
results = sparql.query().convert()&lt;br /&gt;
&lt;br /&gt;
for result in results[&amp;quot;results&amp;quot;][&amp;quot;bindings&amp;quot;]:&lt;br /&gt;
    print(result[&amp;quot;comment&amp;quot;][&amp;quot;value&amp;quot;])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lecture 5: RDFS==&lt;br /&gt;
&lt;br /&gt;
===RDFS-plus (OWL) Properties===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
g.add((ex.married, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
g.add((ex.married, RDF.type, OWL.IrreflexiveProperty))&lt;br /&gt;
g.add((ex.livesWith, RDF.type, OWL.ReflexiveProperty))&lt;br /&gt;
g.add((ex.livesWith, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
g.add((ex.sibling, RDF.type, OWL.TransitiveProperty))&lt;br /&gt;
g.add((ex.sibling, RDF.type, OWL.SymmetricProperty))&lt;br /&gt;
g.add((ex.sibling, RDF.type, OWL.IrreflexiveProperty))&lt;br /&gt;
g.add((ex.hasFather, RDF.type, OWL.FunctionalProperty))&lt;br /&gt;
g.add((ex.hasFather, RDF.type, OWL.AsymmetricProperty))&lt;br /&gt;
g.add((ex.hasFather, RDF.type, OWL.IrreflexiveProperty))&lt;br /&gt;
g.add((ex.fatherOf, RDF.type, OWL.AsymmetricProperty))&lt;br /&gt;
g.add((ex.fatherOf, RDF.type, OWL.IrreflexiveProperty))&lt;br /&gt;
&lt;br /&gt;
# Sometimes there is no definite answer, and it comes down to how we want to model our properties&lt;br /&gt;
# e.g is livesWith a transitive property? Usually yes, but we can also want to specify that a child lives with both of her divorced parents.&lt;br /&gt;
# which means that: (mother livesWith child % child livesWith father) != mother livesWith father. Which makes it non-transitive.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RDFS inference with RDFLib===&lt;br /&gt;
You can use the OWL-RL package to add inference capabilities to RDFLib. Download it [https://github.com/RDFLib/OWL-RL GitHub] and copy the &#039;&#039;owlrl&#039;&#039; subfolder into your project folder next to your Python files.&lt;br /&gt;
&lt;br /&gt;
[https://owl-rl.readthedocs.io/en/latest/owlrl.html OWL-RL documentation.]&lt;br /&gt;
&lt;br /&gt;
Example program to get started:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import rdflib.plugins.sparql.update&lt;br /&gt;
import owlrl.RDFSClosure&lt;br /&gt;
&lt;br /&gt;
g = rdflib.Graph()&lt;br /&gt;
&lt;br /&gt;
ex = rdflib.Namespace(&#039;http://example.org#&#039;)&lt;br /&gt;
g.bind(&#039;&#039;, ex)&lt;br /&gt;
&lt;br /&gt;
g.update(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org#&amp;gt;&lt;br /&gt;
PREFIX owl: &amp;lt;http://www.w3.org/2002/07/owl#&amp;gt;&lt;br /&gt;
INSERT DATA {&lt;br /&gt;
    ex:Socrates rdf:type ex:Man .&lt;br /&gt;
    ex:Man rdfs:subClassOf ex:Mortal .&lt;br /&gt;
}&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# The next three lines add inferred triples to g.&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
rdfs.closure()&lt;br /&gt;
rdfs.flush_stored_triples()&lt;br /&gt;
&lt;br /&gt;
b = g.query(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org#&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
    ex:Socrates rdf:type ex:Mortal .&lt;br /&gt;
} &lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
print(&#039;Result: &#039; + bool(b))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Languaged tagged RDFS labels=== &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Namespace, Literal&lt;br /&gt;
from rdflib.namespace import RDFS&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
g.add((ex.France, RDFS.label, Literal(&amp;quot;Frankrike&amp;quot;, lang=&amp;quot;no&amp;quot;)))&lt;br /&gt;
g.add((ex.France, RDFS.label, Literal(&amp;quot;France&amp;quot;, lang=&amp;quot;en&amp;quot;)))&lt;br /&gt;
g.add((ex.France, RDFS.label, Literal(&amp;quot;Francia&amp;quot;, lang=&amp;quot;es&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lecture 6: RDFS Plus / OWL == &lt;br /&gt;
===RDFS Plus / OWL inference with RDFLib=== &lt;br /&gt;
&lt;br /&gt;
You can use the OWL-RL package again as for Lecture 5.&lt;br /&gt;
&lt;br /&gt;
Instead of: &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# The next three lines add inferred triples to g.&lt;br /&gt;
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)&lt;br /&gt;
rdfs.closure()&lt;br /&gt;
rdfs.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
you can write this to get both RDFS and basic RDFS Plus / OWL inference:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
# The next three lines add inferred triples to g.&lt;br /&gt;
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)&lt;br /&gt;
owl.closure()&lt;br /&gt;
owl.flush_stored_triples()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example updates and queries:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
PREFIX rdfs: &amp;lt;http://www.w3.org/2000/01/rdf-schema#&amp;gt;&lt;br /&gt;
PREFIX owl: &amp;lt;http://www.w3.org/2002/07/owl#&amp;gt;&lt;br /&gt;
PREFIX ex: &amp;lt;http://example.org#&amp;gt;&lt;br /&gt;
&lt;br /&gt;
INSERT DATA {&lt;br /&gt;
    ex:Socrates ex:hasWife ex:Xanthippe .&lt;br /&gt;
    ex:hasHusband owl:inverseOf ex:hasWife .&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
   ex:Xanthippe ex:hasHusband ex:Socrates .&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
   ex:Socrates ^ex:hasHusband ex:Xanthippe .&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
INSERT DATA {&lt;br /&gt;
    ex:hasWife rdfs:subPropertyOf ex:hasSpouse .&lt;br /&gt;
    ex:hasSpouse rdf:type owl:SymmetricProperty . &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
   ex:Socrates ex:hasSpouse ex:Xanthippe .&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ASK {&lt;br /&gt;
   ex:Socrates ^ex:hasSpouse ex:Xanthippe .&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Semantic Lifting - CSV==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, FOAF, RDFS, OWL&lt;br /&gt;
import pandas as pd&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
&lt;br /&gt;
# Load the CSV data as a pandas Dataframe.&lt;br /&gt;
csv_data = pd.read_csv(&amp;quot;task1.csv&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Here I deal with spaces (&amp;quot; &amp;quot;) in the data. I replace them with &amp;quot;_&amp;quot; so that URI&#039;s become valid.&lt;br /&gt;
csv_data = csv_data.replace(to_replace=&amp;quot; &amp;quot;, value=&amp;quot;_&amp;quot;, regex=True)&lt;br /&gt;
&lt;br /&gt;
# Here I mark all missing/empty data as &amp;quot;unknown&amp;quot;. This makes it easy to delete triples containing this later.&lt;br /&gt;
csv_data = csv_data.fillna(&amp;quot;unknown&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Loop through the CSV data, and then make RDF triples.&lt;br /&gt;
for index, row in csv_data.iterrows():&lt;br /&gt;
    # The names of the people act as subjects.&lt;br /&gt;
    subject = row[&#039;Name&#039;]&lt;br /&gt;
    # Create triples: e.g. &amp;quot;Cade_Tracey - age - 27&amp;quot;&lt;br /&gt;
    g.add((URIRef(ex + subject), URIRef(ex + &amp;quot;age&amp;quot;), Literal(row[&amp;quot;Age&amp;quot;])))&lt;br /&gt;
    g.add((URIRef(ex + subject), URIRef(ex + &amp;quot;married&amp;quot;), URIRef(ex + row[&amp;quot;Spouse&amp;quot;])))&lt;br /&gt;
    g.add((URIRef(ex + subject), URIRef(ex + &amp;quot;country&amp;quot;), URIRef(ex + row[&amp;quot;Country&amp;quot;])))&lt;br /&gt;
&lt;br /&gt;
    # If We want can add additional RDF/RDFS/OWL information e.g&lt;br /&gt;
    g.add((URIRef(ex + subject), RDF.type, FOAF.Person))&lt;br /&gt;
&lt;br /&gt;
# I remove triples that I marked as unknown earlier.&lt;br /&gt;
g.remove((None, None, URIRef(&amp;quot;http://example.org/unknown&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
# Clean printing of the graph.&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===CSV file for above example===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;quot;Name&amp;quot;,&amp;quot;Age&amp;quot;,&amp;quot;Spouse&amp;quot;,&amp;quot;Country&amp;quot;&lt;br /&gt;
&amp;quot;Cade Tracey&amp;quot;,&amp;quot;26&amp;quot;,&amp;quot;Mary Jackson&amp;quot;,&amp;quot;US&amp;quot;&lt;br /&gt;
&amp;quot;Bob Johnson&amp;quot;,&amp;quot;21&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;Canada&amp;quot;&lt;br /&gt;
&amp;quot;Mary Jackson&amp;quot;,&amp;quot;25&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;France&amp;quot;&lt;br /&gt;
&amp;quot;Phil Philips&amp;quot;,&amp;quot;32&amp;quot;,&amp;quot;Catherine Smith&amp;quot;,&amp;quot;Japan&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Semantic Lifting - XML==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, URIRef&lt;br /&gt;
from rdflib.namespace import RDF, XSD, RDFS&lt;br /&gt;
import xml.etree.ElementTree as ET&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/TV/&amp;quot;)&lt;br /&gt;
prov = Namespace(&amp;quot;http://www.w3.org/ns/prov#&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;prov&amp;quot;, prov)&lt;br /&gt;
&lt;br /&gt;
tree = ET.parse(&amp;quot;tv_shows.xml&amp;quot;)&lt;br /&gt;
root = tree.getroot()&lt;br /&gt;
&lt;br /&gt;
for tv_show in root.findall(&#039;tv_show&#039;):&lt;br /&gt;
    show_id = tv_show.attrib[&amp;quot;id&amp;quot;]&lt;br /&gt;
    title = tv_show.find(&amp;quot;title&amp;quot;).text&lt;br /&gt;
&lt;br /&gt;
    g.add((URIRef(ex + show_id), ex.title, Literal(title, datatype=XSD.string)))&lt;br /&gt;
    g.add((URIRef(ex + show_id), RDF.type, ex.TV_Show))&lt;br /&gt;
&lt;br /&gt;
    for actor in tv_show.findall(&amp;quot;actor&amp;quot;):&lt;br /&gt;
        first_name = actor.find(&amp;quot;firstname&amp;quot;).text&lt;br /&gt;
        last_name = actor.find(&amp;quot;lastname&amp;quot;).text&lt;br /&gt;
        full_name = first_name + &amp;quot;_&amp;quot; + last_name&lt;br /&gt;
        &lt;br /&gt;
        g.add((URIRef(ex + show_id), ex.stars, URIRef(ex + full_name)))&lt;br /&gt;
        g.add((URIRef(ex + full_name), ex.starsIn, URIRef(title)))&lt;br /&gt;
        g.add((URIRef(ex + full_name), RDF.type, ex.Actor))&lt;br /&gt;
&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===XML Data for above example===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;data&amp;gt;&lt;br /&gt;
    &amp;lt;tv_show id=&amp;quot;1050&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;title&amp;gt;The_Sopranos&amp;lt;/title&amp;gt;&lt;br /&gt;
        &amp;lt;actor&amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;James&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Gandolfini&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/actor&amp;gt;&lt;br /&gt;
    &amp;lt;/tv_show&amp;gt;&lt;br /&gt;
    &amp;lt;tv_show id=&amp;quot;1066&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;title&amp;gt;Seinfeld&amp;lt;/title&amp;gt;&lt;br /&gt;
        &amp;lt;actor&amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Jerry&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Seinfeld&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/actor&amp;gt;&lt;br /&gt;
        &amp;lt;actor&amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Julia&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Louis-dreyfus&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/actor&amp;gt;&lt;br /&gt;
        &amp;lt;actor&amp;gt;&lt;br /&gt;
            &amp;lt;firstname&amp;gt;Jason&amp;lt;/firstname&amp;gt;&lt;br /&gt;
            &amp;lt;lastname&amp;gt;Alexander&amp;lt;/lastname&amp;gt;&lt;br /&gt;
        &amp;lt;/actor&amp;gt;&lt;br /&gt;
    &amp;lt;/tv_show&amp;gt;&lt;br /&gt;
&amp;lt;/data&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Semantic Lifting - HTML==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
from bs4 import BeautifulSoup as bs, NavigableString&lt;br /&gt;
from rdflib import Graph, URIRef, Namespace&lt;br /&gt;
from rdflib.namespace import RDF&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
&lt;br /&gt;
html = open(&amp;quot;tv_shows.html&amp;quot;).read()&lt;br /&gt;
html = bs(html, features=&amp;quot;html.parser&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
shows = html.find_all(&#039;li&#039;, attrs={&#039;class&#039;: &#039;show&#039;})&lt;br /&gt;
for show in shows:&lt;br /&gt;
    title = show.find(&amp;quot;h3&amp;quot;).text&lt;br /&gt;
    actors = show.find(&#039;ul&#039;, attrs={&#039;class&#039;: &#039;actor_list&#039;})&lt;br /&gt;
    for actor in actors:&lt;br /&gt;
        if isinstance(actor, NavigableString):&lt;br /&gt;
            continue&lt;br /&gt;
        else:&lt;br /&gt;
            actor = actor.text.replace(&amp;quot; &amp;quot;, &amp;quot;_&amp;quot;)&lt;br /&gt;
            g.add((URIRef(ex + title), ex.stars, URIRef(ex + actor)))&lt;br /&gt;
            g.add((URIRef(ex + actor), RDF.type, ex.Actor))&lt;br /&gt;
&lt;br /&gt;
    g.add((URIRef(ex + title), RDF.type, ex.TV_Show))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
print(g.serialize(format=&amp;quot;turtle&amp;quot;).decode())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTML code for the example above===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE html&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
    &amp;lt;meta charset=&amp;quot;utf-8&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
    &amp;lt;div class=&amp;quot;tv_shows&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;ul&amp;gt;&lt;br /&gt;
            &amp;lt;li class=&amp;quot;show&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;h3&amp;gt;The_Sopranos&amp;lt;/h3&amp;gt;&lt;br /&gt;
                &amp;lt;div class=&amp;quot;irrelevant_data&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
                &amp;lt;ul class=&amp;quot;actor_list&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;li&amp;gt;James Gandolfini&amp;lt;/li&amp;gt;&lt;br /&gt;
                &amp;lt;/ul&amp;gt;&lt;br /&gt;
            &amp;lt;/li&amp;gt;&lt;br /&gt;
            &amp;lt;li class=&amp;quot;show&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;h3&amp;gt;Seinfeld&amp;lt;/h3&amp;gt;&lt;br /&gt;
                &amp;lt;div class=&amp;quot;irrelevant_data&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
                &amp;lt;ul class=&amp;quot;actor_list&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;li &amp;gt;Jerry Seinfeld&amp;lt;/li&amp;gt;&lt;br /&gt;
                    &amp;lt;li&amp;gt;Jason Alexander&amp;lt;/li&amp;gt;&lt;br /&gt;
                    &amp;lt;li&amp;gt;Julia Louis-Dreyfus&amp;lt;/li&amp;gt;&lt;br /&gt;
                &amp;lt;/ul&amp;gt;&lt;br /&gt;
            &amp;lt;/li&amp;gt;&lt;br /&gt;
        &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==WEB API Calls (In this case JSON)==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import requests&lt;br /&gt;
import json&lt;br /&gt;
import pprint&lt;br /&gt;
&lt;br /&gt;
# Retrieve JSON data from API service URL. Then load it with the json library as a json object.&lt;br /&gt;
url = &amp;quot;http://api.geonames.org/postalCodeLookupJSON?postalcode=46020&amp;amp;#country=ES&amp;amp;username=demo&amp;quot;&lt;br /&gt;
data = requests.get(url).content.decode(&amp;quot;utf-8&amp;quot;)&lt;br /&gt;
data = json.loads(data)&lt;br /&gt;
pprint.pprint(data)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==JSON-LD==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import rdflib&lt;br /&gt;
&lt;br /&gt;
g = rdflib.Graph()&lt;br /&gt;
&lt;br /&gt;
example = &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;@context&amp;quot;: {&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;http://xmlns.com/foaf/0.1/name&amp;quot;,&lt;br /&gt;
    &amp;quot;homepage&amp;quot;: {&lt;br /&gt;
      &amp;quot;@id&amp;quot;: &amp;quot;http://xmlns.com/foaf/0.1/homepage&amp;quot;,&lt;br /&gt;
      &amp;quot;@type&amp;quot;: &amp;quot;@id&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;@id&amp;quot;: &amp;quot;http://me.markus-lanthaler.com/&amp;quot;,&lt;br /&gt;
  &amp;quot;name&amp;quot;: &amp;quot;Markus Lanthaler&amp;quot;,&lt;br /&gt;
  &amp;quot;homepage&amp;quot;: &amp;quot;http://www.markus-lanthaler.com/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# json-ld parsing automatically deals with @contexts&lt;br /&gt;
g.parse(data=example, format=&#039;json-ld&#039;)&lt;br /&gt;
&lt;br /&gt;
# serialisation does expansion by default&lt;br /&gt;
for line in g.serialize(format=&#039;json-ld&#039;).decode().splitlines():&lt;br /&gt;
    print(line)&lt;br /&gt;
&lt;br /&gt;
# by supplying a context object, serialisation can do compaction&lt;br /&gt;
context = {&lt;br /&gt;
    &amp;quot;foaf&amp;quot;: &amp;quot;http://xmlns.com/foaf/0.1/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
for line in g.serialize(format=&#039;json-ld&#039;, context=context).decode().splitlines():&lt;br /&gt;
    print(line)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;credits&amp;quot; style=&amp;quot;text-align: right; direction: ltr; margin-left: 1em;&amp;quot;&amp;gt;&#039;&#039;INFO216, UiB, 2017-2020. All code examples are [https://creativecommons.org/choose/zero/ CC0].&#039;&#039; &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==OWL - Complex Classes and Restrictions==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
import owlrl&lt;br /&gt;
from rdflib import Graph, Literal, Namespace, BNode&lt;br /&gt;
from rdflib.namespace import RDF, OWL, RDFS&lt;br /&gt;
from rdflib.collection import Collection&lt;br /&gt;
&lt;br /&gt;
g = Graph()&lt;br /&gt;
ex = Namespace(&amp;quot;http://example.org/&amp;quot;)&lt;br /&gt;
g.bind(&amp;quot;ex&amp;quot;, ex)&lt;br /&gt;
g.bind(&amp;quot;owl&amp;quot;, OWL)&lt;br /&gt;
&lt;br /&gt;
# a Season is either Autumn, Winter, Spring, Summer&lt;br /&gt;
seasons = BNode()&lt;br /&gt;
Collection(g, seasons, [ex.Winter, ex.Autumn, ex.Spring, ex.Summer])&lt;br /&gt;
g.add((ex.Season, OWL.oneOf, seasons))&lt;br /&gt;
&lt;br /&gt;
# A Parent is a Father or Mother&lt;br /&gt;
b = BNode()&lt;br /&gt;
Collection(g, b, [ex.Father, ex.Mother])&lt;br /&gt;
g.add((ex.Parent, OWL.unionOf, b))&lt;br /&gt;
&lt;br /&gt;
# A Woman is a person who has the &amp;quot;female&amp;quot; gender&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.gender))&lt;br /&gt;
g.add((br, OWL.hasValue, ex.Female))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Woman, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# A vegetarian is a Person who only eats vegetarian food&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.eats))&lt;br /&gt;
g.add((br, OWL.allValuesFrom, ex.VeganFood))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Vegetarian, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# A vegetarian is a Person who can not eat meat.&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.eats))&lt;br /&gt;
g.add((br, OWL.QualifiedCardinality, Literal(0)))&lt;br /&gt;
g.add((br, OWL.onClass, ex.Meat))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Person, br])&lt;br /&gt;
g.add((ex.Vegetarian, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# A Worried Parent is a parent who has at least one sick child&lt;br /&gt;
br = BNode()&lt;br /&gt;
g.add((br, RDF.type, OWL.Restriction))&lt;br /&gt;
g.add((br, OWL.onProperty, ex.hasChild))&lt;br /&gt;
g.add((br, OWL.QualifiedMinCardinality, Literal(1)))&lt;br /&gt;
g.add((br, OWL.onClass, ex.Sick))&lt;br /&gt;
bi = BNode()&lt;br /&gt;
Collection(g, bi, [ex.Parent, br])&lt;br /&gt;
g.add((ex.WorriedParent, OWL.intersectionOf, bi))&lt;br /&gt;
&lt;br /&gt;
# using the restriction above, If we now write...: &lt;br /&gt;
g.add((ex.Bob, RDF.type, ex.Parent))&lt;br /&gt;
g.add((ex.Bob, ex.hasChild, ex.John))&lt;br /&gt;
g.add((ex.John, RDF.type, ex.Sick))&lt;br /&gt;
# ...we can infer with owl reasoning that Bob is a worried Parent even though we didn&#039;t specify it ourselves because Bob fullfills the restriction and Parent requirements.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
	<entry>
		<id>http://info216.wiki.uib.no/index.php?title=Lab:_Getting_started_with_VSCode,_Python_and_RDFlib&amp;diff=1440</id>
		<title>Lab: Getting started with VSCode, Python and RDFlib</title>
		<link rel="alternate" type="text/html" href="http://info216.wiki.uib.no/index.php?title=Lab:_Getting_started_with_VSCode,_Python_and_RDFlib&amp;diff=1440"/>
		<updated>2021-01-25T15:48:57Z</updated>

		<summary type="html">&lt;p&gt;Sas020: Oppdaterte lenken til forelesningsnotater til å lenke til årets.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lab 1: Getting started with Pycharm, Python and RDFlib=&lt;br /&gt;
&lt;br /&gt;
==Topics== &lt;br /&gt;
Today we have 2 goals. &lt;br /&gt;
1. To setup a programming environment that you will use for the lab excercised and group project (if you don&#039;t already have one). &lt;br /&gt;
2. To start some basic programming of RDF. &lt;br /&gt;
&lt;br /&gt;
Installation of and introduction to Python, Pycharm, Pip and RDFlib.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PyCharm&#039;&#039;&#039; is a Python Integrated Development Environment (IDE) that can be used for writing python code.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;Pip&#039;&#039;&#039; command is used to easily install additional python packages for your coding environment.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;RDFLib&#039;&#039;&#039; is a Python package for working with RDF. With this we can create RDF graphs, parse and serialize RDF, perform SPARQL queries on graphs and more.&lt;br /&gt;
&lt;br /&gt;
==Tasks==&lt;br /&gt;
&lt;br /&gt;
You likely already have &#039;&#039;&#039;Python&#039;&#039;&#039; installed on your computer (it comes with MAC for instance). To find out, type &#039;python&#039; in  the commandline on windows or type &#039;python --version&#039; in the terminal on your MAC. &lt;br /&gt;
If you don&#039;t already have Python, download and install the Python for your operative system [https://www.python.org/downloads/ here.]&lt;br /&gt;
We recommend downloading the most recent version. &lt;br /&gt;
&lt;br /&gt;
If you are already using a different Python IDE than Pycharm that you are comfortable with, then you are free to use that program instead.&lt;br /&gt;
&lt;br /&gt;
Otherwise Download and Install the free and open source &#039;&#039;&#039;Pycharm Community Edition&#039;&#039;&#039; IDE [https://www.jetbrains.com/pycharm/download/ here.]&lt;br /&gt;
&lt;br /&gt;
Start Pycharm and create a new project with a new .py file.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;RDFlib&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
Using the Pycharm terminal, install RDFlib by simply entering: pip install rdflib&lt;br /&gt;
&lt;br /&gt;
To import rdflib to your .py file, write &#039;import rdflib&#039; at the top of the file. &lt;br /&gt;
Alternatively to import certain modules of rdflib, you can write e.g &#039;from rdflib import Graph&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Coding Tasks&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When solving the coding tasks you can look at the readings below if needed.&lt;br /&gt;
Use the Classes/interfaces and methods listed at the bottom of the page. ()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1.&#039;&#039;&#039; Write a program that creates an RDF graph containing the triples from the following sentences. Note that one sentence could result in more than one triple.&lt;br /&gt;
When solving these particular tasks I would try to avoid using Blank Nodes, and instead use full URI&#039;s instead. For URIs you can just use an example URI like &amp;quot;http://example.org/&amp;quot;. &lt;br /&gt;
This means that if you talk about a person called Cade, the URI could be  &amp;quot;http://example.org/Cade&amp;quot;. Remember the Namespaces can be used so that you don&#039;t have to write the full URI everytime.&lt;br /&gt;
&lt;br /&gt;
* Cade is married to Mary &lt;br /&gt;
* The capital of France is Paris&lt;br /&gt;
* Cade is 27 years old&lt;br /&gt;
* 26 years is the age of Mary&lt;br /&gt;
* Marys interests include hiking, chocolate and biology&lt;br /&gt;
* Mary is a student&lt;br /&gt;
* Paris is a City in France&lt;br /&gt;
* Cade and Mary are kind people&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==If you have more time...==&lt;br /&gt;
If you have more time you can continue extending your graph with some of the stuff from lab 2:&lt;br /&gt;
&#039;&#039;&amp;quot;Cade has the full name Cade Tracey. He 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.&amp;quot;&#039;&#039;&lt;br /&gt;
Try to use as many different methods as possible to create the triples.&lt;br /&gt;
&lt;br /&gt;
==Documentation and other useful reading==&lt;br /&gt;
If needed here is an introduction of how to create projects and python files in Pycharm [https://www.jetbrains.com/help/pycharm/creating-and-running-your-first-python-project.html Pycharm - Tutorial by jetbrains.com].&lt;br /&gt;
&lt;br /&gt;
[https://rdflib.readthedocs.io/en/stable/index.html# RDFLib Documentation]- Todays focus are the following:&lt;br /&gt;
* [https://rdflib.readthedocs.io/en/stable/gettingstarted.html Intro to RDFlib]&lt;br /&gt;
* [https://rdflib.readthedocs.io/en/stable/intro_to_creating_rdf.html  Intro to creating triples] &lt;br /&gt;
&lt;br /&gt;
Additional recommended in depth documentation:&lt;br /&gt;
* [https://rdflib.readthedocs.io/en/stable/rdf_terms.html RDF Terms]&lt;br /&gt;
* [https://rdflib.readthedocs.io/en/stable/namespaces_and_bindings.html Namespaces and Bindings]&lt;br /&gt;
&lt;br /&gt;
[[:File:S01-KnowledgeGraphs.pdf | Slides from the lecture]]&lt;br /&gt;
&lt;br /&gt;
==Relevant RDFlib interfaces (and methods)==&lt;br /&gt;
import [https://rdflib.readthedocs.io/en/stable/py-modindex.html rdflib]:&lt;br /&gt;
*Graph (add)&lt;br /&gt;
*URIRef&lt;br /&gt;
*Literal&lt;br /&gt;
*NameSpace&lt;br /&gt;
*Bnode&lt;br /&gt;
*RDF, FOAF&lt;br /&gt;
&lt;br /&gt;
All RDFlib modules can be found [https://rdflib.readthedocs.io/en/stable/py-modindex.html here]&lt;br /&gt;
Browser search (often Ctrl-F) is useful here to find the module that you want.&lt;/div&gt;</summary>
		<author><name>Sas020</name></author>
	</entry>
</feed>