Пример #1
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')))
Пример #2
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()),
        )