File: rdf_turtle_write.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (1587 lines) | stat: -rw-r--r-- 51,034 bytes parent folder | download
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2009-2022, University of Amsterdam
                              VU University Amsterdam
                              CWI, Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(rdf_turtle_write,
          [ rdf_save_turtle/2,                  % +File, +Options
            rdf_save_canonical_turtle/2,        % +File, +Options
            rdf_save_trig/2,                    % +File, +Options
            rdf_save_canonical_trig/2,          % +File, +Options
            rdf_save_ntriples/2                 % +File, +Options
          ]).
:- use_module(library(record),[(record)/1, op(_,_,record)]).
:- use_module(library(semweb/turtle), []). % we make calls to public preds here

:- use_module(library(semweb/rdf_prefixes),
              [ rdf_current_prefix/2, rdf_global_id/2
              ]).

:- if(exists_source(library(semweb/rdf_db))).
:- use_module(library(semweb/rdf_db),
              [ rdf_graph/1, rdf_graph_prefixes/3,
                rdf_is_bnode/1, rdf_equal/2, rdf_graph_property/2,
                rdf_statistics/1, rdf/4, rdf_resource/1, rdf_subject/1,
                rdf/3
              ]).
have_rdf_db.
:- else.
have_rdf_db :- fail.
:- endif.

:- autoload(library(apply),[maplist/3,include/3,partition/4]).
:- autoload(library(debug),[assertion/1]).
:- autoload(library(error),[must_be/2,existence_error/2,type_error/2]).
:- autoload(library(lists),
	    [append/2,reverse/2,delete/3,append/3,select/3,member/2]).
:- autoload(library(option),[meta_options/3]).
:- autoload(library(pairs),
	    [ transpose_pairs/2,
	      map_list_to_pairs/3,
	      pairs_values/2,
	      group_pairs_by_key/2
	    ]).
:- autoload(library(rbtrees),
	    [ ord_list_to_rbtree/2,
	      rb_lookup/3,
	      rb_insert/4,
	      rb_empty/1,
	      rb_update/5
	    ]).
:- autoload(library(sgml),
	    [xml_name/1,xml_is_dom/1,xsd_number_string/2]).
:- autoload(library(sgml_write),[xml_write/2]).
:- autoload(library(url),[file_name_to_url/2,parse_url/2]).

:- predicate_options(rdf_save_turtle/2, 2,
                     [ graph(atom),
                       base(atom),
                       encoding(oneof([utf8])),
                       indent(nonneg),
                       tab_distance(nonneg),
                       silent(boolean),
                       subject_white_lines(nonneg),
                       align_prefixes(boolean),
                       user_prefixes(boolean),
                       prefixes(list),
                       only_known_prefixes(boolean),
                       comment(boolean),
                       group(boolean),
                       inline_bnodes(boolean),
                       single_line_bnodes(boolean),
                       abbreviate_literals(boolean),
                       canonize_numbers(boolean),
                       canonical(boolean),
                       a(boolean),
                       expand(any)
                     ]).
:- predicate_options(rdf_save_canonical_turtle/2, 2,
                     [ pass_to(rdf_save_turtle/2, 2)
                     ]).

/** <module> Turtle - Terse RDF Triple Language writer

This module implements the Turtle  language   for  representing  the RDF
triple model as defined by Dave Beckett  from the Institute for Learning
and Research Technology University of Bristol in the document:

  * http://www.w3.org/TeamSubmission/turtle/
  * http://www.w3.org/TeamSubmission/2008/SUBM-turtle-20080114/#sec-conformance

The Turtle format is designed as an   RDF  serialization that is easy to
read and write by both machines and  humans. Due to the latter property,
this library goes a long way in trying to produce human-readable output.

In addition to the  human-readable  format,   this  library  can write a
_canonical_ representation of RDF graphs.   The canonical representation
has the following properties:

  * Equivalent graphs result in the same document.  Graphs are
  considered equivalent iff they contain the same _set_ of
  triples, regardless of the labeling of blank nodes in the
  graph.

  * Changes to the graph are diff-friendly.  This means

    - Prefixes are combined in the header and thus changes
    to the namespaces only result in changes in the header.
    - Blank nodes that are used only once (including collections)
    are written in-line with the object they belong to.
    - For other blank nodes we to realise stable labeling that
    is based on property-values.

@tbd    Low-level string output takes 28% of the time.  Move to C?
*/

:- record
    tw_state(graph,                 % graph being saved
             graphs:list(atom),     % TriG graphs being saved
             base,                  % The base-URI
             encoding=utf8,         % Desired encoding
             indent:nonneg=8,       % Indent for ; and ,-lists
             tab_distance:nonneg=8, % Tab distance
             silent:boolean=false,  % If true, do not print a message
             subject_white_lines:nonneg=1,%Extra lines between subjects
             a:boolean=true,        % Use 'a' for rdf:type
             align_prefixes:boolean=true,%Align prefix declarations
             prefixes:list,         % Provide prefixes
             user_prefixes:boolean=true,% Use rdf_current_ns/2?
             only_known_prefixes:boolean=false,% Only use known prefixes
             comment:boolean=true,  % write some comments into the file
             group:boolean=true,    % Group using ; and ,
             inline_bnodes:boolean=true, % Inline single-used bnodes
             single_line_bnodes:boolean=false, % No newline after ;
             abbreviate_literals:boolean=true, % Abbreviate known types
             canonize_numbers:boolean=false, % How to write numbers
             canonical:boolean=false,
             expand:any=lookup,     % Access to the triples
                                    % Private fields
             bnode_id=0,            % Incrementing bnode-id
             nodeid_map,            % RBTree mapping NodeIDs to Refs
             bnode_hash,            % RBTree holding reuse-count of hashes
             subject_count=0,       % # subjects saved
             triple_count=0,        % # triples saved
             base_root,             % Root URL of base
             base_dir,              % Directory
             base_path,             % Path of base
             prefix_map).           % List of Prefix-Map


:- meta_predicate
    rdf_save_turtle(+, :),
    rdf_save_canonical_turtle(+, :),
    rdf_save_canonical_trig(+, :),
    rdf_save_trig(+, :).

