我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用rdflib.ConjunctiveGraph()。
def checkrequest(request): """Analyze RDF data contained in a POST request. Args: request: A Flask HTTP Request. Returns: data: A list with RDFLib.quads object and the rdflib.ConjunciveGraph object Raises: Exception: I contained data is not valid nquads. """ data = [] reqdata = request.data graph = ConjunctiveGraph() try: graph.parse(data=reqdata, format='nquads') except Exception as e: raise e quads = graph.quads((None, None, None, None)) data = splitinformation(quads, graph) return data
def getgraphfromfile(self): """Return a Conjunctive Graph generated from the referenced file. Returns: A ConjunctiveGraph """ graph = ConjunctiveGraph() try: graph.parse(self.path, format='nquads', publicID='http://localhost:5000/') logger.debug('Success: File', self.path, 'parsed') except KeyError as e: # Given file contains non valid rdf data # logger.debug('Error: File', self.path, 'not parsed') # self.__setcontent([[None][None][None][None]]) pass return graph
def test_time(): with CSVW(csv_path="tests/datatypes.time.csv", metadata_path="tests/datatypes.time.csv-metadata.json") as csvw: rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") NS = Namespace('https://www.example.org/') time1_lit = Literal("19:30:00", datatype=XSD.time) assert len(list(g.triples((NS['event/1'], NS['time1'], time1_lit)))) == 1 time2_lit = Literal("09:30:10.5", datatype=XSD.time) assert len(list(g.triples((NS['event/1'], NS['time2'], time2_lit)))) == 1 time3_lit = Literal("10:30:10Z", datatype=XSD.time) assert len(list(g.triples((NS['event/1'], NS['time3'], time3_lit)))) == 1 time4_lit = Literal("11:30:10-06:00", datatype=XSD.time) assert len(list(g.triples((NS['event/1'], NS['time4'], time4_lit)))) == 1 time5_lit = Literal("04:30:10+04:00", datatype=XSD.time) assert len(list(g.triples((NS['event/1'], NS['time5'], time5_lit)))) == 1
def test_date(): with CSVW(csv_path="tests/datatypes.date.csv", metadata_path="tests/datatypes.date.csv-metadata.json") as csvw: rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") date1_lit = Literal("2017-01-09", datatype=XSD.date) assert len(list(g.triples((NS['event/1'], NS['date1'], date1_lit)))) == 1 date2_lit = Literal("2017-01-10Z", datatype=XSD.date) assert len(list(g.triples((NS['event/1'], NS['date2'], date2_lit)))) == 1 date3_lit = Literal("2017-01-11", datatype=XSD.date) assert len(list(g.triples((NS['event/1'], NS['date3'], date3_lit)))) == 1 date4_lit = Literal("2002-09-24-06:00", datatype=XSD.date) assert len(list(g.triples((NS['event/1'], NS['date4'], date4_lit)))) == 1 date5_lit = Literal("2002-09-24+04:00", datatype=XSD.date) assert len(list(g.triples((NS['event/1'], NS['date5'], date5_lit)))) == 1
def test_datetime(): with CSVW(csv_path="tests/datatypes.datetime.csv", metadata_path="tests/datatypes.datetime.csv-metadata.json") as csvw: rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") dt1_lit = Literal("2002-05-30T09:00:00", datatype=XSD.dateTime) assert len(list(g.triples((NS['event/1'], NS['datetime1'], dt1_lit)))) == 1 dt2_lit = Literal("2002-05-30T09:30:10.5", datatype=XSD.dateTime) assert len(list(g.triples((NS['event/1'], NS['datetime2'], dt2_lit)))) == 1 dt3_lit = Literal("2002-05-30T09:30:10Z", datatype=XSD.dateTime) assert len(list(g.triples((NS['event/1'], NS['datetime3'], dt3_lit)))) == 1 dt4_lit = Literal("2002-05-30T09:30:10-06:00", datatype=XSD.dateTime) assert len(list(g.triples((NS['event/1'], NS['datetime4'], dt4_lit)))) == 1 dt5_lit = Literal("2002-05-30T09:30:10+04:00", datatype=XSD.dateTime) assert len(list(g.triples((NS['event/1'], NS['datetime5'], dt5_lit)))) == 1 datestamp = Literal("2004-04-12T13:20:00-05:00", datatype=XSD.dateTimeStamp) assert len(list(g.triples((NS['event/1'], NS['datetimestamp'], datestamp)))) == 1
def test_bool_with_format(): csvw = CSVW(csv_path="tests/datatypes.bool.csv", metadata_path="tests/datatypes.bool.csv-metadata.json") rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") true_lit = Literal(True, datatype=XSD.boolean) false_lit = Literal(False, datatype=XSD.boolean) assert len(list(g.triples((NS['event/1'], NS['bool1'], true_lit)))) == 1 assert len(list(g.triples((NS['event/1'], NS['bool2'], true_lit)))) == 1 assert len(list(g.triples((NS['event/1'], NS['bool3'], true_lit)))) == 1 assert len(list(g.triples((NS['event/2'], NS['bool1'], false_lit)))) == 1 assert len(list(g.triples((NS['event/2'], NS['bool2'], false_lit)))) == 1 assert len(list(g.triples((NS['event/2'], NS['bool3'], false_lit)))) == 1 assert len(list(g.triples((NS['event/3'], NS['bool1'], false_lit)))) == 1 assert len(list(g.triples((NS['event/3'], NS['bool2'], false_lit)))) == 1 assert len(list(g.triples((NS['event/3'], NS['bool3'], false_lit)))) == 1
def verify_rdf(rdf_output): ids_ns = Namespace("http://foo.example.org/CSV/People-IDs/") ages_ns = Namespace("http://foo.example.org/CSV/People-Ages/") g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") all_subjects = {x for x in g.subjects()} assert len(all_subjects) == 2 bob_subj = ids_ns['1'] joe_subj = ids_ns['2'] assert bob_subj in all_subjects assert joe_subj in all_subjects # Bob's details assert len([g.triples((bob_subj, ids_ns.id, Literal(1)))]) == 1 assert len([g.triples((bob_subj, ids_ns.name, Literal("Bob")))]) == 1 assert len([g.triples((bob_subj, ages_ns.age, Literal(34)))]) == 1 # Joe's details assert len([g.triples((joe_subj, ids_ns.id, Literal(2)))]) == 1 assert len([g.triples((joe_subj, ids_ns.name, Literal("Joe")))]) == 1 assert len([g.triples((joe_subj, ages_ns.age, Literal(54)))]) == 1
def test_encoding_rdf(): # With encoding specified encoding = "ISO-8859-1" csvw = CSVW(csv_path="./tests/iso_encoding.csv", metadata_path="./tests/iso_encoding.csv-metadata.json", csv_encoding=encoding) rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") units = Namespace('http://example.org/units/') cars = Namespace('http://example.org/cars/') meta = Namespace("http://example.org/properties/") expected_unit = units[quote(u"\xb5100".encode('utf-8'))] assert (cars['1'], meta['UnitOfMeasurement'], expected_unit) in g assert expected_unit in list(g.objects())
def serialize(self, add, delete): commit = Namespace("urn:commit:" + str(uuid.uuid1()) + ":") eccrev = Namespace("https://vocab.eccenca.com/revision/") g = ConjunctiveGraph() namespace_manager = NamespaceManager(g) namespace_manager.bind('eccrev', eccrev, override=False) g.add((commit.term(""), RDF.type, eccrev.Commit)) graphUris = set(delete.keys()) | set(add.keys()) for graphUri in graphUris: if (graphUri in delete.keys() and len(delete[graphUri]) > 0) or (graphUri in add.keys() and len(add[graphUri]) > 0): revision = Namespace("urn:revision:" + str(uuid.uuid1()) + ":") g.add((commit.term(""), eccrev.hasRevision, revision.term(""))) g.add((revision.term(""), RDF.type, eccrev.Revision)) if str(graphUri) != 'http://quitdiff.default/': g.add((revision.term(""), eccrev.hasRevisionGraph, graphUri)) if graphUri in delete.keys() and len(delete[graphUri]) > 0: deleteGraphName = revision.term(":delete") g.add((revision.term(""), eccrev.deltaDelete, deleteGraphName)) for triple in delete[graphUri]: g.add(triple + (deleteGraphName,)) if graphUri in add.keys() and len(add[graphUri]) > 0: insertGraphName = revision.term(":insert") g.add((revision.term(""), eccrev.deltaInsert, insertGraphName)) for triple in add[graphUri]: g.add(triple + (insertGraphName,)) return g.serialize(format="trig").decode("utf-8")
def serialize(self, add, delete): diff = Namespace("http://topbraid.org/diff#") g = ConjunctiveGraph() namespace_manager = NamespaceManager(g) namespace_manager.bind('diff', diff, override=False) namespace_manager.bind('owl', OWL, override=False) graphUris = set(delete.keys()) | set(add.keys()) for graphUri in graphUris: if (graphUri in delete.keys() and len(delete[graphUri]) > 0) or (graphUri in add.keys() and len(add[graphUri]) > 0): changeset = Namespace("urn:diff:" + str(uuid.uuid1())) graphTerm = changeset.term("") if str(graphUri) != 'http://quitdiff.default/': g.add((graphTerm, OWL.imports, graphUri, graphTerm)) g.add((graphTerm, RDF.type, OWL.Ontology, graphTerm)) g.add((graphTerm, OWL.imports, diff.term(""), graphTerm)) if graphUri in delete.keys() and len(delete[graphUri]) > 0: i = 0 for triple in delete[graphUri]: deleteStatementName = BNode() g.add((deleteStatementName, RDF.type, diff.DeletedTripleDiff, graphTerm)) g.add((deleteStatementName, RDF.subject, triple[0], graphTerm)) g.add((deleteStatementName, RDF.predicate, triple[1], graphTerm)) g.add((deleteStatementName, RDF.object, triple[2], graphTerm)) i += 1 if graphUri in add.keys() and len(add[graphUri]) > 0: i = 0 for triple in add[graphUri]: insertGraphName = BNode() g.add((insertGraphName, RDF.type, diff.AddedTripleDiff, graphTerm)) g.add((insertGraphName, RDF.subject, triple[0], graphTerm)) g.add((insertGraphName, RDF.predicate, triple[1], graphTerm)) g.add((insertGraphName, RDF.object, triple[2], graphTerm)) i += 1 return g.serialize(format="trig").decode("utf-8")
def serialize(self, add, delete): changeset = Namespace("http://purl.org/vocab/changeset/schema#") g = ConjunctiveGraph() namespace_manager = NamespaceManager(g) namespace_manager.bind('changeset', changeset, override=False) graphUris = set(delete.keys()) | set(add.keys()) for graphUri in graphUris: if (graphUri in delete.keys() and len(delete[graphUri]) > 0) or (graphUri in add.keys() and len(add[graphUri]) > 0): diff = Namespace("urn:changeset:" + str(uuid.uuid1())) graphTerm = diff.term("") g.add((graphTerm, RDF.type, changeset.ChangeSet)) if str(graphUri) != 'http://quitdiff.default/': g.add((graphTerm, changeset.subjectOfChange, graphUri)) if graphUri in delete.keys() and len(delete[graphUri]) > 0: i = 0 for triple in delete[graphUri]: deleteStatementName = BNode() g.add((graphTerm, changeset.removal, deleteStatementName)) g.add((deleteStatementName, RDF.type, RDF.Statement)) g.add((deleteStatementName, RDF.subject, triple[0])) g.add((deleteStatementName, RDF.predicate, triple[1])) g.add((deleteStatementName, RDF.object, triple[2])) i += 1 if graphUri in add.keys() and len(add[graphUri]) > 0: i = 0 for triple in add[graphUri]: insertGraphName = BNode() g.add((graphTerm, changeset.addition, insertGraphName)) g.add((insertGraphName, RDF.type, RDF.Statement)) g.add((insertGraphName, RDF.subject, triple[0])) g.add((insertGraphName, RDF.predicate, triple[1])) g.add((insertGraphName, RDF.object, triple[2])) i += 1 return g.serialize(format="turtle").decode("utf-8")
def parse(self, source=None, publicID=None, format="xml", location=None, file=None, data=None, context=u'http://example.com', **args): # parse to memory first, then do a bulk insert into our DB logging.debug('parsing to memory...') cj = rdflib.ConjunctiveGraph() memg = cj.get_context(context) memg.parse(source=source, publicID=publicID, format=format, location=location, file=file, data=data, **args) # for s, p, o in memg: # if not isinstance (o, rdflib.Literal): # continue # if not 'dateTime' in o.datatype: # continue # # import pdb; pdb.set_trace() # s = unicode(o) # print u"Got one!! %s" % s quads = cj.quads() logging.debug('addN ...') self.addN(quads)
def _load(cls, source): graph = ConjunctiveGraph() graph.parse(source, format='trig') return graph # def get_os(self): # return 'Ubuntu' # # def get_os_version(self): # return '12.04' # # def get_create_date(self): # return '20091004T111800Z' # # def get_environment_vars(self): # # results = self.graph.query( # """SELECT DISTINCT ?variable ?value # WHERE { # ?x nipype:environmentVariable ?variable . # ?x prov:value ?value . # }""") # # return results
def parse(self, inputsource, sink, **kwargs): """Parse f as an N-Triples file.""" assert sink.store.context_aware, ("NQuadsParser must be given" " a context aware store.") self.sink = ConjunctiveGraph(store=sink.store, identifier=sink.identifier) source = inputsource.getByteStream() if not hasattr(source, 'read'): raise ParseError("Item to parse must be a file-like object.") source = getreader('utf-8')(source) self.file = source self.buffer = '' while True: self.line = __line = self.readline() if self.line is None: break try: self.parseline() except ParseError, msg: raise ParseError("Invalid line (%s):\n%r" % (msg, __line)) return self.sink
def __init__(self, graph=None, bindings=None): self.bindings = bindings or Bindings() if isinstance(graph, ConjunctiveGraph): self._dataset = graph if rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION: self.graph = self.dataset else: self.graph = self.dataset.default_context else: self._dataset = None self.graph = graph self.prologue = None self.now = datetime.datetime.now() self.bnodes = collections.defaultdict(BNode)
def check_serialize_parse(fpath, infmt, testfmt, verbose=False): g = ConjunctiveGraph() _parse_or_report(verbose, g, fpath, format=infmt) if verbose: for t in g: print t print "========================================" print "Parsed OK!" s = g.serialize(format=testfmt) if verbose: print s g2 = ConjunctiveGraph() _parse_or_report(verbose, g2, data=s, format=testfmt) if verbose: print g2.serialize() crapCompare(g,g2)
def setUp(self): try: self.graph = ConjunctiveGraph(store=self.store) except ImportError: raise SkipTest( "Dependencies for store '%s' not available!" % self.store) if self.store == "SQLite": _, self.tmppath = mkstemp( prefix='test', dir='/tmp', suffix='.sqlite') else: self.tmppath = mkdtemp() self.graph.open(self.tmppath, create=True) self.michel = URIRef(u'michel') self.tarek = URIRef(u'tarek') self.bob = URIRef(u'bob') self.likes = URIRef(u'likes') self.hates = URIRef(u'hates') self.pizza = URIRef(u'pizza') self.cheese = URIRef(u'cheese') self.c1 = URIRef(u'context-1') self.c2 = URIRef(u'context-2') # delete the graph for each test! self.graph.remove((None, None, None))
def testSameSubject(self): g=rdflib.ConjunctiveGraph() g.get_context('urn:a').add(( rdflib.URIRef('urn:1'), rdflib.URIRef('urn:p1'), rdflib.URIRef('urn:o1') )) g.get_context('urn:b').add(( rdflib.URIRef('urn:1'), rdflib.URIRef('urn:p2'), rdflib.URIRef('urn:o2') )) self.assertEqual(len(g.get_context('urn:a')),1) self.assertEqual(len(g.get_context('urn:b')),1) s=g.serialize(format='trig') self.assertEqual(len(re.findall(b("p1"), s)), 1) self.assertEqual(len(re.findall(b("p2"), s)), 1) self.assert_(b('{}') not in s) # no empty graphs!
def testFinalNewline(): """ http://code.google.com/p/rdflib/issues/detail?id=5 """ import sys import platform if getattr(sys, 'pypy_version_info', None) or platform.system() == 'Java': from nose import SkipTest raise SkipTest( 'Testing under pypy and Jython2.5 fails to detect that ' + \ 'IOMemory is a context_aware store') graph=ConjunctiveGraph() graph.add((URIRef("http://ex.org/a"), URIRef("http://ex.org/b"), URIRef("http://ex.org/c"))) failed = set() for p in rdflib.plugin.plugins(None, rdflib.plugin.Serializer): v = graph.serialize(format=p.name) lines = v.split(b("\n")) if b("\n") not in v or (lines[-1]!=b('')): failed.add(p.name) assert len(failed)==0, "No final newline for formats: '%s'" % failed
def test_serialize(self): g = ConjunctiveGraph() uri1 = URIRef("http://example.org/mygraph1") uri2 = URIRef("http://example.org/mygraph2") bob = URIRef(u'urn:bob') likes = URIRef(u'urn:likes') pizza = URIRef(u'urn:pizza') g.get_context(uri1).add((bob, likes, pizza)) g.get_context(uri2).add((bob, likes, pizza)) s = g.serialize(format='nquads') self.assertEqual(len([x for x in s.split(b("\n")) if x.strip()]), 2) g2 = ConjunctiveGraph() g2.parse(data=s, format='nquads') self.assertEqual(len(g), len(g2)) self.assertEqual(sorted(x.identifier for x in g.contexts()), sorted(x.identifier for x in g2.contexts()))
def test_bnode_publicid(): g = ConjunctiveGraph() b = BNode() data = '<d:d> <e:e> <f:f> .' print ("Parsing %r into %r"%(data, b)) g.parse(data=data, format='turtle', publicID=b) triples = list( g.get_context(b).triples((None,None,None)) ) if not triples: raise Exception("No triples found in graph %r"%b) u = URIRef(b) triples = list( g.get_context(u).triples((None,None,None)) ) if triples: raise Exception("Bad: Found in graph %r: %r"%(u, triples))
def parse(self, inputsource, sink, **kwargs): """Parse f as an N-Triples file.""" assert sink.store.context_aware, ("NQuadsParser must be given" " a context aware store.") self.sink = ConjunctiveGraph(store=sink.store) source = inputsource.getByteStream() if not hasattr(source, 'read'): raise ParseError("Item to parse must be a file-like object.") source = getreader('utf-8')(source) self.file = source self.buffer = '' while True: self.line = __line = self.readline() if self.line is None: break try: self.parseline() except ParseError, msg: raise ParseError("Invalid line (%s):\n%r" % (msg, __line)) return self.sink
def _convertRDF(self): """ Convert a RDF/XML result into an RDFLib triple store. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: RDFLib Graph """ try: from rdflib.graph import ConjunctiveGraph except ImportError: from rdflib import ConjunctiveGraph retval = ConjunctiveGraph() # this is a strange hack. If the publicID is not set, rdflib (or the underlying xml parser) makes a funny #(and, as far as I could see, meaningless) error message... retval.load(self.response, publicID=' ') return retval
def participants_private_as_graph(self, discussion_id): from assembl.models import Discussion, AgentProfile local_uri = self.local_uri() discussion = Discussion.get(discussion_id) d_storage_name = self.private_user_storage.name d_graph_iri = self.private_user_storage.sections[0].graph_iri cg = ConjunctiveGraph(identifier=d_graph_iri) v = get_virtuoso(self.session, d_storage_name) v_main = get_virtuoso(self.session, self.discussion_storage_name()) participant_ids = discussion.get_participants(True) profiles={URIRef(AgentProfile.uri_generic(id, local_uri)) for id in participant_ids} self.add_subject_data(v, cg, profiles) accounts = [account for ((account, p, profile), g) in v_main.triples((None, SIOC.account_of, None)) if profile in profiles] self.add_subject_data(v, cg, accounts) return cg
def instance_view_jsonld(request): from assembl.semantic.virtuoso_mapping import AssemblQuadStorageManager from rdflib import URIRef, ConjunctiveGraph ctx = request.context user_id = authenticated_userid(request) or Everyone permissions = ctx.get_permissions() instance = ctx._instance if not instance.user_can(user_id, CrudPermissions.READ, permissions): raise HTTPUnauthorized() discussion = ctx.get_instance_of_class(Discussion) if not discussion: raise HTTPNotFound() aqsm = AssemblQuadStorageManager() uri = URIRef(aqsm.local_uri() + instance.uri()[6:]) d_storage_name = aqsm.discussion_storage_name(discussion.id) v = get_virtuoso(instance.db, d_storage_name) cg = ConjunctiveGraph(v, d_storage_name) result = cg.triples((uri, None, None)) #result = v.query('select ?p ?o ?g where {graph ?g {<%s> ?p ?o}}' % uri) # Something is wrong here. triples = '\n'.join([ '%s %s %s.' % (uri.n3(), p.n3(), o.n3()) for (s, p, o) in result if '_with_no_name_entry' not in o]) return aqsm.quads_to_jsonld(triples)
def __init__(self): """Initialize a new MemoryStore instance.""" logger = logging.getLogger('quit.core.MemoryStore') logger.debug('Create an instance of MemoryStore') self.store = ConjunctiveGraph(identifier='default') return
def test_all_triples_with_row_numbers(): csvw = CSVW(csv_path='tests/virtual1.csv', metadata_path='tests/virtual1.csv-metadata.json') rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") all_subjects = {x for x in g.subjects()} assert len(all_subjects) == 4 ns = Namespace("http://example.org/") assert ns['sub-1'] in all_subjects assert ns['sub-2'] in all_subjects assert len([g.triples((ns['sub-1'], ns['obj-1'], ns['pred-1']))]) == 1 assert len([g.triples((ns['sub-2'], ns['obj-2'], ns['pred-2']))]) == 1
def test_default(): csvw = CSVW(csv_path='tests/virtual1.csv', metadata_path='tests/virtual1.default.csv-metadata.json') rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") all_subjects = {x for x in g.subjects()} assert len(all_subjects) == 4 ns = Namespace("http://example.org/") assert ns['sub-1'] in all_subjects assert ns['sub-2'] in all_subjects assert len([g.triples((ns['sub-1'], ns['obj-1'], ns['myvalue']))]) == 1 assert len([g.triples((ns['sub-2'], ns['obj-2'], ns['myvalue']))]) == 1
def test_table_level_about_url(): csvw = CSVW(csv_path='tests/virtual1.csv', metadata_path='tests/virtual1.table.about_url.csv-metadata.json') rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") all_subjects = {x for x in g.subjects()} assert len(all_subjects) == 2 ns = Namespace("http://example.org/") assert ns['sub-1'] in all_subjects assert ns['sub-2'] in all_subjects assert len([g.triples((ns['sub-1'], ns['obj-1'], ns['myvalue']))]) == 1 assert len([g.triples((ns['sub-2'], ns['obj-2'], ns['myvalue']))]) == 1
def test_default_with_datatype(): csvw = CSVW(csv_path='tests/virtual1.csv', metadata_path='tests/virtual1.default.datatype.csv-metadata.json') rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") ns = Namespace("http://example.org/") for x in [1, 2]: active_vals = list(g.triples((ns['sub-{}'.format(x)], ns['active'], None))) assert len(active_vals) == 1 active_val = active_vals[0][2] assert isinstance(active_val, Literal) assert active_val.datatype == XSD.boolean assert active_val.value string_vals = list(g.triples((ns['sub-{}'.format(x)], ns['stringprop1'], None))) assert len(string_vals) == 1 string_val = string_vals[0][2] assert isinstance(string_val, Literal) assert string_val.value == "some string" string_vals = list(g.triples((ns['sub-{}'.format(x)], ns['stringprop2'], None))) assert len(string_vals) == 1 string_val = string_vals[0][2] assert isinstance(string_val, Literal) assert "%20" not in string_val.value
def test_null_values_with_single_string(): csvw = CSVW(csv_path="tests/null1.csv", metadata_path="tests/null1.single.csv-metadata.json") rdf_contents = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_contents, format="turtle") # There should be no subject NA all_subjects = {x for x in g.subjects()} assert subj_ns['null_key'] not in all_subjects assert subj_ns['1'] in all_subjects assert len(all_subjects) == 4 # Null valued objects should not be created all_objects = {x for x in g.objects()} assert Literal('null_key', datatype=XSD.token) not in all_objects assert Literal('null_sector') not in all_objects assert Literal('null_id', datatype=XSD.token) not in all_objects assert Literal('PUBLIC') in all_objects assert Literal('12', datatype=XSD.token) in all_objects # Spot check some triples do not exist but other do from the same row null_key_lit = Literal('null_id', datatype=XSD.token) assert len(list(g.triples((subj_ns['2'], id_uri, null_key_lit)))) == 0 priv_lit = Literal('PRIVATE') assert len(list(g.triples((subj_ns['2'], sect_uri, priv_lit)))) == 1 null_sector_lit = Literal('null_sector') assert len(list(g.triples((subj_ns['3'], sect_uri, null_sector_lit)))) == 0 twelve_lit = Literal('12', datatype=XSD.token) assert len(list(g.triples((subj_ns['3'], id_uri, twelve_lit)))) == 1
def test_literals_with_new_lines(): csv_path = "tests/parsing.quoted_newlines.csv" metadata_path = "tests/parsing.quoted_newlines.csv-metadata.json" csvw = CSVW(csv_path=csv_path, metadata_path=metadata_path) rdf_contents = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_contents, format="turtle") ns = Namespace("http://example.org/expense/") desc = URIRef("http://example.org/desc") taxi_triples = list(g.triples((ns['taxi'], desc, None))) assert len(taxi_triples) == 1 taxi_desc = taxi_triples[0][2] assert isinstance(taxi_desc, Literal) assert len(taxi_desc.value.splitlines()) == 2 flight = URIRef("http://example.org/expense/multi-hop%20flight") flight_triples = list(g.triples((flight, desc, None))) assert len(flight_triples) == 1 flight_desc = flight_triples[0][2] assert isinstance(flight_desc, Literal) assert len(flight_desc.value.splitlines()) == 4 dinner_triples = list(g.triples((ns['dinner'], desc, None))) assert len(dinner_triples) == 1 dinner_desc = dinner_triples[0][2] assert isinstance(dinner_desc, Literal) assert u'\u2019' in dinner_desc, "Expected to read unicode characters" assert u"('')" in dinner_desc, "Expected to read apostrophes"
def test_literals_with_escaped_quotes(): csv_path = "tests/parsing.escaped_quotes.csv" metadata_path = "tests/parsing.escaped_quotes.csv-metadata.json" csvw = CSVW(csv_path=csv_path, metadata_path=metadata_path) rdf_contents = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_contents, format="turtle") ns = Namespace("http://example.org/expense/") desc = URIRef("http://example.org/desc") taxi_triples = list(g.triples((ns['taxi'], desc, None))) assert len(taxi_triples) == 1 taxi_desc = taxi_triples[0][2] assert isinstance(taxi_desc, Literal) assert taxi_desc.value == "go from x to y" quoted_expense_triples = list(g.triples((URIRef("http://example.org/expense/quoted%20expense"), desc, None))) assert len(quoted_expense_triples) == 1 quoted_expense_desc = quoted_expense_triples[0][2] assert isinstance(quoted_expense_desc, Literal) assert quoted_expense_desc.value == "for some reason it came with quotes in it" flight_triples = list(g.triples((ns['flight'], desc, None))) assert len(flight_triples) == 1 flight_desc = flight_triples[0][2] assert isinstance(flight_desc, Literal) assert flight_desc.value == "had to fly \"escaped quotes business\" for this trip" car_triples = list(g.triples((ns['car'], desc, None))) assert len(car_triples) == 1 car_desc = car_triples[0][2] assert isinstance(car_desc, Literal) assert car_desc.value == " some \ in it to be escaped"
def verify_rdf(rdf_output): g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") assert len(g) == 6 assert len(set(g.subjects())) == 2 assert len(set(g.predicates())) == 3 assert len(set(g.objects())) == 6
def test_empty(): csvw = CSVW(csv_path="tests/empty.csv", metadata_path="tests/empty.csv-metadata.json") rdf_output = csvw.to_rdf() g = ConjunctiveGraph() g.parse(data=rdf_output, format="turtle") assert len(g) == 0
def verify_rdf_contents(contents, fmt): g = ConjunctiveGraph() g.parse(data=contents, format=fmt) books = Namespace('http://www.books.org/') isbn = Namespace("http://www.books.org/isbn/") # Check number of all triples assert sum(1 for _ in g.triples((None, None, None))) == NUM_SUBJECTS * NUM_TRIPLES_PER_SUBJ # Check number of subject subjs = set(g.subjects()) expected_subjs = ["0062316095", "0374532508", "1610391845", "0374275637"] assert len(subjs) == len(expected_subjs) for s in expected_subjs: assert isbn[s] in subjs # Verify isbn number is positive integer s_isbn = list(g.triples((isbn[s], books['isbnnumber'], None))) assert len(s_isbn) == 1 s_isbn_val = s_isbn[0][2] assert isinstance(s_isbn_val, Literal) assert s_isbn_val.datatype == XSD.positiveInteger # Verify pages is a unsignedShort s_page = list(g.triples((isbn[s], books['pagecount'], None))) assert len(s_page) == 1 s_page_val = s_page[0][2] assert isinstance(s_page_val, Literal) assert s_page_val.datatype == XSD.unsignedShort # Verify hardcover is a boolean s_hardcover = list(g.triples((isbn[s], books['hardcover'], None))) assert len(s_hardcover) == 1 s_hardcover_val = s_hardcover[0][2] assert isinstance(s_hardcover_val, Literal) assert s_hardcover_val.datatype == XSD.boolean # Verify price is a decimal s_price = list(g.triples((isbn[s], books['price'], None))) assert len(s_price) == 1 s_price_val = s_price[0][2] assert isinstance(s_price_val, Literal) assert s_price_val.datatype == XSD.decimal
def test_bibcat_rules_blank_graphs_rml_rules_error(self): self.assertRaises(Exception, processor.Processor, rml_rules=rdflib.Graph()) self.assertRaises(Exception, processor.Processor, rml_rules=rdflib.ConjunctiveGraph())
def __init__(self, rml_rules): self.rml = rdflib.Graph() if isinstance(rml_rules, list): for rule in rml_rules: # First check if rule exists on the filesystem if os.path.exists(rule): with open(rule) as file_obj: raw_rule = file_obj.read() else: raw_rule = get_map(rule).decode() self.rml.parse(data=raw_rule, format='turtle') elif isinstance(rml_rules, (rdflib.Graph, rdflib.ConjunctiveGraph)): self.rml = rml_rules elif os.path.exists(rml_rules): self.rml.parse(rml_rules, format='turtle') else: self.rml.parse(data=get_map(rml_rules).decode(), format='turtle') # Populate Namespaces Manager for prefix, namespace in self.rml.namespaces(): setattr(NS_MGR, prefix, rdflib.Namespace(namespace)) self.output, self.source, self.triplestore_url = None, None, None self.parents = set() self.constants = dict(version=__version__) self.triple_maps = dict() for row in self.rml.query(GET_TRIPLE_MAPS): triple_map_iri = row[0] map_key = str(triple_map_iri) self.triple_maps[map_key] = SimpleNamespace() self.triple_maps[map_key].logicalSource = \ self.__logical_source__(triple_map_iri) self.triple_maps[map_key].subjectMap = \ self.__subject_map__(triple_map_iri) self.triple_maps[map_key].predicateObjectMap = \ self.__predicate_object_map__(triple_map_iri)
def run(self, input_graph, rdf_classes=[]): """Takes a graph and deduplicates various RDF classes Args: ----- graph: rdflib.Graph or rdflib.ConjunctiveGraph rdf_classes: list of RDF Classes to use in filtering IRIs """ self.output = input_graph if rdf_classes is None: rdf_classes = [] all_classes = self.default_classes + rdf_classes for class_ in all_classes: for entity in self.output.subjects( predicate=rdflib.RDF.type, object=class_): label = self.output.value(subject=entity, predicate=rdflib.RDFS.label) if label is not None: self.__get_or_mint__(entity, class_, label) value = self.output.value(subject=entity, predicate=rdflib.RDF.value) if value is not None: self.__get_or_mint__(entity, class_, value)
def __init__(self): self.graph = rdflib.ConjunctiveGraph() self.graph.parse(config.NAMESPACES_PATH, format=config.NAMESPACES_FORMAT) ns = dict(self.graph.namespaces()) self.property_path = prepareQuery(self.property_path, initNs=ns) self.subject_and_class_query = prepareQuery(self.subject_and_class_query, initNs=ns) self.domains_query = prepareQuery(self.domains_query, initNs=ns) self.from_entity = prepareQuery(self.from_entity, initNs=ns) self.single_query = prepareQuery(self.single_query, initNs=ns) rospy.Subscriber("/aide/rdf", RdfGraphStamped, self.extend_onthology)
def query_sparql_endpoint(sparql_endpoint, query=all_data_q): graph = rdflib.ConjunctiveGraph('SPARQLStore') rt = graph.open(sparql_endpoint) rs = graph.query(query) return rs
def __init__(self, db_path): self.g = ConjunctiveGraph() self.path = db_path self.choices = set() self.labels = {}
def create_graph(self): self.g.open(self.path + mirbasegraph_name, create=True) data = self.parse_mirbase(self.path) #g = ConjunctiveGraph(store="SPARQLUpdateStore") # g.bind() mirna_class = URIRef("http://purl.obolibrary.org/obo/SO_0000276") for mid in data: mirna_instance = URIRef(MIRBASE + data[mid]["acc"]) self.g.add((mirna_instance, RDF.type, mirna_class)) label = Literal(data[mid]["name"]) self.g.add((mirna_instance, RDFS.label, label)) description = Literal(data[mid]["description"]) self.g.add((mirna_instance, RDFS.comment, description)) for p in data[mid]["previous_names"]: if p.strip(): previous_name = Literal(p) self.g.add((mirna_instance, MIRBASE["previous_acc"], previous_name)) for mature in data[mid]["mature"]: mature_instance = URIRef(MIRBASE + data[mid]["mature"][mature]["acc"]) self.g.add((mature_instance, RDF.type, mirna_class)) mature_label = Literal(data[mid]["mature"][mature]["name"]) self.g.add((mature_instance, RDFS.label, mature_label)) for mature_p in data[mid]["mature"][mature]["previous_names"]: if mature_p.strip(): mature_previous_name = Literal(mature_p) self.g.add((mature_instance, MIRBASE["previous_acc"], mature_previous_name)) self.g.add((mirna_instance, MIRBASE["stemloopOf"], mature_instance)) self.get_label_to_acc() self.choices = self.labels.keys() goa_data = self.parse_goa_gaf("data/goa_human_rna.gaf") for label in self.labels: if label in goa_data: for go_id in goa_data[label]: mirna_instance = self.labels[label] self.g.add((mirna_instance, MIRBASE["goa"], Literal(go_id)))
def parse(self, source, graph, encoding="utf-8"): if encoding not in [None, "utf-8"]: raise Exception( ("TriG files are always utf-8 encoded, ", "I was passed: %s") % encoding) # we're currently being handed a Graph, not a ConjunctiveGraph assert graph.store.context_aware, "TriG Parser needs a context-aware store!" conj_graph = ConjunctiveGraph(store=graph.store) conj_graph.default_context = graph # TODO: CG __init__ should have a # default_context arg # TODO: update N3Processor so that it can use conj_graph as the sink conj_graph.namespace_manager = graph.namespace_manager sink = RDFSink(conj_graph) baseURI = conj_graph.absolutize( source.getPublicId() or source.getSystemId() or "") p = TrigSinkParser(sink, baseURI=baseURI, turtle=True) p.loadStream(source.getByteStream()) for prefix, namespace in p._bindings.items(): conj_graph.bind(prefix, namespace) # return ???
def _get_dataset(self): if self._dataset is None: raise Exception( 'You performed a query operation requiring ' + 'a dataset (i.e. ConjunctiveGraph), but ' + 'operating currently on a single graph.') return self._dataset
def __init__(self): self.graph = ConjunctiveGraph() if os.path.exists(storefn): self.graph.load(storeuri, format='n3') self.graph.bind('dc', DC) self.graph.bind('foaf', FOAF) self.graph.bind('imdb', IMDB) self.graph.bind('rev', 'http://purl.org/stuff/rev#')
def testData(): data = ConjunctiveGraph() data += [ ( URIRef('urn:a'), URIRef('urn:p'), Literal('a') ), ( URIRef('urn:b'), URIRef('urn:p'), Literal('b') ) ] a = set(g.query("SELECT ?target WHERE { ?target <urn:p> ?val }", initBindings={'val': Literal('a')})) b = set(g.query("SELECT ?target WHERE { ?target <urn:p> ?val } VALUES (?val) {('a')}")) assert a==b, "data: %r != %r"%(a,b)
def testSerialize(self): s1 = URIRef('store:1') r1 = URIRef('resource:1') r2 = URIRef('resource:2') label = URIRef('predicate:label') g1 = Graph(identifier=s1) g1.add((r1, label, Literal("label 1", lang="en"))) g1.add((r1, label, Literal("label 2"))) s2 = URIRef('store:2') g2 = Graph(identifier=s2) g2.add((r2, label, Literal("label 3"))) g = ConjunctiveGraph() for s, p, o in g1.triples((None, None, None)): g.addN([(s, p, o, g1)]) for s, p, o in g2.triples((None, None, None)): g.addN([(s, p, o, g2)]) r3 = URIRef('resource:3') g.add((r3, label, Literal(4))) r = g.serialize(format='trix') g3 = ConjunctiveGraph() g3.parse(BytesIO(r), format='trix') for q in g3.quads((None, None, None)): # TODO: Fix once getGraph/getContext is in conjunctive graph if isinstance(q[3].identifier, URIRef): tg = Graph(store=g.store, identifier=q[3].identifier) else: # BNode, this is a bit ugly # we cannot match the bnode to the right graph automagically # here I know there is only one anonymous graph, # and that is the default one, but this is not always the case tg = g.default_context self.assert_(q[0:3] in tg)
def test_issue_250(self): """ https://github.com/RDFLib/rdflib/issues/250 When I have a ConjunctiveGraph with the default namespace set, for example import rdflib g = rdflib.ConjunctiveGraph() g.bind(None, "http://defaultnamespace") then the Trix serializer binds the default namespace twice in its XML output, once for the Trix namespace and once for the namespace I used: print(g.serialize(format='trix').decode('UTF-8')) <?xml version="1.0" encoding="utf-8"?> <TriX xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="http://defaultnamespace" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns="http://www.w3.org/2004/03/trix/trix-1/" /> """ graph = ConjunctiveGraph() graph.bind(None, "http://defaultnamespace") sg = graph.serialize(format='trix').decode('UTF-8') self.assert_( 'xmlns="http://defaultnamespace"' not in sg, sg) self.assert_( 'xmlns="http://www.w3.org/2004/03/trix/trix-1/' in sg, sg)