Python rdflib.BNode() Examples

The following are 30 code examples of rdflib.BNode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rdflib , or try the search function .
Example #1
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_dataset_has_resources(self):
        node = BNode()
        g = Graph()

        g.add((node, RDF.type, DCAT.Dataset))
        g.add((node, DCT.title, Literal(faker.sentence())))
        for i in range(3):
            rnode = BNode()
            g.set((rnode, RDF.type, DCAT.Distribution))
            g.set((rnode, DCAT.downloadURL, URIRef(faker.uri())))
            g.add((node, DCAT.distribution, rnode))

        dataset = dataset_from_rdf(g)
        dataset.validate()

        assert isinstance(dataset, Dataset)
        assert len(dataset.resources) == 3 
Example #2
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_unicode(self):
        g = Graph()
        title = 'ééé'
        description = 'éééé'

        node = BNode()
        g.add((node, RDF.type, DCAT.Dataset))
        g.add((node, DCT.title, Literal(title)))
        g.add((node, DCT.description, Literal(description)))

        rnode = BNode()
        g.add((rnode, RDF.type, DCAT.Distribution))
        g.add((rnode, DCT.title, Literal(title)))
        g.add((rnode, DCT.description, Literal(description)))
        g.add((rnode, DCAT.downloadURL, URIRef(faker.uri())))
        g.add((node, DCAT.distribution, rnode))

        dataset = dataset_from_rdf(g)
        dataset.validate()
        assert dataset.title == title
        assert dataset.description == description

        resource = dataset.resources[0]
        assert resource.title == title
        assert resource.description == description 
Example #3
Source File: __init__.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def get_nodes_from_node_expression(self, expr, focus_node, data_graph):
        if expr == SH_this:
            return [focus_node]
        elif isinstance(expr, (rdflib.URIRef, rdflib.Literal)):
            return [expr]
        elif isinstance(expr, rdflib.BNode):
            path_nodes = set(self.shape.sg.objects(expr, SH_path))
            if len(path_nodes) > 0:
                path_results = []
                for p in path_nodes:
                    vals = self.shape.value_nodes_from_path(self.shape.sg, focus_node, p, data_graph)
                    path_results.extend(vals)
                return path_results
            else:
                raise NotImplementedError("Unsupported expression s, p, or o, in SHACL TripleRule")
        else:
            raise NotImplementedError("Unsupported expression s, p, or o, in SHACL TripleRule") 
Example #4
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_match_license_from_license_title(self):
        license = LicenseFactory()
        node = BNode()
        g = Graph()

        g.set((node, RDF.type, DCAT.Dataset))
        g.set((node, DCT.title, Literal(faker.sentence())))
        rnode = BNode()
        g.set((rnode, RDF.type, DCAT.Distribution))
        g.set((rnode, DCAT.downloadURL, URIRef(faker.uri())))
        g.set((rnode, DCT.license, Literal(license.title)))
        g.add((node, DCAT.distribution, rnode))

        dataset = dataset_from_rdf(g)
        dataset.validate()

        assert isinstance(dataset.license, License)
        assert dataset.license == license 
Example #5
Source File: rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def organization_to_rdf(org, graph=None):
    '''
    Map a Resource domain model to a DCAT/RDF graph
    '''
    graph = graph or Graph(namespace_manager=namespace_manager)
    if org.id:
        org_url = url_for('organizations.show_redirect',
                          org=org.id,
                          _external=True)
        id = URIRef(org_url)
    else:
        id = BNode()
    o = graph.resource(id)
    o.set(RDF.type, FOAF.Organization)
    o.set(FOAF.name, Literal(org.name))
    o.set(RDFS.label, Literal(org.name))
    if org.url:
        o.set(FOAF.homepage, URIRef(org.url))
    return o 
Example #6
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_match_license_from_license_uri(self):
        license = LicenseFactory()
        node = BNode()
        g = Graph()

        g.set((node, RDF.type, DCAT.Dataset))
        g.set((node, DCT.title, Literal(faker.sentence())))
        rnode = BNode()
        g.set((rnode, RDF.type, DCAT.Distribution))
        g.set((rnode, DCAT.downloadURL, URIRef(faker.uri())))
        g.set((rnode, DCT.license, URIRef(license.url)))
        g.add((node, DCAT.distribution, rnode))

        dataset = dataset_from_rdf(g)
        dataset.validate()

        assert isinstance(dataset.license, License)
        assert dataset.license == license 
