# First few bits of sample code from # http://www.franz.com/agraph/support/documentation/current/python-tutorial.html # tweaked a bit from franz.openrdf.sail.allegrographserver import AllegroGraphServer from franz.openrdf.repository.repository import Repository from franz.openrdf.query.query import QueryLanguage from franz.openrdf.rio.rdfformat import RDFFormat server = AllegroGraphServer("localhost", port=8080) # Catalog: a container for a set of repositories. catalog = server.openCatalog('ag') # Repository: a set of triples myRepository = Repository(catalog, "bobtest1", Repository.RENEW) myRepository.initialize() # Connection used as interface for updating and querying a repository conn = myRepository.getConnection() conn.clear() baseURI = "http://whatever.com" dataFile = "/bob/dev/xml/rdf/fakeAddrBookPt1.rdf" location = "/g1" context = myRepository.getValueFactory().createURI(location) conn.addFile(dataFile, baseURI, format=RDFFormat.RDFXML, context=context); dataFile = "/bob/dev/xml/rdf/fakeAddrBookPt2.rdf" location = "/g2" # Context: a graph name; the fourth part of a # quad identifies the context the triple belongs to. context = myRepository.getValueFactory().createURI(location) conn.addFile(dataFile, baseURI, format=RDFFormat.RDFXML, context=context); myRepository.indexTriples(all=True, asynchronous=False) print "After loading, repository contains %s triples." % (conn.size(context)) # Send a SPARQL query to the connection, print the result. queryString = "SELECT DISTINCT ?p WHERE {?s ?p ?o }" tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString) result = tupleQuery.evaluate(); for bindingSet in result: print bindingSet.getValue("p") result.close(); conn.close();