%!  rdf_save_turtle(+Out, :Options) is det.
%
%   Save an RDF graph as Turtle.  Options processed are:
%
%       * a(+Boolean)
%       If =true= (default), use =a= for the predicate =rdf:type=.
%       Otherwise use the full resource.
%       * align_prefixes(+Boolean)
%       Nicely align the @prefix declarations
%       * base(+Base)
%       Save relative to the given Base
%       * canonize_numbers(+Boolean)
%       If =true= (default =false=), emit numeric datatypes using
%       Prolog's write to achieve canonical output.
%       * comment(+Boolean)
%       It =true= (default), write some informative comments
%       between the output segments
%       * encoding(+Encoding)
%       Encoding used for the output stream.  Default is UTF-8.
%       * expand(:Goal)
%       Query an alternative graph-representation.  See below.
%       * indent(+Column)
%       Indentation for ; -lists.  `0' does not indent, but
%       writes on the same line.  Default is 8.
%       * graph(+Graph)
%       Save only the named graph
%       * group(+Boolean)
%       If =true= (default), using P-O and O-grouping.
%       * inline_bnodes(+Boolean)
%       if =true= (default), inline bnodes that are used once.
%       * abbreviate_literals(+Boolean)
%       if =true= (default), omit the type if allowed by turtle.
%       * only_known_prefixes(+Boolean)
%       Only use prefix notation for known prefixes.  Without, some
%       documents produce _huge_ amounts of prefixes.
%       * prefixes(+List)
%       If provided, uses exactly these prefixes.  List is a list
%       of prefix specifications, where each specification is either
%       a term _Prefix_-_URI_ or a prefix that is known to
%       rdf_current_prefix/2.
%       * silent(+Boolean)
%       If =true= (default =false=), do not print the final
%       informational message.
%       * single_line_bnodes(+Bool)
%       If =true= (default =false=), write [...] and (...) on a
%       single line.
%       * subject_white_lines(+Count)
%       Extra white lines to insert between statements about a
%       different subject.  Default is 1.
%       * tab_distance(+Tab)
%       Distance between tab-stops.  `0' forces the library to
%       use only spaces for layout.  Default is 8.
%       * user_prefixes(+Boolean)
%       If =true= (default), use prefixes from rdf_current_prefix/2.
%
%   The option =expand= allows  for   serializing  alternative graph
%   representations. It is called through   call/5,  where the first
%   argument is the expand-option, followed  by   S,P,O,G.  G is the
%   graph-option (which is by  default   a  variable).  This notably
%   allows for writing RDF graphs   represented  as rdf(S,P,O) using
%   the following code fragment:
%
%       ==
%       triple_in(RDF, S,P,O,_G) :-
%           member(rdf(S,P,O), RDF).
%
%           ...,
%           rdf_save_turtle(Out, [ expand(triple_in(RDF)) ]),
%       ==
%
%   @param  Out is one of stream(Stream), a stream handle, a file-URL
%           or an atom that denotes a filename.

rdf_save_turtle(Spec, QOptions) :-
    meta_options(is_meta, QOptions, Options),
    statistics(cputime, T0),
    must_be(list, Options),
    make_tw_state(Options, State0, _Rest),
    init_base(State0, State1),
    init_prefix_map(State1, State),
    tw_state_encoding(State, Enc),
    setup_call_cleanup(
        open_output(Spec, Enc, Stream, Cleanup),
        ( tw_prefix_map(State, Stream),
          tw_graph(State, Stream)
        ),
        Cleanup),
    statistics(cputime, T1),
    Time is T1-T0,
    tw_state_triple_count(State, SavedTriples),
    tw_state_subject_count(State, SavedSubjects),
    (   tw_state_silent(State, true)
    ->  true
    ;   print_message(informational,
                      rdf(saved(Spec, Time, SavedSubjects, SavedTriples)))
    ).

is_meta(expand).

%!  rdf_save_canonical_turtle(+Spec, :Options) is det.
%
%   Save triples in  a  canonical  format.   This  is  the  same  as
%   rdf_save_turtle/2, but using different defaults. In particular:
%
%       * encoding(utf8),
%       * indent(0),
%       * tab_distance(0),
%       * subject_white_lines(1),
%       * align_prefixes(false),
%       * user_prefixes(false)
%       * comment(false),
%       * group(false),
%       * single_line_bnodes(true)
%
%   @tbd Work in progress. Notably blank-node handling is
%   incomplete.

rdf_save_canonical_turtle(Spec, M:Options) :-
    canonical_options(CannonicalOptions, Options),
    rdf_save_turtle(Spec, M:CannonicalOptions).

canonical_options([ encoding(utf8),
                    indent(0),
                    tab_distance(0),
                    subject_white_lines(1),
                    align_prefixes(false),
                    user_prefixes(false),
                    comment(false),
                    group(false),
                    single_line_bnodes(true),
                    canonical(true)
                  | Options
                  ],
                  Options).


%!  rdf_save_ntriples(+Spec, :Options) is det.
%
%   Save RDF using ntriples format. The  ntriples format is a subset
%   of Turtle, writing each triple fully qualified on its own line.

rdf_save_ntriples(File, Options):-
    rdf_save_turtle(File,
                    [ comment(false),
                      encoding(utf8),
                      group(false),
                      prefixes([]),
                      subject_white_lines(0),
                      a(false),
                      inline_bnodes(false),
                      abbreviate_literals(false)
                    | Options
                    ]).


%!  rdf_save_trig(+Spec, :Options) is det.
%
%   Save multiple RDF graphs into a TriG  file. Options are the same
%   as   for   rdf_save_turtle/2.   rdf_save_trig/2    ignores   the
%   graph(+Graph)  option  and  instead   processes  one  additional
%   option:
%
%     - graphs(+ListOfGraphs)
%     List of graphs to save. When omitted, all graphs in the RDF
%     store are stored in the TriG file.

rdf_save_trig(Spec, QOptions) :-
    meta_options(is_meta, QOptions, Options),
    thread_self(Me),
    thread_statistics(Me, cputime, T0),
    must_be(list, Options),
    make_tw_state(Options, State0, _Rest),
    init_base(State0, State1),
    trig_graphs(State1, Graphs),
    init_prefix_map(State1, Graphs, State2),
    tw_state_encoding(State2, Enc),
    setup_call_cleanup(
        open_output(Spec, Enc, Stream, Cleanup),
        ( tw_prefix_map(State2, Stream),
          tw_trig_graphs(Graphs, Stream, State2, State)
        ),
        Cleanup),
    thread_statistics(Me, cputime, T1),
    Time is T1-T0,
    tw_state_triple_count(State, SavedTriples),
    tw_state_subject_count(State, SavedSubjects),
    length(Graphs, SavedGraphs),
    (   tw_state_silent(State, true)
    ->  true
    ;   print_message(informational,
                      rdf(saved(Spec, Time, SavedSubjects, SavedTriples, SavedGraphs)))
    ).

%!  rdf_save_canonical_trig(+Spec, :Options) is det.
%
%   Save     triples     in     a      canonical     format.     See
%   rdf_save_canonical_turtle/2 foir details.


rdf_save_canonical_trig(Spec, M:Options) :-
    canonical_options(CannonicalOptions, Options),
    rdf_save_trig(Spec, M:CannonicalOptions).

tw_trig_graphs([], _, State, State).
tw_trig_graphs([H|T], Stream, State0, State) :-
    set_graph_of_tw_state(H, State0, State1),
    nl(Stream),
    tw_resource(H, State1, Stream),
    format(Stream, ' {~n', []),
    tw_graph(State1, Stream),
    format(Stream, '~N}~n', []),
    set_bnode_id_of_tw_state(0, State1, State2),
    set_nodeid_map_of_tw_state(_, State2, State3),
    set_bnode_hash_of_tw_state(_, State3, State4),
    tw_trig_graphs(T, Stream, State4, State).


%!  trig_graphs(+State, -Graphs) is det.
%
%   True when Graphs is the (sorted) list of graphs we must save. If
%   the _expand_ argument is used and   no  graphs are specified, it
%   enumerates all triples and extracts the graphs.

