This tutorial shows how to insert RDF from a file (joebloggs_foaf.rdf) into Construct. It then shows how to send a SPARQL query to query this data from Construct. The resulting QueryResults object is printed in N3 format.
-
from construct.proxy import proxy
-
from construct.constructservice import ServiceError
-
from rdflib.Graph import ConjunctiveGraph
-
# Create a new proxy object.
-
proxy = proxy()
-
print “Executing Script”
-
try:
-
# Generate a piece of FOAF RDF
-
store = ConjunctiveGraph()
-
store.load(“joebloggs_foaf.rdf”)
-
data = store.serialize(format=“nt”)
-
# Send the FOAF RDF to the data store
-
if(proxy.insert(data)):
-
# Now query for joebloggs web address
-
query = “”“SELECT ?nickname WHERE{
-
?subject <http://xmlns.com/foaf/0.1/name> “Joe Bloggs“.
-
?subject <http://xmlns.com/foaf/0.1/nick> ?nickname.}
-
““”
-
results = proxy.query(query)
-
print “Here is the N3 form of the QueryResults Object:”
-
print results
-
except ServiceError, e:
-
print e
-
# Close the proxy.
-
proxy.close()
If this script executes properly something like the following should be printed out:
Executing Script
Here is the N3 form of the QueryResults Object:
_:A6a94e801X3aX118515eda06X3aXX2dX7ffa <http://www.w3.org/2001/sw/DataAccess/tests/result-set#value> “joe” .
_:A6a94e801X3aX118515eda06X3aXX2dX7ffa <http://www.w3.org/2001/sw/DataAccess/tests/result-set#variable> “nickname” .
_:A6a94e801X3aX118515eda06X3aXX2dX7ffb <http://www.w3.org/2001/sw/DataAccess/tests/result-set#binding> _:A6a94e801X3aX118515eda06X3aXX2dX7ffa .
_:A6a94e801X3aX118515eda06X3aXX2dX7ffc <http://www.w3.org/2001/sw/DataAccess/tests/result-set#solution> _:A6a94e801X3aX118515eda06X3aXX2dX7ffb .
_:A6a94e801X3aX118515eda06X3aXX2dX7ffc <http://www.w3.org/2001/sw/DataAccess/tests/result-set#resultVariable> “nickname” .
_:A6a94e801X3aX118515eda06X3aXX2dX7ffc <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2001/sw/DataAccess/tests/result-set#ResultSet> .
This post is the final part of a python and Construct tutorial. Part 1 is here and part 2 is here.
![[del.icio.us]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/google.png)
![[StumbleUpon]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Windows Live]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/email.png)
0 Responses to “Construct and Python - Part 3”
Leave a Reply