Example #1
0
    def testMatchTriplePathRealRdfTriples(self):
        # Note: at the moment we follow the convention that only (for performance reasons) the triple's object is wrapped in Literal, Uri or BNode.
        # All variable bindings get wrapped.
        g = Graph()
        g.addTriple('uri:x', 'uri:y', Uri('uri:z'))
        g.addTriple('uri:a', 'uri:b', Literal('c'))
        g.addTriple('uri:z', 'uri:d', Uri('uri:a'))
        g.addTriple('uri:a', 'uri:e', Uri('uri:x'))

        self.assertEquals(
            [{'v': Uri('uri:x')}],
            list(g.matchTriplePatterns(('?v', 'uri:y', Uri('uri:z')))))
        self.assertEquals(
            [{'v': Uri('uri:a')}],
            list(g.matchTriplePatterns(('?v', 'uri:b', None))))
        self.assertEquals(
            [{'v': Uri('uri:a')}, {'v': Uri('uri:x')}, {'v': Uri('uri:z')}],
            sorted(g.matchTriplePatterns((None, None, '?v'), ('?v', None, None))))
        self.assertEquals(
            [{'v': Uri('uri:a')}, {'v': Uri('uri:x')}, {'v': Uri('uri:z')}],
            sorted(g.matchTriplePatterns(('?v', None, None), (None, None, '?v'))))
        self.assertEquals(
            [dict(v0=Uri('uri:a'), v1=Uri('uri:x'), v2=Uri('uri:z'))],
            list(g.matchTriplePatterns(
                ('?v0', None, '?v1'),
                ('?v1', None, '?v2'),
                ('?v2', None, '?v0'),
                ('?v0', None, Literal('c')))))
Example #2
0
    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))
Example #3
0
    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))
Example #4
0
    def testRemoveTripleOfNonExistingDoesNotLeak(self):
        g = Graph()

        self.assertEquals(0, len(list(g.triples())))
        self.assertEquals(0, len(g._tripleDict))  # Whitebox no-leaking defaultdict entries on delete

        g.removeTriple(subject='u:ri', predicate='p:redicate', object='obj2')

        self.assertEquals(0, len(g._tripleDict))  # Whitebox no-leaking defaultdict entries on delete
Example #5
0
    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))
Example #6
0
    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')]))
Example #7
0
    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))
Example #8
0
    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))
Example #9
0
    def testRemoveTriple(self):
        g = Graph()
        g.addTriple(subject='u:ri', predicate='p:redicate', object='obj')
        g.addTriple(subject='u:ri', predicate='p:redicate', object='obj2')

        self.assertEquals(2, len(list(g.triples())))
        self.assertEquals(12, len(g._tripleDict))  # Whitebox no-leaking defaultdict entries on delete

        g.removeTriple(subject='u:ri', predicate='p:redicate', object='obj2')

        self.assertEquals(8, len(g._tripleDict))  # Whitebox no-leaking defaultdict entries on delete

        # Keep 1
        self.assertEquals(1, len(list(g.triples())))
        self.assertEquals(1, len(list(g.triples(None, None, None))))
        self.assertEquals(1, len(list(g.triples(None, None, 'obj'))))
        self.assertEquals(1, len(list(g.triples(None, 'p:redicate', None))))
        self.assertEquals(1, len(list(g.triples(None, 'p:redicate', 'obj'))))
        self.assertEquals(1, len(list(g.triples('u:ri', None, None))))
        self.assertEquals(1, len(list(g.triples('u:ri', None, 'obj'))))
        self.assertEquals(1, len(list(g.triples('u:ri', 'p:redicate', None))))
        self.assertEquals(1, len(list(g.triples('u:ri', 'p:redicate', 'obj'))))

        # Kill 1
        self.assertEquals(0, len(list(g.triples(None, None, 'obj2'))))
        self.assertEquals(0, len(list(g.triples(None, 'p:redicate', 'obj2'))))
        self.assertEquals(0, len(list(g.triples('u:ri', 'p:redicate', 'obj2'))))
