-
Notifications
You must be signed in to change notification settings - Fork 15
/
rdf_db.pl
3468 lines (3038 loc) · 113 KB
/
rdf_db.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) 2003-2023, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
SWI-Prolog Solutions b.v.
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_db,
[ rdf_version/1, % -Version
rdf/3, % ?Subject, ?Predicate, ?Object
rdf/4, % ?Subject, ?Predicate, ?Object, ?DB
rdf_has/3, % ?Subject, +Pred, ?Obj
rdf_has/4, % ?Subject, +Pred, ?Obj, -RealPred
rdf_reachable/3, % ?Subject, +Pred, ?Object
rdf_reachable/5, % ?Subject, +Pred, ?Object, +MaxD, ?D
rdf_resource/1, % ?Resource
rdf_subject/1, % ?Subject
rdf_member_property/2, % ?Property, ?Index
rdf_assert/3, % +Subject, +Predicate, +Object
rdf_assert/4, % +Subject, +Predicate, +Object, +DB
rdf_retractall/3, % ?Subject, ?Predicate, ?Object
rdf_retractall/4, % ?Subject, ?Predicate, ?Object, +DB
rdf_update/4, % +Subject, +Predicate, +Object, +Act
rdf_update/5, % +Subject, +Predicate, +Object, +Src, +Act
rdf_set_predicate/2, % +Predicate, +Property
rdf_predicate_property/2, % +Predicate, ?Property
rdf_current_predicate/1, % -Predicate
rdf_current_literal/1, % -Literal
rdf_transaction/1, % :Goal
rdf_transaction/2, % :Goal, +Id
rdf_transaction/3, % :Goal, +Id, +Options
rdf_active_transaction/1, % ?Id
rdf_monitor/2, % :Goal, +Options
rdf_save_db/1, % +File
rdf_save_db/2, % +File, +DB
rdf_load_db/1, % +File
rdf_reset_db/0,
rdf_node/1, % -Id
rdf_bnode/1, % -Id
rdf_is_bnode/1, % +Id
rdf_is_resource/1, % +Term
rdf_is_literal/1, % +Term
rdf_literal_value/2, % +Term, -Value
rdf_load/1, % +File
rdf_load/2, % +File, +Options
rdf_save/1, % +File
rdf_save/2, % +File, +Options
rdf_unload/1, % +File
rdf_unload_graph/1, % +Graph
rdf_md5/2, % +DB, -MD5
rdf_atom_md5/3, % +Text, +Times, -MD5
rdf_create_graph/1, % ?Graph
rdf_graph_property/2, % ?Graph, ?Property
rdf_set_graph/2, % +Graph, +Property
rdf_graph/1, % ?Graph
rdf_source/1, % ?File
rdf_source/2, % ?DB, ?SourceURL
rdf_make/0, % Reload modified databases
rdf_gc/0, % Garbage collection
rdf_source_location/2, % +Subject, -Source
rdf_statistics/1, % -Key
rdf_set/1, % +Term
rdf_generation/1, % -Generation
rdf_snapshot/1, % -Snapshot
rdf_delete_snapshot/1, % +Snapshot
rdf_current_snapshot/1, % +Snapshot
rdf_estimate_complexity/4, % +S,+P,+O,-Count
rdf_save_subject/3, % +Stream, +Subject, +DB
rdf_save_header/2, % +Out, +Options
rdf_save_footer/1, % +Out
rdf_equal/2, % ?Resource, ?Resource
lang_equal/2, % +Lang1, +Lang2
lang_matches/2, % +Lang, +Pattern
rdf_prefix/2, % :Alias, +URI
rdf_current_prefix/2, % :Alias, ?URI
rdf_register_prefix/2, % +Alias, +URI
rdf_register_prefix/3, % +Alias, +URI, +Options
rdf_unregister_prefix/1, % +Alias
rdf_current_ns/2, % :Alias, ?URI
rdf_register_ns/2, % +Alias, +URI
rdf_register_ns/3, % +Alias, +URI, +Options
rdf_global_id/2, % ?NS:Name, :Global
rdf_global_object/2, % +Object, :NSExpandedObject
rdf_global_term/2, % +Term, :WithExpandedNS
rdf_compare/3, % -Dif, +Object1, +Object2
rdf_match_label/3, % +How, +String, +Label
rdf_split_url/3, % ?Base, ?Local, ?URL
rdf_url_namespace/2, % +URL, ?Base
rdf_warm_indexes/0,
rdf_warm_indexes/1, % +Indexed
rdf_update_duplicates/0,
rdf_debug/1, % Set verbosity
rdf_new_literal_map/1, % -Handle
rdf_destroy_literal_map/1, % +Handle
rdf_reset_literal_map/1, % +Handle
rdf_insert_literal_map/3, % +Handle, +Key, +Literal
rdf_insert_literal_map/4, % +Handle, +Key, +Literal, -NewKeys
rdf_delete_literal_map/3, % +Handle, +Key, +Literal
rdf_delete_literal_map/2, % +Handle, +Key
rdf_find_literal_map/3, % +Handle, +KeyList, -Literals
rdf_keys_in_literal_map/3, % +Handle, +Spec, -Keys
rdf_statistics_literal_map/2, % +Handle, +Name(-Arg...)
rdf_graph_prefixes/2, % ?Graph, -Prefixes
rdf_graph_prefixes/3, % ?Graph, -Prefixes, :Filter
(rdf_meta)/1, % +Heads
op(1150, fx, (rdf_meta))
]).
:- use_module(library(semweb/rdf_prefixes),
[ (rdf_meta)/1,
register_file_prefixes/1,
rdf_global_id/2,
rdf_register_ns/2,
% re-exported predicates
rdf_global_object/2,
rdf_current_ns/2,
rdf_prefix/2,
rdf_global_term/2,
rdf_register_ns/3,
rdf_register_prefix/3,
rdf_register_prefix/2,
rdf_current_prefix/2,
rdf_unregister_prefix/1
]).
:- autoload(library(apply),[maplist/2,maplist/3]).
:- use_module(library(debug),[debug/3,assertion/1]).
:- autoload(library(error),[must_be/2,existence_error/2]).
:- autoload(library(gensym),[gensym/2,reset_gensym/1]).
:- autoload(library(lists),
[member/2,flatten/2,list_to_set/2,append/3,select/3]).
:- autoload(library(memfile),
[atom_to_memory_file/2,open_memory_file/4]).
:- autoload(library(option),
[option/2,option/3,merge_options/3,meta_options/3]).
:- autoload(library(rdf),[process_rdf/3]).
:- autoload(library(sgml),
[ load_structure/3,
xml_quote_attribute/3,
xml_name/1,
xml_quote_cdata/3,
xml_is_dom/1,
iri_xml_namespace/3,
iri_xml_namespace/2
]).
:- autoload(library(sgml_write),[xml_write/3]).
:- autoload(library(uri),
[ uri_file_name/2,
uri_is_global/1,
uri_normalized/2,
uri_components/2,
uri_data/3,
uri_data/4
]).
:- autoload(library(xsdp_types),[xsdp_numeric_uri/2]).
:- autoload(library(semweb/rdf_cache),[rdf_cache_file/3]).
:- if(exists_source(library(thread))).
:- autoload(library(thread), [concurrent/3]).
:- endif.
:- use_foreign_library(foreign(rdf_db)).
:- public rdf_print_predicate_cloud/2. % print matrix of reachable predicates
:- meta_predicate
rdf_transaction(0),
rdf_transaction(0, +),
rdf_transaction(0, +, +),
rdf_monitor(1, +),
rdf_save(+, :),
rdf_load(+, :).
:- predicate_options(rdf_graph_prefixes/3, 3,
[ expand(callable+4),
filter(callable+3),
get_prefix(callable+2),
min_count(nonneg)
]).
:- predicate_options(rdf_load/2, 2,
[ base_uri(atom),
blank_nodes(oneof([share,noshare])),
cache(boolean),
concurrent(positive_integer),
db(atom),
format(oneof([xml,triples,turtle,trig,nquads,ntriples])),
graph(atom),
multifile(boolean),
if(oneof([true,changed,not_loaded])),
modified(-float),
prefixes(-list),
silent(boolean),
register_namespaces(boolean)
]).
:- predicate_options(rdf_save/2, 2,
[ graph(atom),
db(atom),
anon(boolean),
base_uri(atom),
write_xml_base(boolean),
convert_typed_literal(callable),
encoding(encoding),
document_language(atom),
namespaces(list(atom)),
xml_attributes(boolean),
inline(boolean)
]).
:- predicate_options(rdf_save_header/2, 2,
[ graph(atom),
db(atom),
namespaces(list(atom))
]).
:- predicate_options(rdf_save_subject/3, 3,
[ graph(atom),
base_uri(atom),
convert_typed_literal(callable),
document_language(atom)
]).
:- predicate_options(rdf_transaction/3, 3,
[ snapshot(any)
]).
:- discontiguous
term_expansion/2.
/** <module> Core RDF database
The file library(semweb/rdf_db) provides the core of the SWI-Prolog RDF
store.
@deprecated New applications should use library(semweb/rdf11), which
provides a much more intuitive API to the RDF store, notably
for handling literals. The library(semweb/rdf11) runs
currently on top of this library and both can run side-by-side
in the same application. Terms retrieved from the database
however have a different shape and can not be exchanged without
precautions.
*/
/*******************************
* PREFIXES *
*******************************/
% the ns/2 predicate is historically defined in this module. We'll keep
% that for compatibility reasons.
:- multifile ns/2.
:- dynamic ns/2. % ID, URL
:- multifile
rdf_prefixes:rdf_empty_prefix_cache/2.
rdf_prefixes:rdf_empty_prefix_cache(_Prefix, _IRI) :-
rdf_empty_prefix_cache.
:- rdf_meta
rdf(r,r,o),
rdf_has(r,r,o,r),
rdf_has(r,r,o),
rdf_assert(r,r,o),
rdf_retractall(r,r,o),
rdf(r,r,o,?),
rdf_assert(r,r,o,+),
rdf_retractall(r,r,o,?),
rdf_reachable(r,r,o),
rdf_reachable(r,r,o,+,?),
rdf_update(r,r,o,t),
rdf_update(r,r,o,+,t),
rdf_equal(o,o),
rdf_source_location(r,-),
rdf_resource(r),
rdf_subject(r),
rdf_create_graph(r),
rdf_graph(r),
rdf_graph_property(r,?),
rdf_set_graph(r,+),
rdf_unload_graph(r),
rdf_set_predicate(r, t),
rdf_predicate_property(r, -),
rdf_estimate_complexity(r,r,r,-),
rdf_print_predicate_cloud(r,+).
%! rdf_equal(?Resource1, ?Resource2)
%
% Simple equality test to exploit goal-expansion.
rdf_equal(Resource, Resource).
%! lang_equal(+Lang1, +Lang2) is semidet.
%
% True if two RFC language specifiers denote the same language
%
% @see lang_matches/2.
lang_equal(Lang, Lang) :- !.
lang_equal(Lang1, Lang2) :-
downcase_atom(Lang1, LangCannon),
downcase_atom(Lang2, LangCannon).
%! lang_matches(+Lang, +Pattern) is semidet.
%
% True if Lang matches Pattern. This implements XML language
% matching conform RFC 4647. Both Lang and Pattern are
% dash-separated strings of identifiers or (for Pattern) the
% wildcard *. Identifiers are matched case-insensitive and a *
% matches any number of identifiers. A short pattern is the same
% as *.
/*******************************
* BASIC TRIPLE QUERIES *
*******************************/
%! rdf(?Subject, ?Predicate, ?Object) is nondet.
%
% Elementary query for triples. Subject and Predicate are atoms
% representing the fully qualified URL of the resource. Object is
% either an atom representing a resource or literal(Value) if the
% object is a literal value. If a value of the form
% NameSpaceID:LocalName is provided it is expanded to a ground
% atom using expand_goal/2. This implies you can use this
% construct in compiled code without paying a performance penalty.
% Literal values take one of the following forms:
%
% * Atom
% If the value is a simple atom it is the textual representation
% of a string literal without explicit type or language
% qualifier.
%
% * lang(LangID, Atom)
% Atom represents the text of a string literal qualified with
% the given language.
%
% * type(TypeID, Value)
% Used for attributes qualified using the =|rdf:datatype|=
% TypeID. The Value is either the textual representation or a
% natural Prolog representation. See the option
% convert_typed_literal(:Convertor) of the parser. The storage
% layer provides efficient handling of atoms, integers (64-bit)
% and floats (native C-doubles). All other data is represented
% as a Prolog record.
%
% For literal querying purposes, Object can be of the form
% literal(+Query, -Value), where Query is one of the terms below.
% If the Query takes a literal argument and the value has a
% numeric type numerical comparison is performed.
%
% * plain(+Text)
% Perform exact match and demand the language or type qualifiers
% to match. This query is fully indexed.
%
% * icase(+Text)
% Perform a full but case-insensitive match. This query is
% fully indexed.
%
% * exact(+Text)
% Same as icase(Text). Backward compatibility.
%
% * substring(+Text)
% Match any literal that contains Text as a case-insensitive
% substring. The query is not indexed on Object.
%
% * word(+Text)
% Match any literal that contains Text delimited by a non
% alpha-numeric character, the start or end of the string. The
% query is not indexed on Object.
%
% * prefix(+Text)
% Match any literal that starts with Text. This call is intended
% for completion. The query is indexed using the skip list of
% literals.
%
% * ge(+Literal)
% Match any literal that is equal or larger than Literal in the
% ordered set of literals.
%
% * gt(+Literal)
% Match any literal that is larger than Literal in the ordered set
% of literals.
%
% * eq(+Literal)
% Match any literal that is equal to Literal in the ordered set
% of literals.
%
% * le(+Literal)
% Match any literal that is equal or smaller than Literal in the
% ordered set of literals.
%
% * lt(+Literal)
% Match any literal that is smaller than Literal in the ordered set
% of literals.
%
% * between(+Literal1, +Literal2)
% Match any literal that is between Literal1 and Literal2 in the
% ordered set of literals. This may include both Literal1 and
% Literal2.
%
% * like(+Pattern)
% Match any literal that matches Pattern case insensitively,
% where the `*' character in Pattern matches zero or more
% characters.
%
% Backtracking never returns duplicate triples. Duplicates can be
% retrieved using rdf/4. The predicate rdf/3 raises a type-error
% if called with improper arguments. If rdf/3 is called with a
% term literal(_) as Subject or Predicate object it fails
% silently. This allows for graph matching goals like
% rdf(S,P,O),rdf(O,P2,O2) to proceed without errors.
%! rdf(?Subject, ?Predicate, ?Object, ?Source) is nondet.
%
% As rdf/3 but in addition query the graph to which the triple
% belongs. Unlike rdf/3, this predicate does not remove duplicates
% from the result set.
%
% @param Source is a term Graph:Line. If Source is instatiated,
% passing an atom is the same as passing Atom:_.
%! rdf_has(?Subject, +Predicate, ?Object) is nondet.
%
% Succeeds if the triple rdf(Subject, Predicate, Object) is true
% exploiting the rdfs:subPropertyOf predicate as well as inverse
% predicates declared using rdf_set_predicate/2 with the
% =inverse_of= property.
%! rdf_has(?Subject, +Predicate, ?Object, -RealPredicate) is nondet.
%
% Same as rdf_has/3, but RealPredicate is unified to the actual
% predicate that makes this relation true. RealPredicate must be
% Predicate or an rdfs:subPropertyOf Predicate. If an inverse
% match is found, RealPredicate is the term inverse_of(Pred).
%! rdf_reachable(?Subject, +Predicate, ?Object) is nondet.
%
% Is true if Object can be reached from Subject following the
% transitive predicate Predicate or a sub-property thereof, while
% repecting the symetric(true) or inverse_of(P2) properties.
%
% If used with either Subject or Object unbound, it first returns
% the origin, followed by the reachable nodes in breadth-first
% search-order. The implementation internally looks one solution
% ahead and succeeds deterministically on the last solution. This
% predicate never generates the same node twice and is robust
% against cycles in the transitive relation.
%
% With all arguments instantiated, it succeeds deterministically
% if a path can be found from Subject to Object. Searching starts
% at Subject, assuming the branching factor is normally lower. A
% call with both Subject and Object unbound raises an
% instantiation error. The following example generates all
% subclasses of rdfs:Resource:
%
% ==
% ?- rdf_reachable(X, rdfs:subClassOf, rdfs:'Resource').
% X = 'http://www.w3.org/2000/01/rdf-schema#Resource' ;
% X = 'http://www.w3.org/2000/01/rdf-schema#Class' ;
% X = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property' ;
% ...
% ==
%! rdf_reachable(?Subject, +Predicate, ?Object, +MaxD, -D) is nondet.
%
% Same as rdf_reachable/3, but in addition, MaxD limits the number
% of edges expanded and D is unified with the `distance' between
% Subject and Object. Distance 0 means Subject and Object are the
% same resource. MaxD can be the constant =infinite= to impose no
% distance-limit.
%! rdf_subject(?Resource) is nondet.
%
% True if Resource appears as a subject. This query respects the
% visibility rules implied by the logical update view.
%
% @see rdf_resource/1.
rdf_subject(Resource) :-
rdf_resource(Resource),
( rdf(Resource, _, _) -> true ).
%! rdf_resource(?Resource) is nondet.
%
% True when Resource is a resource used as a subject or object in
% a triple.
%
% This predicate is primarily intended as a way to process all
% resources without processing resources twice. The user must be
% aware that some of the returned resources may not appear in any
% _visible_ triple.
/*******************************
* TRIPLE MODIFICATIONS *
*******************************/
%! rdf_assert(+Subject, +Predicate, +Object) is det.
%
% Assert a new triple into the database. This is equivalent to
% rdf_assert/4 using Graph =user=. Subject and Predicate are
% resources. Object is either a resource or a term literal(Value).
% See rdf/3 for an explanation of Value for typed and language
% qualified literals. All arguments are subject to name-space
% expansion. Complete duplicates (including the same graph and
% `line' and with a compatible `lifespan') are not added to the
% database.
%! rdf_assert(+Subject, +Predicate, +Object, +Graph) is det.
%
% As rdf_assert/3, adding the predicate to the indicated named
% graph.
%
% @param Graph is either the name of a graph (an atom) or a term
% Graph:Line, where Line is an integer that denotes a line number.
%! rdf_retractall(?Subject, ?Predicate, ?Object) is det.
%
% Remove all matching triples from the database. As
% rdf_retractall/4 using an unbound graph. See also
% rdf_retractall/4 and rdf_unload/1.
%! rdf_retractall(?Subject, ?Predicate, ?Object, ?Graph) is det.
%
% As rdf_retractall/3, also matching Graph. This is particulary
% useful to remove all triples coming from a loaded file. See also
% rdf_unload/1.
%! rdf_update(+Subject, +Predicate, +Object, ++Action) is det.
%! rdf_update(+Subject, +Predicate, +Object, +Graph, ++Action) is det
%
% Replaces one of the three (four) fields on the matching triples
% depending on Action:
%
% * subject(Resource)
% Changes the first field of the triple.
% * predicate(Resource)
% Changes the second field of the triple.
% * object(Object)
% Changes the last field of the triple to the given resource or
% literal(Value).
% * graph(Graph)
% Moves the triple from its current named graph to Graph.
% This only works with rdf_update/5 and throws an error when
% used with rdf_update/4.
/*******************************
* COLLECTIONS *
*******************************/
%! rdf_member_property(?Prop, ?Index)
%
% Deal with the rdf:_1, ... properties.
term_expansion(member_prefix(x),
member_prefix(Prefix)) :-
rdf_db:ns(rdf, NS),
atom_concat(NS, '_', Prefix).
member_prefix(x).
rdf_member_property(P, N) :-
integer(N),
!,
member_prefix(Prefix),
atom_concat(Prefix, N, P).
rdf_member_property(P, N) :-
member_prefix(Prefix),
atom_concat(Prefix, Sub, P),
atom_number(Sub, N).
/*******************************
* ANONYMOUS SUBJECTS *
*******************************/
%! rdf_node(-Id)
%
% Generate a unique blank node identifier for a subject.
%
% @deprecated New code should use rdf_bnode/1.
rdf_node(Resource) :-
rdf_bnode(Resource).
%! rdf_bnode(-Id)
%
% Generate a unique anonymous identifier for a subject.
rdf_bnode(Value) :-
repeat,
gensym('_:genid', Value),
\+ rdf(Value, _, _),
\+ rdf(_, _, Value),
\+ rdf(_, Value, _),
!.
/*******************************
* TYPES *
*******************************/
%! rdf_is_bnode(+Id)
%
% Tests if a resource is a blank node (i.e. is an anonymous
% resource). A blank node is represented as an atom that starts
% with =|_:|=. For backward compatibility reason, =|__|= is also
% considered to be a blank node.
%
% @see rdf_bnode/1.
%! rdf_is_resource(@Term) is semidet.
%
% True if Term is an RDF resource. Note that this is merely a
% type-test; it does not mean this resource is involved in any
% triple. Blank nodes are also considered resources.
%
% @see rdf_is_bnode/1
rdf_is_resource(Term) :-
atom(Term).
%! rdf_is_literal(@Term) is semidet.
%
% True if Term is an RDF literal object. Currently only checks for
% groundness and the literal functor.
rdf_is_literal(literal(Value)) :-
ground(Value).
/*******************************
* LITERALS *
*******************************/
%! rdf_current_literal(-Literal) is nondet.
%
% True when Literal is a currently known literal. Enumerates each
% unique literal exactly once. Note that it is possible that the
% literal only appears in already deleted triples. Deleted triples
% may be locked due to active queries, transactions or snapshots
% or may not yet be reclaimed by the garbage collector.
%! rdf_literal_value(+Literal, -Value) is semidet.
%
% True when value is the appropriate Prolog representation of
% Literal in the RDF _|value space|_. Current mapping:
%
% | Plain literals | Atom |
% | Language tagged literal | Atom holding plain text |
% | xsd:string | Atom |
% | rdf:XMLLiteral | XML DOM Tree |
% | Numeric XSD type | Number |
%
% @tbd Well, this is the long-term idea.
% @tbd Add mode (-,+)
:- rdf_meta
rdf_literal_value(o, -),
typed_value(r, +, -),
numeric_value(r, +, -).
rdf_literal_value(literal(String), Value) :-
atom(String),
!,
Value = String.
rdf_literal_value(literal(lang(_Lang, String)), String).
rdf_literal_value(literal(type(Type, String)), Value) :-
typed_value(Type, String, Value).
typed_value(Numeric, String, Value) :-
xsdp_numeric_uri(Numeric, NumType),
!,
numeric_value(NumType, String, Value).
typed_value(xsd:string, String, String).
typed_value(rdf:'XMLLiteral', Value, DOM) :-
( atom(Value)
-> setup_call_cleanup(
( atom_to_memory_file(Value, MF),
open_memory_file(MF, read, In, [free_on_close(true)])
),
load_structure(stream(In), DOM, [dialect(xml)]),
close(In))
; DOM = Value
).
numeric_value(xsd:integer, String, Value) :-
atom_number(String, Value),
integer(Value).
numeric_value(xsd:float, String, Value) :-
atom_number(String, Number),
Value is float(Number).
numeric_value(xsd:double, String, Value) :-
atom_number(String, Number),
Value is float(Number).
numeric_value(xsd:decimal, String, Value) :-
atom_number(String, Value).
/*******************************
* SOURCE *
*******************************/
%! rdf_source_location(+Subject, -Location) is nondet.
%
% True when triples for Subject are loaded from Location.
%
% @param Location is a term File:Line.
rdf_source_location(Subject, Source) :-
findall(Source, rdf(Subject, _, _, Source), Sources),
sort(Sources, Unique),
member(Source, Unique).
/*******************************
* GARBAGE COLLECT *
*******************************/
%! rdf_create_gc_thread
%
% Create the garbage collection thread.
:- public
rdf_create_gc_thread/0.
rdf_create_gc_thread :-
thread_create(rdf_gc_loop, _,
[ alias('__rdf_GC')
]).
%! rdf_gc_loop
%
% Take care of running the RDF garbage collection. This predicate
% is called from a thread started by creating the RDF DB.
rdf_gc_loop :-
catch(rdf_gc_loop(0), E, recover_gc(E, Cont)),
( Cont == true
-> rdf_gc_loop
; thread_self(Me),
thread_detach(Me)
).
recover_gc('$aborted', false) :-
!.
recover_gc(unwind(_), false) :-
!.
recover_gc(Error, true) :-
print_message(error, Error).
rdf_gc_loop(CPU) :-
repeat,
( consider_gc(CPU)
-> rdf_gc(CPU1),
sleep(CPU1)
; sleep(0.1)
),
fail.
%! rdf_gc(-CPU) is det.
%
% Run RDF GC one time. CPU is the amount of CPU time spent. We
% update this in Prolog because portable access to thread specific
% CPU is really hard in C.
rdf_gc(CPU) :-
statistics(cputime, CPU0),
( rdf_gc_
-> statistics(cputime, CPU1),
CPU is CPU1-CPU0,
rdf_add_gc_time(CPU)
; CPU = 0.0
).
%! rdf_gc is det.
%
% Run the RDF-DB garbage collector until no garbage is left and all
% tables are fully optimized. Under normal operation a separate thread
% with identifier =|__rdf_GC|= performs garbage collection as long as
% it is considered `useful'.
%
% Using rdf_gc/0 should only be needed to ensure a fully clean
% database for analysis purposes such as leak detection.
rdf_gc :-
has_garbage,
!,
rdf_gc(_),
rdf_gc.
rdf_gc.
%! has_garbage is semidet.
%
% True if there is something to gain using GC.
has_garbage :-
rdf_gc_info_(Info),
has_garbage(Info),
!.
has_garbage(Info) :- arg(2, Info, Garbage), Garbage > 0.
has_garbage(Info) :- arg(3, Info, Reindexed), Reindexed > 0.
has_garbage(Info) :- arg(4, Info, Optimizable), Optimizable > 0.
%! consider_gc(+CPU) is semidet.
%
% @param CPU is the amount of CPU time spent in the most recent
% GC.
consider_gc(_CPU) :-
( rdf_gc_info_(gc_info(Triples, % Total #triples in DB
Garbage, % Garbage triples in DB
Reindexed, % Reindexed & not reclaimed
Optimizable, % Non-optimized tables
_KeepGen, % Oldest active generation
_LastGCGen, % Oldest active gen at last GC
_ReindexGen,
_LastGCReindexGen))
-> ( (Garbage+Reindexed) * 5 > Triples
; Optimizable > 4
)
; print_message(error, rdf(invalid_gc_info)),
sleep(10)
),
!.
/*******************************
* STATISTICS *
*******************************/
%! rdf_statistics(?KeyValue) is nondet.
%
% Obtain statistics on the RDF database. Defined statistics are:
%
% * graphs(-Count)
% Number of named graphs.
%
% * triples(-Count)
% Total number of triples in the database. This is the number
% of asserted triples minus the number of retracted ones. The
% number of _visible_ triples in a particular context may be
% different due to visibility rules defined by the logical
% update view and transaction isolation.
%
% * resources(-Count)
% Number of resources that appear as subject or object in a
% triple. See rdf_resource/1.
%
% * properties(-Count)
% Number of current predicates. See rdf_current_predicate/1.
%
% * literals(-Count)
% Number of current literals. See rdf_current_literal/1.
%
% * gc(GCCount, ReclaimedTriples, ReindexedTriples, Time)
% Information about the garbage collector.
%
% * searched_nodes(-Count)
% Number of nodes expanded by rdf_reachable/3 and
% rdf_reachable/5.
%
% * lookup(rdf(S,P,O,G), Count)
% Number of queries that have been performed for this particular
% instantiation pattern. Each of S,P,O,G is either + or -.
% Fails in case the number of performed queries is zero.
%
% * hash_quality(rdf(S,P,O,G), Buckets, Quality, PendingResize)
% Statistics on the index for this pattern. Indices are created
% lazily on the first relevant query.
%
% * triples_by_graph(Graph, Count)
% This statistics is produced for each named graph. See
% =triples= for the interpretation of this value.
rdf_statistics(graphs(Count)) :-
rdf_statistics_(graphs(Count)).
rdf_statistics(triples(Count)) :-
rdf_statistics_(triples(Count)).
rdf_statistics(duplicates(Count)) :-
rdf_statistics_(duplicates(Count)).
rdf_statistics(lingering(Count)) :-
rdf_statistics_(lingering(Count)).
rdf_statistics(resources(Count)) :-
rdf_statistics_(resources(Count)).
rdf_statistics(properties(Count)) :-
rdf_statistics_(predicates(Count)).
rdf_statistics(literals(Count)) :-
rdf_statistics_(literals(Count)).
rdf_statistics(gc(Count, Reclaimed, Reindexed, Time)) :-
rdf_statistics_(gc(Count, Reclaimed, Reindexed, Time)).
rdf_statistics(searched_nodes(Count)) :-
rdf_statistics_(searched_nodes(Count)).
rdf_statistics(lookup(Index, Count)) :-
functor(Indexed, indexed, 16),
rdf_statistics_(Indexed),
index(Index, I),
Arg is I + 1,
arg(Arg, Indexed, Count),
Count \== 0.
rdf_statistics(hash_quality(Index, Size, Quality,Optimize)) :-
rdf_statistics_(hash_quality(List)),
member(hash(Place,Size,Quality,Optimize), List),
index(Index, Place).
rdf_statistics(triples_by_graph(Graph, Count)) :-
rdf_graph_(Graph, Count).
index(rdf(-,-,-,-), 0).
index(rdf(+,-,-,-), 1).
index(rdf(-,+,-,-), 2).
index(rdf(+,+,-,-), 3).
index(rdf(-,-,+,-), 4).
index(rdf(+,-,+,-), 5).
index(rdf(-,+,+,-), 6).
index(rdf(+,+,+,-), 7).
index(rdf(-,-,-,+), 8).
index(rdf(+,-,-,+), 9).
index(rdf(-,+,-,+), 10).
index(rdf(+,+,-,+), 11).
index(rdf(-,-,+,+), 12).
index(rdf(+,-,+,+), 13).
index(rdf(-,+,+,+), 14).
index(rdf(+,+,+,+), 15).
/*******************************
* PREDICATES *
*******************************/
%! rdf_current_predicate(?Predicate) is nondet.
%
% True when Predicate is a currently known predicate. Predicates
% are created if a triples is created that uses this predicate or
% a property of the predicate is set using rdf_set_predicate/2.
% The predicate may (no longer) have triples associated with it.
%
% Note that resources that have =|rdf:type|= =|rdf:Property|= are
% not automatically included in the result-set of this predicate,
% while _all_ resources that appear as the second argument of a
% triple _are_ included.
%
% @see rdf_predicate_property/2.
rdf_current_predicate(P, DB) :-
rdf_current_predicate(P),
( rdf(_,P,_,DB)
-> true
).
%! rdf_predicate_property(?Predicate, ?Property)