annotate jamendo/sparql-archived/SeRQL/rdf_io.pl @ 27:d95e683fbd35 tip

Enable CORS on urispace redirects as well
author Chris Cannam
date Tue, 20 Feb 2018 14:52:02 +0000
parents df9685986338
children
rev   line source
Chris@0 1 /* $Id$
Chris@0 2
Chris@0 3 Part of SWI-Prolog
Chris@0 4
Chris@0 5 Author: Jan Wielemaker
Chris@0 6 E-mail: jan@swi.psy.uva.nl
Chris@0 7 WWW: http://www.swi-prolog.org
Chris@0 8 Copyright (C): 1985-2004, University of Amsterdam
Chris@0 9
Chris@0 10 This program is free software; you can redistribute it and/or
Chris@0 11 modify it under the terms of the GNU General Public License
Chris@0 12 as published by the Free Software Foundation; either version 2
Chris@0 13 of the License, or (at your option) any later version.
Chris@0 14
Chris@0 15 This program is distributed in the hope that it will be useful,
Chris@0 16 but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 18 GNU General Public License for more details.
Chris@0 19
Chris@0 20 You should have received a copy of the GNU Lesser General Public
Chris@0 21 License along with this library; if not, write to the Free Software
Chris@0 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Chris@0 23
Chris@0 24 As a special exception, if you link this library with other files,
Chris@0 25 compiled with a Free Software compiler, to produce an executable, this
Chris@0 26 library does not by itself cause the resulting executable to be covered
Chris@0 27 by the GNU General Public License. This exception does not however
Chris@0 28 invalidate any other reasons why the executable file might be covered by
Chris@0 29 the GNU General Public License.
Chris@0 30 */
Chris@0 31
Chris@0 32 :- module(rdf_io,
Chris@0 33 [ write_table/2, % +Row, +Options
Chris@0 34 write_graph/2, % +Triples, +Options
Chris@0 35 load_triples/2, % +Input, +Options
Chris@0 36 get_triples/3 % +Input, -Triples, +Options
Chris@0 37 ]).
Chris@0 38 :- use_module(library('semweb/rdf_db')).
Chris@0 39 :- use_module(library(rdf_write)).
Chris@0 40 :- use_module(library(rdf)).
Chris@0 41 :- use_module(library(lists)).
Chris@0 42
Chris@0 43 :- multifile
Chris@0 44 write_table/4, % +Format, +Serialization, +Rows, +Options
Chris@0 45 write_graph/4, % +Format, +Serialization, +Triples, +Options
Chris@0 46 load_triples/3, % +Format, +Input, +Options
Chris@0 47 get_triples/4. % +Format, +Input, -Triples, +Options
Chris@0 48
Chris@0 49 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Chris@0 50 This module acts as a dispatcher module, allowing other modules to add
Chris@0 51 clauses for write_table/4 and write_graph/4 and thus providing
Chris@0 52 additional output formats without modifications to the kernel source.
Chris@0 53 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
Chris@0 54
Chris@0 55 %% write_table(+Rows, +Options)
Chris@0 56 %
Chris@0 57 % Write a result-table in the specified format. Rows is a list of
Chris@0 58 % terms row(C1, C2, ...). Options specifies additional processing
Chris@0 59 % options. Defined options are:
Chris@0 60 %
Chris@0 61 % * variables(+Vars)
Chris@0 62 % Specifies the names of the columns. Vars is a term with
Chris@0 63 % functor =vars= and atom-arguments describing the names
Chris@0 64 % of each subsequent column. For Example:
Chris@0 65 %
Chris@0 66 % ==
Chris@0 67 % variables(vars('Name', 'Address'))
Chris@0 68 % ==
Chris@0 69
Chris@0 70
Chris@0 71 write_table(Rows, Options) :-
Chris@0 72 needed_option(result_format(Format), Options),
Chris@0 73 needed_option(serialization(Serialization), Options),
Chris@0 74 write_table(Format, Serialization, Rows, Options).
Chris@0 75
Chris@0 76
Chris@0 77 write_graph(Triples, Options) :-
Chris@0 78 needed_option(serialization(Serialization), Options),
Chris@0 79 ( Serialization == rdfxml
Chris@0 80 -> ( memberchk(result_format(Format), Options)
Chris@0 81 -> true
Chris@0 82 ; Format = xml
Chris@0 83 )
Chris@0 84 ; needed_option(result_format(Format), Options)
Chris@0 85 ),
Chris@0 86 write_graph(Format, Serialization, Triples, Options).
Chris@0 87
Chris@0 88
Chris@0 89 /*******************************
Chris@0 90 * READING *
Chris@0 91 *******************************/
Chris@0 92
Chris@0 93 load_triples(Input, Options0) :-
Chris@0 94 select(data_format(Format), Options0, Options),
Chris@0 95 load_triples(Format, Input, Options).
Chris@0 96
Chris@0 97
Chris@0 98 load_triples(rdfxml, Input, Options) :- !,
Chris@0 99 rdf_load(Input, Options).
Chris@0 100
Chris@0 101 get_triples(Input, Triples, Options0) :-
Chris@0 102 select(data_format(Format), Options0, Options),
Chris@0 103 get_triples(Format, Input, Triples, Options).
Chris@0 104
Chris@0 105
Chris@0 106 get_triples(rdfxml, Input, Triples, Options) :- !,
Chris@0 107 load_rdf(Input, Triples, Options).
Chris@0 108
Chris@0 109
Chris@0 110 /*******************************
Chris@0 111 * HOOK *
Chris@0 112 *******************************/
Chris@0 113
Chris@0 114 %% write_graph(+ResultFormat, +Serialization, +Triples, +Options)
Chris@0 115 %
Chris@0 116 % Provide hook for rdf_io.pl plugin interface
Chris@0 117
Chris@0 118 write_graph(xml, rdfxml, Triples, _Options) :-
Chris@0 119 format('Content-type: application/rdf+xml~n~n'),
Chris@0 120 rdf_write_xml(current_output, Triples).
Chris@0 121
Chris@0 122 /*******************************
Chris@0 123 * UTIL *
Chris@0 124 *******************************/
Chris@0 125
Chris@0 126 needed_option(Term, Options) :-
Chris@0 127 memberchk(Term, Options), !.
Chris@0 128 needed_option(Term, _) :-
Chris@0 129 functor(Term, Name, _),
Chris@0 130 throw(error(existence_error(option, Name), _)).