Example #7
Source File: rdf.py    From dgl with Apache License 2.0 6 votes vote down vote up
def parse_entity(self, term):
        if isinstance(term, rdf.Literal):
            return Entity(id=str(term), cls="_Literal")
        elif isinstance(term, rdf.BNode):
            return None
        entstr = str(term)
        if entstr.startswith(self.entity_prefix):
            inst = entstr[len(self.entity_prefix):]
            if self.d_entity.match(inst):
                cls = 'd'
            elif self.bond_entity.match(inst):
                cls = 'bond'
            else:
                cls = None
            return Entity(id=inst, cls=cls)
        else:
            return None 
Example #8
Source File: profiles.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def _add_agent(self, _dict, ref, basekey, _type):
        ''' Stores the Agent in this format:
                <dct:publisher rdf:resource="http://dati.gov.it/resource/Amministrazione/r_liguri"/>
                    <dcatapit:Agent rdf:about="http://dati.gov.it/resource/Amministrazione/r_liguri">
                        <rdf:type rdf:resource="&foaf;Agent"/>
                        <dct:identifier>r_liguri</dct:identifier>
                        <foaf:name>Regione Liguria</foaf:name>
                    </dcatapit:Agent>

            Returns the ref to the agent node
        '''

        agent_name = self._get_dict_value(_dict, basekey + '_name', 'N/A')
        agent_id = self._get_dict_value(_dict, basekey + '_identifier','N/A')

        agent = BNode()

        self.g.add((agent, RDF['type'], DCATAPIT.Agent))
        self.g.add((agent, RDF['type'], FOAF.Agent))
        self.g.add((ref, _type, agent))

        self.g.add((agent, FOAF.name, Literal(agent_name)))
        self.g.add((agent, DCT.identifier, Literal(agent_id)))

        return agent 
Example #9
Source File: stringify.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def stringify_node(graph, node, ns_manager=None, recursion=0):
    if ns_manager is None:
        ns_manager = graph.namespace_manager
    if isinstance(ns_manager, rdflib.Graph):
        #json-ld loader can set namespace_manager to the conjunctive graph itself.
        ns_manager = ns_manager.namespace_manager
    ns_manager.bind("sh", SH, override=False, replace=False)
    if isinstance(node, rdflib.Literal):
        return stringify_literal(graph, node, ns_manager=ns_manager)
    if isinstance(node, rdflib.BNode):
        if isinstance(graph, (rdflib.ConjunctiveGraph, rdflib.Dataset)):
            graph = find_node_named_graph(graph, node)
        return stringify_blank_node(graph, node, ns_manager=ns_manager,
                                    recursion=recursion+1)
    if isinstance(node, rdflib.URIRef):
        return node.n3(namespace_manager=ns_manager)
    else:
        node_string = str(node)
    return node_string 
Example #10
Source File: rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def user_to_rdf(user, graph=None):
    '''
    Map a Resource domain model to a DCAT/RDF graph
    '''
    graph = graph or Graph(namespace_manager=namespace_manager)
    if user.id:
        user_url = url_for('users.show_redirect',
                           user=user.id,
                           _external=True)
        id = URIRef(user_url)
    else:
        id = BNode()
    o = graph.resource(id)
    o.set(RDF.type, FOAF.Person)
    o.set(FOAF.name, Literal(user.fullname))
    o.set(RDFS.label, Literal(user.fullname))
    if user.website:
        o.set(FOAF.homepage, URIRef(user.website))
    return o 
Example #11
Source File: rdf.py    From dgl with Apache License 2.0 6 votes vote down vote up
def parse_entity(self, term):
        if isinstance(term, rdf.Literal):
            return None
        elif isinstance(term, rdf.BNode):
            return Entity(id=str(term), cls='_BNode')
        entstr = str(term)
        if entstr.startswith(self.entity_prefix):
            sp = entstr.split('/')
            assert len(sp) == 7, entstr
            spp = sp[6].split('-')
            if len(spp) == 2:
                # instance
                cls, inst = spp
            else:
                cls = 'TYPE'
                inst = spp
            return Entity(id=inst, cls=cls)
        else:
            return None 
