Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add logic for sameAs #402

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions apis_ontology/importers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.apps import apps
from django.core.exceptions import ImproperlyConfigured
from django.db.utils import IntegrityError
from apis_core.generic.importers import GenericModelImporter
from apis_core.utils.helpers import create_object_from_uri
from apis_core.apis_metainfo.models import Uri


class BaseEntityImporter(GenericModelImporter):
Expand All @@ -9,19 +12,38 @@ class BaseEntityImporter(GenericModelImporter):

def create_instance(self):
data = self.get_data(drop_unknown_fields=False)
if "sameas" in data:
data["sameas"] = data["sameas"].split("|")
sa = Uri.objects.filter(uri__in=data["sameas"])
if sa.count() == 1:
return sa.first().root_object
elif sa.count() > 1:
raise IntegrityError(
f"Multiple objects found for sameAs URIs {data['sames']}. "
f"This indicates a data integrity problem as these URIs should be unique."
)
modelfields = [field.name for field in self.model._meta.fields]
data_croped = {key: data[key] for key in data if key in modelfields}
subj = self.model.objects.create(**data_croped)
if "sameas" in data:
for uri in data["sameas"]:
Uri.objects.create(uri=uri, root_object_id=subj.id)
related_keys = [
(x, x.split("__")[1], x.split("__")[2]) for x in data.keys() if "__" in x
]
for rk in related_keys:
key, obj, rel = rk
RelatedModel = apps.get_model("apis_ontology", obj)
RelationType = apps.get_model("apis_ontology", rel)
if key in data:
related_obj = create_object_from_uri(data[key], RelatedModel)
RelationType.objects.create(subj=subj, obj=related_obj)
try:
for rk in related_keys:
key, obj, rel = rk
RelatedModel = apps.get_model("apis_ontology", obj)
RelationType = apps.get_model("apis_ontology", rel)
if key in data:
related_obj = create_object_from_uri(data[key], RelatedModel)
RelationType.objects.create(subj=subj, obj=related_obj)
except: # noqa: E722
subj.delete()
raise ImproperlyConfigured(
f"Error in creating related Objects for {self.model.__class__.__name__}"
)

return subj

Expand All @@ -39,3 +61,7 @@ def mangle_data(self, data):

class InstitutionImporter(BaseEntityImporter):
pass


class PlaceImporter(BaseEntityImporter):
pass
10 changes: 10 additions & 0 deletions apis_ontology/rdfimport/InstitutionFromDNB.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ WHERE {
?subject gndo:dateOfTermination ?end_date_written
}
"""
[[attributes]]
# sameAs
sparql = '''
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT (GROUP_CONCAT(?sameas_pre; separator='|') as ?sameas)
WHERE {
?subject owl:sameAs ?sameas_pre
}
GROUP BY ?subject
'''
10 changes: 10 additions & 0 deletions apis_ontology/rdfimport/PersonFromDNB.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ WHERE {
?subject gndo:placeOfDeath ?place_of_death__Place__StarbIn
}
"""
[[attributes]]
# sameAs
sparql = '''
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT (GROUP_CONCAT(?sameas_pre; separator='|') as ?sameas)
WHERE {
?subject owl:sameAs ?sameas_pre
}
GROUP BY ?subject
'''
53 changes: 53 additions & 0 deletions apis_ontology/rdfimport/PlaceFromDNB.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#####################################################
# Create an E53_Place from a d-nb.info RDF endpoint #
# the second regex is for testing
regex = "https://d-nb.info.*|/.*wien.rdf"
superclass = "apis_ontology.models.Place"
sameas = """
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?sameas
WHERE {
?subject owl:sameAs ?sameas
}
"""
[[attributes]]
# label
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?label
WHERE {
?subject gndo:preferredNameForThePlaceOrGeographicName ?label
}
"""
[[attributes]]
# longitude
sparql = '''
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?longitude
WHERE {
?subject geo:hasGeometry ?geo1 .
?geo1 geo:asWKT ?point .
BIND(REPLACE(str(?point), "Point \\( \\+?(-?\\d+.\\d+).*", "$1") as ?longitude)
}
'''
[[attributes]]
# latitude
sparql = '''
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?latitude
WHERE {
?subject geo:hasGeometry ?geo1 .
?geo1 geo:asWKT ?point .
BIND(REPLACE(str(?point), "^Point\\s*\\(\\s*[+-]?\\d+\\.\\d+\\s+([+-]?\\d+\\.\\d+)\\s*\\)$", "$1") as ?latitude)
}
'''
[[attributes]]
# sameAs
sparql = '''
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT (GROUP_CONCAT(?sameas_pre; separator='|') as ?sameas)
WHERE {
?subject owl:sameAs ?sameas_pre
}
GROUP BY ?subject
'''
Loading