View source with raw comments or as raw
    1/*  $Id$
    2
    3    Part of SWI-Prolog
    4
    5    Author:        Jan Wielemaker
    6    E-mail:        jan@swi.psy.uva.nl
    7    WWW:           http://www.swi-prolog.org
    8    Copyright (C): 1985-2004, University of Amsterdam
    9
   10    This program is free software; you can redistribute it and/or
   11    modify it under the terms of the GNU General Public License
   12    as published by the Free Software Foundation; either version 2
   13    of the License, or (at your option) any later version.
   14
   15    This program is distributed in the hope that it will be useful,
   16    but WITHOUT ANY WARRANTY; without even the implied warranty of
   17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18    GNU General Public License for more details.
   19
   20    You should have received a copy of the GNU Lesser General Public
   21    License along with this library; if not, write to the Free Software
   22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23
   24    As a special exception, if you link this library with other files,
   25    compiled with a Free Software compiler, to produce an executable, this
   26    library does not by itself cause the resulting executable to be covered
   27    by the GNU General Public License. This exception does not however
   28    invalidate any other reasons why the executable file might be covered by
   29    the GNU General Public License.
   30*/
   31
   32:- module(rdf_io,
   33	  [ write_table/2,	% +Row, +Options
   34	    write_graph/2,	% +Triples, +Options
   35	    get_triples/3	% +Input, -Triples, +Options
   36	  ]).   37:- use_module(library('semweb/rdf_db')).   38:- use_module(library(rdf_write)).   39:- use_module(library(rdf)).   40:- use_module(library(lists)).   41
   42:- multifile
   43	write_table/4,		% +Format, +Serialization, +Rows, +Options
   44	write_graph/4,		% +Format, +Serialization, +Triples, +Options
   45	get_triples/4.		% +Format, +Input, -Triples, +Options
   46
   47/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   48This module acts as a dispatcher module,   allowing other modules to add
   49clauses  for  write_table/4  and  write_graph/4    and   thus  providing
   50additional output formats without modifications to the kernel source.
   51- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 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)
   82write_table(Rows, Options) :-
   83	needed_option(result_format(Format), Options),
   84	needed_option(serialization(Serialization), Options),
   85	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.
  101write_graph(Triples, Options) :-
  102	needed_option(serialization(Serialization), Options),
  103	(   Serialization == rdfxml
  104	->  (   memberchk(result_format(Format), Options)
  105	    ->	true
  106	    ;	Format = xml
  107	    )
  108	;   option(result_format(Format), Options)
  109	->  true
  110	;   Format = Serialization
  111	),
  112	write_graph(Format, Serialization, Triples, Options).
  113
  114
  115		 /*******************************
  116		 *	       READING		*
  117		 *******************************/
 get_triples(+Input, -Graph:list, +Options)
Read triples according to the option data_format. This predicate is plugable by get_triples/4.
  124get_triples(Input, Triples, Options0) :-
  125	select(data_format(Format), Options0, Options),
  126	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.
  134get_triples(rdfxml, Input, Triples, Options) :- !,
  135	load_rdf(Input, Triples, Options).
  136
  137
  138		 /*******************************
  139		 *	       HOOK		*
  140		 *******************************/
 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.
  152write_graph(xml, rdfxml, Triples, Options) :-
  153	option(mimetype(Type), Options, 'application/rdf+xml'),
  154	format('Transfer-encoding: chunked~n'),
  155	format('Content-type: Type; charset=UTF-8~n~n', [Type]),
  156	rdf_write_xml(current_output, Triples).
  157
  158
  159		 /*******************************
  160		 *		UTIL		*
  161		 *******************************/
  162
  163needed_option(Term, Options) :-
  164	memberchk(Term, Options), !.
  165needed_option(Term, _) :-
  166	functor(Term, Name, _),
  167	throw(error(existence_error(option, Name), _))