def testCurieToUri(self): self.assertEqual('http://www.loc.gov/zing/srw/record', namespaces.curieToUri('srw:record')) self.assertEqual('http://purl.org/dc/elements/1.1/title', namespaces.curieToUri('dc:title')) self.assertEqual('http://www.w3.org/1999/02/22-rdf-syntax-ns#type', namespaces.curieToUri('rdf:type'))
def testNestedAnonymousBNode(self): graph = Graph() for triple in [ ('http://example.org/item', curieToUri('dcterms:creator'), BNode('_:1')), ('_:1', curieToUri('rdfs:label'), Literal('The Creator', lang='en')), ('_:1', curieToUri('dcterms:spatial'), BNode('_:2')), ('_:2', curieToUri('geo:lat'), Literal('123.123')), ('_:2', curieToUri('geo:long'), Literal('1.3')), ]: graph.addTriple(*triple) self.assertXmlEquals('''<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_dcterms)s %(xmlns_geo)s> <rdf:Description rdf:about="http://example.org/item"> <dcterms:creator> <rdf:Description> <dcterms:spatial> <rdf:Description> <geo:lat>123.123</geo:lat> <geo:long>1.3</geo:long> </rdf:Description> </dcterms:spatial> <rdfs:label xml:lang="en">The Creator</rdfs:label> </rdf:Description> </dcterms:creator> </rdf:Description> </rdf:RDF>''' % namespaces, Triples2RdfXml().asRdfXml(graph))
def testShouldRecognizeParseTypeResource(self): BNode.nextGenId = 0 RDFParser(sink=self.sink).parse( XML( """<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_dcterms)s> <rdf:Description rdf:about="http://example.com/something"> <dcterms:hasFormat rdf:parseType="Resource"> <dcterms:title>Title</dcterms:title> <dcterms:format>application/epub</dcterms:format> </dcterms:hasFormat> </rdf:Description> </rdf:RDF>""" % namespaces ) ) self.assertEquals( set( [ ("http://example.com/something", curieToUri("dcterms:hasFormat"), BNode("_:id0")), ("_:id0", curieToUri("dcterms:format"), Literal("application/epub")), ("_:id0", curieToUri("dcterms:title"), Literal("Title")), ] ), set(self.sink.triples()), )
def testGraphFindLabelSpecifyAllowedLabelPredicates(self): g = Graph() g.addTriple('u:ri', curieToUri('rdfs:label'), Literal('rdfsLabel')) self.assertEquals(None, g.findLabel(uri='u:ri', labelPredicates=[])) self.assertEquals(Literal('rdfsLabel'), g.findLabel(uri='u:ri', labelPredicates=[curieToUri('rdfs:label')])) g.addTriple('u:ri2', curieToUri('skos:altLabel'), Literal('altLabel')) self.assertEquals(None, g.findLabel(uri='u:ri2', labelPredicates=[curieToUri('rdfs:label')])) self.assertEquals(Literal('altLabel'), g.findLabel(uri='u:ri2', labelPredicates=[curieToUri('rdfs:label'), curieToUri('skos:altLabel')]))
def testMoreThanOneTriplePerSubjectAsRdfXml(self): graph = Graph() graph.addTriple('http://example.org/item', curieToUri('rdfs:label'), Literal('The Item', lang="en")) graph.addTriple('http://example.org/item', curieToUri('dcterms:creator'), Literal('The Creator')) self.assertXmlEquals('''<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_dcterms)s> <rdf:Description rdf:about="http://example.org/item"> <dcterms:creator>The Creator</dcterms:creator> <rdfs:label xml:lang="en">The Item</rdfs:label> </rdf:Description> </rdf:RDF>''' % namespaces, Triples2RdfXml().asRdfXml(graph))
def testAnonymousBNode(self): graph = Graph() graph.addTriple('http://example.org/item', curieToUri('dcterms:creator'), BNode('_:1')) graph.addTriple('_:1', curieToUri('rdfs:label'), Literal('The Creator', lang='en')) self.assertXmlEquals('''<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_dcterms)s> <rdf:Description rdf:about="http://example.org/item"> <dcterms:creator> <rdf:Description> <rdfs:label xml:lang="en">The Creator</rdfs:label> </rdf:Description> </dcterms:creator> </rdf:Description> </rdf:RDF>''' % namespaces, Triples2RdfXml().asRdfXml(graph))
def testRdfDescriptionPerUri(self): graph = Graph() graph.addTriple('http://example.org/item', curieToUri('dcterms:creator'), Uri('http://example.org/theCreator')) graph.addTriple('http://example.org/theCreator', curieToUri('rdfs:label'), Literal('The Creator')) graph.addTriple('http://example.org/somethingEntirelyDifferent', curieToUri('dcterms:title'), Literal('Something Entirely Different')) self.assertXmlEquals('''<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_dcterms)s> <rdf:Description rdf:about="http://example.org/item"> <dcterms:creator rdf:resource="http://example.org/theCreator"/> </rdf:Description> <rdf:Description rdf:about="http://example.org/somethingEntirelyDifferent"> <dcterms:title>Something Entirely Different</dcterms:title> </rdf:Description> <rdf:Description rdf:about="http://example.org/theCreator"> <rdfs:label>The Creator</rdfs:label> </rdf:Description> </rdf:RDF>''' % namespaces, Triples2RdfXml().asRdfXml(graph))
def testRdfResource(self): graph = Graph() graph.addTriple('http://example.org/item', curieToUri('dcterms:creator'), Uri('http://example.org/theCreator')) self.assertXmlEquals('''<rdf:RDF %(xmlns_rdf)s %(xmlns_dcterms)s> <rdf:Description rdf:about="http://example.org/item"> <dcterms:creator rdf:resource="http://example.org/theCreator"/> </rdf:Description> </rdf:RDF>''' % namespaces, Triples2RdfXml().asRdfXml(graph))
def testAnnotation(self): graph = Graph() body = BNode() uri = 'uri:a' targetUri = 'uri:target' graph.addTriple(subject=uri, predicate=curieToUri('oa:hasBody'), object=body) graph.addTriple(subject=uri, predicate=curieToUri('rdf:type'), object=Uri(curieToUri('oa:Annotation'))) graph.addTriple(subject=uri, predicate=curieToUri('oa:annotatedBy'), object=Uri("uri:testAnnotation")) graph.addTriple(subject=uri, predicate=curieToUri('oa:motivatedBy'), object=Uri("uri:testAnnotation:motive")) graph.addTriple(subject=uri, predicate=curieToUri('oa:hasTarget'), object=Uri(targetUri)) graph.addTriple(subject=body, predicate=curieToUri('dcterms:title'), object=Literal("Title")) graph.addTriple(subject=body, predicate=curieToUri('dcterms:source'), object=Uri("uri:source")) graph.addTriple(subject=Uri("uri:source"), predicate=curieToUri('rdfs:label'), object=Literal("A Source")) self.assertXmlEquals('''<rdf:RDF %(xmlns_dcterms)s %(xmlns_oa)s %(xmlns_rdf)s %(xmlns_rdfs)s> <oa:Annotation rdf:about="uri:a"> <oa:annotatedBy rdf:resource="uri:testAnnotation"/> <oa:hasBody> <rdf:Description> <dcterms:source> <rdf:Description rdf:about="uri:source"> <rdfs:label>A Source</rdfs:label> </rdf:Description> </dcterms:source> <dcterms:title>Title</dcterms:title> </rdf:Description> </oa:hasBody> <oa:hasTarget rdf:resource="uri:target"/> <oa:motivatedBy rdf:resource="uri:testAnnotation:motive"/> </oa:Annotation> </rdf:RDF>''' % namespaces, Triples2RdfXml(inlineDescriptions=True).asRdfXml(graph)) self.assertXmlEquals('''<rdf:RDF %(xmlns_dcterms)s %(xmlns_oa)s %(xmlns_rdf)s %(xmlns_rdfs)s> <oa:Annotation rdf:about="uri:a"> <oa:annotatedBy rdf:resource="uri:testAnnotation"/> <oa:hasBody> <rdf:Description> <dcterms:source rdf:resource="uri:source"/> <dcterms:title>Title</dcterms:title> </rdf:Description> </oa:hasBody> <oa:hasTarget rdf:resource="uri:target"/> <oa:motivatedBy rdf:resource="uri:testAnnotation:motive"/> </oa:Annotation> <rdf:Description rdf:about="uri:source"> <rdfs:label>A Source</rdfs:label> </rdf:Description> </rdf:RDF>''' % namespaces, Triples2RdfXml().asRdfXml(graph))
def testInitializeGraphComponentFromRdfObjects(self): class A(object): @property def context(self): return 'uri:context' def asRdfXml(self): yield '''\ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"> <rdf:Description rdf:about="uri:someUri"> <rdfs:label xml:lang="nl">Some resource</rdfs:label> </rdf:Description> </rdf:RDF>''' with stdout_replaced(): gc = GraphComponent(rdfSources=[A()]) self.assertEquals([("uri:someUri", curieToUri("rdfs:label"), Literal("Some resource", lang="nl"))], list(gc.triples()))
def testRecognizedRdfIDForReification(self): BNode.nextGenId = 0 RDFParser(sink=self.sink).parse( XML( """<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_dcterms)s> <rdf:Description rdf:about="http://example.com/something"> <dcterms:title rdf:ID="triple2">Title</dcterms:title> </rdf:Description> <rdf:Statement rdf:about="#triple2"> <dcterms:source>source</dcterms:source> </rdf:Statement> </rdf:RDF>""" % namespaces ) ) self.assertEquals( set( [ ("http://example.com/something", curieToUri("dcterms:title"), Literal("Title")), ( u"#triple2", u"http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate", Uri(u"http://purl.org/dc/terms/title"), ), (u"#triple2", u"http://www.w3.org/1999/02/22-rdf-syntax-ns#object", Literal(u"Title")), ( u"#triple2", u"http://www.w3.org/1999/02/22-rdf-syntax-ns#type", Uri(u"http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement"), ), (u"#triple2", u"http://purl.org/dc/terms/source", Literal(u"source")), ( u"#triple2", u"http://www.w3.org/1999/02/22-rdf-syntax-ns#subject", Uri(u"http://example.com/something"), ), ] ), set(self.sink.triples()), )
def testGraphFindLabel(self): g = Graph() g.addTriple('u:ri', curieToUri('rdfs:label'), Literal('rdfsLabel')) self.assertEquals(Literal('rdfsLabel'), g.findLabel(uri='u:ri')) g.addTriple('u:ri', curieToUri('rdfs:label'), Literal('rdfsLabelEN', lang='en')) self.assertEquals(Literal('rdfsLabelEN', lang='en'), g.findLabel(uri='u:ri')) g.addTriple('u:ri', curieToUri('skos:prefLabel'), Literal('skosPrefLabel')) self.assertEquals(Literal('rdfsLabelEN', lang='en'), g.findLabel(uri='u:ri')) g.addTriple('u:ri', curieToUri('skos:prefLabel'), Literal('skosPrefLabelNL', lang='nl')) self.assertEquals(Literal('skosPrefLabelNL', lang='nl'), g.findLabel(uri='u:ri')) g.addTriple('u:ri', curieToUri('rdfs:label'), Literal('rdfsLabelNL', lang='nl')) self.assertEquals(Literal('rdfsLabelNL', lang='nl'), g.findLabel(uri='u:ri')) g.addTriple('u:ri', curieToUri('foaf:name'), Literal('foafNameNL', lang='nl')) self.assertEquals(Literal('foafNameNL', lang='nl'), g.findLabel(uri='u:ri'))
for rdfType in rdfTypes: typeTagCurie = self.nodePromotedTypes.get(rdfType) if typeTagCurie: resourceDescription['relations'].remove((RDF_TYPE, Uri(rdfType))) return typeTagCurie return 'rdf:Description' def _subjectUriOrder(self, (s, resourceDescription)): return ( min([self.relativeTypePositions.get(type, 0) for type in resourceDescription['types']] or [0]), len(self._leftHandSides(BNode(s) if s.startswith('_:') else Uri(s))), -len(resourceDescription['relations']) ) RDF_STATEMENT_TAG = curieToTag('rdf:Statement') RDF_ABOUT_TAG = curieToTag('rdf:about') RDF_TYPE = curieToUri('rdf:type') RDF_SUBJECT = curieToUri('rdf:subject') RDF_PREDICATE = curieToUri('rdf:predicate') RDF_OBJECT = curieToUri('rdf:object') REIFICATION_RELATIONS = set([RDF_SUBJECT, RDF_PREDICATE, RDF_OBJECT]) NODE_PROMOTED_TYPES = set(['rdf:Statement', 'oa:Annotation']) RELATIVE_TYPE_POSITIONS = { curieToUri('oa:Annotation'): -10, curieToUri('rdf:Statement'): 100, }
def testCurieToUri(self): self.assertEquals('http://www.loc.gov/zing/srw/record', namespaces.curieToUri('srw:record')) self.assertEquals('http://purl.org/dc/elements/1.1/title', namespaces.curieToUri('dc:title')) self.assertEquals('http://www.w3.org/1999/02/22-rdf-syntax-ns#type', namespaces.curieToUri('rdf:type'))
# Copyright (C) 2015 Drents Archief http://www.drentsarchief.nl # Copyright (C) 2015 Seecr (Seek You Too B.V.) http://seecr.nl # # This file is part of "Meresco RDF" # # "Meresco RDF" is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # "Meresco RDF" is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with "Meresco RDF"; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # ## end license ## from meresco.xml.namespaces import curieToUri FOAF_NAME = curieToUri('foaf:name') RDFS_LABEL = curieToUri('rdfs:label') SKOS_PREFLABEL = curieToUri('skos:prefLabel') DCTERMS_TITLE = curieToUri('dcterms:title') SKOS_ALTLABEL = curieToUri('skos:altLabel') PRIMARY_LABEL_PREDICATES = [FOAF_NAME, RDFS_LABEL, SKOS_PREFLABEL, DCTERMS_TITLE] LABEL_PREDICATES = PRIMARY_LABEL_PREDICATES + [SKOS_ALTLABEL]
return allText or None x_lang_tag = curieToTag("xml:lang") rdf_RDF_tag = curieToTag("rdf:RDF") rdf_ID_tag = curieToTag("rdf:ID") rdf_about_tag = curieToTag("rdf:about") rdf_aboutEach_tag = curieToTag("rdf:aboutEach") rdf_aboutEachPrefix_tag = curieToTag("rdf:aboutEachPrefix") rdf_type_tag = curieToTag("rdf:type") rdf_resource_tag = curieToTag("rdf:resource") rdf_Description_tag = curieToTag("rdf:Description") rdf_bagID_tag = curieToTag("rdf:bagID") rdf_parseType_tag = curieToTag("rdf:parseType") rdf_nodeID_tag = curieToTag("rdf:nodeID") rdf_datatype_tag = curieToTag("rdf:datatype") rdf_li_tag = curieToTag("rdf:li") rdf_Statement_uri = curieToUri('rdf:Statement') rdf_type_uri = curieToUri('rdf:type') rdf_subject_uri = curieToUri('rdf:subject') rdf_predicate_uri = curieToUri('rdf:predicate') rdf_object_uri = curieToUri('rdf:object') DISALLOWED = set([rdf_RDF_tag, rdf_ID_tag, rdf_about_tag, rdf_bagID_tag, rdf_parseType_tag, rdf_resource_tag, rdf_nodeID_tag, rdf_datatype_tag, rdf_li_tag, rdf_aboutEach_tag, rdf_aboutEachPrefix_tag])
def testSingleTripleAsRdfXml(self): self.assertXmlEquals('''<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s> <rdf:Description rdf:about="http://example.org/item"> <rdfs:label xml:lang="nl">The Item</rdfs:label> </rdf:Description> </rdf:RDF>''' % namespaces, Triples2RdfXml().asRdfXml([('http://example.org/item', curieToUri('rdfs:label'), Literal('The Item', lang="nl"))]))