Example #10
0
    def testGraph(self):
        g = Graph()
        g.addTriple('x', 'y', 'z')
        g.addTriple(subject='a', predicate='b', object='c')
        self.assertEquals([('x', 'y', 'z'), ('a', 'b', 'c')], list(g.triples()))

        # 'x', 'y', 'z'  -->       # 000
        g.addTriple('x', 'y', '3') # 001
        g.addTriple('x', '2', 'z') # 010
        g.addTriple('x', '2', '3') # 011
        g.addTriple('1', 'y', 'z') # 100
        g.addTriple('1', 'y', '3') # 101
        g.addTriple('1', '2', 'z') # 110
        g.addTriple('1', '2', '3') # 111

        self.assertEquals([('x', 'y', 'z')], sorted(g.triples('x', 'y', 'z')))
        self.assertEquals([('x', 'y', 'z')], sorted(g.triples(subject='x', predicate='y', object='z')))
        self.assertEquals([('x', 'y', '3'), ('x', 'y', 'z')], sorted(g.triples('x', 'y', None)))
        self.assertEquals([('x', '2', 'z'), ('x', 'y', 'z')], sorted(g.triples('x', None, 'z')))
        self.assertEquals([('x', '2', '3'), ('x', '2', 'z'), ('x', 'y', '3'), ('x', 'y', 'z')], sorted(g.triples('x', None, None)))
        self.assertEquals([('1', 'y', 'z'), ('x', 'y', 'z')], sorted(g.triples(None, 'y', 'z')))
        self.assertEquals([('1', 'y', '3'), ('1', 'y', 'z'), ('x', 'y', '3'), ('x', 'y', 'z')], sorted(g.triples(None, 'y', None)))
        self.assertEquals([('1', '2', 'z'), ('1', 'y', 'z'), ('x', '2', 'z'), ('x', 'y', 'z')], sorted(g.triples(None, None, 'z')))
        self.assertEquals(sorted(g.triples()), sorted(g.triples(None, None, None)))
        self.assertEquals(9, len(list(g.triples())))

        # objects()
        self.assertEquals(['3', 'z'], sorted(g.objects(subject='x', predicate='y')))
Example #11
0
    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'))
Example #12
0
    def testGraphContains(self):
        g = Graph()
        g.addTriple('u:ri', 'p:redicate', 'obj')
        self.assertTrue(('u:ri', 'p:redicate', 'obj') in g)
        self.assertTrue(('u:ri', 'p:redicate', None) in g)
        self.assertTrue(('u:ri', None, 'obj') in g)
        self.assertTrue(('u:ri', None, None) in g)
        self.assertTrue((None, 'p:redicate', 'obj') in g)
        self.assertTrue((None, 'p:redicate', None) in g)
        self.assertTrue((None, None, 'obj') in g)
        self.assertTrue((None, None, None) in g)
        self.assertFalse(('U:ri', 'p:redicate', 'obj') in g)
        self.assertFalse(('u:ri', 'P:redicate', 'obj') in g)
        self.assertFalse(('u:ri', 'p:redicate', 'Obj') in g)

        # Same for Uri/Literal/BNode
        g = Graph()
        g.addTriple('u:ri', 'p:redicate', Uri('o:bj'))
        self.assertTrue(('u:ri', 'p:redicate', Uri('o:bj')) in g)
        self.assertTrue((None, 'p:redicate', Uri('o:bj')) in g)
        self.assertTrue(('u:ri', 'p:redicate', None) in g)
        self.assertFalse(('u:ri', 'p:redicate', Uri('O:bj')) in g)

        g = Graph()
        g.addTriple('u:ri', 'p:redicate', BNode('_:42'))
        self.assertTrue(('u:ri', 'p:redicate', BNode('_:42')) in g)
        self.assertTrue((None, 'p:redicate', BNode('_:42')) in g)
        self.assertTrue(('u:ri', 'p:redicate', None) in g)
        self.assertFalse(('u:ri', 'p:redicate', BNode('_:666')) in g)

        g = Graph()
        g.addTriple('u:ri', 'p:redicate', Literal('obj', lang='en'))
        self.assertTrue(('u:ri', 'p:redicate', Literal('obj', lang='en')) in g)
        self.assertTrue((None, 'p:redicate', Literal('obj', lang='en')) in g)
        self.assertTrue(('u:ri', 'p:redicate', None) in g)
        self.assertFalse(('u:ri', 'p:redicate', Literal('Obj', lang='en')) in g)
        # Also false, different lang or datatype (**no guessing**, inspect triples & compare yourself if thats your game).
        self.assertFalse(('u:ri', 'p:redicate', Literal('obj', lang='nl')) in g)
        self.assertFalse(('u:ri', 'p:redicate', Literal('obj')) in g)
Example #13
0
 def setUp(self):
     SeecrTestCase.setUp(self)
     self.sink = Graph()
