-
Notifications
You must be signed in to change notification settings - Fork 15
/
rdf11.pl
2106 lines (1847 loc) · 62.7 KB
/
rdf11.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 and Wouter Beek
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2015-2018, 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(rdf11,
[ rdf/3, % ?S, ?P, ?O
rdf/4, % ?S, ?P, ?O, ?G
rdf_has/3, % ?S, ?P, ?O
rdf_has/4, % ?S, ?P, ?O, -RealP
rdf_update/4, % +S, +P, +O, +Action
rdf_update/5, % +S, +P, +O, +G, +Action
rdf_reachable/3, % ?S, ?P, ?O
rdf_reachable/5, % ?S, ?P, ?O, +MaxD, -D
rdf_assert/3, % +S, +P, +O
rdf_assert/4, % +S, +P, +O, ?G
rdf_retractall/3, % ?S, ?P, ?O
rdf_retractall/4, % ?S, ?P, ?O, ?G
{}/1, % +Where
rdf_where/1, % +Where
rdf_compare/3, % -Diff, +Left, +Right
rdf_term/1, % ?Term
rdf_literal/1, % ?Term
rdf_bnode/1, % ?Term
rdf_iri/1, % ?Term
rdf_name/1, % ?Term
rdf_is_iri/1, % @Term
rdf_is_bnode/1, % @Term
rdf_is_literal/1, % @Term
rdf_is_name/1, % @Term
rdf_is_object/1, % @Term
rdf_is_predicate/1, % @Term
rdf_is_subject/1, % @Term
rdf_is_term/1, % @Term
rdf_subject/1, % ?Term
rdf_predicate/1, % ?Term
rdf_object/1, % ?Term
rdf_node/1, % ?Term
rdf_create_bnode/1, % ?Term
rdf_canonical_literal/2, % +In, -Canonical
rdf_lexical_form/2, % +Literal, -Lexical
rdf_default_graph/1, % -Graph
rdf_default_graph/2, % -Old, +New
rdf_estimate_complexity/4, % ?S, ?P, ?O, -Estimate
rdf_assert_list/2, % +PrologList, ?RDFList
rdf_assert_list/3, % +PrologList, ?RDFList, +G
rdf_last/2, % +RDFList, ?Last
rdf_list/1, % ?RDFList
rdf_list/2, % +RDFList, -PrologList
rdf_length/2, % ?RDFList, ?Length
rdf_member/2, % ?Member, +RDFList
rdf_nextto/2, % ?X, ?Y
rdf_nextto/3, % ?X, ?Y, ?RdfList
rdf_nth0/3, % ?Index, +RDFList, ?X
rdf_nth1/3, % ?Index, +RDFList, ?X
rdf_retract_list/1, % +RDFList
op(110, xfx, @), % must be above .
op(650, xfx, ^^), % must be above :
op(1150, fx, rdf_meta)
]).
:- use_module(library(semweb/rdf_prefixes),
[ (rdf_meta)/1, op(_,_,rdf_meta)
]).
:- use_module(library(semweb/rdf_db),
[ rdf_transaction/2,
rdf_match_label/3,
rdf_equal/2,
rdf_is_bnode/1,
rdf_transaction/1
]).
:- autoload(library(apply),[partition/4]).
:- autoload(library(c14n2),[xml_write_canonical/3]).
:- use_module(library(debug),[assertion/1,debug/3]).
:- autoload(library(error),
[ must_be/2,
domain_error/2,
instantiation_error/1,
existence_error/2,
type_error/2,
is_of_type/2,
uninstantiation_error/1
]).
:- autoload(library(lists),[select/3,append/3]).
:- autoload(library(memfile),
[new_memory_file/1,open_memory_file/3,free_memory_file/1]).
:- autoload(library(sgml),
[ xsd_number_string/2,
xsd_time_string/3,
xml_is_dom/1,
load_xml/3,
load_html/3
]).
:- autoload(library(sgml_write),[html_write/3,xml_write/2]).
:- autoload(library(solution_sequences),[distinct/2]).
:- reexport(library(semweb/rdf_db),
except([ rdf/3,
rdf/4,
rdf_assert/3,
rdf_assert/4,
rdf_current_literal/1,
rdf_current_predicate/1,
rdf_has/3,
rdf_has/4,
rdf_update/4,
rdf_update/5,
rdf_reachable/3,
rdf_reachable/5,
rdf_retractall/3,
rdf_retractall/4,
rdf_node/1,
rdf_bnode/1,
rdf_is_literal/1,
rdf_is_resource/1,
rdf_literal_value/2,
rdf_compare/3,
rdf_estimate_complexity/4
])
).
/** <module> RDF 1.1 API
This library provides a new API on top of library(semweb/rdf_db). The
new API follows the RDF 1.1 terminology and notation as much as
possible. It runs on top of the old API, which implies that applications
can use the new API in one file and the other in another one. Once the
new API is considered stable and robust the old API will be deprecated.
In a nutshell, the following issues are addressed:
- Literals are now represented by Value^^Type or Text@Lang. Plain
literals no longer exist. Value is a Prolog representation of
the value for known types. In particular:
- xsd:double, xsd:float and xsd:decimal are represented by a Prolog
float
- Integer types are represented by a Prolog integer
- The date/time types are presented by Prolog terms
- Literal matching and comparison operations are represented as
Prolog _constraints_. This replaces the literal(+Search,-Value)
construct used by library(semweb/rdf_db). For example, the following
query returns literals with prefix "ams", exploiting the RDF literal
index.
==
{ prefix(Name, "ams") },
rdf(S,P,Name).
==
- Graphs are always identified by the graph name only, i.e., the
notation Graph:Line is no longer supported. If a graph name is an IRI
then RDF prefix notation can now be used.
- The enumeration and type-testing predicates are now more closely based
on the RDF 1.1 specification and use consistent naming.
@author Jan Wielemaker
@author Wouter Beek
@see https://github.com/SWI-Prolog/packages-semweb/wiki/Proposal-for-Semweb-library-redesign
@version 2016
*/
:- multifile
in_ground_type_hook/3, % +Type, +Input, -Lexical:atom
out_type_hook/3, % +Type, -Output, +Lexical:atom
invalid_lexical_form_hook/3. % +Type, +Lexical, -Prolog
:- meta_predicate
parse_partial_xml(3,+,-).
:- rdf_meta
rdf(r,r,o),
rdf(r,r,o,r),
rdf_assert(r,r,o),
rdf_assert(r,r,o,r),
rdf_has(r,r,o),
rdf_has(r,r,o,-),
rdf_update(r,r,o,t),
rdf_update(r,r,o,r,t),
rdf_reachable(r,r,o),
rdf_reachable(r,r,o,+,-),
rdf_retractall(r,r,o),
rdf_retractall(r,r,o,r),
{}(t),
rdf_where(t),
rdf_canonical_literal(o,-),
rdf_lexical_form(o,-),
rdf_compare(-,o,o),
rdf_iri(r),
rdf_is_iri(r),
rdf_is_literal(o),
rdf_is_name(o),
rdf_is_object(o),
rdf_is_predicate(r),
rdf_is_subject(r),
rdf_is_term(o),
rdf_term(o),
rdf_literal(o),
rdf_name(o),
rdf_object(o),
rdf_estimate_complexity(r,r,o,-),
rdf_assert_list(t,r),
rdf_assert_list(t,r,r),
rdf_last(r,o),
rdf_list(r),
rdf_list(r,-),
rdf_length(r,-),
rdf_member(o,r),
rdf_nextto(o,o),
rdf_nth0(?,r,o),
rdf_nth1(?,r,o),
rdf_retract_list(r).
%! rdf(?S, ?P, ?O) is nondet.
%! rdf(?S, ?P, ?O, ?G) is nondet.
%
% True if an RDF triple <S,P,O> exists, optionally in the graph G.
% The object O is either a resource (atom) or one of the terms
% listed below. The described types apply for the case where O is
% unbound. If O is instantiated it is converted according to the
% rules described with rdf_assert/3.
%
% Triples consist of the following three terms:
%
% - Blank nodes are encoded by atoms that start with `_:`.
% - IRIs appear in two notations:
% - Full IRIs are encoded by atoms that do not start with
% `_:`. Specifically, an IRI term is not required to follow
% the IRI standard grammar.
% - Abbreviated IRI notation that allows IRI prefix aliases
% that are registered by rdf_register_prefix/[2,3] to be
% used. Their notation is `Alias:Local`, where Alias and
% Local are atoms. Each abbreviated IRI is expanded by the
% system to a full IRI.
% - Literals appear in two notations:
% - String@Lang
% A language-tagged string, where String is a Prolog string
% and Lang is an atom.
% - Value^^Type
% A type qualified literal. For unknown types, Value is a
% Prolog string. If type is known, the Prolog representations
% from the table below are used.
%
% | **Datatype IRI** | **Prolog term** |
% |:----------------------|:--------------------------------|
% | xsd:float | float |
% | xsd:double | float |
% | xsd:decimal | float (1) |
% | xsd:integer | integer |
% | XSD integer sub-types | integer |
% | xsd:boolean | `true` or `false` |
% | xsd:date | date(Y,M,D) |
% | xsd:dateTime | date_time(Y,M,D,HH,MM,SS) (2,3) |
% | xsd:gDay | integer |
% | xsd:gMonth | integer |
% | xsd:gMonthDay | month_day(M,D) |
% | xsd:gYear | integer |
% | xsd:gYearMonth | year_month(Y,M) |
% | xsd:time | time(HH,MM,SS) (2) |
%
% Notes:
%
% (1) The current implementation of `xsd:decimal` values
% as floats is formally incorrect. Future versions
% of SWI-Prolog may introduce decimal as a subtype
% of rational.
%
% (2) `SS` fields denote the number of seconds. This can
% either be an integer or a float.
%
% (3) The `date_time` structure can have a 7th field that
% denotes the timezone offset *in seconds* as an
% integer.
%
% In addition, a _ground_ object value is translated into a
% properly typed RDF literal using rdf_canonical_literal/2.
%
% There is a fine distinction in how duplicate statements are
% handled in rdf/[3,4]: backtracking over rdf/3 will never return
% duplicate triples that appear in multiple graphs. rdf/4 will
% return such duplicate triples, because their graph term differs.
%
% @arg S is the subject term. It is either a blank node or IRI.
% @arg P is the predicate term. It is always an IRI.
% @arg O is the object term. It is either a literal, a blank
% node or IRI (except for `true` and `false` that denote the
% values of datatype XSD boolean).
% @arg G is the graph term. It is always an IRI.
%
% @see [Triple pattern querying](http://www.w3.org/TR/sparql11-query/#sparqlTriplePatterns)
% @see xsd_number_string/2 and xsd_time_string/3 are used to
% convert between lexical representations and Prolog terms.
rdf(S,P,O) :-
pre_object(O,O0,S,P),
rdf_db:rdf(S,P,O0),
post_object(O,O0).
rdf(S,P,O,G) :-
pre_object(O,O0,S,P),
pre_graph(G,G0),
rdf_db:rdf(S,P,O0,G0),
post_graph(G, G0),
post_object(O,O0).
%! rdf_has(?S, +P, ?O) is nondet.
%! rdf_has(?S, +P, ?O, -RealP) is nondet.
%
% Similar to rdf/3 and rdf/4, but P matches all predicates that
% are defined as an rdfs:subPropertyOf of P. This predicate also
% recognises the predicate properties `inverse_of` and
% `symmetric`. See rdf_set_predicate/2.
rdf_has(S,P,O) :-
pre_object(O,O0,S,P),
rdf_db:rdf_has(S,P,O0),
post_object(O,O0).
rdf_has(S,P,O,RealP) :-
pre_object(O,O0,S,P),
rdf_db:rdf_has(S,P,O0,RealP),
post_object(O,O0).
%! rdf_update(+S, +P, +O, ++Action) is det.
%! rdf_update(+S, +P, +O, +G, ++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.
%
% The argument matching Action must be ground. If this argument is
% equivalent to the current value, no action is performed. Otherwise,
% the requested action is performed on all matching triples. For
% example, all resources typed `rdfs:Class` can be changed to
% `owl:Class` using
%
% ```
% ?- rdf_update(_, rdf:type, rdfs:'Class',
% object(owl:'Class')).
% ```
%
% @error instantiation_error if Action or the matching argument is
% not ground.
% @error domain_error(rdf_update_action, Action) if Action is not
% one of the above terms.
rdf_update(S, P, O, Action) :-
rdf_update(S, P, O, _, Action).
rdf_update(S, P, O, G, Action) :-
must_be(ground, Action),
( update_column(Action, S,P,O,G, On)
-> must_be(ground, On),
arg(1, Action, Old),
( On == Old
-> true
; rdf_transaction(rdf_update_(S, P, O, G, Action), update)
)
; domain_error(rdf_update_action, Action)
).
update_column(subject(_), S,_,_,_, S).
update_column(predicate(_), _,P,_,_, P).
update_column(object(_), _,_,O,_, O).
update_column(graph(_), _,_,_,G, G).
rdf_update_(S1, P, O, G, subject(S2)) :-
!,
forall(rdf(S1, P, O, G),
( rdf_retractall(S1, P, O, G),
rdf_assert(S2, P, O, G)
)).
rdf_update_(S, P1, O, G, predicate(P2)) :-
!,
forall(rdf(S, P1, O, G),
( rdf_retractall(S, P1, O, G),
rdf_assert(S, P2, O, G)
)).
rdf_update_(S, P, O1, G, object(O2)) :-
!,
forall(rdf(S, P, O1, G),
( rdf_retractall(S, P, O1, G),
rdf_assert(S, P, O2, G)
)).
rdf_update_(S, P, O, G1, graph(G2)) :-
!,
forall(rdf(S, P, O, G1),
( rdf_retractall(S, P, O, G1),
rdf_assert(S, P, O, G2)
)).
%! rdf_reachable(?S, +P, ?O) is nondet.
%! rdf_reachable(?S, +P, ?O, +MaxD, -D) is nondet.
%
% True when O can be reached from S using the transitive closure
% of P. The predicate uses (the internals of) rdf_has/3 and thus
% matches both rdfs:subPropertyOf and the `inverse_of` and
% `symmetric` predicate properties. The version rdf_reachable/5
% maximizes the steps considered and returns the number of steps
% taken.
%
% If both S and O are given, these predicates are `semidet`. The
% number of steps D is minimal because the implementation uses
% _breadth first_ search.
rdf_reachable(S,P,O) :-
pre_object(O,O0,S,P),
rdf_db:rdf_reachable(S,P,O0),
post_object(O,O0).
rdf_reachable(S,P,O,MaxD,D) :-
pre_object(O,O0,S,P),
rdf_db:rdf_reachable(S,P,O0,MaxD,D),
post_object(O,O0).
%! rdf_assert(+S, +P, +O) is det.
%! rdf_assert(+S, +P, +O, +G) is det.
%
% Assert a new triple. If O is a literal, certain Prolog terms are
% translated to typed RDF literals. These conversions are
% described with rdf_canonical_literal/2.
%
% If a type is provided using Value^^Type syntax, additional
% conversions are performed. All types accept either an atom or
% Prolog string holding a valid RDF lexical value for the type and
% xsd:float and xsd:double accept a Prolog integer.
rdf_assert(S,P,O) :-
rdf_default_graph(G),
rdf_assert(S,P,O,G).
rdf_assert(S,P,O,G) :-
must_be(ground, O),
pre_ground_object(O,O0),
rdf_db:rdf_assert(S,P,O0,G).
%! rdf_retractall(?S, ?P, ?O) is nondet.
%! rdf_retractall(?S, ?P, ?O, ?G) is nondet.
%
% Remove all matching triples from the database. Matching is
% performed using the same rules as rdf/3. The call does not
% instantiate any of its arguments.
rdf_retractall(S,P,O) :-
pre_object(O,O0,S,P),
rdf_db:rdf_retractall(S,P,O0).
rdf_retractall(S,P,O,G) :-
pre_object(O,O0,S,P),
pre_graph(G,G0),
rdf_db:rdf_retractall(S,P,O0,G0).
%! rdf_compare(-Diff, +Left, +Right) is det.
%
% True if the RDF terms Left and Right are ordered according to
% the comparison operator Diff. The ordering is defines as:
%
% - Literal < BNode < IRI
% - For literals
% - Numeric < non-numeric
% - Numeric literals are ordered by value. If both are
% equal, floats are ordered before integers.
% - Other data types are ordered lexicographically.
% - BNodes and IRIs are ordered lexicographically.
%
% Note that this ordering is a complete ordering of RDF terms that
% is consistent with the partial ordering defined by SPARQL.
%
% @arg Diff is one of `<`, `=` or `>`
rdf_compare(Diff, Left, Right) :-
pre_ground_object(Left, Left0),
pre_ground_object(Right, Right0),
rdf_db:rdf_compare(Diff, Left0, Right0).
%! {}(+Where) is semidet.
%! rdf_where(+Where) is semidet.
%
% Formulate constraints on RDF terms, notably literals. These are
% intended to be used as illustrated below. RDF constraints are
% pure: they may be placed before, after or inside a graph pattern
% and, provided the code contains no _commit_ operations (!, ->),
% the semantics of the goal remains the same. Preferably,
% constraints are placed _before_ the graph pattern as they often
% help the RDF database to exploit its literal indexes. In the
% example below, the database can choose between using the subject
% and/or predicate hash or the ordered literal table.
%
% ==
% { Date >= "2000-01-01"^^xsd:date },
% rdf(S, P, Date)
% ==
%
% The following constraints are currently defined:
%
% - >, >=, ==, =<, <
% The comparison operators are defined between numbers (of any
% recognised type), typed literals of the same type and
% langStrings of the same language.
% - prefix(String, Pattern)
% - substring(String, Pattern)
% - word(String, Pattern)
% - like(String, Pattern)
% - icase(String, Pattern)
% Text matching operators that act on both typed literals
% and langStrings.
% - lang_matches(Term, Pattern)
% Demands a full RDF term (Text@Lang) or a plain `Lang` term
% to match the language pattern Pattern.
%
% The predicates rdf_where/1 and {}/1 are identical. The
% rdf_where/1 variant is provided to avoid ambiguity in
% applications where {}/1 is used for other purposes. Note that it
% is also possible to write `rdf11:{...}`.
{}(Where) :-
rdf_where(Where).
rdf_where(Var) :-
var(Var),
!,
instantiation_error(Var).
rdf_where((A,B)) :-
!,
rdf_where(A),
rdf_where(B).
rdf_where(Constraint) :-
rdf_constraint(Constraint, Goal),
!,
call(Goal).
rdf_where(Constraint) :-
existence_error(rdf_constraint, Constraint).
% Comparison operators
rdf_constraint(Term >= Value,
add_value_constraint(Term, >=, Value)).
rdf_constraint(Term > Value,
add_value_constraint(Term, >, Value)).
rdf_constraint(Term == Value,
add_value_constraint(Term, ==, Value)).
rdf_constraint(Term < Value,
add_value_constraint(Term, <, Value)).
rdf_constraint(Term =< Value,
add_value_constraint(Term, =<, Value)).
% String selection
rdf_constraint(prefix(Term, Pattern),
add_text_constraint(Term, prefix(PatternA))) :-
atom_string(PatternA, Pattern).
rdf_constraint(substring(Term, Pattern),
add_text_constraint(Term, substring(PatternA))) :-
atom_string(PatternA, Pattern).
rdf_constraint(word(Term, Pattern),
add_text_constraint(Term, word(PatternA))) :-
atom_string(PatternA, Pattern).
rdf_constraint(like(Term, Pattern),
add_text_constraint(Term, like(PatternA))) :-
atom_string(PatternA, Pattern).
rdf_constraint(icase(Term, Pattern),
add_text_constraint(Term, icase(PatternA))) :-
atom_string(PatternA, Pattern).
% Lang selection
rdf_constraint(lang_matches(Term, Pattern),
add_lang_constraint(Term, lang_matches(Pattern))).
add_text_constraint(Var, Cond) :-
var(Var),
!,
( get_attr(Var, rdf11, Cond0)
-> put_attr(Var, rdf11, [Cond|Cond0])
; put_attr(Var, rdf11, [Cond])
).
add_text_constraint(Text^^_Type, Cond) :-
!,
add_text_constraint(Text, Cond).
add_text_constraint(Text@_Lang, Cond) :-
!,
add_text_constraint(Text, Cond).
add_text_constraint(Var, Cond) :-
eval_condition(Cond, Var).
%! add_lang_constraint(?Term, +Constraint)
%
% Add a constraint on the language of a literal
add_lang_constraint(Var, Constraint) :-
var(Var),
!,
( get_attr(Var, rdf11, Cond0)
-> put_attr(Var, rdf11, [Constraint|Cond0])
; put_attr(Var, rdf11, [Constraint])
).
add_lang_constraint(_Text@Lang, Constraint) :-
!,
add_lang_constraint(Lang, Constraint).
add_lang_constraint(_Text^^_Type, _Constraint) :-
!,
fail.
add_lang_constraint(Term, Constraint) :-
eval_condition(Constraint, Term).
%! add_value_constraint(?Term, +Constraint, +Value)
%
% Apply a value constraint to the RDF Term.
add_value_constraint(Term, Constraint, ValueIn) :-
constraint_literal_value(ValueIn, Value),
add_value_constraint_cann(Value, Constraint, Term).
constraint_literal_value(Value, Value^^_Type) :-
number(Value),
!.
constraint_literal_value(Value, Literal) :-
rdf_canonical_literal(Value, Literal).
add_value_constraint_cann(RefVal^^Type, Constraint, Term) :-
var(Term), var(Type),
!,
add_text_constraint(Term, value(Constraint, RefVal, Type)).
add_value_constraint_cann(RefVal^^Type, Constraint, Val^^Type2) :-
!,
Type = Type2,
add_text_constraint(Val, value(Constraint, RefVal, Type)).
add_value_constraint_cann(RefVal@Lang, Constraint, Val@Lang) :-
!,
add_text_constraint(Val, value(Constraint, RefVal, lang(Lang))).
add_value_constraint_cann(RefVal^^Type, Constraint, Val) :-
!,
ground(Val),
Val \= _@_,
eval_condition(value(Constraint, RefVal, Type), Val).
put_cond(Var, []) :-
!,
del_attr(Var, rdf11).
put_cond(Var, List) :-
put_attr(Var, rdf11, List).
eval_condition(Cond, Literal) :-
text_condition(Cond),
!,
text_of(Literal, Text),
text_condition(Cond, Text).
eval_condition(Cond, Literal) :-
lang_condition(Cond),
!,
lang_of(Literal, Lang),
lang_condition(Cond, Lang).
eval_condition(value(Comp, Ref, _Type), Value) :-
( number(Ref)
-> number(Value),
compare_numeric(Comp, Ref, Value)
; compare_std(Comp, Ref, Value)
).
compare_numeric(<, Ref, Value) :- Value < Ref.
compare_numeric(=<, Ref, Value) :- Value =< Ref.
compare_numeric(==, Ref, Value) :- Value =:= Ref.
compare_numeric(>=, Ref, Value) :- Value >= Ref.
compare_numeric( >, Ref, Value) :- Value > Ref.
compare_std(<, Ref, Value) :- Value @< Ref.
compare_std(=<, Ref, Value) :- Value @=< Ref.
compare_std(==, Ref, Value) :- Value == Ref.
compare_std(>=, Ref, Value) :- Value @>= Ref.
compare_std( >, Ref, Value) :- Value @> Ref.
text_condition(prefix(_)).
text_condition(substring(_)).
text_condition(word(_)).
text_condition(like(_)).
text_condition(icase(_)).
text_of(Literal, Text) :-
atomic(Literal),
!,
Text = Literal.
text_of(Text@_Lang, Text).
text_of(Text^^_Type, Text).
text_condition(prefix(Pattern), Text) :-
rdf_match_label(prefix, Pattern, Text).
text_condition(substring(Pattern), Text) :-
rdf_match_label(substring, Pattern, Text).
text_condition(word(Pattern), Text) :-
rdf_match_label(word, Pattern, Text).
text_condition(like(Pattern), Text) :-
rdf_match_label(like, Pattern, Text).
text_condition(icase(Pattern), Text) :-
rdf_match_label(icase, Pattern, Text).
lang_condition(lang_matches(_)).
lang_of(_Text@Lang0, Lang) :-
!,
Lang = Lang0.
lang_of(Lang, Lang) :-
atom(Lang).
lang_condition(lang_matches(Pattern), Lang) :-
rdf_db:lang_matches(Lang, Pattern).
%! literal_condition(+Object, -Cond) is semidet.
%
% True when some of the constraints on Object can be translated
% into an equivalent query of the form literal(Cond, _Value).
% Translated constraints are removed from object.
literal_condition(Object, Cond) :-
get_attr(Object, rdf11, Cond0),
best_literal_cond(Cond0, Cond, Rest),
put_cond(Object, Rest).
%! best_literal_cond(+Conditions, -Best, -Rest) is semidet.
%
% Extract the constraints that can be translated into the _Search_
% of literal(Search, Value).
%
% @tbd Select the best rather than the first.
best_literal_cond(Conditions, Best, Rest) :-
sort(Conditions, Unique),
best_literal_cond2(Unique, Best, Rest).
best_literal_cond2(Conds, Best, Rest) :-
select(Cond, Conds, Rest0),
rdf10_cond(Cond, Best, Rest0, Rest),
!.
rdf10_cond(value(=<, URef, UType), Cond, Rest0, Rest) :-
( select(value(>=, LRef, LType), Rest0, Rest)
-> true
; memberchk(value(>, LRef, LType), Rest0)
-> Rest = Rest0
),
!,
in_constaint_type(LType, SLType, LRef, LRef0),
in_constaint_type(UType, SUType, URef, URef0),
Cond = between(type(SLType, LRef0), type(SUType, URef0)).
rdf10_cond(value(<, URef, UType), Cond, Rest0, Rest) :-
( select(value(>=, LRef, LType), Rest0, Rest1)
-> true
; memberchk(value(>, LRef, LType), Rest0)
-> Rest1 = Rest0
),
!,
Rest = [value(<, URef, UType)|Rest1],
in_constaint_type(LType, SLType, LRef, LRef0),
in_constaint_type(UType, SUType, URef, URef0),
Cond = between(type(SLType, LRef0), type(SUType, URef0)).
rdf10_cond(value(Cmp, Ref, Type), Pattern, Rest, Rest) :-
!,
rdf10_compare(Cmp, Ref, Type, Pattern).
rdf10_cond(lang_matches(_), _, _, _) :- !, fail.
rdf10_cond(Cond, Cond, Rest, Rest).
rdf10_compare(Cmp, Ref, Type, Pattern) :-
nonvar(Type), Type = lang(Lang),
!,
atom_string(Ref0, Ref),
rdf10_lang_cond(Cmp, Ref0, Lang, Pattern).
rdf10_compare(Cmp, Ref, Type, Pattern) :-
in_constaint_type(Type, SType, Ref, Ref0),
rdf10_type_cond(Cmp, Ref0, SType, Pattern).
rdf10_lang_cond( <, Ref, Lang, lt(lang(Lang,Ref))).
rdf10_lang_cond(=<, Ref, Lang, le(lang(Lang,Ref))).
rdf10_lang_cond(==, Ref, Lang, eq(lang(Lang,Ref))).
rdf10_lang_cond(>=, Ref, Lang, ge(lang(Lang,Ref))).
rdf10_lang_cond(>, Ref, Lang, gt(lang(Lang,Ref))).
rdf10_type_cond( <, Ref, Type, lt(type(Type,Ref))).
rdf10_type_cond(=<, Ref, Type, le(type(Type,Ref))).
rdf10_type_cond(==, Ref, Type, eq(type(Type,Ref))).
rdf10_type_cond(>=, Ref, Type, ge(type(Type,Ref))).
rdf10_type_cond( >, Ref, Type, gt(type(Type,Ref))).
%! in_constaint_type(?Type, -SType, ++Val, -Val0)
in_constaint_type(Type, SType, Val, Val0) :-
nonvar(Type), ground(Val),
!,
SType = Type,
in_ground_type(Type, Val, Val0).
in_constaint_type(Type, SType, Val, Val0) :-
var(Type), number(Val),
!,
( integer(Val)
-> rdf_equal(SType, xsd:integer),
in_ground_type(xsd:integer, Val, Val0)
; float(Val)
-> rdf_equal(SType, xsd:double),
in_ground_type(xsd:double, Val, Val0)
; assertion(fail)
).
%! literal_class(+Term, -Class)
%
% Classify Term as literal and if possible as lang or typed
% literal on the basis of the constraints that apply to it.
literal_class(Term, Class) :-
get_attr(Term, rdf11, Conds),
select(Cond, Conds, Rest),
lang_condition(Cond),
!,
Term = Text@Lang,
put_attr(Lang, rdf11, [Cond]),
put_cond(Text, Rest),
( var(Text)
-> true
; atom_string(Text2, Text)
),
Class = lang(Lang, Text2).
%! attr_unify_hook(+AttributeValue, +Value)
attr_unify_hook(Cond, Value) :-
get_attr(Value, rdf11, Cond2),
!,
append(Cond, Cond2, CondJ),
sort(CondJ, Unique),
put_cond(Value, Unique).
attr_unify_hook(Cond, Text^^_Type) :-
var(Text),
!,
put_cond(Text, Cond).
attr_unify_hook(Cond, Text@Lang) :-
var(Text), var(Lang),
!,
partition(lang_condition, Cond, LangCond, TextCond),
put_cond(Text, TextCond),
put_cond(Lang, LangCond).
attr_unify_hook(Cond, Value) :-
sort(Cond, Unique),
propagate_conditions(Unique, Value).
propagate_conditions([], _).
propagate_conditions([H|T], Val) :-
propagate_condition(H, Val),
propagate_conditions(T, Val).
propagate_condition(value(Comp, Ref, Type), Value) :-
!,
( Value = Plain^^VType
-> VType = Type
; Plain = Value
),
cond_compare(Comp, Ref, Plain).
propagate_condition(lang_matches(Pattern), Value) :-
!,
( Value = _@Lang
-> true
; Lang = Value
),
rdf_db:lang_matches(Lang, Pattern).
propagate_condition(Cond, Value) :-
Cond =.. [Name|Args],
Constraint =.. [Name,Value|Args],
rdf_constraint(Constraint, Continuation),
call(Continuation).
cond_compare(>, Ref, Value) :- Value @> Ref.
cond_compare(>=, Ref, Value) :- Value @>= Ref.
cond_compare(==, Ref, Value) :- Value == Ref.
cond_compare(=<, Ref, Value) :- Value @=< Ref.
cond_compare( <, Ref, Value) :- Value @< Ref.
%! rdf_default_graph(-Graph) is det.
%! rdf_default_graph(-Old, +New) is det.
%
% Query/set the notion of the default graph. The notion of the
% default graph is local to a thread. Threads created inherit the
% default graph from their creator. See set_prolog_flag/2.
:- create_prolog_flag(rdf_default_graph, default,
[ type(atom),
keep(true)
]).
rdf_default_graph(Graph) :-
current_prolog_flag(rdf_default_graph, Graph).
rdf_default_graph(Old, New) :-
current_prolog_flag(rdf_default_graph, Old),
( New == Old
-> true
; set_prolog_flag(rdf_default_graph, New)
).
pre_graph(G, _G0) :-
var(G),
!.
pre_graph(G, G) :-
atom(G),
!.
pre_graph(G, _) :-
type_error(rdf_graph, G).
post_graph(G, G0:_) :-
!,
G = G0.
post_graph(G, G).
% left for code calling this directly
pre_object(Atom, URI) :-
pre_object(Atom, URI, _, _).
%! pre_object(+APIObject, -DBObject, +APISubject, +APIPredicate)
pre_object(Atom, URI, _, _) :-
atom(Atom),
\+ boolean(Atom),
!,
URI = Atom.
pre_object(Var, Var1, Subj, Pred) :-
var(Var),
!,
( literal_condition(Var, Cond)
-> Var1 = literal(Cond, _)
; literal_class(Var, Value)
-> Var1 = literal(Value)
; ( Var == Subj
-> Var1 = Subj
; true
),
( Var == Pred
-> Var1 = Pred
; true
)
).
pre_object(Val@Lang, Var1, _, _) :-
!,
( literal_condition(Val, Cond)
-> Var1 = literal(Cond, lang(Lang, _))
; literal_class(Val@Lang, Class)
-> Var1 = literal(Class)
; in_lang_string(Val, Val0),
Var1 = literal(lang(Lang, Val0))
).
pre_object(Val^^Type, Var1, _, _) :-
!,