2323from rdflib import BNode , Graph
2424from rdflib .term import Node , URIRef
2525
26- from rocrate_validator .utils import log as logging
2726from rocrate_validator .constants import (DEFAULT_ONTOLOGY_FILE ,
2827 RDF_SERIALIZATION_FORMATS ,
2928 RDF_SERIALIZATION_FORMATS_TYPES ,
3433from rocrate_validator .requirements .shacl .models import ShapesRegistry
3534from rocrate_validator .requirements .shacl .utils import (make_uris_relative ,
3635 map_severity )
36+ from rocrate_validator .utils import log as logging
37+ from rocrate_validator .utils .rdf import extract_base_from_jsonld
3738
3839# set up logging
3940logger = logging .getLogger (__name__ )
@@ -189,6 +190,22 @@ def __get_ontology_path__(self, profile_path: Path, ontology_filename: str = DEF
189190 self ._ontology_path = Path (f"{ profile_path } /{ ontology_filename } " )
190191 return self ._ontology_path
191192
193+ def __get_data_graph_base__ (self ) -> Optional [str ]:
194+ """
195+ Get the @base from the RO-Crate metadata JSON-LD.
196+
197+ This extracts the @base from the @context of the data graph metadata,
198+ which can be used to align the ontology graph's base URI with the data graph.
199+
200+ :return: The @base value if found, None otherwise
201+ """
202+ try :
203+ metadata_dict = self .ro_crate .metadata .as_dict ()
204+ return extract_base_from_jsonld (metadata_dict )
205+ except Exception as e :
206+ logger .debug ("Unable to extract @base from data graph metadata: %s" , e )
207+ return None
208+
192209 def __load_ontology_graph__ (self , profile_path : Path ,
193210 ontology_filename : Optional [str ] = DEFAULT_ONTOLOGY_FILE ) -> Graph :
194211 # load the graph of ontologies
@@ -197,8 +214,20 @@ def __load_ontology_graph__(self, profile_path: Path,
197214 if os .path .exists (ontology_path ):
198215 logger .debug ("Loading ontologies: %s" , ontology_path )
199216 ontology_graph = Graph ()
217+
218+ # Determine the publicID to use:
219+ # 1. First, try to get @base from the data graph metadata
220+ # 2. Fall back to the default publicID (RO-Crate URI)
221+ data_graph_base = self .__get_data_graph_base__ ()
222+ public_id = data_graph_base if data_graph_base else self .publicID
223+
224+ if data_graph_base :
225+ logger .debug ("Using @base from data graph metadata: %s" , data_graph_base )
226+ else :
227+ logger .debug ("Using default publicID: %s" , self .publicID )
228+
200229 ontology_graph .parse (ontology_path , format = "ttl" ,
201- publicID = self . publicID )
230+ publicID = public_id )
202231 logger .debug ("Ontologies loaded: %s" , ontology_graph )
203232 return ontology_graph
204233
0 commit comments