View source with raw comments or as raw
    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-2018, 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(rdf_io,
   36          [ write_table/2,      % +Row, +Options
   37            write_graph/2,      % +Triples, +Options
   38            get_triples/3       % +Input, -Triples, +Options
   39          ]).   40:- use_module(library('semweb/rdf_db')).   41:- use_module(library(rdf_write)).   42:- use_module(library(rdf)).   43:- use_module(library(lists)).   44
   45:- multifile
   46    write_table/4,          % +Format, +Serialization, +Rows, +Options
   47    write_graph/4,          % +Format, +Serialization, +Triples, +Options
   48    get_triples/4.          % +Format, +Input, -Triples, +Options
   49
   50/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   51This module acts as a dispatcher module,   allowing other modules to add
   52clauses  for  write_table/4  and  write_graph/4    and   thus  providing
   53additional output formats without modifications to the kernel source.
   54- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 write_table(+Rows, +Options)
Write a result-table in the specified format. Rows is a list of terms row(C1, C2, ...). Options specifies additional processing options. Defined options are:
result_format(+Format)
Specifies the output format. Defined formats depend on the loaded plugins. Passed as first argument to the write_table/4 hook. This option must be present.
serialization(+Serialization)
Specifies the serialization of the output. Passed as second argument to the write_table/4 hook. This option must be present.
variables(+Vars)
Specifies the names of the columns. Vars is a term with functor vars and atom-arguments describing the names of each subsequent column. For Example:
variables(vars('Name', 'Address'))

The output hooks may support additional options.

Arguments:
Rows- is a list of terms row(Col1, Col2, ..., ColN)
   85write_table(Rows, Options) :-
   86    needed_option(result_format(Format), Options),
   87    needed_option(serialization(Serialization), Options),
   88    write_table(Format, Serialization, Rows, Options).
 write_graph(+Triples, +Options)
Write a graph, represented as a list of rdf(S,P,O) triples. Options:
result_format(+Format)
Specifies the output format. Defined formats depend on the loaded plugins. Passed as first argument to the write_graph/4 hook.
serialization(+Serialization)
Specifies the serialization of the output. Passed as second argument to the write_table/4 hook. This option must be present.
  104write_graph(Triples, Options) :-
  105    needed_option(serialization(Serialization), Options),
  106    (   Serialization == rdfxml
  107    ->  (   memberchk(result_format(Format), Options)
  108        ->  true
  109        ;   Format = xml
  110        )
  111    ;   option(result_format(Format), Options)
  112    ->  true
  113    ;   Format = Serialization
  114    ),
  115    write_graph(Format, Serialization, Triples, Options).
  116
  117
  118                 /*******************************
  119                 *             READING          *
  120                 *******************************/
 get_triples(+Input, -Graph:list, +Options)
Read triples according to the option data_format. This predicate is plugable by get_triples/4.
  127get_triples(Input, Triples, Options0) :-
  128    select(data_format(Format), Options0, Options),
  129    get_triples(Format, Input, Triples, Options).
 get_triples(+Format, +Input, -Graph:list, +Options)
Hook to read triples into a list of rdf(S,P,O) for a given Format. The default implementation supports rdfxml by means of load_rdf/3.
  137get_triples(rdfxml, Input, Triples, Options) :-
  138    !,
  139    load_rdf(Input, Triples, Options).
  140
  141
  142                 /*******************************
  143                 *             HOOK             *
  144                 *******************************/
 write_table(+ResultFormat, +Serialization, +Triples, +Options)
Hook for write_table/2. There is no default implementation.
 write_graph(+ResultFormat, +Serialization, +Triples, +Options)
Hook for write_graph/2. The default implementation supports Format = xml and Serialization = rdfxml. It uses rdf_write_xml/2 to emit the graph.
  156write_graph(xml, rdfxml, Triples, Options) :-
  157    option(mimetype(Type), Options, 'application/rdf+xml'),
  158    format('Transfer-encoding: chunked~n'),
  159    format('Content-type: ~w; charset=UTF-8~n~n', [Type]),
  160    rdf_write_xml(current_output, Triples).
  161
  162
  163                 /*******************************
  164                 *              UTIL            *
  165                 *******************************/
  166
  167needed_option(Term, Options) :-
  168    memberchk(Term, Options),
  169    !.
  170needed_option(Term, _) :-
  171    functor(Term, Name, _),
  172    throw(error(existence_error(option, Name), _))