#basic exercise of RDFLib

#while you could load/run this file in Python, we suggest you cut/paste
#sections of it into your intpreter so you can play with the results
#interactively

#load the library
from rdflib import *

#create a shortcut for the base of the URI we will be working with
fb = Namespace("http://rdf.freebase.com/example/")
g = ConjunctiveGraph() #create the triple store in memory

#g.bind("fb", fb) #give our namespace a pretty name in the graph

#now we can use br instead of http://rdf.freebase.com/example/blade_runner
br = fb["blade_runner"] 

#add two triples describing blade runner
g.add((br, fb["name"], Literal("Blade Runner")))
g.add((br, fb["release_date"], Literal("Jun 25, 1982")))

#do the same for harrison ford
hf = fb["harrison_ford"]
g.add((hf, fb["name"], Literal("Harrison Ford")))
g.add((hf, fb["birth_date"], Literal("Jul 13, 1942")))

#make harrison ford an actor that was in blade runner
g.add((br, fb["actor"], hf))

#show us the graph we have made in the simplest of output - NTriples
g.serialize(format="nt")

#Show it to us in a nicely formated XML
g.serialize(format="pretty-xml")


###################

#create a new graph
g = ConjunctiveGraph()

fb = Namespace("http://rdf.freebase.com/example/")
g.bind("fb", fb)

#use Linked Open Data (LOD) to retrieve what Freebase knows about St. Paul, MN, USA
g.parse("http://rdf.freebase.com/ns/en.saint_paul")

#show everything in the graph that has a location.location.people_born_here predicate
list(g.triples((None, fb["location.location.people_born_here"], None)))

#where was Jamie Taylor born?
list(g.triples((None, fb["location.location.people_born_here"], fb["en.jamie_taylor"])))


###################

#a new graph
g = ConjunctiveGraph()

fb = Namespace("http://rdf.freebase.com/ns/")
g.bind("fb", fb)

#get some data from the LOD Cloud
g.parse("http://rdf.freebase.com/ns/en.mel_blanc")
g.parse("http://rdf.freebase.com/ns/en.monica_lewinsky")
g.parse("http://rdf.freebase.com/ns/en.harrison_ford")
g.parse("http://rdf.freebase.com/ns/en.jamie_taylor")

#who was born in San Francisco?
list(g.triples((None, fb["people.person.place_of_birth"], fb["en.san_francisco"])))


#####################
# SPARQL in RDFLib - not covered in session
#####################

#what do Monica Lewinsky and Mel Blanc have in common?

oursparql = """
SELECT ?what WHERE {
    fb:en.mel_blanc ?what ?something .
    fb:en.monica_lewinsky ?what ?something .
}"""

#the query method takes an optional parameter "initNs" which allows you to 
#define namespace prefixes used in the query - here we associate it with
#the fb namespace we defined previously when we were fetching the linked data above

results = g.query(oursparql, initNs={'fb':fb}).serialize(format="xml") 
print results

#we can also run the query using standard SPARQL prefixes

oursparql = """
PREFIX fb:<http://rdf.freebase.com/ns/>
SELECT ?what WHERE {
    fb:en.mel_blanc ?what ?something .
    fb:en.monica_lewinsky ?what ?something .
}"""

#instead of xml results - look at the items returned as Python tuples
results = g.query(oursparql)

for t in results:
    print t