Example #12
Source File: rdf.py    From dgl with Apache License 2.0 6 votes vote down vote up
def parse_entity(self, term):
        if isinstance(term, rdf.Literal):
            return None
        elif isinstance(term, rdf.BNode):
            return None
        entstr = str(term)
        if entstr.startswith(self.status_prefix):
            return None
        if entstr.startswith(self.entity_prefix):
            sp = entstr.split('/')
            if len(sp) != 7:
                return None
            # instance
            cls = '%s/%s' % (sp[4], sp[5])
            inst = sp[6]
            return Entity(id=inst, cls=cls)
        else:
            return None 
Example #13
Source File: test_euro_dcatap_profile_serialize.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def test_spatial_bad_json_no_wkt(self):
        dataset = {
            'id': '4b6fe9ca-dc77-4cec-92a4-55c6624a5bd6',
            'name': 'test-dataset',
            'extras': [
                {'key': 'spatial', 'value': 'NotJSON'},

            ]
        }
        extras = self._extras(dataset)

        s = RDFSerializer()
        g = s.g

        dataset_ref = s.graph_from_dataset(dataset)

        spatial = self._triple(g, dataset_ref, DCT.spatial, None)[2]
        assert spatial
        assert_true(isinstance(spatial, BNode))
        # Geometry in GeoJSON
        assert self._triple(g, spatial, LOCN.geometry, extras['spatial'], GEOJSON_IMT)

        # Geometry in WKT
        eq_(len([t for t in g.triples((spatial, LOCN.geometry, None))]), 1) 
Example #14
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_dataset_has_resources_from_literal_instead_of_uriref(self):
        node = BNode()
        g = Graph()

        g.add((node, RDF.type, DCAT.Dataset))
        g.add((node, DCT.title, Literal(faker.sentence())))
        rnode = BNode()
        g.set((rnode, RDF.type, DCAT.Distribution))
        # Resource URL is expressed as a Literal
        g.set((rnode, DCAT.downloadURL, Literal(faker.uri())))
        g.add((node, DCAT.distribution, rnode))

        dataset = dataset_from_rdf(g)
        dataset.validate()

        assert isinstance(dataset, Dataset)
        assert len(dataset.resources) == 1 
Example #15
Source File: string_based_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def _evaluate_string_rule(self, r, target_graph, f_v_dict):
        reports = []
        non_conformant = False
        assert isinstance(r, rdflib.Literal)
        min_len = int(r.value)
        if min_len < 0:
            raise ReportableRuntimeError("Minimum length cannot be less than zero!")
        for f, value_nodes in f_v_dict.items():
            for v in value_nodes:
                flag = False
                if min_len == 0:
                    flag = True  # min len zero always passes
                elif isinstance(v, rdflib.BNode):
                    # blank nodes cannot pass minLen validation
                    pass
                else:
                    v_string = self.value_node_to_string(v)
                    flag = len(v_string) >= min_len
                if not flag:
                    non_conformant = True
                    rept = self.make_v_result(target_graph, f, value_node=v)
                    reports.append(rept)
        return non_conformant, reports 
Example #16
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_dataset_has_resources_from_buggy_plural_distribution(self):
        '''Try to extract resources from the wrong distributions attribute'''
        node = BNode()
        g = Graph()

        g.add((node, RDF.type, DCAT.Dataset))
        g.add((node, DCT.title, Literal(faker.sentence())))
        rnode = BNode()
        g.set((rnode, RDF.type, DCAT.Distribution))
        g.set((rnode, DCAT.downloadURL, URIRef(faker.uri())))
        g.add((node, DCAT.distributions, rnode))  # use plural name

        dataset = dataset_from_rdf(g)
        dataset.validate()

        assert isinstance(dataset, Dataset)
        assert len(dataset.resources) == 1 
Example #17
Source File: property_pair_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def evaluate(self, target_graph, focus_value_nodes, _evaluation_path):
        """

        :type focus_value_nodes: dict
        :type target_graph: rdflib.Graph
        """
        reports = []
        non_conformant = False

        for lt in iter(self.property_compare_set):
            if isinstance(lt, rdflib.Literal) or\
               isinstance(lt, rdflib.BNode):
                raise ReportableRuntimeError(
                    "Value of sh:lessThan MUST be a URI Identifier.")
            _nc, _r = self._evaluate_less_than(lt, target_graph, focus_value_nodes)
            non_conformant = non_conformant or _nc
            reports.extend(_r)
        return (not non_conformant), reports 
