Examples from the lectures: Difference between revisions
From info216
No edit summary |
|||
Line 183: | Line 183: | ||
""", initNs=NS) | """, initNs=NS) | ||
print(res.askAnswer) | print(res.askAnswer) | ||
</syntaxhighlight> | |||
==S08== | |||
===owl:inverseOf=== | |||
<syntaxhighlight> | |||
from rdflib import Graph, Namespace, RDF, RDFS, FOAF | |||
import owlrl | |||
EX = Namespace('http://ex.org/') | |||
g = Graph() | |||
g.bind('', EX) | |||
g.add((EX.Andreas, RDF.type, EX.Teacher)) | |||
g.add((EX.Martin, RDF.type, EX.Teacher)) | |||
g.add((EX.Tobias, RDF.type, EX.Teacher)) | |||
g.add((EX.Martin, RDF.type, EX.Student)) | |||
g.add((EX.Tobias, RDF.type, EX.Student)) | |||
g.add((EX.Mariah, RDF.type, EX.Student)) | |||
g.add((EX.Bahareh, RDF.type, EX.Student)) | |||
# g.add((EX.Teacher, RDFS.subClassOf, FOAF.Person)) | |||
# g.add((EX.Student, RDFS.subClassOf, FOAF.Person)) | |||
g.update(""" | |||
INSERT DATA { | |||
:Martin :hasSupervisor :Andreas . | |||
:hasSupervisor owl:inverseOf :supervisorOf . | |||
} | |||
""") | |||
</syntaxhighlight> | |||
This gives no response: | |||
<syntaxhighlight> | |||
res = g.query(""" | |||
SELECT ?person WHERE { | |||
:Andreas :supervisorOf ?person . | |||
} | |||
""") | |||
print(res.serialize(format='txt').decode()) | |||
</syntaxhighlight> | |||
Not this either: | |||
<syntaxhighlight> | |||
engine = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False) | |||
engine.closure() | |||
engine.flush_stored_triples() | |||
res = g.query(""" | |||
SELECT ?person WHERE { | |||
:Andreas :supervisorOf ?person . | |||
} | |||
""") | |||
print(res.serialize(format='txt').decode()) | |||
</syntaxhighlight> | |||
But this prints out a response: | |||
<syntaxhighlight> | |||
engine = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False) | |||
engine.closure() | |||
engine.flush_stored_triples() | |||
res = g.query(""" | |||
SELECT ?person WHERE { | |||
:Andreas :supervisorOf ?person . | |||
} | |||
""") | |||
print(res.serialize(format='txt').decode()) | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 15:25, 15 March 2022
This page contains code examples from the lectures.
S02
Getting started with RDFlib
from inspect import formatargspec
from rdflib import Graph, Literal, RDF, FOAF, Namespace
g = Graph()
EX = Namespace('http://ex.org/')
g.add((EX.Bob, RDF.type, FOAF.Person))
g.add((EX.Alice, RDF.type, FOAF.Person))
g.add((EX.Carol, RDF.type, FOAF.Person))
g.add((EX.Bob, FOAF.knows, EX.Alice))
g.add((EX.Bob, FOAF.knows, EX.Carol))
g.add((EX.Bob, FOAF.name, Literal('Bob')))
g.add((EX.Alice, FOAF.name, Literal('Alice')))
g.add((EX.Carol, FOAF.name, Literal('Carol')))
g.namespace_manager.bind('ex', EX)
print(g.serialize(format='json-ld'))
for p, o in g[ EX.Bob : : ]:
print(p, o)
g2 = Graph()
g2.parse('https://www.wikidata.org/entity/Q935079.ttl')
print(g2.serialize())
S03
Local query with RDFlib
from rdflib import Graph, Namespace
REX = Namespace('http://example.org/royal#')
g = Graph()
g.parse('family.ttl', format='ttl')
res = g.query("""
CONSTRUCT {
?child fam:aunt ?aunt .
} WHERE {
?child fam:hasParent / fam:hasSister ?aunt .
}
""",
initNs={
'fam': 'http://example.org/family#'
},
initBindings={
'child': REX.SverreMagnus
})
S04
Remote query with SPARQLWrapper
import SPARQLWrapper
endpoint = 'http://sandbox.i2s.uib.no/bigdata/namespace/kb/sparql'
paper_uri = 'http://semanticscholar.org/paper/c93a37e6922e09f34fc093f7e4f9675775d4557d'
client = SPARQLWrapper.SPARQLWrapper(endpoint=endpoint)
client.setReturnFormat('json')
client.setQuery(f'''
DESCRIBE <{paper_uri}>
''')
res = client.queryAndConvert()
Remote update with SPARQLWrapper
client.setReturnFormat('xml')
client.setMethod('POST')
client.setQuery('''
DELETE DATA {
<http://example.org/Abra> <http://example.org/ka> <http://example.org/Dabra> .
}
''')
res = client.queryAndConvert()
client.setMethod('GET')
Local update with RDFlib
from rdflib import Graph, Namespace
g = Graph()
g.parse('family.ttl', format='turtle')
FAM = Namespace('http://example.org/family#')
g.update('''
DELETE {
?child fam:hasAunt ?parent .
} INSERT {
?child fam:hasAunt ?sister .
} WHERE {
?child fam:hasParent ?parent .
?parent fam:hasSister ?sister .
}
''',
initNs={
'fam': FAM
})
print(g.serialize(format='turtle'))
S07
RDFS programming with owlrl .
Syllogism (rule rdfs9)
from rdflib import Graph, RDF, RDFS, OWL, Namespace
import owlrl
EX = Namespace('http://example.org#')
g = Graph()
g.bind('', EX)
NS = {
'': EX,
'rdf': RDF,
'rdfs': RDFS,
}
g.update("""
INSERT DATA {
:Socrates rdf:type :Man .
:Man rdfs:subClassOf :Mortal .
}
""", initNs=NS)
rdfs_engine = owlrl.RDFSClosure.RDFS_Semantics(g, True, False, False)
rdfs_engine.closure()
rdfs_engine.flush_stored_triples()
res = g.query("""
ASK { :Socrates rdf:type :Mortal . }
""", initNs=NS)
print(res.askAnswer)
Domain and range (rules rdfs2-3)
from rdflib import Graph, RDF, RDFS, OWL, Namespace
import owlrl
EX = Namespace('http://example.org#')
g = Graph()
g.bind('', EX)
NS = {
'': EX,
'rdf': RDF,
'rdfs': RDFS,
}
g.update("""
INSERT DATA {
:Socrates :husbandOf :Xantippe .
:husbandOf rdfs:domain :Man .
:husbandOf rdfs:range :Woman .
}
""", initNs=NS)
rdfs_engine = owlrl.RDFSClosure.RDFS_Semantics(g, True, False, False)
rdfs_engine.closure()
rdfs_engine.flush_stored_triples()
res = g.query("""
ASK { :Xantippe rdf:type :Woman . }
""", initNs=NS)
print(res.askAnswer)
S08
owl:inverseOf
from rdflib import Graph, Namespace, RDF, RDFS, FOAF
import owlrl
EX = Namespace('http://ex.org/')
g = Graph()
g.bind('', EX)
g.add((EX.Andreas, RDF.type, EX.Teacher))
g.add((EX.Martin, RDF.type, EX.Teacher))
g.add((EX.Tobias, RDF.type, EX.Teacher))
g.add((EX.Martin, RDF.type, EX.Student))
g.add((EX.Tobias, RDF.type, EX.Student))
g.add((EX.Mariah, RDF.type, EX.Student))
g.add((EX.Bahareh, RDF.type, EX.Student))
# g.add((EX.Teacher, RDFS.subClassOf, FOAF.Person))
# g.add((EX.Student, RDFS.subClassOf, FOAF.Person))
g.update("""
INSERT DATA {
:Martin :hasSupervisor :Andreas .
:hasSupervisor owl:inverseOf :supervisorOf .
}
""")
This gives no response:
res = g.query("""
SELECT ?person WHERE {
:Andreas :supervisorOf ?person .
}
""")
print(res.serialize(format='txt').decode())
Not this either:
engine = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
engine.closure()
engine.flush_stored_triples()
res = g.query("""
SELECT ?person WHERE {
:Andreas :supervisorOf ?person .
}
""")
print(res.serialize(format='txt').decode())
But this prints out a response:
engine = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
engine.closure()
engine.flush_stored_triples()
res = g.query("""
SELECT ?person WHERE {
:Andreas :supervisorOf ?person .
}
""")
print(res.serialize(format='txt').decode())