View source with formatted comments or as raw
    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                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(rdf_html,
   37          [
   38          ]).   39:- use_module(library(http/html_write)).   40:- use_module(library(http/http_dispatch)).   41:- use_module(library(http/html_head)).   42:- use_module(library(semweb/rdf_db)).   43:- use_module(library(semweb/rdfs)).   44
   45:- use_module(components(label)).   46
   47
   48/** <module> Write query-results as HTML table.
   49
   50This module writes a SPARQL-table as an HTML   table.  It acts as a hook
   51into rdf_io.pl.
   52*/
   53
   54                 /*******************************
   55                 *        RESULT TABLES         *
   56                 *******************************/
   57
   58%!  rdf_io:write_table(+Format, +Serialization, +Rows, +Options) is semidet.
   59%
   60%   Write a result-table in human-readable HTML format
   61%
   62%   @param Format This clause only succeeds of Format is =html=
   63
   64:- multifile
   65    rdf_io:write_table/4,
   66    rdf_io:write_graph/4.   67
   68rdf_io:write_table(html, _Serialization, Rows, Options) :-
   69    !,
   70    length(Rows, Count),
   71    reply_html_page(cliopatria(default),
   72                    title('Query result'),
   73                    [ \query_statistics([count(Count)|Options], rows),
   74                      \select_result_table(Rows, Options),
   75                      \new_query
   76                    ]).
   77
   78select_result_table(Rows, Options) -->
   79    html_requires(css('rdf.css')),
   80    html(table([ class(block)
   81               ],
   82               [ \variables(Options)
   83               | \rows(Rows, Options, odd)
   84               ])).
   85
   86
   87variables(Options) -->
   88    { memberchk(variables(Vars), Options),
   89      Vars =.. [_|Names]
   90    },
   91    !,
   92    html(tr(\varnames(Names))).
   93
   94varnames([]) -->
   95    [].
   96varnames([Name|T]) -->
   97    html(th(Name)),
   98    varnames(T).
   99
  100rows([], _, _) --> [].
  101rows([H|T], Options, Class) -->
  102    { H =.. [_|Cells],
  103      odd_even(Class, NextClass)
  104    },
  105    html(tr(class(Class), \cells(Cells, Options))),
  106    rows(T, Options, NextClass).
  107
  108cells([], _) -->
  109    [].
  110cells([H|T], Options) -->
  111    cell(H, Options),
  112    cells(T, Options).
  113
  114cell(H, _) -->
  115    { var(H) },
  116    !,
  117    html(td(span(class(rdf_unbound), '<unbound>'))).
  118cell(H, Options) -->
  119    html(td(\rdf_link(H, Options))).
  120
  121odd_even(odd, even).
  122odd_even(even, odd).
  123
  124
  125                 /*******************************
  126                 *          GRAPH OUTPUT        *
  127                 *******************************/
  128
  129%!  rdf_io:write_graph(+Format, +Serialization, +Triples, +Options)
  130%
  131%   Write an RDF result-graph as an HTML table, where resources
  132%   are links to the ClioPatria local view.
  133%
  134%   @param Format must be =html=
  135%   @param Serialization is ignored
  136%   @param Triples is a list of rdf(S,P,O) triples
  137%   @param Options is passed to rdf_link//2.  It normally defines
  138%   the preferred resource representation.
  139
  140rdf_io:write_graph(html, _Serialization, Triples, Options) :-
  141    length(Triples, Count),
  142    reply_html_page(cliopatria(default),
  143                    title('Query result'),
  144                    [ \query_statistics([count(Count)|Options], triples),
  145                      \consult_result_table(Triples, Options)
  146                    ]).
  147
  148
  149consult_result_table(Triples, Options) -->
  150    html_requires(css('rdf.css')),
  151    html(table([ class(block)
  152               ],
  153               [ tr([th('Subject'), th('Predicate'), th('Object')])
  154               | \triples(Triples, Options, odd)
  155               ])).
  156
  157triples([], _, _) -->
  158    [].
  159triples([H|T], Options, Class) -->
  160    { odd_even(Class, NextClass) },
  161    triple(H, Options, Class),
  162    triples(T, Options, NextClass).
  163
  164triple(rdf(S,P,O), Options, Class) -->
  165    html(tr(class(Class),
  166            [ td(\rdf_link(S, Options)),
  167              td(\rdf_link(P, Options)),
  168              td(\rdf_link(O, Options))])).
  169
  170
  171%!  query_statistics(+Options, +Units)// is det.
  172%
  173%   Emit a short line above the page summarizing resource usage
  174%   and result size.
  175
  176query_statistics(Options, Units) -->
  177    { memberchk(cputime(CPU), Options),
  178      memberchk(count(Count), Options)
  179    },
  180    !,
  181    html(p(class(msg_informational),
  182           'Query completed in ~3f seconds ~D ~w'-[CPU, Count, Units])).
  183query_statistics(_, _) -->
  184    [].
  185
  186%!  new_query//
  187
  188new_query -->
  189    { http_link_to_id(query_form, [], QueryForm)
  190    },
  191    html([ br(clear(all)),
  192           a([class('new-query'), href(QueryForm)], 'New query')
  193         ])