forked from SWI-Prolog/packages-RDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_write.pl
181 lines (153 loc) · 5.03 KB
/
test_write.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2007, University of 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 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(test_rdf_write,
[ test_write/0,
run_tests/0,
run_tests/1
]).
:- asserta(user:file_search_path(foreign, '../sgml')).
:- asserta(user:file_search_path(foreign, '../semweb')).
:- asserta(user:file_search_path(foreign, '../clib')).
:- asserta(user:file_search_path(library, '..')).
:- asserta(user:file_search_path(library, '../sgml')).
:- asserta(user:file_search_path(library, '.')).
:- asserta(user:file_search_path(library, '../plunit')).
:- asserta(user:file_search_path(library, '../clib')).
:- use_module(library(plunit)).
:- use_module(library(rdf_write)).
:- use_module(library(sgml)).
:- use_module(library(lists)).
:- use_module(library(debug)).
:- use_module(library(semweb/rdf_db)).
:- use_module(rdf).
test_write :-
run_tests([ rdf_write
]).
/*******************************
* ROUND TRIP *
*******************************/
test_graph(Triples) :-
tmp_file(rdf, Tmp),
open(Tmp, write, Out, [encoding(utf8)]),
rdf_write_xml(Out, Triples),
close(Out),
cat(Tmp),
load_rdf(Tmp, ReadTriples),
delete_file(Tmp),
compare_triples(Triples, ReadTriples, _).
cat(File) :-
debugging(rdf_write), !,
open(File, read, In, [encoding(utf8)]),
copy_stream_data(In, current_output),
close(In).
cat(_).
/*******************************
* COMPARING *
*******************************/
% compare_triples(+PlRDF, +NTRDF, -Substitions)
%
% Compare two models and if they are equal, return a list of
% PlID = NTID, mapping NodeID elements.
compare_triples(A, B, Substitutions) :-
compare_list(A, B, [], Substitutions), !.
compare_list([], [], S, S).
compare_list([H1|T1], In2, S0, S) :-
select(H2, In2, T2),
compare_triple(H1, H2, S0, S1),
compare_list(T1, T2, S1, S).
compare_triple(rdf(Subj1,P1,O1), rdf(Subj2, P2, O2), S0, S) :-
compare_field(Subj1, Subj2, S0, S1),
compare_field(P1, P2, S1, S2),
compare_field(O1, O2, S2, S).
compare_field(X, X, S, S) :- !.
compare_field(literal(X), xml(X), S, S) :- !. % TBD
compare_field(rdf:Name, Atom, S, S) :-
atom(Atom),
rdf_parser:rdf_name_space(NS),
atom_concat(NS, Name, Atom), !.
compare_field(NS:Name, Atom, S, S) :-
atom(Atom),
atom_concat(NS, Name, Atom), !.
compare_field(X, Id, S, S) :-
memberchk(X=Id, S), !.
compare_field(X, Y, S, [X=Y|S]) :-
\+ memberchk(X=_, S),
rdf_is_bnode(X),
rdf_is_bnode(Y),
debug(bnode, 'Assume ~w = ~w~n', [X, Y]).
/*******************************
* TESTS *
*******************************/
:- begin_tests(rdf_write).
test(1, true) :-
test_graph([ rdf(s, p, o)
]).
test(anon_s, true) :-
test_graph([ rdf('__s', p, o)
]).
test(anon_o, true) :-
test_graph([ rdf(s, p, '__o')
]).
test(anon_loop, blocked('NodeID map must check for cycles')) :-
test_graph([ rdf('__r1', p1, '__r2'),
rdf('__r2', p1, '__r1')
]).
test(anon_loop, true) :-
test_graph([ rdf('__r1', p1, '__r2'),
rdf('__r1', p2, '__r2'),
rdf('__r2', p1, '__r1'),
rdf('__r2', p2, '__r1')
]).
test(anon_reuse, true) :-
test_graph([ rdf('__s1', p1, '__o1'),
rdf('__s2', p1, '__o1')
]).
test(anon_reuse, true) :-
test_graph([ rdf('__s1', p1, '__o1'),
rdf('__s2', p1, '__o1'),
rdf('__o1', name, literal(foo))
]).
test(literal, true) :-
test_graph([ rdf(s, p, literal(hello))
]).
test(lang, true) :-
test_graph([ rdf(s, p, literal(lang(en, hello)))
]).
test(type, true) :-
test_graph([ rdf(s, p, literal(type(t, hello)))
]).
test(iri_l1, true) :-
R = 'http://www.example.com/één_twee#r',
test_graph([ rdf(R,R,R)
]).
test(iri_amp, true) :-
R = 'http://www.example.com/een&twee#r',
test_graph([ rdf(R,R,R)
]).
test(iri_space, true) :-
R = 'http://www.example.com/een%20twee#r',
test_graph([ rdf(R,R,R)
]).
:- end_tests(rdf_write).