File: test_rdf.pl

package info (click to toggle)
swi-prolog 8.0.2%2Bdfsg-3%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72,036 kB
  • sloc: ansic: 349,612; perl: 306,654; java: 5,208; cpp: 4,436; sh: 3,042; ruby: 1,594; yacc: 845; makefile: 136; xml: 82; sed: 12; sql: 6
file content (337 lines) | stat: -rw-r--r-- 9,511 bytes parent folder | download | duplicates (5)
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
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2010-2012, University of 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(test_rdf,
          [ suite/1,                    % +Test-number
            test_dir/1,                 % +Directory
            test_file/1,                % +File
            time_file/1,                % +File
            passed/1,                   % +Test-numberOrFile
            test_rdf/0,                 % run whole suite
            show_ok/1                   % +Test
          ]).

:- multifile
    user:file_search_path/2.

user:file_search_path(library, .).
user:file_search_path(library, '../sgml').
user:file_search_path(library, '../clib').
user:file_search_path(library, '..').
user:file_search_path(foreign, '../sgml').
user:file_search_path(foreign, '../clib').
user:file_search_path(foreign, '../semweb').

:- use_module(library(sgml)).
:- use_module(library(semweb/rdf_compare)).
:- use_module(library(rdf_parser)).
:- use_module(library(rdf_triple)).
:- use_module(library(rdf)).
:- use_module(library(apply)).
:- use_module(library(pprint)).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Test file for the SWI-Prolog RDF parser.  Toplevel predicates:

        # test/0
        Run all tests from the `suite' directory and validate the
        the result if the correct result is stored in a .ok file.

        # suite(N)
        Run test on suite/t<N>.rdf, showing RDF, intermediate
        representation and triples on the console.

        # passed(N)
        Parse suite/t<N>.rdf and save the result in suite/t<N>.ok

The intention is to write  tests,  use   suite/1  to  make sure they are
parsed correctly and then run passed/1 to   save  the correct answer, so
running test/0 can validate all results.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

suite(N) :-
    atomic_list_concat(['suite/t', N, '.rdf'], File),
    test_file(File).

test_file(File) :-
    rdf_reset_ids,
    format('************* Test ~w ***~n', [File]),
    cat(File),
    load_structure(File,
                   [ RDFElement ],
                   [ dialect(xmlns),
                     space(sgml)
                   ]),
    rdf_start_file([], Cleanup),
    make_rdf_state([base_uri('http://test.org/test/')], State, _),
    xml_to_plrdf(RDFElement, RDF, State),
    rdf_end_file(Cleanup),
    format('============= Prolog term ==============~n', []),
    print_term(RDF, []),
    rdf_triples(RDF, Triples),
    format('============= Triples ==================~n', []),
    write_triples(Triples).

time_file(File) :-
    time(load_rdf(File, Triples)),
    length(Triples, Len),
    format('Created ~w triples~n', [Len]).

passed(Id) :-
    integer(Id),
    !,
    atomic_list_concat(['suite/t', Id, '.rdf'], File),
    passed(File).
passed(File) :-
    rdf_reset_ids,
    ok_file(File, OkFile),
    load_rdf(File, Triples),
    open(OkFile, write, Fd, [encoding(utf8)]),
    save_triples(Triples, Fd),
    close(Fd),
    length(Triples, N),
    format('Saved ~d triples to ~w~n', [N, OkFile]).

:- dynamic failed/1.

test_rdf :-
    test(load_rdf),
    test(process_rdf).

test(How) :-
    retractall(failed(_)),
    test_dir(suite, How),
    findall(F, failed(F), Failed),
    (   Failed == []
    ->  true
    ;   length(Failed, N),
        format('ERROR: ~w tests failed~n', [N]),
        fail
    ).


test_dir(Dir) :-
    test_dir(Dir, load_rdf).

test_dir(Dir, How) :-
    format('Tests from "~w" [~w]: ', [Dir, How]),
    atom_concat(Dir, '/*.rdf', Pattern),
    expand_file_name(Pattern, TestFiles),
    maplist(test(How), TestFiles),
    format(' done~n').

test(How, File) :-
    format('.'), flush_output,
    rdf_reset_ids,
    ok_file(File, OkFile),
    (   call(How, File, Triples)
    ->  (   catch(open(OkFile, read, Fd, [encoding(utf8)]), _, fail)
        ->  (   read_triples(Fd, OkTriples),
                close(Fd),
                rdf_equal_graphs(Triples, OkTriples, _Subst)
            ->  true
            ;   assert(failed(File)),
                format('~N~w: WRONG ANSWER~n', [File])
            )
        ;   format('~N~w: (no .ok file)~n', [File])
        )
    ;   assert(failed(File)),
        format('~N~w: PARSE FAILED~n', [File])
    ).

ok_file(File, OkFile) :-
    file_base_name(File, BaseFile),
    file_name_extension(Base, _, BaseFile),
    file_directory_name(File, Dir),
    atomic_list_concat([Dir, /, ok, /, Base, '.ok'], OkFile).


save_triples([], _).
save_triples([H|T], Fd) :-
    format(Fd, '~q.~n', [H]),
    save_triples(T, Fd).

read_triples(Fd, Terms) :-
    read(Fd, T0),
    read_triples(T0, Fd, Terms).

read_triples(end_of_file, _, []) :- !.
read_triples(rdf(S0,P0,O0), Fd, [rdf(S,P,O)|R]) :-
    global_ref(S0, S),
    global_ref(P0, P),
    global_obj(O0, O),
    read(Fd, T1),
    read_triples(T1, Fd, R).

global_ref(rdf:Local, Global) :-
    rdf_name_space(NS),
    !,
    atom_concat(NS, Local, Global).
global_ref(NS:Local, Global) :-
    !,
    atom_concat(NS, Local, Global).
global_ref(URI, URI).

global_obj(literal(X), literal(X)) :- !.
global_obj(Local, Global) :-
    global_ref(Local, Global).


write_triples([]) :- !.
write_triples([H|T]) :-
    !,
    write_triple(H),
    write_triples(T).

write_triple(Triple) :-
    is_rdf_triple(Triple),
    !,
    Triple = rdf(S,P,O),
    format('{~q, ~q, ~q}~n', [S,P,O]).
write_triple(Triple) :-
    format('@@@@@ Bad Triple: ~p~n', [Triple]),
    fail.

cat(File) :-
    open(File, read, Fd),
    copy_stream_data(Fd, user_output),
    close(Fd).

:- dynamic triple/1.

process_rdf(File, Triples) :-
    retractall(triple(_)),
    process_rdf(File, assert_triples, []),
    findall(T, retract(triple(T)), Triples).

assert_triples([], _).
assert_triples([H|T], Loc) :-
    assert(triple(H)),
    assert_triples(T, Loc).


                 /*******************************
                 *            VALIDATE          *
                 *******************************/

is_rdf_triple(rdf(Subject, Predicate, Object)) :-
    is_subject(Subject),
    is_predicate(Predicate),
    is_object(Object).

is_subject(0) :- !, fail.               % Variables
is_subject(URI) :- is_uri(URI), !.
is_subject(each(URI)) :- is_uri(URI), !.
is_subject(prefix(Pattern)) :-
    atom(Pattern),
    !.

is_predicate(0) :- !, fail.
is_predicate(rdf:RdfPred) :-
    !,
    is_rdf_predicate(RdfPred).
is_predicate(NS:Pred) :-
    !,
    atom(NS),
    atom(Pred).
is_predicate(Pred) :-
    atom(Pred).

is_object(0) :-
    !,
    fail.
is_object(literal(XML)) :-
    !,
    is_xml(XML).
is_object(rdf:RdfType) :-
    !,
    is_rdf_type(RdfType).
is_object(URI) :-
    is_uri(URI).

is_object(Subject) :-
    is_subject(Subject),
    !.
is_object(Pred) :-
    is_predicate(Pred),
    !.

is_uri(URI) :- atom(URI).

is_xml(_XML). % for now

is_rdf_predicate(RdfPred) :- atom(RdfPred).

is_rdf_type(RdfType) :- atom(RdfType).

                 /*******************************
                 *             UTIL             *
                 *******************************/

%       find_rdf(+XMLTerm, -RDFTerm)
%
%       If the document contains an embedded RDF term, return it, else
%       return the whole document.  The latter is a bit dubious, but good
%       for the purpose of this test-file

find_rdf(Term, RDFTerm) :-
    RDFTerm = element(NS:'RDF', _, _),
    term_member(RDFTerm, Term),
    !,
    (   rdf_name_space(NS)
    ->  true
    ;   assert(rdf_parser:rdf_name_space(NS)),
        assert(new_rdf_namespace(NS))
    ).
find_rdf(Term, Term).

term_member(X, X).
term_member(X, Compound) :-
    compound(Compound),
    arg(_, Compound, Arg),
    term_member(X, Arg).


                 /*******************************
                 *          SHOW DIAGRAM        *
                 *******************************/

show_ok(Test) :-
    ok_file(Test, File),
    open(File, read, Fd, [encoding(utf8)]),
    read_triples(Fd, OkTriples),
    close(Fd),
    new(D, rdf_diagram(string('Ok for %s', File))),
    send(D, triples, OkTriples),
    send(D, open).