3434
3535T = TypeVar ("T" )
3636
37+ SPARQL_PREFIXES = """PREFIX schema: <http://schema.org/>
38+ """
39+
3740
3841def first (c : Collection [T ]) -> T :
3942 return next (iter (c ))
4043
4144
42- def load_graph_and_preserve_relative_ids (json_data , dummy_base = "http://example.org/" ):
45+ def load_graph_and_preserve_relative_ids (json_data , base = "http://example.org/" ):
4346
4447 rel_ids = set ()
4548
@@ -58,11 +61,11 @@ def collect_ids(obj):
5861 collect_ids (json_data )
5962
6063 g = rdflib .Graph ()
61- g .parse (data = json_data , format = "json-ld" , publicID = dummy_base )
64+ g .parse (data = json_data , format = "json-ld" , publicID = base )
6265
6366 mapping = {}
6467 for rid in rel_ids :
65- expanded = urljoin (dummy_base , rid )
68+ expanded = urljoin (base , rid )
6669 mapping [expanded ] = rid
6770
6871 def replace_uri_in_graph (graph , old_uri_str , new_uri_str ):
@@ -95,7 +98,7 @@ def do_entity_test(
9598 abort_on_first : bool = False ,
9699 profile_identifier : str = DEFAULT_PROFILE_IDENTIFIER ,
97100 rocrate_entity_patch : Optional [dict ] = None ,
98- rocrate_entity_mod_function : Optional [Callable ] = None ,
101+ rocrate_entity_mod_sparql : Optional [str ] = None ,
99102 skip_checks : Optional [list [str ]] = ()
100103):
101104 """
@@ -109,7 +112,7 @@ def do_entity_test(
109112 rocrate_path = Path (rocrate_path )
110113
111114 temp_rocrate_path = None
112- if any ([rocrate_entity_patch , rocrate_entity_mod_function ]) and rocrate_path .is_dir ():
115+ if any ([rocrate_entity_patch , rocrate_entity_mod_sparql ]) and rocrate_path .is_dir ():
113116 # create a temporary copy of the RO-Crate
114117 temp_rocrate_path = Path (tempfile .TemporaryDirectory ().name )
115118 # copy the RO-Crate to the temporary path using shutil
@@ -127,19 +130,27 @@ def do_entity_test(
127130 # save the updated RO-Crate metadata
128131 with open (temp_rocrate_path / "ro-crate-metadata.json" , "w" ) as f :
129132 json .dump (rocrate , f )
130- # update the RO-Crate metadata using RDF triples, if required
131- if rocrate_entity_mod_function is not None :
132- rocrate_graph = load_graph_and_preserve_relative_ids (rocrate )
133- rocrate_graph = rocrate_entity_mod_function (rocrate_graph )
133+ # update the RO-Crate metadata using SPARQL, if required
134+ if rocrate_entity_mod_sparql is not None :
135+ base = "https://example.org/"
136+ rocrate_graph = rdflib .Graph ()
137+ rocrate_graph .parse (data = rocrate , format = "json-ld" , publicID = base )
138+
139+ query = f"""BASE <{ base } >
140+ { rocrate_entity_mod_sparql }
141+ """
142+ rocrate_graph .update (query )
134143 # save the updated RO-Crate metadata
135144 context = "https://w3id.org/ro/crate/1.1/context"
136145 rocrate_graph .serialize (
137146 Path (temp_rocrate_path , "ro-crate-metadata.json" ),
138147 format = "json-ld" ,
139148 context = context ,
140149 indent = 2 ,
141- use_native_types = True
150+ use_native_types = True ,
151+ base = base ,
142152 )
153+ # TODO: tweak the generated crate to strip leading slash from relative URIs, and ensure RDE ID is ./
143154 # set the new rocrate path
144155 rocrate_path = temp_rocrate_path
145156
0 commit comments