trig_graphs(State, Graphs) :-
    tw_state_graphs(State, Graphs),
    (   nonvar(Graphs)
    ->  true
    ;   tw_state_expand(State, Expand),
        graphs(Expand, Graphs0),
        sort(Graphs0, Graphs)
    ).

:- if(have_rdf_db).
graphs(lookup, Graphs) :-
    findall(G, rdf_graph(G), Graphs).
:- endif.
graphs(Expand, Graphs) :-
    findall(G, distinct(G, call(Expand,_S,_P,_O,G)), Graphs).


%!  open_output(+Spec, +Encoding, -Stream, -Cleanup) is det.
%
%   Open output Spec, returning a stream using Encoding.
%
%   @param  Cleanup is a goal that must be used to revert the side
%           effects of open_output/4.

open_output(stream(Out), Encoding, Out, Cleanup) :-
    !,
    stream_property(Out, encoding(Old)),
    (   (   Old == Encoding
        ;   Old == wchar_t          % Internal encoding
        )
    ->  Cleanup = true
    ;   set_stream(Out, encoding(Encoding)),
        Cleanup = set_stream(Out, encoding(Old))
    ).
open_output(Stream, Encoding, Out, Cleanup) :-
    \+ atom(Stream),
    is_stream(Stream),
    !,
    open_output(stream(Stream), Encoding, Out, Cleanup).
open_output(Spec, Encoding, Out,
            close(Out)) :-
    out_to_file(Spec, File),
    open(File, write, Out, [encoding(Encoding)]).

out_to_file(URL, File) :-
    atom(URL),
    file_name_to_url(File, URL),
    !.
out_to_file(File, File).


                 /*******************************
                 *            PREFIXES          *
                 *******************************/

%!  init_prefix_map(+State, -State) is det.
%
%   Set  the  prefix_map  of  State.  The  prefix  map  is  list  of
%   Prefix-URI of prefixes to use for   emitting the graph requested
%   in State. If multiple prefixes are present   where  the one is a
%   prefix of the other, the longer one appears first in the list.

init_prefix_map(State0, State) :-
    tw_state_prefixes(State0, Prefixes),
    nonvar(Prefixes),
    !,
    user_prefix_map(Prefixes, PrefixMap),
    set_prefix_map_of_tw_state(PrefixMap, State0, State).
init_prefix_map(State0, State) :-
    tw_state_graph(State0, Graph),
    graph_prefix_map(State0, Graph, PrefixMap),
    set_prefix_map_of_tw_state(PrefixMap, State0, State).

init_prefix_map(State0, _Graphs, State) :-      % TriG version
    tw_state_prefixes(State0, Prefixes),
    nonvar(Prefixes),
    !,
    user_prefix_map(Prefixes, PrefixMap),
    set_prefix_map_of_tw_state(PrefixMap, State0, State).
init_prefix_map(State0, Graphs, State) :-       % TriG version
    maplist(graph_prefixes(State0), Graphs, NestedPrefixes),
    append(NestedPrefixes, Prefixes0),
    sort(Prefixes0, Prefixes),
    prefix_map(State0, Prefixes, PrefixMap),
    set_prefix_map_of_tw_state(PrefixMap, State0, State).

graph_prefix_map(State, Graph, PrefixMap) :-
    graph_prefixes(State, Graph, Prefixes),
    prefix_map(State, Prefixes, PrefixMap).

graph_prefixes(State0, Graph, Prefixes) :-
    tw_state_expand(State0, Expand),
    tw_state_only_known_prefixes(State0, OnlyKnown),
    rdf_graph_prefixes(Graph, Prefixes,
                       [ filter(turtle_prefix(OnlyKnown)),
                         expand(Expand),
                         min_count(2),
                         get_prefix(turtle:iri_turtle_prefix)
                       ]).

prefix_map(State, Prefixes, PrefixMap) :-
    remove_base(State, Prefixes, Prefixes2),
    prefix_names(Prefixes2, State, Pairs),
    transpose_pairs(Pairs, URI_Abrevs),
    reverse(URI_Abrevs, RURI_Abrevs),
    flip_pairs(RURI_Abrevs, PrefixMap).

%!  user_prefix_map(+Prefixes, -PrefixMap) is det.
%
%   Convert a list of prefix specifications   to  a list Prefix-URI,
%   longest URI first.

user_prefix_map(Prefixes, PrefixMap) :-
    must_be(list, Prefixes),
    maplist(prefix_pair, Prefixes, Pairs),
    map_list_to_pairs(prefix_length, Pairs, LenPairs),
    sort(LenPairs, LenPairs1),
    pairs_values(LenPairs1, RevPrefixMap),
    reverse(RevPrefixMap, PrefixMap).

prefix_pair(Prefix-URI, Prefix-URI) :-
    !,
    must_be(atom, Prefix),
    must_be(atom, URI).
prefix_pair(Prefix, Prefix-URI) :-
    must_be(atom, Prefix),
    (   rdf_current_prefix(Prefix, URI)
    ->  true
    ;   existence_error(prefix, Prefix)
    ).

prefix_length(_-URI, Len) :- atom_length(URI, Len).

%!  turtle_prefix(+OnlyKnown, +Where, +Prefix, +URI) is semidet.
%
%   Test whether we want  to  include   the  proposed  prefix in the
%   @prefix declaration.

:- public turtle_prefix/4.              % called through rdf_graph_prefixes/3.

turtle_prefix(true, _, Prefix, _) :-
    !,
    rdf_current_prefix(_, Prefix),
    !.
turtle_prefix(_, _, Prefix, URI) :-
    sub_atom(Prefix, _, 1, 0, Last),
    turtle_prefix_char(Last),
    atom_concat(Prefix, Local, URI),
    \+ sub_atom(Local, _, _, _, '.').

turtle_prefix_char('#').
turtle_prefix_char('/').


remove_base(State, Prefixes, PrefixesNoBase) :-
    tw_state_base_dir(State, BaseDir),
    atom(BaseDir),
    !,
    delete(Prefixes, BaseDir, PrefixesNoBase).
remove_base(_State, Prefixes, Prefixes).

flip_pairs([], []).
flip_pairs([Key-Val|Pairs], [Val-Key|Flipped]) :-
    flip_pairs(Pairs, Flipped).

prefix_names(URIs, State, Prefixes) :-
    prefix_names(URIs, State, 1, Prefixes, []).

prefix_names([], _, _, List, List) :- !.
prefix_names(URIs, State, Len, Prefixes, Tail) :-
    prefix_names(URIs, State, Len, Prefixes, PTail, Rest),
    Len1 is Len + 1,
    prefix_names(Rest, State, Len1, PTail, Tail).

prefix_names(URIs, State, Len, Prefixes, PTail, Rest) :-
    map_list_to_pairs(propose_abbrev(State, Len), URIs, Pairs),
    !,
    keysort(Pairs, Sorted),
    unique(Sorted, Prefixes, PTail, Rest).
prefix_names(URIs, _, _, Prefixes, PTail, []) :-
    number_prefixes(URIs, 1, Prefixes, PTail).

