1/*  Part of SWI-Prolog
2
3    Author:        Jan Wielemaker
4    E-mail:        J.Wielemaker@vu.nl
5    WWW:           http://www.swi-prolog.org
6    Copyright (c)  2010-2011, University of Amsterdam
7    All rights reserved.
8
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions
11    are met:
12
13    1. Redistributions of source code must retain the above copyright
14       notice, this list of conditions and the following disclaimer.
15
16    2. Redistributions in binary form must reproduce the above copyright
17       notice, this list of conditions and the following disclaimer in
18       the documentation and/or other materials provided with the
19       distribution.
20
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32    POSSIBILITY OF SUCH DAMAGE.
33*/
34
35:- module(test_rdf_write,
36          [ test_write/0,
37            run_tests/0,
38            run_tests/1
39          ]).
40
41:- asserta(user:file_search_path(foreign, '../sgml')).
42:- asserta(user:file_search_path(foreign, '../semweb')).
43:- asserta(user:file_search_path(foreign, '../clib')).
44:- asserta(user:file_search_path(library, '..')).
45:- asserta(user:file_search_path(library, '../sgml')).
46:- asserta(user:file_search_path(library, '.')).
47:- asserta(user:file_search_path(library, '../plunit')).
48:- asserta(user:file_search_path(library, '../clib')).
49
50:- use_module(library(plunit)).
51:- use_module(library(rdf_write)).
52:- use_module(library(sgml)).
53:- use_module(library(lists)).
54:- use_module(library(debug)).
55:- use_module(library(semweb/rdf_db)).
56:- use_module(library(rdf)).
57
58test_write :-
59    run_tests([ rdf_write
60              ]).
61
62
63                 /*******************************
64                 *          ROUND TRIP          *
65                 *******************************/
66
67test_graph(Triples) :-
68    tmp_file(rdf, Tmp),
69    open(Tmp, write, Out, [encoding(utf8)]),
70    rdf_write_xml(Out, Triples),
71    close(Out),
72    cat(Tmp),
73    load_rdf(Tmp, ReadTriples),
74    delete_file(Tmp),
75    compare_triples(Triples, ReadTriples, _).
76
77cat(File) :-
78    debugging(rdf_write),
79    !,
80    open(File, read, In, [encoding(utf8)]),
81    copy_stream_data(In, current_output),
82    close(In).
83cat(_).
84
85                 /*******************************
86                 *           COMPARING          *
87                 *******************************/
88
89%       compare_triples(+PlRDF, +NTRDF, -Substitions)
90%
91%       Compare two models and if they are equal, return a list of
92%       PlID = NTID, mapping NodeID elements.
93
94
95compare_triples(A, B, Substitutions) :-
96    compare_list(A, B, [], Substitutions),
97    !.
98
99compare_list([], [], S, S).
100compare_list([H1|T1], In2, S0, S) :-
101    select(H2, In2, T2),
102    compare_triple(H1, H2, S0, S1),
103    compare_list(T1, T2, S1, S).
104
105compare_triple(rdf(Subj1,P1,O1), rdf(Subj2, P2, O2), S0, S) :-
106    compare_field(Subj1, Subj2, S0, S1),
107    compare_field(P1, P2, S1, S2),
108    compare_field(O1, O2, S2, S).
109
110compare_field(X, X, S, S) :- !.
111compare_field(literal(X), xml(X), S, S) :- !. % TBD
112compare_field(rdf:Name, Atom, S, S) :-
113    atom(Atom),
114    rdf_parser:rdf_name_space(NS),
115    atom_concat(NS, Name, Atom),
116    !.
117compare_field(NS:Name, Atom, S, S) :-
118    atom(Atom),
119    atom_concat(NS, Name, Atom),
120    !.
121compare_field(X, Id, S, S) :-
122    memberchk(X=Id, S),
123    !.
124compare_field(X, Y, S, [X=Y|S]) :-
125    \+ memberchk(X=_, S),
126    rdf_is_bnode(X),
127    rdf_is_bnode(Y),
128    debug(bnode, 'Assume ~w = ~w~n', [X, Y]).
129
130
131                 /*******************************
132                 *            TESTS             *
133                 *******************************/
134
135:- begin_tests(rdf_write).
136
137test(1, true) :-
138    test_graph([ rdf(s, p, o)
139               ]).
140test(anon_s, true) :-
141    test_graph([ rdf('_:s', p, o)
142               ]).
143test(anon_o, true) :-
144    test_graph([ rdf(s, p, '_:o')
145               ]).
146test(anon_loop, blocked('NodeID map must check for cycles')) :-
147    test_graph([ rdf('_:r1', p1, '_:r2'),
148                 rdf('_:r2', p1, '_:r1')
149               ]).
150test(anon_loop, true) :-
151    test_graph([ rdf('_:r1', p1, '_:r2'),
152                 rdf('_:r1', p2, '_:r2'),
153                 rdf('_:r2', p1, '_:r1'),
154                 rdf('_:r2', p2, '_:r1')
155               ]).
156test(anon_reuse, true) :-
157    test_graph([ rdf('_:s1', p1, '_:o1'),
158                 rdf('_:s2', p1, '_:o1')
159               ]).
160test(anon_reuse, true) :-
161    test_graph([ rdf('_:s1', p1, '_:o1'),
162                 rdf('_:s2', p1, '_:o1'),
163                 rdf('_:o1', name, literal(foo))
164               ]).
165test(literal, true) :-
166    test_graph([ rdf(s, p, literal(hello))
167               ]).
168test(lang, true) :-
169    test_graph([ rdf(s, p, literal(lang(en, hello)))
170               ]).
171test(type, true) :-
172    test_graph([ rdf(s, p, literal(type(t, hello)))
173               ]).
174test(iri_l1, true) :-
175    R = 'http://www.example.com/één_twee#r',
176    test_graph([ rdf(R,R,R)
177               ]).
178test(iri_amp, true) :-
179    R = 'http://www.example.com/een&twee#r',
180    test_graph([ rdf(R,R,R)
181               ]).
182test(iri_space, true) :-
183    R = 'http://www.example.com/een%20twee#r',
184    test_graph([ rdf(R,R,R)
185               ]).
186
187:- end_tests(rdf_write).
188
189
190