Ejemplo n.º 1
0
 def test_namespace_split_uri(self):
     nm = NamespaceManager(Graph())
     nm.bind('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
     ns, property_ = nm.split_uri(
         'http://www.w3.org/1999/02/22-rdf-syntax-ns#label')
     self.assertEqual(str(ns),
                      'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
     self.assertEqual(nm.store.prefix(ns), 'rdf')
     self.assertEqual(property_, 'label')
Ejemplo n.º 2
0
    def test_namespace_check_uriref(self):
        nm = NamespaceManager(Graph())
        uri = URIRef('http://dig.isi.edu/ontologies/dig/Event')
        self.assertEqual(nm.check_uriref(uri), uri)

        text = 'http://dig.isi.edu/ontologies/dig/Event'
        self.assertEqual(nm.check_uriref(text), uri)

        text = URI('http://dig.isi.edu/ontologies/dig/Event')
        self.assertEqual(nm.check_uriref(text), uri)
Ejemplo n.º 3
0
 def test_namespace_parse_uri(self):
     nm = NamespaceManager(Graph())
     nm.bind('dig', DIG)
     uri = URIRef('http://dig.isi.edu/ontologies/dig/Event')
     self.assertEqual(uri, nm.parse_uri(uri))
     for case in ('http://dig.isi.edu/ontologies/dig/Event', 'dig:Event'):
         self.assertEqual(uri, nm.parse_uri(case))
     with self.assertRaises(PrefixNotFoundException):
         nm.parse_uri(':Event')
     nm.bind('', 'http://dig.isi.edu/ontologies/dig/')
     self.assertEqual(uri, nm.parse_uri(':Event'))
     self.assertEqual(uri, nm.parse_uri('Event'))
     self.assertEqual(uri, nm.parse_uri(URI(':Event')))
     with self.assertRaises(WrongFormatURIException):
         nm.parse_uri(None)
Ejemplo n.º 4
0
    def test_namespace_bind(self):
        correct_content = '@prefix : <http://w.org/> . @prefix schema: <http://schema.org/> .'
        replace_content = '@prefix schema: <http://dig.schema.org/> .'
        nm = NamespaceManager(Graph())
        nm.bind('owl', rlns.OWL)
        nm.bind('schema', SCHEMA)
        nm.bind('dig', DIG)
        namespace = {x[0] for x in nm.namespaces()}
        self.assertNotIn('', namespace)
        for name in ('owl', 'rdfs', 'rdf', 'schema', 'xsd', 'xml', 'dig'):
            self.assertIn(name, namespace)

        nm.graph.parse(data=correct_content, format='ttl')
        namespace = {x[0] for x in nm.namespaces()}
        self.assertIn('', namespace)

        # this update https://github.com/RDFLib/rdflib/commit/b94488da7a0ba1e664c87659eabb61ce1d3b489d
        # breaks `replace` argument in this workflow: graph.parse -> parser.parse -> graph.bind -> namespace.bind
        # concrete parsers don't pass this argument to graph.bind hence it's always False in namespace.bind
        # as its default value set in graph.bind
        # the proper solution is: every concrete parser should accept argument `replace` and pass it to graph.bind
        # hence, if you need to replace namespace, change what I mentioned above or wait for official update.
        # here I commented out the tests of this feature
        nm.graph.parse(data=replace_content, format='ttl')
        namespace = {x[0] for x in nm.namespaces()}
Ejemplo n.º 5
0
 def __init__(self, graph=None, identifier=None):
     if graph:
         self._g = graph
     else:
         self._g = rdflib.Graph(identifier=identifier)
     self._ns = NamespaceManager(self._g)
Ejemplo n.º 6
0
class Graph(object):
    def __init__(self, graph=None, identifier=None):
        if graph:
            self._g = graph
        else:
            self._g = rdflib.Graph(identifier=identifier)
        self._ns = NamespaceManager(self._g)

    def bind(self, prefix, namespace, override=True, replace=False):
        self._ns.bind(prefix, namespace, override, replace)

    def add_subject(self, subjects, context=None):
        if not context:
            context = set()

        for t in subjects:
            s, p, o = t
            if isinstance(o, Subject) and o not in context:
                context.add(o)
                self.add_subject(o, context)
                o = o.subject

            # convert triple to RDFLib recognizable format
            triple = self._convert_triple_rdflib((s, p, o))
            self._g.add(triple)

    def add_triple(self, s, p, o):
        t = Subject(s)
        t.add_property(p, o)
        self.add_subject(t)

    def parse(self, content, format='turtle'):
        self._g.parse(data=content, format=format)

    def serialize(self, format='ttl', namespace_manager=None, **kwargs):
        # may need some way to serialize ttl, json-ld
        if format.lower() in ('ttl', 'turtle'):
            b_string = self._g.serialize(format=format,
                                         namespace_manager=namespace_manager,
                                         **kwargs)
        elif format.lower() == 'json-ld':
            b_string = self._g.serialize(format=format,
                                         contexts=namespace_manager,
                                         **kwargs)
        else:
            b_string = self._g.serialize(format=format, **kwargs)
        return b_string.decode('UTF-8')

    @lru_cache()
    def _resolve_uri(self, uri: URI) -> rdflib.URIRef:
        """
        Convert a URI object into a RDFLib URIRef, including resolve its context

        :param uri: URI
        :return: rdflib.URIRef
        """
        return self._ns.parse_uri(uri.value)

    def _is_rdf_type(self, uri: URI) -> bool:
        if not isinstance(uri, URI):
            return False
        return self._resolve_uri(uri) == rdflib.RDF.type

    def _convert_triple_rdflib(self, triple):
        """
        Convert a Node triple into RDFLib triple
        """
        s, p, o = triple
        sub = self._resolve_uri(s) if isinstance(s, URI) else rdflib.BNode(
            s.value)
        pred = self._resolve_uri(p)
        if isinstance(o, URI):
            obj = self._resolve_uri(o)
        elif isinstance(o, Subject):
            if isinstance(o.subject, URI):
                obj = self._resolve_uri(o.subject)
            else:
                obj = rdflib.BNode(o.subject.value)
        elif isinstance(o, BNode):
            obj = rdflib.BNode(o.value)
        else:
            obj = rdflib.Literal(o.value, o.lang, o.raw_type)
        return sub, pred, obj
Ejemplo n.º 7
0
    def test_namespace_bind(self):
        correct_content = '@prefix : <http://w.org/> . @prefix schema: <http://schema.org/> .'
        replace_content = '@prefix schema: <http://dig.schema.org/> .'
        nm = NamespaceManager(Graph())
        nm.bind('owl', rlns.OWL)
        nm.bind('schema', SCHEMA)
        nm.bind('dig', DIG)
        namespace = {x[0] for x in nm.namespaces()}
        self.assertNotIn('', namespace)
        for name in ('owl', 'rdfs', 'rdf', 'schema', 'xsd', 'xml', 'dig'):
            self.assertIn(name, namespace)

        nm.graph.parse(data=correct_content, format='ttl')
        namespace = {x[0] for x in nm.namespaces()}
        self.assertIn('', namespace)

        nm.graph.parse(data=replace_content, format='ttl')
        namespace = {x[0] for x in nm.namespaces()}
        self.assertIn('schema', namespace)
        self.assertNotEqual(nm.store.namespace('schema'), SCHEMA)
        self.assertEqual(nm.store.namespace('schema'),
                         URIRef('http://dig.schema.org/'))