File: rdf_triple.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (448 lines) | stat: -rw-r--r-- 12,598 bytes parent folder | download | duplicates (3)
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
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2010-2013, 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(rdf_triple,
          [ rdf_triples/2,              % +Parsed, -Tripples
            rdf_triples/3,              % +Parsed, -Tripples, +Tail
            rdf_reset_ids/0,            % Reset gensym id's
            rdf_start_file/2,           % +Options, -Cleanup
            rdf_end_file/1,             % +Cleanup
            anon_prefix/1               % Prefix for anonynmous resources
          ]).
:- autoload(library(gensym),[gensym/2,reset_gensym/1]).
:- autoload(library(option),[option/3,option/2]).
:- autoload(library(rdf_parser),[rdf_name_space/1]).
:- autoload(library(uri),[iri_normalized/2]).


:- predicate_options(rdf_start_file/2, 1,
                     [ base_uri(atom),
                       blank_nodes(oneof([share,noshare]))
                     ]).

/** <module> Create triples from intermediate representation

Convert the output of xml_to_rdf/3  from   library(rdf)  into  a list of
triples of the format described   below. The intermediate representation
should be regarded a proprietary representation.

        rdf(Subject, Predicate, Object).

Where `Subject' is

        * Atom
        The subject is a resource

        * each(URI)
        URI is the URI of an RDF Bag

        * prefix(Pattern)
        Pattern is the prefix of a fully qualified Subject URI

And `Predicate' is

        * Atom
        The predicate is always a resource

And `Object' is

        * Atom
        URI of Object resource

        * literal(Value)
        Literal value (Either a single atom or parsed XML data)
*/

%!  rdf_triples(+Term, -Triples) is det.
%!  rdf_triples(+Term, -Tridpples, +Tail) is det.
%
%   Convert an object as parsed by rdf.pl into a list of rdf/3
%   triples.  The identifier of the main object created is returned
%   by rdf_triples/3.
%
%   Input is the `content' of the RDF element in the format as
%   generated by load_structure(File, Term, [dialect(xmlns)]).
%   rdf_triples/3 can process both individual descriptions as
%   well as the entire content-list of an RDF element.  The first
%   mode is suitable when using library(sgml) in `call-back' mode.

rdf_triples(RDF, Tripples) :-
    rdf_triples(RDF, Tripples, []).

rdf_triples([]) -->
    !,
    [].
rdf_triples([H|T]) -->
    !,
    rdf_triples(H),
    rdf_triples(T).
rdf_triples(Term) -->
    triples(Term, _).

%!  triples(-Triples, -Id, +In, -Tail)
%
%   DGC set processing the output of  xml_to_rdf/3. Id is unified to
%   the identifier of the main description.

triples(description(Type, About, Props), Subject) -->
    { var(About),
      share_blank_nodes(true)
    },
    !,
    (   { shared_description(description(Type, Props), Subject)
        }
    ->  []
    ;   { make_id('_:Description', Id)
        },
        triples(description(Type, about(Id), Props), Subject),
        { assert_shared_description(description(Type, Props), Subject)
        }
    ).
triples(description(description, IdAbout, Props), Subject) -->
    !,
    { description_id(IdAbout, Subject)
    },
    properties(Props, Subject).
triples(description(TypeURI, IdAbout, Props), Subject) -->
    { description_id(IdAbout, Subject)
    },
    properties([ rdf:type = TypeURI
               | Props
               ], Subject).
triples(unparsed(Data), Id) -->
    { make_id('_:Error', Id),
      print_message(error, rdf(unparsed(Data)))
    },
    [].


                 /*******************************
                 *          DESCRIPTIONS        *
                 *******************************/

:- thread_local
    node_id/2,                      % nodeID --> ID
    unique_id/1.                    % known rdf:ID

rdf_reset_node_ids :-
    retractall(node_id(_,_)),
    retractall(unique_id(_)).

description_id(Id, Id) :-
    var(Id),
    !,
    make_id('_:Description', Id).
description_id(about(Id), Id).
description_id(id(Id), Id) :-
    (   unique_id(Id)
    ->  print_message(error, rdf(redefined_id(Id)))
    ;   assert(unique_id(Id))
    ).
