Skip to content

Commit

Permalink
Merge pull request #83 from opendata-swiss/feat/harvest-dataset-quali…
Browse files Browse the repository at this point in the history
…fied-relation

feat: Map qualified_relations onto dataset
  • Loading branch information
bellisk authored Oct 10, 2023
2 parents fd5d1b0 + 4311987 commit 8b4ca15
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
44 changes: 43 additions & 1 deletion ckanext/dcatapchharvest/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ def _relations(self, subject):

return relations

def _qualified_relations(self, subject):
qualified_relations = []

for relation_node in self.g.objects(subject, DCAT.qualifiedRelation):
qualified_relations.append({
"relation": self._object_value(relation_node, DCT.relation),
"role": self._object_value(relation_node, DCAT.hadRole),
})

return qualified_relations

def _license_rights_name(self, subject, predicate):
for node in self.g.objects(subject, predicate):
# DCAT-AP CH v1: the license as a literal (should be
Expand Down Expand Up @@ -416,6 +427,7 @@ def parse_dataset(self, dataset_dict, dataset_ref): # noqa
dataset_dict['resources'] = []
dataset_dict['relations'] = []
dataset_dict['see_alsos'] = []
dataset_dict['qualified_relations'] = []

# Basic fields
for key, predicate in (
Expand Down Expand Up @@ -501,6 +513,10 @@ def parse_dataset(self, dataset_dict, dataset_ref): # noqa
for see_also in see_alsos:
dataset_dict['see_alsos'].append({'dataset_identifier': see_also})

dataset_dict["qualified_relations"] = self._qualified_relations(
dataset_ref
)

# Dataset URI
dataset_uri = dh.dataset_uri(dataset_dict, dataset_ref)
dataset_dict['extras'].append({'key': 'uri', 'value': dataset_uri})
Expand Down Expand Up @@ -711,7 +727,7 @@ def graph_from_dataset(self, dataset_dict, dataset_ref): # noqa
if dataset_dict.get('see_alsos'):
references = dataset_dict.get('see_alsos')
for reference in references:
# we only excpect dicts here
# we only expect dicts here
if not isinstance(reference, dict):
continue
reference_identifier = reference.get('dataset_identifier')
Expand All @@ -722,6 +738,32 @@ def graph_from_dataset(self, dataset_dict, dataset_ref): # noqa
Literal(reference_identifier)
))

if dataset_dict.get("qualified_relations"):
for reference in dataset_dict["qualified_relations"]:
if not reference.get("relation"):
continue

qualified_relation = BNode()
g.add((qualified_relation, RDF.type, DCAT.Relationship))
g.add((
qualified_relation,
DCT.relation,
URIRef(reference["relation"])
))

if reference.get("role"):
g.add((
qualified_relation,
DCAT.hadRole,
URIRef(reference["role"])
))

g.add((
dataset_ref,
DCAT.qualifiedRelation,
qualified_relation
))

# Contact details
if dataset_dict.get('contact_points'):
contact_points = self._get_dataset_value(dataset_dict, 'contact_points') # noqa
Expand Down
12 changes: 12 additions & 0 deletions ckanext/dcatapchharvest/tests/fixtures/1901.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
</dcat:distribution>
<dct:language>de</dct:language>
<rdfs:seeAlso>4682791@bundesamt-fur-statistik-bfs</rdfs:seeAlso>
<dcat:qualifiedRelation>
<dcat:Relationship>
<dct:relation rdf:resource="http://example.org/Original987"/>
<dcat:hadRole rdf:resource="http://www.iana.org/assignments/relation/original"/>
</dcat:Relationship>
</dcat:qualifiedRelation>
<dcat:qualifiedRelation>
<dcat:Relationship>
<dct:relation rdf:resource="http://example.org/Related486"/>
<dcat:hadRole rdf:resource="http://www.iana.org/assignments/relation/related"/>
</dcat:Relationship>
</dcat:qualifiedRelation>
<dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">1900-12-31T00:00:00</dct:issued>
<dcat:keyword xml:lang="fr">bases-statistiques-et-generalites</dcat:keyword>
<dcat:keyword xml:lang="de">publikation</dcat:keyword>
Expand Down
27 changes: 22 additions & 5 deletions ckanext/dcatapchharvest/tests/test_dcatap_ch_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
class TestSwissDCATAPProfileParsing(BaseParseTest):

def test_rights_license(self):

contents = self._get_file_contents('dataset-rights.xml')
p = RDFParser(profiles=['swiss_dcat_ap'])
p.parse(contents)

datasets = [d for d in p.datasets()]

# Dataset
eq_(len(datasets), 1)
dataset = datasets[0]

# Resources
eq_(len(dataset['resources']), 1)
resource = dataset['resources'][0]
eq_(resource['rights'], u'NonCommercialAllowed-CommercialAllowed-ReferenceRequired')
eq_(resource['license'], u'NonCommercialAllowed-CommercialWithPermission-ReferenceRequired')

def test_dataset_all_fields(self):

contents = self._get_file_contents('1901.xml')
Expand Down Expand Up @@ -101,6 +101,23 @@ def test_dataset_all_fields(self):
see_also = dataset['see_alsos'][0]
eq_(see_also['dataset_identifier'], u'4682791@bundesamt-fur-statistik-bfs')

# Qualified relations
qualified_relations = sorted(dataset["qualified_relations"])
eq_(
qualified_relations[0],
{
"relation": "http://example.org/Original987",
"role": "http://www.iana.org/assignments/relation/original"
}
)
eq_(
qualified_relations[1],
{
"relation": "http://example.org/Related486",
"role": "http://www.iana.org/assignments/relation/related"
}
)

# Lists
eq_(sorted(dataset['language']), [u'de', u'fr'])
eq_(sorted(dataset['groups']), [{'name': u'statistical-basis'}])
Expand Down

0 comments on commit 8b4ca15

Please sign in to comment.