number_prefixes([], _, PL, PL).
number_prefixes([H|T0], N, [P-H|PL], T) :-
    atomic_concat(ns, N, P),
    succ(N, N1),
    number_prefixes(T0, N1, PL, T).

unique([], L, L, []).
unique([A-U|T0], [A-U|T], L, Rest) :-
    T0 \= [A-_|_],
    !,
    unique(T0, T, L, Rest).
unique([A-U|T0], Prefixes, L, [U|Rest0]) :-
    strip_keys(T0, A, T1, Rest0, Rest),
    unique(T1, Prefixes, L, Rest).

strip_keys([A-U|T0], A, T, [U|R0], R) :-
    !,
    strip_keys(T0, A, T, R0, R).
strip_keys(L, _, L, R, R).


%!  propose_abbrev(+State, +Len, +URI, -Abbrev) is multi.
%
%   Propose an abbreviation for URI.  Backtracking yields longer
%   ones.

propose_abbrev(_, _, URI, Abbrev) :-
    well_known_ns(Abbrev, URI),
    !.
propose_abbrev(State, _, URI, Abbrev) :-
    tw_state_user_prefixes(State, true),
    rdf_current_prefix(Abbrev, URI),
    !.
propose_abbrev(_, Len, URI, Abbrev) :-
    namespace_parts(URI, Parts),
    include(abbrev_part, Parts, Names),
    reverse(Names, RevNames),
    length(Use, Len),
    append(Use, _, RevNames),
    atomic_list_concat(Use, -, Abbrev).

abbrev_part(X) :-
    xml_name(X),
    \+ well_known_ns(X, _),
    \+ well_known_extension(X).

well_known_ns(rdf,  'http://www.w3.org/1999/02/22-rdf-syntax-ns#').
well_known_ns(rdfs, 'http://www.w3.org/2000/01/rdf-schema#').
well_known_ns(owl,  'http://www.w3.org/2002/07/owl#').
well_known_ns(xsd,  'http://www.w3.org/2001/XMLSchema#').
well_known_ns(dc,   'http://purl.org/dc/elements/1.1/').

well_known_extension(ttl).
well_known_extension(nt).
well_known_extension(n3).
well_known_extension(xml).
well_known_extension(rdf).
well_known_extension(owl).

%!  namespace_parts(+URL, -Parts)

namespace_parts(URL, Parts) :-
    atom_codes(URL, Codes),
    phrase(parts(Parts), Codes),
    !.
namespace_parts(URL, _) :-
    format(user_error, 'Couldn\'t split ~q~n', [URL]),
    fail.

parts(List) --> sep2, parts2(List).

parts2([H|T]) -->
    string(Codes),  {Codes \== []},
    sep,
    !,
    {atom_codes(H, Codes)},
    parts2(T).
parts2([]) --> [].

string([]) --> [].
string([H|T]) --> [H], string(T).

sep --> sep_char, sep2.
sep([], []).

sep2 --> sep_char, !, sep2.
sep2 --> [].

sep_char --> "/".
sep_char --> ":".
sep_char --> ".".
sep_char --> "?".
sep_char --> "#".


%!  init_base(+State0, -State) is det.
%
%   Initialise dealing with the base URI.  It sets two attributes of
%   the state: base_root and base_path.

init_base(State0, State) :-
    tw_state_base(State0, BaseURI),
    atom(BaseURI),
    !,
    parse_url(BaseURI, Attributes),
    include(root_part, Attributes, RootAttrs),
    parse_url(BaseRoot, RootAttrs),
    memberchk(path(BasePath), Attributes),
    file_directory_name(BasePath, BaseDir),
    atomic_list_concat([BaseRoot, BaseDir, /], BaseDirURI),
    set_base_root_of_tw_state(BaseRoot, State0, State1),
    set_base_path_of_tw_state(BasePath, State1, State2),
    set_base_dir_of_tw_state(BaseDirURI, State2, State).
init_base(State, State).

root_part(protocol(_)).
root_part(host(_)).
root_part(port(_)).


                 /*******************************
                 *              SAVE            *
                 *******************************/

%!  tw_graph(+State, +Out) is det.
%
%   Write an RDF graph as Turtle data.
%
%   @tbd Write unconnected and multi-connected blank-nodes.

tw_graph(State, Out) :-
    subjects(State, Subjects),
    length(Subjects, SubjectCount),
    inc_subject_count(State, SubjectCount),
    partition(rdf_is_bnode, Subjects, BNodes, ProperSubjects),
    maplist(pair_var, BNodes, Pairs),
    ord_list_to_rbtree(Pairs, BNTree),
    tw_state_nodeid_map(State, BNTree),
    (   ProperSubjects == []
    ->  true
    ;   length(ProperSubjects, PSCount),
        comment(State, 'Named toplevel resources (~D)', [PSCount], Out),
        tw_proper_subjects(ProperSubjects, State, Out)
    ),
    tw_bnodes(Pairs, State, Out).

pair_var(BNode, BNode-_).

tw_prefix_map(State, Out) :-
    tw_state_prefix_map(State, PrefixMap),
    tw_prefix_map(PrefixMap, State, Out).

%!  tw_prefix_map(+PrefixMap, +State, +Out) is det.
%
%   Write the @base and @prefix declarations

tw_prefix_map(PrefixMap, State, Out) :-
    tw_state_align_prefixes(State, true),
    !,
    longest_prefix(PrefixMap, 0, Length),
    PrefixCol is Length+10,
    tw_base(PrefixCol, State, Out),
    tw_prefix_map(PrefixMap, PrefixCol, State, Out).
tw_prefix_map(PrefixMap, State, Out) :-
    tw_base(0, State, Out),
    tw_prefix_map(PrefixMap, 0, State, Out).

longest_prefix([], L, L).
longest_prefix([Prefix-_|T], L0, L) :-
    atom_length(Prefix, L1),
    L2 is max(L0, L1),
    longest_prefix(T, L2, L).


tw_base(Col, State, Out) :-
    tw_state_base(State, Base),
    atom(Base),
    !,
    format(Out, '@base ~t~*|', [Col]),
    turtle:turtle_write_uri(Out, Base),
    format(Out, ' .~n', []).
tw_base(_, _, _).


tw_prefix_map([], _, _, _).
tw_prefix_map([Prefix-URI|T], Col, State, Out) :-
    format(Out, '@prefix ~t~w: ~*|', [Prefix, Col]),
    tw_relative_uri(URI, State, Out),
    format(Out, ' .~n', []),
    (   T == []
    ->  true
    ;   tw_prefix_map(T, Col, State, Out)
    ).


%!  tw_proper_subjects(+Subjects, +State, +Out) is det.
%
%   Write the subjects that are not Bnodes.

tw_proper_subjects([], _, _).
tw_proper_subjects([H|T], State, Out) :-
    separate_subjects(State, Out),
    tw_subject(H, H, State, Out),
    tw_proper_subjects(T, State, Out).


separate_subjects(State, Out) :-
    tw_state_subject_white_lines(State, ExtraLines),
    put_n(ExtraLines, '\n', Out).

