@@ -74,7 +74,10 @@ struct Args {
74
74
nest_objects_of_predicate : Vec < String > ,
75
75
76
76
#[ arg( long) ]
77
- exclude_objects_of_predicate : Vec < String > // if an object is used with this predicate, ignore the object
77
+ exclude_objects_of_predicate : Vec < String > , // if an object is used with this predicate, ignore the object
78
+
79
+ #[ arg( long, default_value_t = false ) ]
80
+ rdf_types_are_grebi_types : bool
78
81
}
79
82
80
83
fn main ( ) -> std:: io:: Result < ( ) > {
@@ -93,6 +96,7 @@ fn main() -> std::io::Result<()> {
93
96
94
97
let nest_preds: BTreeSet < String > = args. nest_objects_of_predicate . into_iter ( ) . collect ( ) ;
95
98
let ignore_preds: BTreeSet < String > = args. exclude_objects_of_predicate . into_iter ( ) . collect ( ) ;
99
+ let rdf_types_are_grebi_types = args. rdf_types_are_grebi_types ;
96
100
97
101
let gr: CustomGraph = match args. rdf_type . as_str ( ) {
98
102
"rdf_triples_xml" => {
@@ -102,14 +106,11 @@ fn main() -> std::io::Result<()> {
102
106
} ,
103
107
"rdf_quads_nq" => {
104
108
105
- if args. rdf_graph . len ( ) == 0 {
106
- panic ! ( "must specify at least one graph to load for nquads" ) ;
107
- }
108
-
109
109
let parser = NQuadsParser { } ;
110
110
111
111
let quad_source = parser. parse ( reader) ;
112
- let mut filtered_quads = quad_source. filter_quads ( |q| args. rdf_graph . contains ( & q. g ( ) . unwrap ( ) . value ( ) . to_string ( ) ) ) ;
112
+ let mut filtered_quads = quad_source. filter_quads ( |q|
113
+ args. rdf_graph . len ( ) == 0 || args. rdf_graph . contains ( & q. g ( ) . unwrap ( ) . value ( ) . to_string ( ) ) ) ;
113
114
114
115
let mut g: CustomGraph = CustomGraph :: new ( ) ;
115
116
@@ -160,7 +161,7 @@ fn main() -> std::io::Result<()> {
160
161
161
162
eprintln ! ( "Building reification index took {} seconds" , start_time. elapsed( ) . as_secs( ) ) ;
162
163
163
- write_subjects ( ds, & mut output_nodes, & nest_preds, & exclude_subjects, & exclude_subjects_at_toplevel, reifs) ;
164
+ write_subjects ( ds, & mut output_nodes, & nest_preds, & exclude_subjects, & exclude_subjects_at_toplevel, reifs, rdf_types_are_grebi_types ) ;
164
165
165
166
eprintln ! ( "Total time elapsed: {} seconds" , start_time. elapsed( ) . as_secs( ) ) ;
166
167
@@ -189,7 +190,7 @@ fn populate_reifs(
189
190
let annotated_predicate = ds. triples_matching ( & s, & pred_prop, & ANY ) . next ( ) . unwrap ( ) . unwrap ( ) . o ( ) . clone ( ) ;
190
191
let annotated_object = ds. triples_matching ( & s, & obj_prop, & ANY ) . next ( ) . unwrap ( ) . unwrap ( ) . o ( ) . clone ( ) ;
191
192
192
- let obj_json = term_to_json ( & annotated_object, ds, nest_preds, None ) . to_string ( ) ;
193
+ let obj_json = term_to_json ( & annotated_object, ds, nest_preds, None , false ) . to_string ( ) ;
193
194
194
195
let lhs = ReifLhs {
195
196
s : annotated_subject. clone ( ) ,
@@ -212,7 +213,14 @@ fn populate_reifs(
212
213
}
213
214
214
215
215
- fn write_subjects ( ds : & CustomGraph , nodes_writer : & mut BufWriter < StdoutLock > , nest_preds : & BTreeSet < String > , exclude_subjects : & HashSet < Term < Rc < str > > > , exclude_subjects_at_toplevel : & HashSet < Term < Rc < str > > > , reifs : HashMap < ReifLhs , BTreeMap < String , Term < Rc < str > > > > ) {
216
+ fn write_subjects (
217
+ ds : & CustomGraph ,
218
+ nodes_writer : & mut BufWriter < StdoutLock > ,
219
+ nest_preds : & BTreeSet < String > ,
220
+ exclude_subjects : & HashSet < Term < Rc < str > > > ,
221
+ exclude_subjects_at_toplevel : & HashSet < Term < Rc < str > > > ,
222
+ reifs : HashMap < ReifLhs , BTreeMap < String , Term < Rc < str > > > > ,
223
+ rdf_types_are_grebi_types : bool ) {
216
224
217
225
let start_time2 = std:: time:: Instant :: now ( ) ;
218
226
@@ -229,7 +237,7 @@ fn write_subjects(ds:&CustomGraph, nodes_writer:&mut BufWriter<StdoutLock>, nest
229
237
continue ;
230
238
}
231
239
232
- let json = term_to_json ( s, ds, nest_preds, Some ( & reifs) ) ;
240
+ let json = term_to_json ( s, ds, nest_preds, Some ( & reifs) , rdf_types_are_grebi_types ) ;
233
241
234
242
let json_obj = json. as_object ( ) . unwrap ( ) ;
235
243
let types = json_obj. get ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) ;
@@ -252,7 +260,13 @@ fn write_subjects(ds:&CustomGraph, nodes_writer:&mut BufWriter<StdoutLock>, nest
252
260
eprintln ! ( "Writing JSONL took {} seconds" , start_time2. elapsed( ) . as_secs( ) ) ;
253
261
}
254
262
255
- fn term_to_json ( term : & Term < Rc < str > > , ds : & CustomGraph , nest_preds : & BTreeSet < String > , reifs : Option < & HashMap < ReifLhs , BTreeMap < String , Term < Rc < str > > > > > ) -> Value {
263
+ fn term_to_json (
264
+ term : & Term < Rc < str > > ,
265
+ ds : & CustomGraph ,
266
+ nest_preds : & BTreeSet < String > ,
267
+ reifs : Option < & HashMap < ReifLhs , BTreeMap < String , Term < Rc < str > > > > > ,
268
+ rdf_types_are_grebi_types : bool
269
+ ) -> Value {
256
270
257
271
let triples = ds. triples_matching ( term, & ANY , & ANY ) ;
258
272
@@ -285,7 +299,7 @@ fn term_to_json(term:&Term<Rc<str>>, ds:&CustomGraph, nest_preds:&BTreeSet<Strin
285
299
let reifs_for_this_sp = reifs_u. get ( & ReifLhs { s : tu. s ( ) . clone ( ) , p : tu. p ( ) . clone ( ) } ) ;
286
300
if reifs_for_this_sp. is_some ( ) {
287
301
let reifs_for_this_sp_u = reifs_for_this_sp. unwrap ( ) ;
288
- let o_json = term_to_json ( & o, ds, nest_preds, None ) . to_string ( ) ;
302
+ let o_json = term_to_json ( & o, ds, nest_preds, None , false ) . to_string ( ) ;
289
303
let reif = reifs_for_this_sp_u. get ( & o_json) ;
290
304
if reif. is_some ( ) {
291
305
Some ( reif. unwrap ( ) )
@@ -304,7 +318,7 @@ fn term_to_json(term:&Term<Rc<str>>, ds:&CustomGraph, nest_preds:&BTreeSet<Strin
304
318
if nest_preds. contains ( p) {
305
319
match o. kind ( ) {
306
320
Iri |Literal |BlankNode => {
307
- let mut obj = term_to_json ( o, ds, nest_preds, reifs) ;
321
+ let mut obj = term_to_json ( o, ds, nest_preds, reifs, false ) ;
308
322
let obj_o = obj. as_object_mut ( ) . unwrap ( ) ;
309
323
obj_o. remove_entry ( "id" ) ;
310
324
obj
@@ -314,14 +328,14 @@ fn term_to_json(term:&Term<Rc<str>>, ds:&CustomGraph, nest_preds:&BTreeSet<Strin
314
328
} else {
315
329
match o. kind ( ) {
316
330
Iri |Literal => Value :: String ( o. value ( ) . to_string ( ) ) ,
317
- BlankNode => term_to_json ( o, ds, nest_preds, reifs) ,
331
+ BlankNode => term_to_json ( o, ds, nest_preds, reifs, false ) ,
318
332
Variable => todo ! ( ) ,
319
333
}
320
334
}
321
335
} ;
322
336
323
337
if reif_subj. is_some ( ) {
324
- let mut reif_as_json = term_to_json ( reif_subj. unwrap ( ) , ds, nest_preds, None ) ;
338
+ let mut reif_as_json = term_to_json ( reif_subj. unwrap ( ) , ds, nest_preds, None , false ) ;
325
339
let reif_as_json_o = reif_as_json. as_object_mut ( ) . unwrap ( ) ;
326
340
reif_as_json_o. remove_entry ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) ;
327
341
reif_as_json_o. remove_entry ( "id" ) ;
@@ -340,6 +354,10 @@ fn term_to_json(term:&Term<Rc<str>>, ds:&CustomGraph, nest_preds:&BTreeSet<Strin
340
354
}
341
355
}
342
356
357
+ if rdf_types_are_grebi_types && json. contains_key ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) {
358
+ json. insert ( "grebi:type" . to_string ( ) , json. get ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) . unwrap ( ) . clone ( ) ) ;
359
+ }
360
+
343
361
return Value :: Object ( json) ;
344
362
}
345
363
0 commit comments