-
Notifications
You must be signed in to change notification settings - Fork 15
/
rdf_turtle_write.pl
1587 lines (1372 loc) · 49.8 KB
/
rdf_turtle_write.pl
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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
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]).
:- use_module(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 for 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),