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_html, Chris@0: [ Chris@0: ]). Chris@0: :- use_module(library('http/html_write')). Chris@0: :- use_module(library('semweb/rdf_db')). Chris@0: :- use_module(library('semweb/rdfs')). Chris@0: Chris@0: Chris@0: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Chris@0: This library provides primitives based on the html_write library dealing Chris@0: with emitting RDF related material in HTML human-readable format. Chris@0: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ Chris@0: Chris@0: /******************************* Chris@0: * RESULT TABLES * Chris@0: *******************************/ Chris@0: Chris@0: %% write_table(+Format, +Serialization, +Rows, +Options) Chris@0: % Chris@0: % Write a result-table in human-readable HTML format Chris@0: % Chris@0: % @param Format Must be =html= Chris@0: Chris@0: :- multifile Chris@0: rdf_io:write_table/4, Chris@0: rdf_io:write_graph/4. Chris@0: Chris@0: rdf_io:write_table(html, _Serialization, Rows, Options) :- !, Chris@0: length(Rows, Count), Chris@0: phrase(page(title('Query result'), Chris@0: [ \query_statistics([count(Count)|Options], rows), Chris@0: table([ align(center), Chris@0: border(1) Chris@0: ], Chris@0: [ \variables(Options) Chris@0: | \rows(Rows, Options) Chris@0: ]) Chris@0: ]), HTML), Chris@0: format('Content-type: text/html~n~n'), Chris@0: print_html(HTML). Chris@0: Chris@0: variables(Options) --> Chris@0: { memberchk(variables(Vars), Options), Chris@0: Vars =.. [_|Names] Chris@0: }, !, Chris@0: html(tr(\varnames(Names))). Chris@0: Chris@0: varnames([]) --> Chris@0: []. Chris@0: varnames([Name|T]) --> Chris@0: html(th(Name)), Chris@0: varnames(T). Chris@0: Chris@0: rows([], _) --> Chris@0: []. Chris@0: rows([H|T], Options) --> Chris@0: { H =.. [_|Cells] }, Chris@0: html(tr(\cells(Cells, Options))), Chris@0: rows(T, Options). Chris@0: Chris@0: cells([], _) --> Chris@0: []. Chris@0: cells([H|T], Options) --> Chris@0: html(td(\object(H, Options))), Chris@0: cells(T, Options). Chris@0: Chris@0: Chris@0: /******************************* Chris@0: * GRAPH OUTPUT * Chris@0: *******************************/ Chris@0: Chris@0: rdf_io:write_graph(html, _Serialization, Triples, Options) :- Chris@0: length(Triples, Count), Chris@0: phrase(page(title('Query result'), Chris@0: [ \query_statistics([count(Count)|Options], triples), Chris@0: table([ align(center), Chris@0: border(1) Chris@0: ], Chris@0: [ tr([th('Subject'), th('Predicate'), th('Object')]) Chris@0: | \triples(Triples, Options) Chris@0: ]) Chris@0: ]), HTML), Chris@0: format('Content-type: text/html~n~n'), Chris@0: print_html(HTML). Chris@0: Chris@0: triples([], _) --> Chris@0: []. Chris@0: triples([H|T], Options) --> Chris@0: triple(H, Options), Chris@0: triples(T, Options). Chris@0: Chris@0: triple(rdf(S,P,O), Options) --> Chris@0: html(tr([ td(\resource(S, Options)), Chris@0: td(\resource(P, Options)), Chris@0: td(\object(O, Options))])). Chris@0: Chris@0: resource(R, Options) --> Chris@0: { memberchk(resource_format(Fmt), Options) Chris@0: -> true Chris@0: ; Fmt = plain Chris@0: }, Chris@0: resource(Fmt, R, Options). Chris@0: Chris@0: resource(plain, R, _) --> !, Chris@0: html(R). Chris@0: resource(_, R, _) --> Chris@0: { atomic(R), sub_atom(R, 0, L, _, '__file:/'), !, % bnode Chris@0: sub_atom(R, L, _, 0, Path), Chris@0: file_base_name(Path, Base), Chris@0: atom_concat('__', Base, Label) Chris@0: }, Chris@0: html(Label). Chris@0: resource(ns, R, _) --> !, Chris@0: ( { rdf_global_id(NS:Local, R) } Chris@0: -> html([NS, :, Local]) Chris@0: ; html(R) Chris@0: ). Chris@0: resource(nslabel, R, _) --> !, Chris@0: { rdfs_ns_label(R, Label) Chris@0: }, Chris@0: html(Label). Chris@0: Chris@0: object(V,Options) --> {var(V)}, !. Chris@0: object(literal(type(T, V)), Options) --> !, Chris@0: html(['"', V, '"^^', \resource(T, Options)]). Chris@0: object(literal(lang(L, V)), _) --> !, Chris@0: html(['"', V, '"@', L]). Chris@0: object(literal(V), _) --> !, Chris@0: html(['"', V, '"']). Chris@0: object(R, Options) --> Chris@0: resource(R, Options). Chris@0: Chris@0: Chris@0: %% query_statistics(+Options, +Units)// is det. Chris@0: % Chris@0: % Emit a short line above the page summarizing resource usage Chris@0: % and result size. Chris@0: Chris@0: query_statistics(Options, Units) --> Chris@0: { memberchk(cputime(CPU), Options), Chris@0: sformat(T, '~2f', CPU), Chris@0: memberchk(count(Count), Options), Chris@0: sformat(C, '~D', Count) Chris@0: }, !, Chris@0: html(center(blockquote(i(['Query completed in ', T, ' seconds, ', Chris@0: C, ' ', Units])))). Chris@0: query_statistics(_, _) --> Chris@0: [].