Example #14
0
class RdfParserTest(SeecrTestCase):
    def setUp(self):
        SeecrTestCase.setUp(self)
        self.sink = Graph()

    def testOne(self):
        RDFParser(sink=self.sink).parse(XML(INPUT_RDF))
        objects = sorted(list(self.sink.objects(subject=uri, curie="rdfs:seeAlso")))
        self.assertEquals(sorted([Uri("http://example.com"), Literal("http://example.org")]), objects)

    def testConvenienceGraph(self):
        graph = RDFParser().parse(XML(INPUT_RDF))
        objects = sorted(list(graph.objects(subject=uri, curie="rdfs:seeAlso")))
        self.assertEquals(sorted([Uri("http://example.com"), Literal("http://example.org")]), objects)

    def testTypeFromElementTag(self):
        based_xml = (
            """<rdf:RDF
        %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_owl)s
        xml:base="http://purl.org/ontology/mo/"
        >
        <owl:Class rdf:about="Track">
            <rdfs:label>track</rdfs:label>
            <rdfs:subClassOf rdf:resource="MusicalManifestation"/>
        </owl:Class>
        </rdf:RDF>"""
            % namespaces
        )
        RDFParser(sink=self.sink).parse(XML(based_xml))
        self.assertEquals(
            [Uri(namespaces.owl + "Class")],
            list(self.sink.objects(subject="http://purl.org/ontology/mo/Track", curie="rdf:type")),
        )

    def testParseNodeWithoutRdfContainer(self):
        xml = (
            """<owl:Class %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_owl)s rdf:about="http://purl.org/ontology/mo/Track">
            <rdfs:label>track</rdfs:label>
            <rdfs:subClassOf rdf:resource="http://purl.org/ontology/mo/MusicalManifestation"/>
        </owl:Class>"""
            % namespaces
        )
        RDFParser(sink=self.sink).parse(XML(xml))
        self.assertEquals(
            [Uri(namespaces.owl + "Class")],
            list(self.sink.objects(subject="http://purl.org/ontology/mo/Track", curie="rdf:type")),
        )

    def testLiteralWithCommentAndPI(self):
        RDFParser(sink=self.sink).parse(XML(INPUT_RDF))
        self.assertEquals(
            sorted([Literal("1970"), Literal("1970-01-01")], key=lambda l: (l.value, l.lang)),
            sorted(
                [o for s, p, o in self.sink.triples(subject=uri, predicate=namespaces.curieToUri("dcterms:date"))],
                key=lambda l: (l.value, l.lang),
            ),
        )

    def testBlankNodesAndLiterals(self):
        BNode.nextGenId = 0
        RDFParser(sink=self.sink).parse(XML(INPUT_RDF))
        self.assertEquals([BNode("_:id0")], list(self.sink.objects(subject=uri, curie="dcterms:creator")))
        self.assertEquals(
            [Uri("http://dbpedia.org/ontology/Person")], list(self.sink.objects(subject="_:id0", curie="rdf:type"))
        )

        contributor = self.sink.objects(subject=uri, predicate=namespaces.curieToUri("dcterms:contributor"))[0]
        self.assertEquals(
            [Literal("Anonymous", lang="en")], list(self.sink.objects(subject=contributor.value, curie="rdfs:label"))
        )

        self.assertTrue(
            Literal("An illustrated history of Black Americans", lang="en")
            in set(self.sink.objects(subject=uri, curie="dcterms:title"))
        )

    def testBase(self):
        RDFParser(sink=self.sink).parse(XML(RDF_WITH_BASE))
        self.assertEquals(
            [
                (
                    "http://example.org/base/2",
                    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
                    Uri("http://example.org/base/Book"),
                )
            ],
            list(self.sink.triples()),
        )

    def testBase2(self):
        based_xml = (
            """<rdf:RDF
        %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_owl)s
        xml:base="http://purl.org/ontology/mo/"
        >
        <owl:Class rdf:about="Track">
            <rdfs:label>track</rdfs:label>
            <rdfs:subClassOf rdf:resource="MusicalManifestation"/>
        </owl:Class>
        </rdf:RDF>"""
            % namespaces
        )
        RDFParser(sink=self.sink).parse(XML(based_xml))
        self.assertTrue(
            (
                "http://purl.org/ontology/mo/Track",
                "http://www.w3.org/2000/01/rdf-schema#subClassOf",
                Uri("http://purl.org/ontology/mo/MusicalManifestation"),
            )
            in set(self.sink.triples())
        )

    def testParsingEntitiesNoProblem(self):
        custom_type_relations_rdf = parse(open(join(testDatadir, "custom_type_relations.rdf")))
        RDFParser(sink=self.sink).parse(custom_type_relations_rdf)

        self.assertTrue(
            (
                "http://purl.org/ontology/mo/Track",
                "http://www.w3.org/2000/01/rdf-schema#subClassOf",
                Uri("http://dbpedia.org/ontology/MusicalWork"),
            )
            in set(self.sink.triples())
        )

    def testEmptyPropertyAttribs(self):
        RDFParser(sink=self.sink).parse(XML(INPUT_RDF))
        relationBnode = self.sink.objects(subject=uri, curie="dcterms:relation")[0]
        self.assertEquals(
            [Literal("JPM")],
            list(self.sink.objects(subject=relationBnode.value, predicate=namespaces.curieToUri("dcterms:title"))),
        )
        self.assertEquals(
            [Uri(namespaces.curieToUri("foaf:Person"))],
            list(self.sink.objects(subject=relationBnode.value, curie="rdf:type")),
        )

    def testGetText(self):
        node = XML("<node>v<!-- com -->w<!-- ment -->x<?pro ce?>y<?ss ing?>z</node>")
        self.assertEquals("vwxyz", getText(node))

        node = XML("<node>x<sub>subtext<subsub />subsubtail</sub>y<a><b>text</b>text</a>z</node>")
        self.assertEquals("xyz", getText(node))

        node = XML("<node><a><b /></a></node>")
        self.assertEquals(None, getText(node))

        node = XML("<node> <a><b /></a></node>")
        self.assertEquals(" ", getText(node))

        node = XML("<node><a><b /></a> </node>")
        self.assertEquals(" ", getText(node))

        node = XML("<node><a /> <b /></node>")
        self.assertEquals(" ", getText(node))

        node = XML("<node><!-- comment --></node>")
        self.assertEquals(None, getText(node))

        node = XML("<node><?pi 3.14?></node>")
        self.assertEquals(None, getText(node))

    def testRdfID(self):
        RDFParser(sink=self.sink).parse(
            XML(
                """<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
               xmlns:exterms="http://www.example.com/terms/"
               xml:base="http://www.example.com/2002/04/products">
    <rdf:Description rdf:ID="item10245">
        <exterms:model>Overnighter</exterms:model>
    </rdf:Description>
</rdf:RDF>
"""
            )
        )
        self.assertEquals(
            [
                (
                    "http://www.example.com/2002/04/products#item10245",
                    "http://www.example.com/terms/model",
                    Literal("Overnighter"),
                )
            ],
            list(self.sink.triples()),
        )

    def testNodeID(self):
        RDFParser(sink=self.sink).parse(
            XML(
                """<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" %(xmlns_rdfs)s
               xmlns:exterms="http://www.example.com/terms/">
    <rdf:Description rdf:about="http://example.com/something">
        <exterms:relatedTo rdf:nodeID="abc"/>
    </rdf:Description>
    <rdf:Description rdf:nodeID="abc">
        <rdfs:label>ABC</rdfs:label>
    </rdf:Description>
</rdf:RDF>"""
                % namespaces
            )
        )
        self.assertEquals(
            [
                ("http://example.com/something", "http://www.example.com/terms/relatedTo", BNode("_:abc")),
                ("_:abc", namespaces.rdfs + "label", Literal("ABC")),
            ],
            list(self.sink.triples()),
        )

    def testShouldIgnorePropertyEltWithoutValue(self):
        RDFParser(sink=self.sink).parse(
            XML(
                """<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s
               xmlns:exterms="http://www.example.com/terms/">
    <rdf:Description rdf:about="http://example.com/something">
        <exterms:relatedTo/>
    </rdf:Description>
</rdf:RDF>"""
                % namespaces
            )
        )
        self.assertEquals(
            [("http://example.com/something", "http://www.example.com/terms/relatedTo", Literal(""))],
            list(self.sink.triples()),
        )

    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 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 testReificationFromBNodeSubject(self):
        BNode.nextGenId = 0
        RDFParser(sink=self.sink).parse(
            XML(
                """<rdf:RDF %(xmlns_rdf)s %(xmlns_rdfs)s %(xmlns_dcterms)s>
    <rdf:Description>
        <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(
                [
                    ("_:id0", 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", BNode("_:id0")),
                ]
            ),
            set(self.sink.triples()),
        )
Example #15
0
    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))