Chris@0: /* $Id$ Chris@0: Chris@0: Part of SWI-Prolog Chris@0: Chris@0: Author: Jan Wielemaker Chris@0: E-mail: jan@swi.psy.uva.nl Chris@0: WWW: http://www.swi-prolog.org Chris@0: Copyright (C): 1985-2004, University of Amsterdam Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License Chris@0: as published by the Free Software Foundation; either version 2 Chris@0: of the License, or (at your option) any later version. Chris@0: Chris@0: This program is distributed in the hope that it will be useful, Chris@0: but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: GNU General Public License for more details. Chris@0: Chris@0: You should have received a copy of the GNU Lesser General Public Chris@0: License along with this library; if not, write to the Free Software Chris@0: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@0: Chris@0: As a special exception, if you link this library with other files, Chris@0: compiled with a Free Software compiler, to produce an executable, this Chris@0: library does not by itself cause the resulting executable to be covered Chris@0: by the GNU General Public License. This exception does not however Chris@0: invalidate any other reasons why the executable file might be covered by Chris@0: the GNU General Public License. Chris@0: */ Chris@0: Chris@0: :- module(rdf_io, Chris@0: [ write_table/2, % +Row, +Options Chris@0: write_graph/2, % +Triples, +Options Chris@0: load_triples/2, % +Input, +Options Chris@0: get_triples/3 % +Input, -Triples, +Options Chris@0: ]). Chris@0: :- use_module(library('semweb/rdf_db')). Chris@0: :- use_module(library(rdf_write)). Chris@0: :- use_module(library(rdf)). Chris@0: :- use_module(library(lists)). Chris@0: Chris@0: :- multifile Chris@0: write_table/4, % +Format, +Serialization, +Rows, +Options Chris@0: write_graph/4, % +Format, +Serialization, +Triples, +Options Chris@0: load_triples/3, % +Format, +Input, +Options Chris@0: get_triples/4. % +Format, +Input, -Triples, +Options Chris@0: Chris@0: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Chris@0: This module acts as a dispatcher module, allowing other modules to add Chris@0: clauses for write_table/4 and write_graph/4 and thus providing Chris@0: additional output formats without modifications to the kernel source. Chris@0: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ Chris@0: Chris@0: %% write_table(+Rows, +Options) Chris@0: % Chris@0: % Write a result-table in the specified format. Rows is a list of Chris@0: % terms row(C1, C2, ...). Options specifies additional processing Chris@0: % options. Defined options are: Chris@0: % Chris@0: % * variables(+Vars) Chris@0: % Specifies the names of the columns. Vars is a term with Chris@0: % functor =vars= and atom-arguments describing the names Chris@0: % of each subsequent column. For Example: Chris@0: % Chris@0: % == Chris@0: % variables(vars('Name', 'Address')) Chris@0: % == Chris@0: Chris@0: Chris@0: write_table(Rows, Options) :- Chris@0: needed_option(result_format(Format), Options), Chris@0: needed_option(serialization(Serialization), Options), Chris@0: write_table(Format, Serialization, Rows, Options). Chris@0: Chris@0: Chris@0: write_graph(Triples, Options) :- Chris@0: needed_option(serialization(Serialization), Options), Chris@0: ( Serialization == rdfxml Chris@0: -> ( memberchk(result_format(Format), Options) Chris@0: -> true Chris@0: ; Format = xml Chris@0: ) Chris@0: ; needed_option(result_format(Format), Options) Chris@0: ), Chris@0: write_graph(Format, Serialization, Triples, Options). Chris@0: Chris@0: Chris@0: /******************************* Chris@0: * READING * Chris@0: *******************************/ Chris@0: Chris@0: load_triples(Input, Options0) :- Chris@0: select(data_format(Format), Options0, Options), Chris@0: load_triples(Format, Input, Options). Chris@0: Chris@0: Chris@0: load_triples(rdfxml, Input, Options) :- !, Chris@0: rdf_load(Input, Options). Chris@0: Chris@0: get_triples(Input, Triples, Options0) :- Chris@0: select(data_format(Format), Options0, Options), Chris@0: get_triples(Format, Input, Triples, Options). Chris@0: Chris@0: Chris@0: get_triples(rdfxml, Input, Triples, Options) :- !, Chris@0: load_rdf(Input, Triples, Options). Chris@0: Chris@0: Chris@0: /******************************* Chris@0: * HOOK * Chris@0: *******************************/ Chris@0: Chris@0: %% write_graph(+ResultFormat, +Serialization, +Triples, +Options) Chris@0: % Chris@0: % Provide hook for rdf_io.pl plugin interface Chris@0: Chris@0: write_graph(xml, rdfxml, Triples, _Options) :- Chris@0: format('Content-type: application/rdf+xml~n~n'), Chris@0: rdf_write_xml(current_output, Triples). Chris@0: Chris@0: /******************************* Chris@0: * UTIL * Chris@0: *******************************/ Chris@0: Chris@0: needed_option(Term, Options) :- Chris@0: memberchk(Term, Options), !. Chris@0: needed_option(Term, _) :- Chris@0: functor(Term, Name, _), Chris@0: throw(error(existence_error(option, Name), _)).