%!  tw_subject(+URI, +State, +Out) is det.
%
%   Write a toplevel non-bnode subject.

tw_subject(URI, Ref, State, Out) :-
    subject_triples(URI, State, Pairs),
    length(Pairs, Count),
    inc_triple_count(State, Count),
    group_po(Pairs, Grouped),
    tw_subject_triples(Grouped, Ref, State, Out).

group_po(Pairs, Grouped) :-
    group_pairs_by_key(Pairs, Grouped0),
    rdf_equal(rdf:type, RDFType),
    (   select(RDFType-Types, Grouped0, Grouped1)
    ->  Grouped = [RDFType-Types|Grouped1]
    ;   Grouped = Grouped0
    ).

%!  tw_bnodes(+Pairs, +State, +Out) is det.
%
%   Write the Bnodes. Pairs is a list   URI-Ref, where Ref is one of
%   =written= if the Bnode is already written;   an integer if it is
%   used multiple times or a variable if   it  has not been written.
%   The order in which we deal with bnodes is defined as follows:
%
%       * First, write the bnodes that are not referenced at all
%       as toplevel bnodes using [ ... ] notation.
%
%       * Next, write the bnodes that need written as toplevel
%       nodes using the _:XX notation because they are referenced
%       multiple times in the graph. Continue this process until it
%       is exhausted.

tw_bnodes(Pairs, State, Out) :-
    tw_top_bnodes(Pairs, State, Out, Rest1),
    tw_numbered_bnodes(Rest1, State, Out, 1, Rest2),
    tw_cyclic_bnodes(Rest2, State, Out, 0).


tw_numbered_bnodes([], _, _, _, []) :- !.
tw_numbered_bnodes(Pairs, State, Out, Level, Rest) :-
    multi_referenced(Pairs, RefPairs, Rest0),
    (   RefPairs == []
    ->  Rest = Rest0
    ;   length(RefPairs, Count),
        comment(State, 'Level ~D multi-referenced blank-nodes (~D)',
                [ Level, Count ], Out),
        tw_ref_bnodes(RefPairs, State, Out),
        Level1 is Level + 1,
        tw_numbered_bnodes(Rest0, State, Out, Level1, Rest)
    ).

multi_referenced([], [], []).
multi_referenced([H|T], RefPairs, Rest) :-
    H = _-Ref,
    (   Ref == written
    ->  multi_referenced(T, RefPairs, Rest)
    ;   var(Ref)
    ->  Rest = [H|TR],
        multi_referenced(T, RefPairs, TR)
    ;   assertion(Ref = bnode(_)),
        RefPairs = [H|TRP],         % assigned reference
        multi_referenced(T, TRP, Rest)
    ).

tw_ref_bnodes([], _, _).
tw_ref_bnodes([BNode-Ref|T], State, Out) :-
    separate_subjects(State, Out),
    tw_subject(BNode, Ref, State, Out),
    tw_ref_bnodes(T, State, Out).


%!  tw_top_bnodes(+Pairs, +State, +Out, -Rest)
%
%   Write the top bnodes: those that  do   not  appear  as an object
%   anywhere.

tw_top_bnodes(Pairs, State, Out, Rest) :-
    unreferenced(Pairs, State, TopBNodes, Rest),
    (   TopBNodes == []
    ->  true
    ;   length(TopBNodes, Count),
        comment(State, 'Toplevel blank-nodes (~D)', [Count], Out),
        sort_bnodes(TopBNodes, SortedTopBNodes, State),
        tw_top_bnodes(SortedTopBNodes, State, Out)
    ).

unreferenced([], _, [], []).
unreferenced([H|T], State, UnrefPairs, Rest) :-
    H = BNode-Ref,
    (   Ref == written
    ->  unreferenced(T, State, UnrefPairs, Rest)
    ;   var(Ref),
        object_link_count(BNode, State, 0)
    ->  UnrefPairs = [H|URT],
        unreferenced(T, State, URT, Rest)
    ;   Rest = [H|TR],
        unreferenced(T, State, UnrefPairs, TR)
    ).

tw_top_bnodes([], _, _).
tw_top_bnodes([BNode-_|T], State, Out) :-
    tw_bnode(BNode, State, Out),
    tw_top_bnodes(T, State, Out).


tw_bnode(BNode, State, Out) :-
    subject_triples(BNode, State, Pairs),
    length(Pairs, Count),
    inc_triple_count(State, Count),
    (   tw_state_inline_bnodes(State, true)
    ->  tw_bnode_triples(Pairs, State, Out),
        format(Out, ' .~n', [])
    ;   next_bnode_id(State, BNode, Ref),
        tw_bnode_ntriples(Pairs, Ref, State, Out)
    ).

tw_bnode_triples(Pairs, State, Out) :-
    group_po(Pairs, Grouped),
    (   tw_state_single_line_bnodes(State, true)
    ->  format(Out, '[ ', []),
        tw_triples(Grouped, -1, State, Out),
        format(Out, ' ]', [])
    ;   line_position(Out, Indent),
        format(Out, '[ ', []),
        line_position(Out, AIndent),
        tw_triples(Grouped, AIndent, State, Out),
        nl_indent(Out, State, Indent),
        format(Out, ']', [])
    ).

tw_bnode_ntriples([], _, _, _).
tw_bnode_ntriples([P-O|T], Ref, State, Out) :-
    tw_bnode_ref(Ref, Out),
    format(Out, ' ', []),
    tw_predicate(P, State, Out),
    format(Out, ' ', []),
    tw_object(O, State, Out),
    format(Out, ' .~n', []),
    tw_bnode_ntriples(T, Ref, State, Out).


%!  tw_cyclic_bnodes(+Pairs, +BNode, +State, +Out, +Cycle)
%
%   The rest. These are groups of bnodes  that are reachable, but we
%   cannot find a starting point, neither from a named resource, nor
%   from an unlinked bnode. As long as we are not considering stable
%   canonical output, we can break the cycle at any point.

tw_cyclic_bnodes([], _State, _Out, _) :- !.
tw_cyclic_bnodes(Pairs, State, Out, Cycle0) :-
    (   tw_state_canonical(State, true)
    ->  sort_bnode_pairs(Pairs, BNodes, State)
    ;   BNodes = Pairs
    ),
    succ(Cycle0, Cycle),
    BNodes = [BNode-Ref|_],
    next_bnode_id(State, BNode, Ref),
    comment(State, 'Breaking cycle ~D', [Cycle], Out),
    tw_numbered_bnodes(Pairs, State, Out, 1, Rest),
    tw_cyclic_bnodes(Rest, State, Out, Cycle).


%!  tw_subject_triples(+Grouped, +Subject, +State, +Out)
%
%   Save triples on Subject.  Combine groups of triples with the
%   same subject (;) and same subject+predicate (,).
%
%   @param  Subject is either a URI or an integer.  The latter is
%           used for writing a named bnode.

tw_subject_triples([], _, _, _) :- !.
tw_subject_triples(Grouped, URI, State, Out) :-
    tw_state_group(State, false),
    !,
    tw_ungrouped_triples(Grouped, URI, State, Out).