description_id(each(Id), each(Id)).
description_id(prefix(Id), prefix(Id)).
description_id(node(NodeID), Id) :-
    (   node_id(NodeID, Id)
    ->  true
    ;   make_id('_:Node', Id),
        assert(node_id(NodeID, Id))
    ).

properties(PlRDF, Subject) -->
    properties(PlRDF, 1, [], [], Subject).

properties([], _, Bag, Bag, _) -->
    [].
properties([H0|T0], N, Bag0, Bag, Subject) -->
    property(H0, N, NN, Bag0, Bag1, Subject),
    properties(T0, NN, Bag1, Bag, Subject).

%!  property(Property, N, NN, Subject)// is det.
%
%   Generate triples for {Subject,  Pred,   Object}.  Also generates
%   triples for Object if necessary.
%
%   @param Property One of
%
%           * Pred = Object
%           Used for normal statements
%           * id(Id, Pred = Object)
%           Used for reified statements

property(Pred0 = Object, N, NN, BagH, BagT, Subject) --> % inlined object
    triples(Object, Id),
    !,
    { li_pred(Pred0, Pred, N, NN)
    },
    statement(Subject, Pred, Id, _, BagH, BagT).
property(Pred0 = collection(Elems), N, NN, BagH, BagT, Subject) -->
    !,
    { li_pred(Pred0, Pred, N, NN)
    },
    statement(Subject, Pred, Object, _Id, BagH, BagT),
    collection(Elems, Object).
property(Pred0 = Object, N, NN, BagH, BagT, Subject) -->
    !,
    { li_pred(Pred0, Pred, N, NN)
    },
    statement(Subject, Pred, Object, _Id, BagH, BagT).
property(id(Id, Pred0 = Object), N, NN, BagH, BagT, Subject) -->
    triples(Object, ObjectId),
    !,
    { li_pred(Pred0, Pred, N, NN)
    },
    statement(Subject, Pred, ObjectId, Id, BagH, BagT).
property(id(Id, Pred0 = collection(Elems)), N, NN, BagH, BagT, Subject) -->
    !,
    { li_pred(Pred0, Pred, N, NN)
    },
    statement(Subject, Pred, Object, Id, BagH, BagT),
    collection(Elems, Object).
property(id(Id, Pred0 = Object), N, NN, BagH, BagT, Subject) -->
    { li_pred(Pred0, Pred, N, NN)
    },
    statement(Subject, Pred, Object, Id, BagH, BagT).

%!  statement(+Subject, +Pred, +Object, +Id, +BagH, -BagT)
%
%   Add a statement to the model. If nonvar(Id), we reinify the
%   statement using the given Id.

statement(Subject, Pred, Object, Id, BagH, BagT) -->
    rdf(Subject, Pred, Object),
    {   BagH = [Id|BagT]
    ->  statement_id(Id)
    ;   BagT = BagH
    },
    (   { nonvar(Id)
        }
    ->  rdf(Id, rdf:type, rdf:'Statement'),
        rdf(Id, rdf:subject, Subject),
        rdf(Id, rdf:predicate, Pred),
        rdf(Id, rdf:object, Object)
    ;   []
    ).


statement_id(Id) :-
    nonvar(Id),
    !.
statement_id(Id) :-
    make_id('_:Statement', Id).

%!  li_pred(+Pred, -Pred, +Nth, -NextNth)
%
%   Transform rdf:li predicates into _1, _2, etc.

li_pred(rdf:li, rdf:Pred, N, NN) :-
    !,
    NN is N + 1,
    atom_concat('_', N, Pred).
li_pred(Pred, Pred, N, N).

%!  collection(+Elems, -Id)
%
%   Handle the elements of a collection and return the identifier
%   for the whole collection in Id.

collection([], Nil) -->
    { global_ref(rdf:nil, Nil)
    }.
collection([H|T], Id) -->
    triples(H, HId),
    { make_id('_:List', Id)
    },
    rdf(Id, rdf:type, rdf:'List'),
    rdf(Id, rdf:first, HId),
    rdf(Id, rdf:rest, TId),
    collection(T, TId).


