def sesame_is_sparql_test(self): url = 'http://dbpedia.org/sparql' g1 = SesameGraph(url) g2 = SPARQLGraph(url) q1 = "select distinct ?Concept where {[] a ?Concept} LIMIT 10" r1 = set(list(g1.query(q1,resultMethod='xml'))) r2 = set(list(g2.query(q1,resultMethod='xml'))) assert r1==r2
def initBindings_test2(self): query = 'select * where {?s ?P ?oo.?ss ?p \n?oo}' initBindings = dict(oo=URIRef("OHO"), P=Decimal("4.40")) processed = SesameGraph._processInitBindings(query, initBindings) assert processed == \ 'select * where {?s "4.40"^^' + \ '<http://www.w3.org/2001/XMLSchema#decimal> <OHO>.?ss ?p \n<OHO>}'
from rdfalchemy.sparql.sesame2 import SesameGraph import os url = os.environ.get('SESAME2_URL', 'http://example.com/sparql') if 'example.com' in url: from nose import SkipTest raise SkipTest('Please provide a functioning Sesame2 endpoint URL') g = SesameGraph(url) q1 = "select ?s ?p ?o where {?s ?p ?o} limit 100" responses = {} x = set(list(g.query(q1, resultMethod='xml'))) j = set(list(g.query(q1, resultMethod='json'))) b = set(list(g.query(q1, resultMethod='brtr'))) # print(len(x)) def sizes_test(): assert len(b) == len(x) == len(j) def eq_bx_test(): assert b == x def eq_bj_test(): assert b == j
def create_engine(url='', identifier="", create=False): """ :returns: returns an opened rdflib ConjunctiveGraph :param url: a string of the url :param identifier: URIRef of the default context for writing e.g.: - create_engine('sleepycat://~/working/rdf_db') - create_engine('kyotocabinet://~/working/rdf_db') - create_engine('zodb:///var/rdflib/Data.fs') - create_engine('zodb://*****:*****@localhost/rdflibdb') - create_engine('sqlalchemy+postgresql://myname@localhost/rdflibdb') etc. """ if url == '' or url.startswith('IOMemory'): from rdflib import ConjunctiveGraph db = ConjunctiveGraph('IOMemory') elif url.lower().startswith('sleepycat://'): from rdflib import ConjunctiveGraph db = ConjunctiveGraph('Sleepycat', identifier=identifier) openstr = os.path.abspath(os.path.expanduser(url[12:])) db.open(openstr, create=create) elif url.lower().startswith('kyotocabinet://'): from rdflib import ConjunctiveGraph db = ConjunctiveGraph('Kyotocabinet', identifier=identifier) openstr = os.path.abspath(os.path.expanduser(url[15:])) db.open(openstr, create=create) elif url.lower().startswith('sqlalchemy+'): from rdflib import ConjunctiveGraph db = ConjunctiveGraph('SQLAlchemy', identifier=identifier) db.open(url[11:], create=create) elif url.lower().startswith('zodb://'): import ZODB # import transaction from rdflib import ConjunctiveGraph db = ConjunctiveGraph('ZODB') if url.endswith('.fs'): from ZODB.FileStorage import FileStorage openstr = os.path.abspath(os.path.expanduser(url[7:])) if not os.path.exists(openstr) and not create: raise "File not found: %s" fs = FileStorage(openstr) else: from ZEO.ClientStorage import ClientStorage schema, opts = _parse_rfc1738_args(url) fs = ClientStorage((opts['host'], int(opts['port']))) # get the Zope Database zdb = ZODB.DB(fs) # open it conn = zdb.open() #get the root root = conn.root() # get the Conjunctive Graph if 'rdflib' not in root and create: root['rdflib'] = ConjunctiveGraph('ZODB') db = root['rdflib'] elif url.lower().startswith('sesame://'): from rdfalchemy.sparql.sesame2 import SesameGraph db = SesameGraph("http://" + url[9:]) elif url.lower().startswith('sparql://'): from rdfalchemy.sparql import SPARQLGraph db = SPARQLGraph("http://" + url[9:]) else: raise "Could not parse string '%s'" % url return db
from rdfalchemy.sparql.sesame2 import SesameGraph import os url = os.environ.get('SESAME2_URL', 'http://example.com/sparql') if 'example.com' in url: from nose import SkipTest raise SkipTest('Please provide a functioning Sesame2 endpoint URL') g = SesameGraph(url) q1 = "select ?s ?p ?o where {?s ?p ?o} limit 100" responses = {} x = set(list(g.query(q1, resultMethod='xml'))) # j = set(list(g.query(q1, resultMethod='json'))) # b = set(list(g.query(q1, resultMethod='brtr'))) b = j = x def sizes_test(): assert len(b) == len(x) == len(j) def eq_bx_test(): assert b == x def eq_bj_test(): assert b == j
q2 = "select ?o where {?s ?p ?o} limit 10000" def mtester(method, small, all): start = datetime.now() i = 0 for x in g.query(q, resultMethod=method): i = i + 1 if (i % small) == 0: print("c %s %s" % (i, datetime.now() - start)) if (i % all) == 0: print("f %s %s" % (i, datetime.now() - start)) return g = SesameGraph(url1) q = q1 # print "testing\n url: %s\n query: %s\n" % (url1,q) # print "\n method: %s" % ('brtr') # tester('brtr',100,1000) print("\n method: %s" % ('xml')) mtester('xml', 100, 1000) # print "\n method: %s" % ('json') # tester('json',100,1000) g = SesameGraph(url2) q = q2 # print "testing\n url: %s\n query: %s\n" % (url2,q) # print "\n method: %s" % ('brtr')
from rdflib import Namespace, Literal, URIRef, BNode from datetime import date import time import urllib rep = 'http://localhost:8080/openrdf-sesame/repositories/newss' TRIES = 3 WAIT = 3 TWITTER = 'twitter' services = [TWITTER] serviceUri = {TWITTER: 'http://twitter.com/'} # import the sesame repository db = SesameGraph(rep) # namespaces BOOKMARK = Namespace("http://www.w3.org/2002/01/bookmark#") FOAF = Namespace("http://xmlns.com/foaf/0.1/") RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") RDFS = Namespace("http://www.w3.org/2000/01/rdf-schema#") SCOT = Namespace("http://scot-project.org/scot/ns#") VIDEO = Namespace("http://digitalbazaar.com/media/video#") TAGS = Namespace("http://www.holygoat.co.uk/owl/redwood/0.1/tags/") DC = Namespace("http://purl.org/dc/elements/1.1/") SIOC = Namespace("http://rdfs.org/sioc/types#") OWL = Namespace("http://www.w3.org/2002/07/owl#") def dbAdd(triplet, msg=None, tries=0):