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)  2009-2020, University of Amsterdam
    7                              VU University Amsterdam
    8                              CWI, Amsterdam
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(rdf_portray,
   38          [ rdf_portray_as/1,           % +Style
   39            rdf_portray_lang/1          % +Lang
   40          ]).   41:- use_module(library(semweb/rdf_db),[rdf_global_id/2,rdf_has/3]).   42:- use_module(library(semweb/rdfs),[rdfs_ns_label/2]).   43
   44:- autoload(library(error),[must_be/2]).   45:- autoload(library(lists),[member/2]).   46
   47/** <module> Portray RDF resources
   48
   49This module defines  rules  for  user:portray/1   to  help  tracing  and
   50debugging  RDF  resources  by  printing   them    in   a   more  concise
   51representation and optionally adding comment  from   the  label field to
   52help the user interpreting the URL. The main predicates are:
   53
   54        * rdf_portray_as/1 defines the overall style
   55        * rdf_portray_lang/1 selects languages for extracting label comments
   56
   57@tbd    Define alternate predicate to use for providing a comment
   58@tbd    Use rdf:type if there is no meaningful label?
   59@tbd    Smarter guess whether or not the local identifier might be
   60        meaningful to the user without a comment.  I.e. does it look
   61        `word-like'?
   62*/
   63
   64:- dynamic
   65    style/1,
   66    lang/1.   67
   68%!  rdf_portray_as(+Style) is det.
   69%
   70%   Set the style used to portray resources.  Style is one of:
   71%
   72%           $ =|prefix:id|= :
   73%           Write as NS:ID, compatible with what can be handed to
   74%           the rdf predicates.  This is the default.
   75%
   76%           $ =writeq= :
   77%           Use quoted write of the full resource.
   78%
   79%           $ =|prefix:label|= :
   80%           Write namespace followed by the label.  This format
   81%           cannot be handed to rdf/3 and friends, but can be
   82%           useful if resource-names are meaningless identifiers.
   83%
   84%           $ =|prefix:id=label|= :
   85%           This combines prefix:id with prefix:label, providing both human
   86%           readable output and output that can be pasted into the
   87%           commandline.
   88
   89rdf_portray_as(Style) :-
   90    must_be(oneof([writeq, prefix:id, prefix:label, prefix:id=label]), Style),
   91    retractall(style(_)),
   92    assert(style(Style)).
   93
   94%!  rdf_portray_lang(+Lang) is det.
   95%
   96%   If Lang is a list, set the list or preferred languages. If it is
   97%   a  single  atom,  push  this  language  as  the  most  preferred
   98%   language.
   99
  100rdf_portray_lang(Lang) :-
  101    (   is_list(Lang)
  102    ->  must_be(list(atom), Lang),
  103        retractall(lang(_)),
  104        forall(member(L,Lang), assert(lang(L)))
  105    ;   must_be(atom, Lang),
  106        asserta(lang(Lang))
  107    ).
  108
  109try_lang(L) :- lang(L).
  110try_lang(_).
  111
  112
  113:- multifile
  114    user:portray/1.  115
  116user:portray(URL) :-
  117    atom(URL),
  118    http_url(URL),
  119    !,
  120    (   style(Style)
  121    ->  true
  122    ;   Style = prefix:id
  123    ),
  124    portray_url(Style, URL).
  125user:portray(URL) :-
  126    atom(URL),
  127    atom_concat('_:file://', URL2, URL),
  128    sub_atom(URL2, S, _, A, #),
  129    sub_atom(URL2, _, A, 0, Local),
  130    sub_atom(URL2, 0, S, _, Path),
  131    file_base_name(Path, Base),
  132    format('_:~w#~w', [Base, Local]).
  133
  134http_url(URL) :- sub_atom(URL, 0, _, _, 'http://'), !.
  135http_url(URL) :- sub_atom(URL, 0, _, _, 'https://'), !.
  136
  137portray_url(writeq, URL) :-
  138    writeq(URL).
  139portray_url(prefix:id, URL) :-
  140    (   rdf_global_id(NS:Id, URL)
  141    ->  writeq(NS:Id)
  142    ;   writeq(URL)
  143    ).
  144portray_url(prefix:id=label, URL) :-
  145    (   rdf_global_id(NS:Id, URL)
  146    ->  Value = NS:Id
  147    ;   Value = URL
  148    ),
  149    (   Id \== '',
  150        (   (   try_lang(Lang),
  151                rdf_has(URL, rdfs:label, literal(lang(Lang, Label)))
  152            ->  nonvar(Lang),
  153                \+ label_is_id(Label, Id)
  154            )
  155        ->  format('~q/*"~w"@~w*/', [Value, Label, Lang])
  156        ;   rdf_has(URL, rdfs:label, literal(type(Type, Label))),
  157            nonvar(Type),
  158            \+ label_is_id(Label, Id)
  159        ->  (   rdf_global_id(TNS:TId, Type)
  160            ->      TVal = TNS:TId
  161            ;       TVal = Type
  162            ),
  163            format('~q/*"~w"^^~w*/', [Value, Label, TVal])
  164        ;   rdf_has(URL, rdfs:label, literal(Label)),
  165            atom(Label),
  166            Label \== Id
  167        ->  format('~q/*"~w"*/', [Value, Label])
  168        )
  169    ->  true
  170    ;   writeq(Value)
  171    ).
  172portray_url(prefix:label, URL) :-
  173    rdfs_ns_label(URL, Label),
  174    write(Label).
  175
  176label_is_id(_, Var) :-
  177    var(Var), !, fail.
  178label_is_id(Label, Label) :- !.
  179label_is_id(L0, L1) :-
  180    downcase_atom(L0, Lwr),
  181    downcase_atom(L1, Lwr)