Exemple #1
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'))))
Exemple #2
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')))))
    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 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))
Exemple #5
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')]))
    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))
Exemple #8
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)
    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))
Exemple #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')))
Exemple #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'))
    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))