tw_subject_triples(Grouped, URI, State, Out) :-
    tw_resource(URI, State, Out),
    (   tw_state_indent(State, Indent),
        Indent > 0
    ->  nl_indent(Out, State, Indent)
    ;   put_char(Out, ' '),
        line_position(Out, Indent)
    ),
    tw_triples(Grouped, Indent, State, Out),
    format(Out, ' .~n', []).

%!  tw_ungrouped_triples(+Grouped, +URI, +State, +Out)
%
%   Write triples for subject URI as one line per triple.  Used
%   for canonical output.

tw_ungrouped_triples([], _, _, _).
tw_ungrouped_triples([P-Vs|Groups], URI, State, Out) :-
    partition(rdf_is_bnode, Vs, BNVs, ProperVs),
    tw_ungrouped_values(ProperVs, P, URI, State, Out),
    sort_bnodes(BNVs, SortedBNVs, State),
    tw_ungrouped_values(SortedBNVs, P, URI, State, Out),
    tw_ungrouped_triples(Groups, URI, State, Out).

tw_ungrouped_values([], _, _, _, _).
tw_ungrouped_values([V|T], P, URI, State, Out) :-
    tw_resource(URI, State, Out),
    put_char(Out, ' '),
    tw_predicate(P, State, Out),
    put_char(Out, ' '),
    tw_object(V, State, Out),
    format(Out, ' .~n', []),
    tw_ungrouped_values(T, P, URI, State, Out).


%!  tw_triples(+Groups, +Indent, +State, +Out) is det.
%
%   Triple writer that uses ; and ,- grouping

tw_triples([P-Vs|MoreGroups], Indent, State, Out) :-
    tw_write_pvs(Vs, P, State, Out),
    (   MoreGroups == []
    ->  true
    ;   format(Out, ' ;', []),
        nl_indent(Out, State, Indent),
        tw_triples(MoreGroups, Indent, State, Out)
    ).

tw_write_pvs(Values, P, State, Out) :-
    tw_predicate(P, State, Out),
    put_char(Out, ' '),
    line_position(Out, Indent),
    tw_write_vs(Values, Indent, State, Out).

tw_predicate(P, State, Out) :-
    (   rdf_equal(P, rdf:type),
        tw_state_a(State, true)
    ->  format(Out, 'a', [])
    ;   tw_resource(P, State, Out)
    ).

tw_write_vs([H|T], Indent, State, Out) :-
    tw_object(H, State, Out),
    (   T == []
    ->  true
    ;   format(Out, ' ,', []),
        nl_indent(Out, State, Indent),
        tw_write_vs(T, Indent, State, Out)
    ).

%!  tw_object(+Value, +State, +Out) is det.
%
%   Write the object of a triple.

tw_object(Value, State, Out) :-
    rdf_is_bnode(Value),
    !,
    tw_bnode_object(Value, State, Out).
tw_object(Value, State, Out) :-
    atom(Value),
    !,
    tw_resource(Value, State, Out).
tw_object(Literal, State, Out) :-
    tw_literal(Literal, State, Out).

%!  tw_bnode_object(+Value, +State, +Out) is det.
%
%   Write a Bnode value.  There are a number of cases:
%
%       * The BNode was already written.  Write the same ref.
%       * The BNode is not shared.  Inline and set =written=
%       * The BNode is shared.  Generate a NodeID and store it
%       * The BNode is once as object: Generate a NodeID
%       * The BNode is more than once object: Generate a NodeID
%         and put in table.

tw_bnode_object(BNode, State, Out) :-
    tw_state_nodeid_map(State, BNTree),
    rb_lookup(BNode, Ref, BNTree),
    !,
    (   var(Ref)
    ->  (   tw_state_inline_bnodes(State, true),
            tw_unshared_bnode(BNode, State, Out)
        ->  Ref = written
        ;   next_bnode_id(State, BNode, Ref),
            tw_bnode_ref(Ref, Out)
        )
    ;   tw_bnode_ref(Ref, Out)
    ).
tw_bnode_object(BNode, State, Out) :-
    object_link_count(BNode, State, N),
    N > 1,
    !,
    tw_state_nodeid_map(State, BNTree0),
    rb_insert(BNTree0, BNode, Ref, BNTree),
    set_nodeid_map_of_tw_state(BNTree, State),
    next_bnode_id(State, BNode, Ref),
    tw_bnode_ref(Ref, Out).
tw_bnode_object(BNode, State, Out) :-
    next_bnode_id(State, BNode, Ref),
    tw_bnode_ref(Ref, Out).

tw_bnode_ref(bnode(Ref), Out) :-
    (   integer(Ref)
    ->  format(Out, '_:bn~w', [Ref])
    ;   format(Out, '_:~w', [Ref])
    ).

%!  tw_unshared_bnode(+BNode, +State, +Out) is semidet.
%
%   Write a bnode if this is the only place it is used.

tw_unshared_bnode(BNode, State, Out) :-
    object_link_count(BNode, State, 1),
    subject_triples(BNode, State, Pairs),
    (   Pairs == []
    ->  format(Out, '[]', [])
    ;   pairs_unshared_collection(Pairs, State, Collection)
    ->  (   Collection == []
        ->  format(Out, '()', [])
        ;   tw_state_nodeid_map(State, BNTree),
            rb_lookup(BNode, written, BNTree),
            length(Collection, NMembers),
            Triples is 2*NMembers,
            inc_triple_count(State, Triples),
            (   tw_state_single_line_bnodes(State, true)
            ->  format(Out, '( ', []),
                tw_collection(Collection, -1, State, Out),
                format(Out, ' )', [])
            ;   line_position(Out, Indent),
                format(Out, '( ', []),
                line_position(Out, AIndent),
                tw_collection(Collection, AIndent, State, Out),
                nl_indent(Out, State, Indent),
                format(Out, ')', [])
            )
        )
    ;   tw_bnode_triples(Pairs, State, Out)
    ).

tw_collection([H|T], Indent, State, Out) :-
    tw_object(H, State, Out),
    (   T \== []
    ->  nl_indent(Out, State, Indent),
        tw_collection(T, Indent, State, Out)
    ;   true
    ).

%!  unshared_collection(+URI, +State, -Members) is semidet.
%
%   True if URI denodes an RDF list that  is made up from bnodes, is
%   linked exactly once  to  its  context   and  contains  no  extra
%   triples.

unshared_collection(C, _, []) :-
    rdf_equal(C, rdf:nil),
    !.
unshared_collection(C, State, List) :-
    rdf_is_bnode(C),
    object_link_count(C, State, 1),
    tw_state_nodeid_map(State, BNTree),
    rb_lookup(C, written, BNTree),
    subject_triples(C, State, Pairs),
    pairs_unshared_collection(Pairs, State, List).

pairs_unshared_collection(Pairs, State, [H|T]) :-
    rdf_equal(rdf:first, RDFFirst),
    rdf_equal(rdf:rest, RDFRest),
    Pairs = [ RDFFirst-H,
              RDFRest-Rest
            | More
            ],
    (   More == []
    ;   rdf_equal(rdf:type, RDFType),
        rdf_equal(rdf:'List', RDFList),
        More == [RDFType-RDFList]
    ),
    unshared_collection(Rest, State, T).


