In [27]:
data = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@base <http://my.org/> .
@prefix : <#> .
"""
In [28]:
data += """
:Person a rdfs:Class .
:Man a rdfs:Class ; rdfs:subClassOf :Person .
:Woman a rdfs:Class ; rdfs:subClassOf :Person .
"""
In [29]:
data += """
:tom a :Man ; foaf:name "Tom" .
:jane a :Woman ; foaf:name "Jane" .
:jack a :Man .
:jack foaf:name "Big Jack" .
:tom foaf:knows :jane .
:jane foaf:knows :tom .
:jane foaf:knows :jack .
:jack foaf:knows :jane .
:jane foaf:age 28 .
:tom :brother :jack .
:jack :brother :tom .
:jane :brother :jack .
:brother a rdf:Property .
:sister a rdf:Property .
:brother rdfs:domain :Person; rdfs:range :Man .
:sister rdfs:domain :Person; rdfs:range :Woman .
:jane :child :albert .
:jane :child :martha .
:jane :age 28 .
:jane :eyecolor "blue" .
# here I name thee..
:albert foaf:name "Albert" .
:martha foaf:name "Martha" .
"""
In [30]:
# pip install rdflib
In [31]:
import rdflib
g = rdflib.Graph()
g.parse(data=data, format='turtle')
print(len(g))
31
In [32]:
for s, p, o in g:
print(s, p, o)
http://my.org/#tom http://xmlns.com/foaf/0.1/knows http://my.org/#jane http://my.org/#jack http://my.org/#brother http://my.org/#tom http://my.org/#jane http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://my.org/#Woman http://my.org/#sister http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property http://my.org/#jack http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://my.org/#Man http://my.org/#brother http://www.w3.org/2000/01/rdf-schema#range http://my.org/#Man http://my.org/#jane http://my.org/#eyecolor blue http://my.org/#Woman http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class http://my.org/#martha http://xmlns.com/foaf/0.1/name Martha http://my.org/#brother http://www.w3.org/2000/01/rdf-schema#domain http://my.org/#Person http://my.org/#jane http://xmlns.com/foaf/0.1/age 28 http://my.org/#jack http://xmlns.com/foaf/0.1/name Big Jack http://my.org/#brother http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property http://my.org/#jane http://my.org/#brother http://my.org/#jack http://my.org/#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class http://my.org/#tom http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://my.org/#Man http://my.org/#sister http://www.w3.org/2000/01/rdf-schema#range http://my.org/#Woman http://my.org/#jane http://my.org/#child http://my.org/#martha http://my.org/#jane http://my.org/#child http://my.org/#albert http://my.org/#jack http://xmlns.com/foaf/0.1/knows http://my.org/#jane http://my.org/#sister http://www.w3.org/2000/01/rdf-schema#domain http://my.org/#Person http://my.org/#Man http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class http://my.org/#jane http://xmlns.com/foaf/0.1/name Jane http://my.org/#tom http://xmlns.com/foaf/0.1/name Tom http://my.org/#Woman http://www.w3.org/2000/01/rdf-schema#subClassOf http://my.org/#Person http://my.org/#jane http://xmlns.com/foaf/0.1/knows http://my.org/#tom http://my.org/#albert http://xmlns.com/foaf/0.1/name Albert http://my.org/#tom http://my.org/#brother http://my.org/#jack http://my.org/#jane http://my.org/#age 28 http://my.org/#jane http://xmlns.com/foaf/0.1/knows http://my.org/#jack http://my.org/#Man http://www.w3.org/2000/01/rdf-schema#subClassOf http://my.org/#Person
In [33]:
for s, p, o in g.query("SELECT ?s ?p ?o WHERE { ?s ?p ?o }"):
print(s, p, o)
http://my.org/#tom http://xmlns.com/foaf/0.1/knows http://my.org/#jane http://my.org/#jack http://my.org/#brother http://my.org/#tom http://my.org/#jane http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://my.org/#Woman http://my.org/#sister http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property http://my.org/#jack http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://my.org/#Man http://my.org/#brother http://www.w3.org/2000/01/rdf-schema#range http://my.org/#Man http://my.org/#jane http://my.org/#eyecolor blue http://my.org/#Woman http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class http://my.org/#martha http://xmlns.com/foaf/0.1/name Martha http://my.org/#brother http://www.w3.org/2000/01/rdf-schema#domain http://my.org/#Person http://my.org/#jane http://xmlns.com/foaf/0.1/age 28 http://my.org/#jack http://xmlns.com/foaf/0.1/name Big Jack http://my.org/#brother http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property http://my.org/#jane http://my.org/#brother http://my.org/#jack http://my.org/#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class http://my.org/#tom http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://my.org/#Man http://my.org/#sister http://www.w3.org/2000/01/rdf-schema#range http://my.org/#Woman http://my.org/#jane http://my.org/#child http://my.org/#martha http://my.org/#jane http://my.org/#child http://my.org/#albert http://my.org/#jack http://xmlns.com/foaf/0.1/knows http://my.org/#jane http://my.org/#sister http://www.w3.org/2000/01/rdf-schema#domain http://my.org/#Person http://my.org/#Man http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class http://my.org/#jane http://xmlns.com/foaf/0.1/name Jane http://my.org/#tom http://xmlns.com/foaf/0.1/name Tom http://my.org/#Woman http://www.w3.org/2000/01/rdf-schema#subClassOf http://my.org/#Person http://my.org/#jane http://xmlns.com/foaf/0.1/knows http://my.org/#tom http://my.org/#albert http://xmlns.com/foaf/0.1/name Albert http://my.org/#tom http://my.org/#brother http://my.org/#jack http://my.org/#jane http://my.org/#age 28 http://my.org/#jane http://xmlns.com/foaf/0.1/knows http://my.org/#jack http://my.org/#Man http://www.w3.org/2000/01/rdf-schema#subClassOf http://my.org/#Person
In [34]:
q = """
SELECT ?who
WHERE {
?who <http://xmlns.com/foaf/0.1/knows> <http://my.org/#jack>
}
"""
for row in g.query(q):
print(row)
(rdflib.term.URIRef('http://my.org/#jane'),)
In [35]:
q = """
SELECT ?who
WHERE {
?who <http://xmlns.com/foaf/0.1/knows> <http://my.org/#jack>
}
"""
for row in g.query(q):
print(row['who'])
http://my.org/#jane
In [36]:
q = """
SELECT ?name WHERE {
?s <http://xmlns.com/foaf/0.1/knows> <http://my.org/#jack> .
?s <http://xmlns.com/foaf/0.1/name> ?name
}
"""
for row in g.query(q):
print(row['name'])
Jane
In [37]:
q = """
SELECT ?who WHERE {
?sub <http://xmlns.com/foaf/0.1/name> "Jane" .
?sub <http://xmlns.com/foaf/0.1/knows> ?obj .
?obj <http://xmlns.com/foaf/0.1/name> ?who
}
"""
for row in g.query(q):
print(row['who'])
Tom Big Jack
In [38]:
q = """
SELECT ?name WHERE {
?s a/rdfs:subClassOf* :Person .
?s foaf:name ?name
}
"""
for row in g.query(q):
print(row['name'])
Tom Big Jack Jane
In [39]:
# family
q = """
SELECT ?c WHERE {
?x foaf:name "Jane" .
{ ?x :child ?y . } UNION { ?x :brother ?y }
?y foaf:name ?c
}
"""
for row in g.query(q):
print(row['c'])
Albert Martha Big Jack
In [ ]: