From 6b9c2e37589850989354102d69558bb82a8ca8b5 Mon Sep 17 00:00:00 2001 From: Cyril Matthey-Doret Date: Mon, 12 Aug 2024 07:32:52 +0000 Subject: [PATCH] fix(rdf): inject schema:identifier (#99) * fix(rdf): inject schema:identifier * chore: add comment NOTE on linkml rdf dumper workaround --- modos/helpers/schema.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/modos/helpers/schema.py b/modos/helpers/schema.py index 922d8fd0..16446c9c 100644 --- a/modos/helpers/schema.py +++ b/modos/helpers/schema.py @@ -252,11 +252,29 @@ def instance_to_graph(instance) -> Graph: p.prefix_prefix: URIRef(p.prefix_reference) for p in load_prefixmap().values() } - return rdflib_dumper.as_rdf_graph( + g = rdflib_dumper.as_rdf_graph( instance, prefix_map=prefixes, schemaview=load_schema(), ) + # NOTE: This is a hack to get around the fact that the linkml's + # rdf dumper does not iunclude schema:identifier in the graph. + # Patch schema -> http://schema.org (rdflib's default is https) + g.bind("schema", "http://schema.org/", replace=True) + try: + id_slot = ( + load_schema().get_identifier_slot(type(instance).__name__).slot_uri + ) + g.add( + ( + URIRef(instance.id), + g.namespace_manager.expand_curie(str(id_slot)), + URIRef(instance.id), + ) + ) + except AttributeError: + pass + return g def get_slot_range(slot_name: str) -> str: