package rdf

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file sparql_print.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
(*********************************************************************************)
(*                OCaml-RDF                                                      *)
(*                                                                               *)
(*    Copyright (C) 2012-2024 Institut National de Recherche en Informatique     *)
(*    et en Automatique. All rights reserved.                                    *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU Lesser General Public License version        *)
(*    3 as published by the Free Software Foundation.                            *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *)
(*    GNU General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU General Public License          *)
(*    along with this program; if not, write to the Free Software                *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** Print Sparql abstract syntax tree. *)

open Sparql_types

let map_opt f = function
  None -> None
| Some x -> Some (f x)
;;

let do_opt f = function
  None -> ()
| Some x -> f x
;;

let p = Buffer.add_string ;;
let pp = Printf.bprintf ;;

let print_list ?sep b f l =
  let rec iter = function
    [] -> ()
  | [path] -> f b path
  | path :: q ->
      f b path ;
      (match sep with None -> () | Some s -> p b s);
      iter q
  in
  iter l
;;

let print_iriref b ir = p b ("<" ^ (Iri.to_string ir.ir_iri) ^ ">")
let print_iriloc b r = p b ("<" ^ (Iri.to_string r.iri_iri) ^ ">")
let print_var b v = p b ("?"^v.var_name)
let print_bnode b bnode =
  match bnode.bnode_label with
    None -> p b "[]"
  | Some s -> p b ("_:"^s)

let print_path_mod b = function
| ModOptional -> p b "?"
| ModList -> p b "*"
| ModOneOrMore -> p b "+"
;;

let print_iri b = function
| Iriref ir -> print_iriref b ir
| Iri r -> print_iriloc b r
| PrefixedName pname ->
    p b (pname.pname_ns.pname_ns_name^":");
    (match pname.pname_local with
        None -> ()
      | Some l ->
         p b l.pname_local_name
    )
;;

let print_query_prolog_decl b = function
| BaseDecl iriref ->
   p b "BASE ";
   print_iriref b iriref;
   p b "\n"

| PrefixDecl (pname_ns, iriref) ->
   p b ("PREFIX "^pname_ns.pname_ns_name^": ");
   print_iriref b iriref;
   p b "\n"
;;

let print_query_prolog b decls =
  List.iter (print_query_prolog_decl b) decls
;;

let print_string_lit b s = p b (Term.quote_str s)

let print_rdf_literal b t =
  match t.rdf_lit.Term.lit_type with
    Some iri when Iri.equal iri Rdf_.xsd_integer ->
      p b t.rdf_lit.Term.lit_value
  | _ ->
      let lit = t.rdf_lit in
      let lit, s_type =
        match t.rdf_lit_type with
          None -> lit, None
        | Some iri ->
            (
             { lit with Term.lit_type = None },
             Some (fun () -> p b "^^" ; print_iri b iri)
        )
      in
      p b (Term.string_of_term (Term.Literal lit));
      match s_type with
        None -> ()
      | Some f -> f ()
;;

let print_data_block_value b = function
  | DataBlockValueIri iri -> print_iri b iri
  | DataBlockValueRdf lit -> print_rdf_literal b lit
  | DataBlockValueNumeric lit -> print_rdf_literal b lit
  | DataBlockValueBoolean lit -> print_rdf_literal b lit
  | DataBlockValueUndef -> p b "UNDEF"
;;

let print_data_full_block_value b = function
  | Nil -> p b "()"
  | Value l ->
    p b "(";
    print_list ~sep: " " b print_data_block_value l ;
    p b ")";
;;

let print_inline_data_one_var b t =
  print_var b t.idov_var;
  p b " { ";
  print_list ~sep: " " b print_data_block_value t.idov_data;
  p b " } "
;;

let print_inline_data_full b t =
 p b "(";
 print_list ~sep: " " b print_var t.idf_vars ;
 p b ") { ";
 print_list ~sep: " " b print_data_full_block_value t.idf_values;
 p b " } "
;;

let print_datablock b = function
  | InLineDataOneVar i -> print_inline_data_one_var b i
  | InLineDataFull i -> print_inline_data_full b i

let print_values_clause b = function
  None -> ()
| Some d ->
    p b "\nVALUES " ;
    print_datablock b d
;;

let print_var_or_iri b = function
  | VIVar v -> print_var b v
  | VIIri iri -> print_iri b iri
;;


let rec print_select_var b t =
  match t.sel_var_expr with
    None -> print_var b t.sel_var
  | Some e ->
      p b "(" ;
      print_expression b e ;
      p b " AS ";
      print_var b t.sel_var;
      p b ")"

and print_select_vars b = function
  | SelectAll -> p b "*"
  | SelectVars l ->
    print_list ~sep: " " b print_select_var l

and print_select_clause b t =
  p b "SELECT ";
  (match t.sel_flag with
       None -> ()
     | Some Distinct -> p b "DISTINCT "
     | Some Reduced -> p b "REDUCED "
  );
  print_select_vars b t.sel_vars

and print_source_selector = print_iri

and print_dataset_clause b = function
  | DefaultGraphClause s ->
      p b "\nFROM ";
      print_source_selector b s
  | NamedGraphClause s ->
      p b "\nFROM NAMED ";
      print_source_selector b s

and print_arg_list b t  =
  p b "(";
  if t.argl_distinct then p b "DISTINCT ";
  print_list ~sep: ", " b print_expression t.argl ;
  p b ")"

and print_function_call b t =
  print_iri b t.func_iri ;
  print_arg_list b t.func_args

and string_of_bin_op = function
  | EEqual -> "="
  | ENotEqual -> "!="
  | ELt -> "<"
  | ELte -> "<="
  | EGt -> ">"
  | EGte -> ">="
  | EPlus -> "+"
  | EMinus -> "-"
  | EMult -> "*"
  | EDiv -> "/"
  | EOr -> "||"
  | EAnd -> "&&"

and print_expr b = function
  | EBin (e1, op, e2) ->
      print_expression b e1;
      p b (" "^(string_of_bin_op op)^" ");
      print_expression b e2
  | EIn (ne, l) ->
      print_expression b ne ;
      p b " IN (";
      print_list ~sep: ", " b print_expression l;
      p b ")"
  | ENotIn (ne, l) ->
      print_expression b ne;
      p b " NOT IN (";
      print_list ~sep: ", " b print_expression l;
      p b ")"

  | EUMinus e ->
      p b "- ";
      print_expression b e;
  | ENot e ->
      p b "!";
      print_expression b e

  | EBic c -> print_built_in_call b c
  | EFuncall c -> print_function_call b c
  | ELit lit -> print_rdf_literal b lit
  | ENumeric lit -> print_rdf_literal b lit
  | EBoolean lit -> print_rdf_literal b lit
  | EVar v -> print_var b v
  | EIri iri -> print_iri b iri

and print_expression b e =
  match e.expr with
    EVar _
  | EFuncall _
  | EBic _
  | ELit _
  | ENumeric _
  | EBoolean _  -> print_expr b e.expr
  | _ ->
      p b "(";
      print_expr b e.expr ;
      p b ")"

and print_aggregate b = function
  | Bic_COUNT (dis, eopt) ->
      pp b "COUNT(%s" (if dis then "DISTINCT " else "");
      (match eopt with
       | None -> p b "*"
       | Some e -> print_expression b e
      );
      p b ")"
  | Bic_SUM (dis, e) ->
      pp b "SUM(%s" (if dis then "DISTINCT " else "");
      print_expression b e;
      p b ")"
  | Bic_MIN (dis, e) ->
      pp b "MIN(%s" (if dis then "DISTINCT " else "");
      print_expression b e;
      p b ")"
  | Bic_MAX (dis, e) ->
      pp b "MAX(%s" (if dis then "DISTINCT " else "");
      print_expression b e;
      p b ")";
  | Bic_AVG (dis, e) ->
      pp b "AVG(%s" (if dis then "DISTINCT " else "");
      print_expression b e;
      p b ")"
  | Bic_SAMPLE (dis, e) ->
      pp b "SAMPLE(%s" (if dis then "DISTINCT " else "");
      print_expression b e;
      p b ")"
  | Bic_GROUP_CONCAT (dis, e, s_opt) ->
      pp b "GROUP_CONCAT(%s" (if dis then "DISTINCT " else "");
      print_expression b e ;
      (match s_opt with
        None -> ()
      | Some s -> p b "; SEPARATOR="; print_string_lit b s
      );
      p b ")"

and print_built_in_call b = function
  | Bic_agg agg -> print_aggregate b agg
  | Bic_fun (name, l) ->
      p b (name^"(");
      print_list ~sep: ", " b print_expression l;
      p b ")"
  | Bic_BOUND v ->
      p b "BOUND(";
      print_var b v ;
      p b ")"
  | Bic_EXISTS g ->
      p b "EXISTS";
      print_group_graph_pattern b g;
  | Bic_NOTEXISTS g ->
      p b "NOT EXISTS";
      print_group_graph_pattern b g

and print_group_condition b = function
  | GroupBuiltInCall c -> print_built_in_call b c
  | GroupFunctionCall c -> print_function_call b c
  | GroupVar gv ->
      match gv.grpvar_expr, gv.grpvar with
        None, None -> assert false
      | Some e, None ->
          p b "(";
          print_expression b e ;
          p b ")"
      | Some e, Some v ->
          p b "(" ;
          print_expression b e ;
          p b " AS ";
          print_var b v ;
          p b ")"
      | None, Some v ->
          print_var b v

and print_constraint b = function
  | ConstrBuiltInCall c -> print_built_in_call b c
  | ConstrFunctionCall c -> print_function_call b c
  | ConstrExpr e -> p b "("; print_expression b e ; p b ")"

and print_having_condition b t = print_constraint b t

and print_order_condition b = function
  | OrderAsc e ->
      p b "ASC (";
      print_expression b e;
      p b ")"
  | OrderDesc e ->
      p b "DESC (";
      print_expression b e;
      p b ")"
  | OrderConstr c -> print_constraint b c
  | OrderVar v -> print_var b v

and print_limit_offset_clause b t =
  (match t.limoff_offset with
    None -> ()
  | Some n -> pp b "OFFSET %d" n
  );
  (match t.limoff_limit with
    None -> ()
  | Some n -> pp b "LIMIT %d" n
  )

and print_solution_modifier b t =
  (match t.solmod_group with
     [] -> ()
   | l ->
       p b " GROUP BY ";
       print_list ~sep: " " b print_group_condition l
  );
  (match t.solmod_having with
     [] -> ()
   | l ->
      p b " HAVING " ;
      print_list ~sep: " " b print_having_condition l
  );
  (match t.solmod_order with
    None -> ()
  | Some l ->
      p b " ORDER BY " ;
      print_list ~sep: " " b print_order_condition l
  );
  do_opt (print_limit_offset_clause b) t.solmod_limoff

and print_bind b t =
  p b " BIND (";
  print_expression b t.bind_expr ;
  p b " AS ";
  print_var b t.bind_var ;
  p b ")"

and print_service_graph_pattern b t =
  p b " SERVICE ";
  if t.servgp_silent then p b "SILENT ";
  print_var_or_iri b t.servgp_name ;
  p b " ";
  print_group_graph_pattern b t.servgp_pat

and print_graph_graph_pattern b t =
  p b " GRAPH ";
  print_var_or_iri b t.graphgp_name ;
  print_group_graph_pattern b t.graphgp_pat

and print_graph_pattern_elt b = function
  | Triples l -> print_triples_block b l
  | Union l -> print_list ~sep: " UNION " b print_group_graph_pattern l
  | Optional g -> p b " OPTIONAL " ; print_group_graph_pattern b g
  | Minus g -> p b " MINUS "; print_group_graph_pattern b g
  | GGP g -> print_graph_graph_pattern b g
  | Service s -> print_service_graph_pattern b s
  | Filter c -> p b " FILTER "; print_constraint b c
  | Bind bind -> print_bind b bind
  | InlineData d -> p b " VALUES "; print_datablock b d

and print_graph_term b = function
  | GraphTermIri iri -> print_iri b iri
  | GraphTermLit lit -> print_rdf_literal b lit
  | GraphTermNumeric lit -> print_rdf_literal b lit
  | GraphTermBoolean lit -> print_rdf_literal b lit
  | GraphTermBlank bnode -> print_bnode b bnode
  | GraphTermNil -> p b "()"
  | GraphTermNode _ -> assert false

and print_var_or_term b = function
  | Var v -> print_var b v
  | GraphTerm t -> print_graph_term b t

and print_path_one_in_prop_set b = function
  | PathOneInIri iri -> print_iri b iri
  | PathOneInA -> p b "a"
  | PathOneInNotIri iri -> p b "^"; print_iri b iri
  | PathOneInNotA -> p b "^a"

and print_path_primary b = function
  | PathIri iri -> print_iri b iri
  | PathA -> p b "a"
  | PathNegPropSet l ->
      p b "!";
      (match l with
        [] -> assert false
      | [one] -> print_path_one_in_prop_set b one
      | l ->
        p b "( " ;
        print_list ~sep: " | " b print_path_one_in_prop_set l;
        p b " )";
      )
  | Path path ->
      p b "( "; print_path b path; p b " )";

and print_path_elt b t =
  print_path_primary b t.pelt_primary ;
  do_opt (print_path_mod b) t.pelt_mod

and print_path_elt_or_inverse b = function
  | Elt e -> print_path_elt b e
  | Inv e -> p b "^"; print_path_elt b e

and print_path_sequence b l =
  print_list ~sep: " / " b print_path_elt_or_inverse l

and print_path b l =
  print_list ~sep: " | " b print_path_sequence l

and print_verb b = function
  | VerbPath p -> print_path b p
  | VerbVar v -> print_var b v
  | VerbIri iri -> print_iri b iri
  | VerbA -> p b "a"

and print_triples_node b = function
  | TNodeCollection l ->
      p b "(";
      List.iter (print_graph_node b) l;
      p b ")"
  | TNodeBlank l ->
      p b "[ " ;
      print_list ~sep: "; " b print_prop_object_list l ;
      p b " ]";

and print_graph_node b = function
  | GraphNodeVT t -> print_var_or_term b t
  | GraphNodeTriples t -> print_triples_node b t

and print_object b t = print_graph_node b t

and print_prop_object_list b t =
  print_verb b t.propol_verb ;
  p b " ";
  print_list ~sep: ";\n  " b print_object t.propol_objects

and print_triples_same_subject b = function
| TriplesVar (s, path) ->
      print_var_or_term b s;
      p b " " ;
      print_list ~sep:" ; " b print_prop_object_list path ;

| TriplesNode (t, path) ->
      print_triples_node b t;
      p b " " ;
      List.iter (print_prop_object_list b) path

and print_triples_block =
  let f b t =
    print_triples_same_subject b t;
    p b " .\n"
  in
  fun b t -> List.iter (f b) t.triples

and print_ggp_sub b t =
  print_list ~sep: "\n" b print_graph_pattern_elt t.ggp_sub_elts

and print_group_graph_pattern b pat =
  p b "{\n" ;
   (match pat with
    | SubSelect s -> print_sub_select b s
    | GGPSub g -> print_ggp_sub b g
  );
  p b "\n}"

and print_sub_select b t =
  print_select_clause b t.subsel_select ;
  p b "\nWHERE ";
  print_group_graph_pattern b t.subsel_where ;
  p b "\n";
  print_solution_modifier b t.subsel_modifier ;
  print_values_clause b t.subsel_values
;;

let print_select_query b t =
  print_select_clause b t.select_select ;
  p b " " ;
  List.iter (print_dataset_clause b) t.select_dataset ;
  p b "\nWHERE " ;
  print_group_graph_pattern b t.select_where ;
  p b "\n";
  print_solution_modifier b t.select_modifier ;
;;

let print_triples_template b l =
  print_list ~sep: " .\n" b print_triples_same_subject l
let print_construct_template = print_triples_template

let print_construct_where b = function
  | Constr_ggp p -> print_group_graph_pattern b p
  | Constr_template t -> print_triples_template b t


let print_construct_query b t =
  p b "CONSTRUCT ";
  (
   match t.constr_template with
     Some tmpl ->
       print_construct_template b tmpl ;
       p b " ";
       List.iter (print_dataset_clause b) t.constr_dataset ;
       p b "\nWHERE ";
       print_construct_where b t.constr_where ;
   | None ->
       List.iter (print_dataset_clause b) t.constr_dataset ;
       p b "\nWHERE { ";
       print_construct_where b t.constr_where ;
       p b " }";

  );
  p b "\n";
  print_solution_modifier b t.constr_modifier

let print_describe_query b t =
  p b "DESCRIBE ";
  (match t.desc_sel with
    [] -> p b "*"
  | l -> print_list ~sep: " " b print_var_or_iri l;
  );
  p b " ";
  List.iter (print_dataset_clause b) t.desc_dataset ;
  (match t.desc_where with
     None -> ()
   | Some w ->
       p b "\nWHERE " ;
       print_group_graph_pattern b w
  );
  p b "\n";
  print_solution_modifier b t.desc_modifier
;;

let print_ask_query b t =
  p b "ASK ";
  List.iter (print_dataset_clause b) t.ask_dataset;
  p b "\nWHERE " ;
  print_group_graph_pattern b t.ask_where;
  p b "\n";
  print_solution_modifier b t.ask_modifier
;;

let print_quad_not_triples b qnt =
  p b "GRAPH ";
  print_var_or_iri b qnt.quadsnt_graph ;
  p b " {";
  (match qnt.quadsnt_triples with
     None -> ()
   | Some t ->
       p b " " ;
       print_triples_template b t ;
       p b " "
  );
  p b "}"

let print_quads_item b (qnt, topt) =
  print_quad_not_triples b qnt ;
  match topt with
    None -> ()
  | Some t -> p b " . " ; print_triples_template b t

let print_quads b q =
  (match q.quads_triples with
     None -> ()
   | Some t ->
       print_triples_template b t ;
       p b " "
  );
  print_list ~sep: " " b print_quads_item q.quads_list

let print_quad_data b qd =
  p b "{ ";
  print_quads b qd ;
  p b " }"

let print_quad_pattern = print_quad_data

let print_using_clause b (named, iri, _loc) =
  if named then p b "NAMED " ;
  print_iri b iri

let print_update_modify b t =
  (match t.umod_iri with
     None -> ()
   | Some i -> p b "WITH " ; print_iri b i; p b " ");
   (match t.umod_delete with
     None -> ()
   | Some qp -> p b "DELETE "; print_quad_pattern b qp; p b " ");
   (match t.umod_insert with
     None -> ()
   | Some qp -> p b "INSERT "; print_quad_pattern b qp; p b " ");
   print_list ~sep: " " b print_using_clause t.umod_using ;
   p b " WHERE ";
   print_group_graph_pattern b t.umod_where

let print_update_action b a =
  let () =
    match a with
    | Update_load -> p b "LOAD"
    | Update_clear -> p b "CLEAR"
    | Update_drop -> p b "DROP"
    | Update_add -> p b "ADD"
    | Update_move -> p b "MOVE"
    | Update_copy -> p b "COPY"
    | Update_create  -> p b "CREATE"
    | Update_insert_data qd -> p b "INSERT DATA "; print_quad_data b qd
    | Update_delete_data qd -> p b "DELETE DATA "; print_quad_data b qd
    | Update_delete_where qp -> p b "DELETE WHERE "; print_quad_pattern b qp
    | Update_modify m -> print_update_modify b m
  in
  p b "\n"

let print_update_query b t =
  print_list ~sep:";\n" b print_update_action t

let print_query_kind b = function
  | Select q -> print_select_query b q
  | Construct q -> print_construct_query b q
  | Describe q -> print_describe_query b q
  | Ask q -> print_ask_query b q
  | Update q -> print_update_query b q
;;


let print_query b q =
  print_query_prolog b q.q_prolog ;
  p b "\n";
  print_query_kind b q.q_kind ;
  p b "\n" ;
  print_values_clause b q.q_values
;;
OCaml

Innovation. Community. Security.