%!  object_link_count(+BNode, +STate, -Count) is det.
%
%   Number of times BNode is used as an object in the graph

object_link_count(BNode, State, Count) :-
    tw_state_graph(State, Graph),
    tw_state_expand(State, Expand),
    findall(S-P, call(Expand,S,P,BNode,Graph), Pairs0),
    sort(Pairs0, Pairs),            % remove duplicates
    length(Pairs, Count).

%!  nl_indent(+Out, +State, +Indent) is det.
%
%   Write a newline and indent to column Indent.

nl_indent(Out, _, -1) :-
    !,
    put_char(Out, ' ').
nl_indent(Out, State, Indent) :-
    nl(Out),
    tw_state_tab_distance(State, TD),
    (   TD == 0
    ->  tab(Out, Indent)
    ;   Tabs is Indent//TD,
        Spaces is Indent mod TD,
        put_n(Tabs, '\t', Out),
        put_n(Spaces, ' ', Out)
    ).

put_n(N, Char, Out) :-
    N > 0,
    !,
    put_char(Out, Char),
    N2 is N - 1,
    put_n(N2, Char, Out).
put_n(_, _, _).


%!  subject_triples(+URI, +State, -Pairs) is det.
%
%   Pairs is a sorted list of P-O  pairs representing all triples on
%   the subject URI.

subject_triples(URI, State, Pairs) :-
    tw_state_graph(State, Graph),
    tw_state_expand(State, Expand),
    findall(P-O, call(Expand, URI, P, O, Graph), Pairs0),
    sort(Pairs0, Pairs).


                 /*******************************
                 *          GRAPH-LOGIC         *
                 *******************************/

%!  subjects(+State, -Subjects:ord_set) is det.
%
%   Subjects is a list of all subjects in the graph requested in
%   State.

subjects(State, Subjects) :-
    tw_state_expand(State, Expand),
    tw_state_graph(State, Graph),
    (   Expand == lookup,
        atom(Graph),
        (   rdf_graph_property(Graph, triples(Count))
        ->  true
        ;   Count = 0                       % non-existing graph
        ),
        rdf_statistics(triples(Total)),
        Count * 10 < Total
    ->  findall(S, rdf(S,_,_,Graph), List),
        sort(List, Subjects)
    ;   Expand \== lookup
    ->  findall(S, call(Expand, S,_,_,Graph), List),
        sort(List, Subjects)
    ;   findall(Subject, subject(State, Subject), AllSubjects),
        sort(AllSubjects, Subjects)
    ).


subject(State, Subject) :-
    tw_state_graph(State, Graph),
    (   atom(Graph)
    ->  rdf_resource(Subject),
        (   rdf(Subject, _, _, Graph)
        ->  true
        )
    ;   rdf_subject(Subject)
    ).


:- if(have_rdf_db).
:- public lookup/4.                     % called from expand hook.

lookup(S,P,O,G) :-
    (   var(G)
    ->  rdf(S,P,O)
    ;   rdf(S,P,O,G)
    ).
:- else.
lookup(_S,_P,_O,_G) :-
    print_message(error, turtle_write(no_rdf_db)),
    fail.
:- endif.


                 /*******************************
                 *        CANONICAL ORDERING    *
                 *******************************/

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This section deals with the two problems of canonical graphs:

    * Keep blank nodes in the same order
    * Assign stable names to blank nodes that we need to
      give a name.  There are two cases: (1) a blank nodes is
      used in more than one place and (2) a blank node series
      is cyclic.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

%!  sort_bnodes(+BNodes, -Sorted, +State) is det.
%
%   Sort a list of blank nodes.

sort_bnodes(BNodes, Sorted, _State) :-
    sort(BNodes, Sorted).

%!  sort_bnode_pairs(+Pairs, -Sorted, +State) is det.
%
%   Sort a list of Pairs BNode-Ref

sort_bnode_pairs(Pairs, Sorted, _State) :-
    sort(Pairs, Sorted).

%!  bnode_to_term(+BNode, -Term, +State)
%
%   Term is a canonical representation of the graph formed by BNode.
%   The transformation of a bnode is
%
%           bnode(p-[o1,o2,..], ..)
%
%   The arguments are alphabetically sorted   on predicate (can't we
%   leave the preds out them?) and   the  objects are alphabetically
%   sorted.  Sorting multiple bnode values?


%!  next_bnode_id(+State, +BNode, -Ref) is det.
%
%   Generate a node-id for BNode.   When writing non-canonically, we
%   simply number the bnodes.  Otherwise  we   want  a  more  stable
%   numbering. Our numbering is a hash of  the content of the bnode.
%   It is not unlikely that we find muliple copies, and therefore we
%   number the full id is bn_<hash>_<n>, <n> counting 0...

next_bnode_id(State, _BNode, bnode(Ref)) :-
    tw_state_canonical(State, false),
    !,
    tw_state_bnode_id(State, Ref0),
    Ref is Ref0+1,
    nb_set_bnode_id_of_tw_state(Ref, State).
next_bnode_id(State, BNode, bnode(Ref)) :-
    bnode_hash(BNode, Hash),
    tw_state_bnode_hash(State, BNHash),
    (   var(BNHash)
    ->  rb_empty(BNHash)
    ;   true
    ),
    (   rb_update(BNHash, Hash, C0, C, BNHash1)
    ->  C is C0+1
    ;   C = 0,
        rb_insert(BNHash, Hash, C, BNHash1)
    ),
    set_bnode_hash_of_tw_state(BNHash1, State),
    format(atom(Ref), 'bn_~w_~d', [Hash, C]).

%!  bnode_hash(+BNode, -Hash) is det.
%
%   Hash is the hash-value for a bnode.
%
%   @tbd: Hash on content.

bnode_hash(BNode, Hash) :-
    term_hash(BNode, Hash).


                 /*******************************
                 *           PRIMITIVES         *
                 *******************************/

%!  tw_resource(+Resource, +State, +Out) is det.
%
%   Write a resource

tw_resource(BNodeID, _, Out) :-
    BNodeID = bnode(_),
    !,
    tw_bnode_ref(BNodeID, Out).
tw_resource(Resource, State, Out) :-
    tw_state_prefix_map(State, PrefixMap),
    member(Prefix-Full, PrefixMap),
    atom_concat(Full, Name, Resource),
    (   turtle:turtle_pn_local(Name)
    ->  true
    ;   Name == ''
    ),
    !,
    format(Out, '~w:', [Prefix]),
    turtle:turtle_write_pn_local(Out, Name).
tw_resource(Resource, State, Out) :-
    tw_relative_uri(Resource, State, Out).


tw_relative_uri(Resource, State, Out) :-
    tw_state_base_root(State, Root),
    atom(Root),
    atom_concat(Root, ResPath, Resource),
    sub_atom(ResPath, 0, _, _, /),
    tw_state_base_path(State, BasePath),
    relative_path(ResPath, BasePath, RelPath),
    !,
    turtle:turtle_write_uri(Out, RelPath).
