def testFindLabelUsingRealOntology(self):
     subdir = join(self.tempdir, 'subdir')
     makedirs(subdir)
     copy(join(rdfDir, 'nl_property_labels.rdf'), subdir)
     with stdout_replaced():
         g = GraphComponent(rdfSources=[self.tempdir])
     self.assertEquals(Literal('Titel', lang='nl'), g.findLabel(namespaces.dcterms + 'title'))
     self.assertEquals(Literal('Maker', lang='nl'), g.findLabel(namespaces.dcterms + 'creator'))
     self.assertEquals(Literal('Tijd', lang='nl'), g.findLabel("http://purl.org/NET/c4dm/event.owl#time"))
 def testTriples(self):
     with stdout_replaced():
         g = GraphComponent(rdfSources=[])
     g = g.makeGraph(parseXml(RDF_XML))
     self.assertEquals([('uri:uri', namespaces.dcterms + 'title', Literal('The title'))], list(g.triples(subject='uri:uri', predicate=namespaces.dcterms + 'title')))
     self.assertEquals([('uri:uri', namespaces.dcterms + 'title', Literal('The title'))], list(g.triples(subject=None, predicate=namespaces.dcterms + 'title', object=Literal('The title'))))
     self.assertEquals([('uri:uri', namespaces.rdf + 'type', Uri('type:type#'))], list(g.triples(object=Uri('type:type#'))))
     self.assertEquals(1, len(list(g.triples(object=Uri('type:type#')))))
     self.assertEquals(1, len(list(g.triples(subject='uri:uri', predicate=namespaces.rdf+'type', object=Uri('type:type#')))))
 def testMakeGraph(self):
     with stdout_replaced():
         g = GraphComponent(rdfSources=[])
     g = g.makeGraph(parseXml(RDF_XML))
     self.assertEquals(set([
             ('uri:uri', namespaces.dcterms + 'title', Literal('The title')),
             ('uri:uri', namespaces.rdf + 'type', Uri('type:type#'))
         ]),
         set(list(g.triples(None, None, None)))
     )
    def testGraphNavigationByTriples(self):
        with stdout_replaced():
            gc = GraphComponent(rdfSources=[])
        g = gc.makeGraph(lxmlNode=parseXml(RDF_XML_NAVIGATION))

        result = list(g.triples(subject='uri:nav', predicate=namespaces.dcterms+'publisher'))
        self.assertEquals(1, len(result))
        self.assertEquals(
            ('uri:nav', namespaces.dcterms+'publisher'),
            result[0][:2])
        bnode = result[0][2]
        self.assertTrue(isinstance(bnode, BNode))

        result = list(g.triples(subject=bnode.value))
        self.assertEquals(
            [(bnode.value, namespaces.rdfs+'label', Literal('Pub'))],
            result)
    def testTriplesUsingRealOntology(self):
        subdir = join(self.tempdir, 'subdir')
        makedirs(subdir)
        copy(join(rdfDir, 'nl_property_labels.rdf'), subdir)
        with stdout_replaced():
            g = GraphComponent(rdfSources=[self.tempdir])

        self.assertTrue(10 < len(list(g.triples())), list(g.triples()))
        self.assertEquals([
                (namespaces.curieToUri('dcterms:title'),
                 namespaces.curieToUri('rdfs:label'),
                 Literal('Titel', lang='nl')
                )
            ],
            list(g.triples(
                subject=namespaces.curieToUri('dcterms:title'),
                predicate=namespaces.curieToUri('rdfs:label'))),
        )
    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()))