Example #18
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_match_existing_resource_by_url(self):
        dataset = DatasetFactory(resources=ResourceFactory.build_batch(3))
        existing_resource = dataset.resources[1]
        node = BNode()
        g = Graph()

        new_title = faker.sentence()
        g.add((node, RDF.type, DCAT.Distribution))
        g.add((node, DCT.title, Literal(new_title)))
        g.add((node, DCAT.downloadURL, Literal(existing_resource.url)))

        resource = resource_from_rdf(g, dataset)
        resource.validate()

        assert isinstance(resource, Resource)
        assert resource.title == new_title
        assert resource.id == existing_resource.id 
Example #19
Source File: property_pair_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def evaluate(self, target_graph, focus_value_nodes, _evaluation_path):
        """

        :type focus_value_nodes: dict
        :type target_graph: rdflib.Graph
        """
        reports = []
        non_conformant = False

        for lt in iter(self.property_compare_set):
            if isinstance(lt, rdflib.Literal) or\
               isinstance(lt, rdflib.BNode):
                raise ReportableRuntimeError(
                    "Value of sh:lessThanOrEquals MUST be a URI Identifier.")
            _nc, _r = self._evaluate_ltoe(lt, target_graph, focus_value_nodes)
            non_conformant = non_conformant or _nc
            reports.extend(_r)
        return (not non_conformant), reports 
Example #20
Source File: test_euro_dcatap_profile_serialize.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def test_contact_details_author(self):
        dataset = {
            'id': '4b6fe9ca-dc77-4cec-92a4-55c6624a5bd6',
            'name': 'test-dataset',
            'author': 'Example Author',
            'author_email': 'ped@example.com',
        }

        s = RDFSerializer()
        g = s.g

        dataset_ref = s.graph_from_dataset(dataset)

        contact_details = self._triple(g, dataset_ref, DCAT.contactPoint, None)[2]
        assert contact_details
        assert_true(isinstance(contact_details, BNode))
        assert self._triple(g, contact_details, VCARD.fn, dataset['author'])
        assert self._triple(g, contact_details, VCARD.hasEmail, dataset['author_email']) 
Example #21
Source File: test_euro_dcatap_profile_serialize.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def test_contact_details_maintainer(self):
        dataset = {
            'id': '4b6fe9ca-dc77-4cec-92a4-55c6624a5bd6',
            'name': 'test-dataset',
            'maintainer': 'Example Maintainer',
            'maintainer_email': 'dep@example.com',
            'author': 'Example Author',
            'author_email': 'ped@example.com',
        }

        s = RDFSerializer()
        g = s.g

        dataset_ref = s.graph_from_dataset(dataset)

        contact_details = self._triple(g, dataset_ref, DCAT.contactPoint, None)[2]
        assert contact_details
        assert_true(isinstance(contact_details, BNode))
        assert self._triple(g, contact_details, VCARD.fn, dataset['maintainer'])
        assert self._triple(g, contact_details, VCARD.hasEmail, dataset['maintainer_email']) 
Example #22
Source File: expander.py    From dspl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _ExpandObservationDimensionValue(self, dim, data, row_id, row):
    node_id = rdflib.BNode()
    self.graph.add((row_id, SCHEMA.dimensionValue, node_id))
    self.graph.add((node_id, rdflib.RDF.type, SCHEMA.DimensionValue))
    self.graph.add((node_id, SCHEMA.dimension, data['id']))
    for dim_type in data['type']:
      if dim_type.endswith('CategoricalDimension'):
        for type_id in data['types']:
          self.graph.add((node_id, rdflib.RDF.type, type_id))
        self.graph.add((node_id, SCHEMA.codeValue, rdflib.Literal(row[dim])))
      else:
        if data['types']:
          self.graph.add(
              (node_id, SCHEMA.value,
               rdflib.Literal(row[dim],
                              datatype=rdflib.URIRef(data['types'][0]))))
        else:
          self.graph.add((node_id, SCHEMA.value, rdflib.Literal(row[dim]))) 
Example #23
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_download_url_over_access_url(self):
        node = BNode()
        g = Graph()

        access_url = faker.uri()
        g.add((node, RDF.type, DCAT.Distribution))
        g.add((node, DCT.title, Literal(faker.sentence())))
        g.add((node, DCAT.accessURL, Literal(access_url)))

        resource = resource_from_rdf(g)
        resource.validate()
        assert resource.url == access_url

        download_url = faker.uri()
        g.add((node, DCAT.downloadURL, Literal(download_url)))

        resource = resource_from_rdf(g)
        resource.validate()
        assert resource.url == download_url 