tw_relative_uri(Resource, _, Out) :-
    turtle:turtle_write_uri(Out, Resource).

relative_path(Path, RelTo, RelPath) :-
    atomic_list_concat(PL, /, Path),
    atomic_list_concat(RL, /, RelTo),
    delete_common_prefix(PL, RL, PL1, PL2),
    to_dot_dot(PL2, DotDot, PL1),
    atomic_list_concat(DotDot, /, RelPath).

delete_common_prefix([H|T01], [H|T02], T1, T2) :-
    !,
    delete_common_prefix(T01, T02, T1, T2).
delete_common_prefix(T1, T2, T1, T2).

to_dot_dot([], Tail, Tail).
to_dot_dot([_], Tail, Tail) :- !.
to_dot_dot([_|T0], ['..'|T], Tail) :-
    to_dot_dot(T0, T, Tail).


%!  tw_literal(+Literal, +State, +Out) is det.
%
%   Write a literal value to the stream Out.

tw_literal(^^(Value, Type), State, Out) :-
    !,
    tw_typed_literal(Type, Value, State, Out).
tw_literal(literal(type(Type, Value)), State, Out) :-
    !,
    tw_typed_literal(Type, Value, State, Out).
tw_literal(@(Value, Lang), State, Out) :-
    !,
    tw_quoted_string(Value, State, Out),
    downcase_atom(Lang, TurtleLang),        % Turtle lang = [a-z]+('-'[a-z0-9]+)*
    format(Out, '@~w', [TurtleLang]).
tw_literal(literal(lang(Lang, Value)), State, Out) :-
    !,
    tw_quoted_string(Value, State, Out),
    downcase_atom(Lang, TurtleLang),        % Turtle lang = [a-z]+('-'[a-z0-9]+)*
    format(Out, '@~w', [TurtleLang]).
tw_literal(literal(Value), State, Out) :-
    atom(Value),
    !,
    rdf_equal(xsd:string, TypeString),
    tw_typed_literal(TypeString, Value, State, Out).
                                                % Add types automatically
tw_literal(literal(Value), State, Out) :-
    integer(Value),
    !,
    rdf_equal(Type, xsd:integer),
    tw_typed_literal(Type, Value, State, Out).
tw_literal(literal(Value), State, Out) :-
    float(Value),
    !,
    rdf_equal(Type, xsd:double),
    tw_typed_literal(Type, Value, State, Out).
tw_literal(literal(Value), State, Out) :-
    xml_is_dom(Value),
    !,
    rdf_equal(Type, rdf:'XMLLiteral'),
    tw_typed_literal(Type, Value, State, Out).
tw_literal(Literal, _State, _Out) :-
    type_error(rdf_literal, Literal).


tw_typed_literal(Type, Value, State, Out) :-
    tw_state_abbreviate_literals(State, true),
    tw_abbreviated_literal(Type, Value, State, Out),
    !.
tw_typed_literal(Type, Value, State, Out) :-
    (atom(Value) ; string(Value)),
    !,
    tw_quoted_string(Value, State, Out),
    write(Out, '^^'),
    tw_resource(Type, State, Out).
tw_typed_literal(Type, Value, State, Out) :-
    rdf_equal(Type, rdf:'XMLLiteral'),
    !,
    with_output_to(string(Tmp),
                   xml_write(Value, [header(false)])),
    tw_quoted_string(Tmp, State, Out),
    write(Out, '^^'),
    tw_resource(Type, State, Out).
tw_typed_literal(Type, Value, State, Out) :-
    format(string(Tmp), '~q', [Value]),
    tw_quoted_string(Tmp, State, Out),
    write(Out, '^^'),
    tw_resource(Type, State, Out).


%!  tw_abbreviated_literal(+Type, +Value, +State, +Out) is semidet.
%
%   Turtle abbreviated typed literals.
%
%   @tbd:   Deal with canonical forms (or is this a task of the
%           RDF parser?
%   @tbd:   What if the value is not in the lexical space of the type?

term_expansion((tw_abbreviated_literal(NS:Local, Value, State, Out) :- Body),
               (tw_abbreviated_literal(Type, Value, State, Out) :- Body)) :-
    atom(NS),
    rdf_global_id(NS:Local, Type).

tw_abbreviated_literal(xsd:integer, Value, State, Out) :-
    (   tw_state_canonize_numbers(State, false)
    ->  write(Out, Value)
    ;   atom_number(Value, Int),
        format(Out, '~d', [Int])
    ).
tw_abbreviated_literal(xsd:double, Value, State, Out) :-
    (   tw_state_canonize_numbers(State, false)
    ->  write(Out, Value)
    ;   ValueF is float(Value),
        xsd_number_string(ValueF, FloatS),
        format(Out, '~s', [FloatS])
    ).
tw_abbreviated_literal(xsd:string, Value, State, Out) :-
    tw_quoted_string(Value, State, Out).
tw_abbreviated_literal(xsd:decimal, Value, _, Out) :-
    format(Out, '~w', [Value]).
tw_abbreviated_literal(xsd:boolean, Value, _, Out) :-
    format(Out, '~w', [Value]).


%!  tw_quoted_string(+Atom, +State, +Out) is det.
%
%   Write  Atom  to  Out  as  a  quoted  string.  We  only  use  the
%   single-"..." representation.

tw_quoted_string(Atom, _, Out) :-
    turtle:turtle_write_quoted_string(Out, Atom).


                 /*******************************
                 *             COMMENT          *
                 *******************************/

comment(State, Format, Args, Out) :-
    tw_state_comment(State, true),
    !,
    format(Out, '~n# ', []),
    format(Out, Format, Args),
    format(Out, '~n', []).
comment(_, _, _, _).



                 /*******************************
                 *           STATISTICS         *
                 *******************************/

inc_triple_count(State, Count) :-
    tw_state_triple_count(State, C0),
    C1 is C0+Count,
    nb_set_triple_count_of_tw_state(C1, State).

inc_subject_count(State, Count) :-
    tw_state_subject_count(State, C0),
    C1 is C0+Count,
    nb_set_subject_count_of_tw_state(C1, State).

:- multifile
    prolog:message//1.

prolog:message(rdf(saved(File, Time, SavedSubjects, SavedTriples))) -->
    [ 'Saved ~D triples about ~D subjects into '-[SavedTriples, SavedSubjects] ],
    rdf_output(File),
    [ ' (~3f sec)'-[Time] ].
prolog:message(rdf(saved(File, Time, SavedSubjects, SavedTriples,
                         SavedGraphs))) -->
    [ 'Saved ~D graphs, ~D triples about ~D subjects into '-
      [SavedGraphs, SavedTriples, SavedSubjects] ],
    rdf_output(File),
    [ ' (~3f sec)'-[Time] ].

rdf_output(StreamSpec) -->
    { (   StreamSpec = stream(Stream)
      ->  true
      ;   Stream = StreamSpec
      ),
      is_stream(Stream),
      stream_property(Stream, file_name(File))
    },
    !,
    [ '~p'-[File] ].
rdf_output(File) -->
    [ '~p'-[File] ].