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)  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]).

Portray RDF resources

This module defines rules for portray/1 to help tracing and debugging RDF resources by printing them in a more concise representation and optionally adding comment from the label field to help the user interpreting the URL. The main predicates are:

To be done
- Define alternate predicate to use for providing a comment
- Use rdf:type if there is no meaningful label?
- Smarter guess whether or not the local identifier might be meaningful to the user without a comment. I.e. does it look `word-like'? */
   64:- dynamic
   65    style/1,
   66    lang/1.
 rdf_portray_as(+Style) is det
Set the style used to portray resources. Style is one of:
prefix:id
Write as NS:ID, compatible with what can be handed to the rdf predicates. This is the default.
writeq
Use quoted write of the full resource.
prefix:label
Write namespace followed by the label. This format cannot be handed to rdf/3 and friends, but can be useful if resource-names are meaningless identifiers.
prefix:id=label
This combines prefix:id with prefix:label, providing both human readable output and output that can be pasted into the commandline.
   89rdf_portray_as(Style) :-
   90    must_be(oneof([writeq, prefix:id, prefix:label, prefix:id=label]), Style),
   91    retractall(style(_)),
   92    assert(style(Style)).
 rdf_portray_lang(+Lang) is det
If Lang is a list, set the list or preferred languages. If it is a single atom, push this language as the most preferred language.
  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)