Example #24
Source File: profiles.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def _object_value_int(self, subject, predicate):
        '''
        Given a subject and a predicate, returns the value of the object as an
        integer

        Both subject and predicate must be rdflib URIRef or BNode objects

        If the value can not be parsed as intger, returns None
        '''
        object_value = self._object_value(subject, predicate)
        if object_value:
            try:
                return int(object_value)
            except ValueError:
                pass
        return None 
Example #25
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_minimal_resource_fields(self):
        node = BNode()
        g = Graph()

        title = faker.sentence()
        url = faker.uri()
        g.add((node, RDF.type, DCAT.Distribution))
        g.add((node, DCT.title, Literal(title)))
        g.add((node, DCAT.downloadURL, Literal(url)))

        resource = resource_from_rdf(g)
        resource.validate()

        assert isinstance(resource, Resource)
        assert resource.title == title
        assert resource.url == url 
Example #26
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_theme_and_tags(self):
        node = BNode()
        g = Graph()

        tags = faker.words(nb=3)
        themes = faker.words(nb=3)
        g.add((node, RDF.type, DCAT.Dataset))
        g.add((node, DCT.title, Literal(faker.sentence())))
        for tag in tags:
            g.add((node, DCAT.keyword, Literal(tag)))
        for theme in themes:
            g.add((node, DCAT.theme, Literal(theme)))

        dataset = dataset_from_rdf(g)
        dataset.validate()

        assert isinstance(dataset, Dataset)
        assert set(dataset.tags) == set(tags + themes) 
Example #27
Source File: profiles.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def _contact_details(self, subject, predicate):
        '''
        Returns a dict with details about a vcard expression

        Both subject and predicate must be rdflib URIRef or BNode objects

        Returns keys for uri, name and email with the values set to
        None if they could not be found
        '''

        contact = {}

        for agent in self.g.objects(subject, predicate):

            contact['uri'] = (unicode(agent) if isinstance(agent,
                              rdflib.term.URIRef) else None)

            contact['name'] = self._object_value(agent, VCARD.fn)

            contact['email'] = self._object_value(agent, VCARD.hasEmail)

        return contact 
Example #28
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_minimal_from_multiple(self):
        node = BNode()
        g = Graph()

        title = faker.sentence()
        g.add((node, RDF.type, DCAT.Dataset))
        g.add((node, DCT.title, Literal(title)))

        for i in range(3):
            other = BNode()
            g.add((other, RDF.type, DCAT.Dataset))
            g.add((other, DCT.title, Literal(faker.sentence())))

        dataset = dataset_from_rdf(g, node=node)
        dataset.validate()

        assert isinstance(dataset, Dataset)
        assert dataset.title == title 
Example #29
Source File: test_dataset_rdf.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_update_from_multiple(self):
        original = DatasetFactory()

        node = URIRef('https://test.org/dataset')
        g = Graph()

        new_title = faker.sentence()
        g.add((node, RDF.type, DCAT.Dataset))
        g.add((node, DCT.title, Literal(new_title)))

        for i in range(3):
            other = BNode()
            g.add((other, RDF.type, DCAT.Dataset))
            g.add((other, DCT.title, Literal(faker.sentence())))

        dataset = dataset_from_rdf(g, dataset=original, node=node)
        dataset.validate()

        assert isinstance(dataset, Dataset)
        assert dataset.id == original.id
        assert dataset.title == new_title 
Example #30
Source File: test_euro_dcatap_profile_parse.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def test_dataset_license_from_distribution_by_title(self):
        # license_id retrieved from dct:title of dcat:license object
        g = Graph()

        dataset = URIRef("http://example.org/datasets/1")
        g.add((dataset, RDF.type, DCAT.Dataset))

        distribution = URIRef("http://example.org/datasets/1/ds/1")
        g.add((distribution, RDF.type, DCAT.Distribution))
        g.add((dataset, DCAT.distribution, distribution))
        license = BNode()
        g.add((distribution, DCT.license, license))
        g.add((license, DCT.title, Literal("Creative Commons Attribution")))

        p = RDFParser(profiles=['euro_dcat_ap'])

        p.g = g

        dataset = [d for d in p.datasets()][0]
        eq_(dataset['license_id'], 'cc-by')