rdf(S0, P0, O0) -->
    { global_ref(S0, S),
      global_ref(P0, P),
      global_obj(O0, O)
    },
    [ rdf(S, P, O) ].


global_ref(In, Out) :-
    (   nonvar(In),
        In = NS:Local
    ->  (   NS == rdf,
            rdf_name_space(RDF)
        ->  atom_concat(RDF, Local, Out)
        ;   atom_concat(NS, Local, Out0),
            iri_normalized(Out0, Out)
        )
    ;   Out = In
    ).

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


                 /*******************************
                 *             SHARING          *
                 *******************************/

:- thread_local
    shared_description/3,           % +Hash, +Term, -Subject
    share_blank_nodes/1,            % Boolean
    shared_nodes/1.                 % counter

reset_shared_descriptions :-
    retractall(shared_description(_,_,_)),
    retractall(shared_nodes(_)).

shared_description(Term, Subject) :-
    term_hash(Term, Hash),
    shared_description(Hash, Term, Subject),
    (   retract(shared_nodes(N))
    ->  N1 is N + 1
    ;   N1 = 1
    ),
    assert(shared_nodes(N1)).


assert_shared_description(Term, Subject) :-
    term_hash(Term, Hash),
    assert(shared_description(Hash, Term, Subject)).


                 /*******************************
                 *            START/END         *
                 *******************************/

%!  rdf_start_file(+Options, -Cleanup) is det.
%
%   Initialise for the translation of a file.

rdf_start_file(Options, Cleanup) :-
    rdf_reset_node_ids,             % play safe
    reset_shared_descriptions,
    set_bnode_sharing(Options, C1),
    set_anon_prefix(Options, C2),
    add_cleanup(C1, C2, Cleanup).

%!  rdf_end_file(:Cleanup) is det.
%
%   Cleanup reaching the end of an RDF file.

rdf_end_file(Cleanup) :-
    rdf_reset_node_ids,
    (   shared_nodes(N)
    ->  print_message(informational, rdf(shared_blank_nodes(N)))
    ;   true
    ),
    reset_shared_descriptions,
    Cleanup.

set_bnode_sharing(Options, erase(Ref)) :-
    option(blank_nodes(Share), Options, noshare),
    (   Share == share
    ->  assert(share_blank_nodes(true), Ref), !
    ;   Share == noshare
    ->  fail                        % next clause
    ;   throw(error(domain_error(share, Share), _))
    ).
set_bnode_sharing(_, true).

set_anon_prefix(Options, erase(Ref)) :-
    option(base_uri(BaseURI), Options),
    nonvar(BaseURI),
    !,
    (   BaseURI == []
    ->  AnonBase = '_:'
    ;   atomic_list_concat(['_:', BaseURI, '#'], AnonBase)
    ),
    asserta(anon_prefix(AnonBase), Ref).
set_anon_prefix(_, true).

add_cleanup(true, X, X) :- !.
add_cleanup(X, true, X) :- !.
add_cleanup(X, Y, (X, Y)).


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

%!  anon_prefix(-Prefix) is semidet.
%
%   If defined, it is the prefix used to generate a blank node.

:- thread_local
    anon_prefix/1.

make_id(For, ID) :-
    anon_prefix(Prefix),
    !,
    atom_concat(Prefix, For, Base),
    gensym(Base, ID).
make_id(For, ID) :-
    gensym(For, ID).

anon_base('_:Description').
anon_base('_:Statement').
anon_base('_:List').
anon_base('_:Node').

%!  rdf_reset_ids is det.
%
%   Utility predicate to reset the gensym counters for the various
%   generated identifiers.  This simplifies debugging and matching
%   output with the stored desired output (see rdf_test.pl).

rdf_reset_ids :-
    anon_prefix(Prefix),
    !,
    (   anon_base(Base),
        atom_concat(Prefix, Base, X),
        reset_gensym(X),
        fail
    ;   true
    ).
rdf_reset_ids :-
    (   anon_base(Base),
        reset_gensym(Base),
        fail
    ;   true
    ).