forked from SWI-Prolog/packages-semweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdf_db.pl
3529 lines (3057 loc) · 105 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): 1985-2013, University of Amsterdam
VU University Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- 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_current_prefix/2, % ?Alias, ?URI
rdf_register_prefix/2, % +Alias, +URI
rdf_register_prefix/3, % +Alias, +URI, +Options
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_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))
]).
:- expects_dialect(swi).
:- use_module(library(rdf)).
:- use_module(library(lists)).
:- use_module(library(shlib)).
:- use_module(library(gensym)).
:- use_module(library(sgml)).
:- use_module(library(sgml_write)).
:- use_module(library(option)).
:- use_module(library(error)).
:- use_module(library(uri)).
:- use_module(library(debug)).
:- use_module(library(apply)).
:- if(exists_source(library(thread))).
:- use_module(library(thread)).
:- endif.
:- use_module(library(semweb/rdf_cache)).
:- 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), filter(callable), min_count(nonneg)]).
:- predicate_options(rdf_load/2, 2,
[ base_uri(atom),
cache(boolean),
concurrent(positive_integer),
db(atom),
format(oneof([xml,triples,turtle])),
graph(atom),
if(oneof([true,changed,not_loaded])),
modified(-float),
silent(boolean),
register_namespaces(boolean)
]).
:- predicate_options(rdf_register_ns/3, 3, [force(boolean), keep(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)
]).
:- multifile
ns/2,
rdf_meta_specification/3. % UnboundHead, Module, Head
:- dynamic
ns/2. % ID, URL
:- discontiguous
term_expansion/2.
/** <module> Core RDF database
The file library(semweb/rdf_db) provides the core of the SWI-Prolog RDF
store.
*/
/*******************************
* PREFIXES *
*******************************/
%% rdf_current_prefix(?Alias, ?URI) is nondet.
%
% Query predefined prefixes and prefixes defined with
% rdf_register_ns/2.
rdf_current_prefix(Alias, URI) :-
ns(Alias, URI).
%% ns(?Alias, ?URI) is nondet.
%
% Dynamic and multifile predicate that maintains the registered
% namespace aliases.
%
% @deprecated New code must modify the namespace table using
% rdf_register_ns/3 and query using rdf_current_ns/2.
ns(rdf, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#').
ns(rdfs, 'http://www.w3.org/2000/01/rdf-schema#').
ns(owl, 'http://www.w3.org/2002/07/owl#').
ns(xsd, 'http://www.w3.org/2001/XMLSchema#').
ns(dc, 'http://purl.org/dc/elements/1.1/').
ns(dcterms, 'http://purl.org/dc/terms/').
ns(eor, 'http://dublincore.org/2000/03/13/eor#').
ns(skos, 'http://www.w3.org/2004/02/skos/core#').
ns(serql, 'http://www.openrdf.org/schema/serql#').
%% rdf_register_prefix(+Prefix, +URI) is det.
%% rdf_register_prefix(+Prefix, +URI, +Options) is det.
%
% Register Prefix as an abbreviation for URI. Options:
%
% * force(Boolean)
% If =true=, Replace existing namespace alias. Please note
% that replacing a namespace is dangerous as namespaces
% affect preprocessing. Make sure all code that depends on
% a namespace is compiled after changing the registration.
%
% * keep(Boolean)
% If =true= and Alias is already defined, keep the
% original message and succeed silently.
%
% Without options, an attempt to redefine an alias raises a
% permission error.
%
% Predefined prefixes are:
%
% | rdf | http://www.w3.org/1999/02/22-rdf-syntax-ns#' |
% | rdfs | http://www.w3.org/2000/01/rdf-schema#' |
% | owl | http://www.w3.org/2002/07/owl#' |
% | xsd | http://www.w3.org/2001/XMLSchema#' |
% | dc | http://purl.org/dc/elements/1.1/' |
% | dcterms | http://purl.org/dc/terms/' |
% | eor | http://dublincore.org/2000/03/13/eor#' |
% | skos | http://www.w3.org/2004/02/skos/core#' |
% | serql | http://www.openrdf.org/schema/serql#' |
rdf_register_prefix(Alias, URI) :-
rdf_register_prefix(Alias, URI, []).
rdf_register_prefix(Alias, URI, _) :-
ns(Alias, URI), !.
rdf_register_prefix(Alias, URI, Options) :-
ns(Alias, _), !,
( option(force(true), Options, false)
-> retractall(ns(Alias, _)),
assert(ns(Alias, URI))
; option(keep(true), Options, false)
-> true
; throw(error(permission_error(register, namespace, Alias),
context(_, 'Already defined')))
).
rdf_register_prefix(Alias, URI, _) :-
assert(ns(Alias, URI)).
%% rdf_current_ns(?Prefix, ?URI) is nondet.
%
% @deprecated. Use rdf_current_prefix/2.
rdf_current_ns(Prefix, URI) :-
rdf_current_prefix(Prefix, URI).
%% rdf_register_ns(?Prefix, ?URI) is det.
%
% @deprecated. Use rdf_register_prefix/2.
%% rdf_register_ns(?Prefix, ?URI, +Options) is det.
%
% @deprecated. Use rdf_register_prefix/3.
rdf_register_ns(Prefix, URI) :-
rdf_register_prefix(Prefix, URI).
rdf_register_ns(Prefix, URI, Options) :-
rdf_register_prefix(Prefix, URI, Options).
%% register_file_ns(+Map:list(pair)) is det.
%
% Register a namespace as encounted in the namespace list of an
% RDF document. We only register if both the abbreviation and URL
% are not already known. Is there a better way? This code could
% also do checks on the consistency of RDF and other well-known
% namespaces.
%
% @tbd Better error handling
register_file_ns([]) :- !.
register_file_ns([Decl|T]) :- !,
register_file_ns(Decl),
register_file_ns(T).
register_file_ns([]=_) :- !. % xmlns= (overall default)
register_file_ns(NS=URL) :- !, % compatibility
register_file_ns(NS-URL).
register_file_ns(NS-URL) :-
( ns(NS, URL)
-> true
; ns(NS, _)
-> true % redefined abbreviation
; ns(_, URL)
-> true % redefined URL
; rdf_register_ns(NS, URL)
).
%% rdf_global_id(?Id, ?GlobalId) is det.
%
% Convert between NS:Local and global atomic identifier.
% To be completed.
rdf_global_id(NS:Local, Global) :-
global(NS, Local, Global), !.
rdf_global_id(Global, Global).
%% rdf_global_object(+Object, -GlobalObject) is semidet.
%% rdf_global_object(-Object, +GlobalObject) is semidet.
%
% Same as rdf_global_id/2, but intended for dealing with the
% object part of a triple, in particular the type for typed
% literals.
%
% @error existence_error(rdf_namespace, NS)
rdf_global_object(NS:Local, Global) :-
global(NS, Local, Global), !.
rdf_global_object(literal(type(NS:Local, Value)),
literal(type(Global, Value))) :-
global(NS, Local, Global), !.
rdf_global_object(Global, Global).
global(NS, Local, Global) :-
( atom(Global)
-> ns(NS, Full),
atom_concat(Full, Local, Global)
; atom(NS), atom(Local)
-> ( ns(NS, Full)
*-> atom_concat(Full, Local, Global)
; current_prolog_flag(xref, true)
-> Global = NS:Local
; existence_error(rdf_namespace, NS)
)
).
%% rdf_global_term(+TermIn, -GlobalTerm) is det.
%
% Does rdf_global_id/2 on all terms NS:Local by recursively analysing
% the term.
rdf_global_term(Var, Var) :-
var(Var), !.
rdf_global_term(NS:Local, Global) :-
atom(NS), atom(Local), ns(NS, Full), !,
atom_concat(Full, Local, Global).
rdf_global_term([H0|T0], [H|T]) :- !,
rdf_global_term(H0, H),
rdf_global_term(T0, T).
rdf_global_term(Term0, Term) :-
compound(Term0), !,
Term0 =.. [H|L0],
rdf_global_term(L0, L),
Term =.. [H|L].
rdf_global_term(Term, Term).
%% rdf_global_graph(+TermIn, -GlobalTerm) is det.
%
% Preforms rdf_global_id/2 on rdf/4, etc graph arguments
rdf_global_graph(NS:Local, Global) :-
atom(NS), atom(Local), !,
global(NS, Local, Global).
rdf_global_graph(G, G).
/*******************************
* EXPANSION *
*******************************/
:- multifile
system:term_expansion/2,
system:goal_expansion/2.
system:term_expansion((:- rdf_meta(Heads)), Clauses) :-
prolog_load_context(module, M),
mk_clauses(Heads, M, Clauses).
mk_clauses((A,B), M, [H|T]) :- !,
mk_clause(A, M, H),
mk_clauses(B, M, T).
mk_clauses(A, M, [C]) :-
mk_clause(A, M, C).
mk_clause(Head0, M0, rdf_db:rdf_meta_specification(Unbound, Module, Head)) :-
strip_module(M0:Head0, Module, Head),
valid_rdf_meta_head(Head),
functor(Head, Name, Arity),
functor(Unbound, Name, Arity).
valid_rdf_meta_head(Head) :-
callable(Head), !,
Head =.. [_|Args],
valid_args(Args).
valid_rdf_meta_head(Head) :-
throw(error(type_error(callable, Head), _)).
valid_args([]).
valid_args([H|T]) :-
valid_arg(H), !,
valid_args(T).
valid_arg(:). % meta argument
valid_arg(+). % non-var
valid_arg(-). % var
valid_arg(?). % either var or non-var
valid_arg(@). % not modified
valid_arg(r). % RDF resource
valid_arg(o). % RDF object
valid_arg(t). % term with RDF resources
valid_arg(g). % graph argument
valid_arg(A) :-
throw(error(type_error(rdf_meta_argument, A), _)).
%% rdf_meta(+Heads)
%
% This directive defines the argument types of the named
% predicates, which will force compile time namespace expansion
% for these predicates. Heads is a coma-separated list of callable
% terms. Defined argument properties are:
%
% $ : :
% Argument is a goal. The goal is processed using expand_goal/2,
% recursively applying goal transformation on the argument.
%
% $ + :
% The argument is instantiated at entry. Nothing is changed.
%
% $ - :
% The argument is not instantiated at entry. Nothing is changed.
%
% $ ? :
% The argument is unbound or instantiated at entry. Nothing is
% changed.
%
% $ @ :
% The argument is not changed.
%
% $ r :
% The argument must be a resource. If it is a term
% <prefix>:<local> it is translated.
%
% $ o :
% The argument is an object or resource. See
% rdf_global_object/2.
%
% $ t :
% The argument is a term that must be translated. Expansion will
% translate all occurences of <prefix>:<local> appearing
% anywhere in the term. See rdf_global_term/2.
%
% As it is subject to term_expansion/2, the rdf_meta/1 declaration
% can only be used as a directive. The directive must be processed
% before the definition of the predicates as well as before
% compiling code that uses the rdf meta-predicates. The atom
% =rdf_meta= is declared as an operator exported from
% library(semweb/rdf_db). Files using rdf_meta/1 must explicitely
% load this library.
rdf_meta(Heads) :-
throw(error(context_error(nodirective, rdf_meta(Heads)), _)).
system:goal_expansion(G, Expanded) :-
rdf_meta_specification(G, _, _), !,
prolog_load_context(module, LM),
( rdf_meta_specification(G, Module, Spec),
right_module(LM, G, Module)
-> rdf_expand(G, Spec, Expanded)
; debugging(rdf_meta),
sub_term(G, NS:Local),
atom(NS), atom(Local)
-> print_message(warning, rdf(meta(not_expanded(LM:G)))),
fail
),
rdf_expand(G, Spec, Expanded).
system:term_expansion(Fact, Expanded) :-
rdf_meta_specification(Fact, Module, Spec),
prolog_load_context(module, Module), !,
rdf_expand(Fact, Spec, Expanded).
system:term_expansion((Head :- Body), (Expanded :- Body)) :-
rdf_meta_specification(Head, Module, Spec),
prolog_load_context(module, Module), !,
rdf_expand(Head, Spec, Expanded).
right_module(M, _, M) :- !.
right_module(LM, G, M) :-
predicate_property(LM:G, imported_from(M)).
rdf_expand(G, Spec, Expanded) :-
functor(G, Name, Arity),
functor(Expanded, Name, Arity),
rdf_expand_args(0, Arity, G, Spec, Expanded).
rdf_expand_args(Arity, Arity, _, _, _) :- !.
rdf_expand_args(I0, Arity, Goal, Spec, Expanded) :-
I is I0 + 1,
arg(I, Goal, GA),
arg(I, Spec, SA),
arg(I, Expanded, EA),
rdf_expand_arg(SA, GA, EA),
rdf_expand_args(I, Arity, Goal, Spec, Expanded).
rdf_expand_arg(r, A, E) :- !,
mk_global(A, E).
rdf_expand_arg(o, A, E) :- !,
rdf_global_object(A, E).
rdf_expand_arg(t, A, E) :- !,
rdf_global_term(A, E).
rdf_expand_arg(g, A, E) :- !,
rdf_global_graph(A, E).
rdf_expand_arg(:, A, E) :- !,
expand_goal(A, E).
rdf_expand_arg(_, A, A).
%% mk_global(+Src, -Resource)
%
% Realised rdf_global_id(+, -), but adds compiletime checking,
% notably to see whether a namespace is not yet defined.
mk_global(X, X) :-
var(X), !.
mk_global(X, X) :-
atom(X), !.
mk_global(NS:Local, Global) :-
must_be(atom, NS),
must_be(atom, Local),
( ns(NS, Full)
-> atom_concat(Full, Local, Global)
; current_prolog_flag(xref, true)
-> Global = NS:Local
; existence_error(namespace, NS)
).
:- 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(r,r),
rdf_source_location(r,-),
rdf_resource(r),
rdf_subject(r),
rdf_create_graph(r),
rdf_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
% wildcart *. 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.
%
% * plain(+Text)
% Perform exact match and demand the language or type qualifiers
% to match. This query is fully indexed.
%
% * exact(+Text)
% Perform exact, but case-insensitive match. This query is
% fully indexed.
%
% * 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 then Literal in the
% ordered set of literals.
%
% * le(+Literal)
% Match any literal that is equal or smaller then 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 breath-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.
%% 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.
%
% Replaces one of the three 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.
%% rdf_update(+Subject, +Predicate, +Object, +Graph, +Action) is det
%
% As rdf_update/4 but allows for specifying the graph.
/*******************************
* 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('__bnode', 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).
%
% @see rdf_bnode/1.
rdf_is_bnode(Id) :-
atom(Id),
sub_atom(Id, 0, _, _, '__').
%% 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
typed_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) :-