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- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 55 56%! write_table(+Rows, +Options) 57% 58% Write a result-table in the specified format. Rows is a list of 59% terms row(C1, C2, ...). Options specifies additional processing 60% options. Defined options are: 61% 62% * result_format(+Format) 63% Specifies the output format. Defined formats depend on 64% the loaded plugins. Passed as first argument to the 65% write_table/4 hook. This option *must* be present. 66% 67% * serialization(+Serialization) 68% Specifies the serialization of the output. Passed as second 69% argument to the write_table/4 hook. This option *must* be present. 70% 71% * variables(+Vars) 72% Specifies the names of the columns. Vars is a term with 73% functor =vars= and atom-arguments describing the names 74% of each subsequent column. For Example: 75% 76% == 77% variables(vars('Name', 'Address')) 78% == 79% 80% The output hooks may support additional options. 81% 82% @param Rows is a list of terms row(Col1, Col2, ..., ColN) 83 84 85write_table(Rows, Options) :- 86 needed_option(result_format(Format), Options), 87 needed_option(serialization(Serialization), Options), 88 write_table(Format, Serialization, Rows, Options). 89 90%! write_graph(+Triples, +Options) 91% 92% Write a graph, represented as a list of rdf(S,P,O) triples. 93% Options: 94% 95% * result_format(+Format) 96% Specifies the output format. Defined formats depend on 97% the loaded plugins. Passed as first argument to the 98% write_graph/4 hook. 99% 100% * serialization(+Serialization) 101% Specifies the serialization of the output. Passed as second 102% argument to the write_table/4 hook. This option *must* be present. 103 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 *******************************/ 121 122%! get_triples(+Input, -Graph:list, +Options) 123% 124% Read triples according to the option data_format. This predicate 125% is plugable by get_triples/4. 126 127get_triples(Input, Triples, Options0) :- 128 select(data_format(Format), Options0, Options), 129 get_triples(Format, Input, Triples, Options). 130 131%! get_triples(+Format, +Input, -Graph:list, +Options) 132% 133% Hook to read triples into a list of rdf(S,P,O) for a given 134% Format. The default implementation supports =rdfxml= by means of 135% load_rdf/3. 136 137get_triples(rdfxml, Input, Triples, Options) :- 138 !, 139 load_rdf(Input, Triples, Options). 140 141 142 /******************************* 143 * HOOK * 144 *******************************/ 145 146%! write_table(+ResultFormat, +Serialization, +Triples, +Options) 147% 148% Hook for write_table/2. There is no default implementation. 149 150%! write_graph(+ResultFormat, +Serialization, +Triples, +Options) 151% 152% Hook for write_graph/2. The default implementation supports 153% Format = =xml= and Serialization = =rdfxml=. It uses 154% rdf_write_xml/2 to emit the graph. 155 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), _))