View source with raw 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)).

Write query-results as HTML table.

This module writes a SPARQL-table as an HTML table. It acts as a hook into rdf_io.pl. */

   54                 /*******************************
   55                 *        RESULT TABLES         *
   56                 *******************************/
 rdf_io:write_table(+Format, +Serialization, +Rows, +Options) is semidet
Write a result-table in human-readable HTML format
Arguments:
Format- This clause only succeeds of Format is html
   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                 *******************************/
 rdf_io:write_graph(+Format, +Serialization, +Triples, +Options)
Write an RDF result-graph as an HTML table, where resources are links to the ClioPatria local view.
Arguments:
Format- must be html
Serialization- is ignored
Triples- is a list of rdf(S,P,O) triples
Options- is passed to rdf_link//2. It normally defines the preferred resource representation.
  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))])).
 query_statistics(+Options, +Units)// is det
Emit a short line above the page summarizing resource usage and result size.
  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    [].
 new_query//
  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         ])