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-2012, 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_portray,
   37          [ rdf_portray_as/1,           % +Style
   38            rdf_portray_lang/1          % +Lang
   39          ]).   40:- use_module(library(semweb/rdf_db)).   41:- use_module(library(semweb/rdfs)).   42:- use_module(library(error)).

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'? */
   61:- dynamic
   62    style/1,
   63    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.
   86rdf_portray_as(Style) :-
   87    must_be(oneof([writeq, prefix:id, prefix:label, prefix:id=label]), Style),
   88    retractall(style(_)),
   89    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.
   97rdf_portray_lang(Lang) :-
   98    (   is_list(Lang)
   99    ->  must_be(list(atom), Lang),
  100        retractall(lang(_)),
  101        forall(member(L,Lang), assert(lang(L)))
  102    ;   must_be(atom, Lang),
  103        asserta(lang(Lang))
  104    ).
  105
  106try_lang(L) :- lang(L).
  107try_lang(_).
  108
  109
  110:- multifile
  111    user:portray/1.  112
  113user:portray(URL) :-
  114    atom(URL),
  115    http_url(URL),
  116    !,
  117    (   style(Style)
  118    ->  true
  119    ;   Style = prefix:id
  120    ),
  121    portray_url(Style, URL).
  122user:portray(URL) :-
  123    atom(URL),
  124    atom_concat('_:file://', URL2, URL),
  125    sub_atom(URL2, S, _, A, #),
  126    sub_atom(URL2, _, A, 0, Local),
  127    sub_atom(URL2, 0, S, _, Path),
  128    file_base_name(Path, Base),
  129    format('_:~w#~w', [Base, Local]).
  130
  131http_url(URL) :- sub_atom(URL, 0, _, _, 'http://'), !.
  132http_url(URL) :- sub_atom(URL, 0, _, _, 'https://'), !.
  133
  134portray_url(writeq, URL) :-
  135    writeq(URL).
  136portray_url(prefix:id, URL) :-
  137    (   rdf_global_id(NS:Id, URL)
  138    ->  writeq(NS:Id)
  139    ;   writeq(URL)
  140    ).
  141portray_url(prefix:id=label, URL) :-
  142    (   rdf_global_id(NS:Id, URL)
  143    ->  Value = NS:Id
  144    ;   Value = URL
  145    ),
  146    (   Id \== '',
  147        (   (   try_lang(Lang),
  148                rdf_has(URL, rdfs:label, literal(lang(Lang, Label)))
  149            ->  nonvar(Lang),
  150                \+ label_is_id(Label, Id)
  151            )
  152        ->  format('~q/*"~w"@~w*/', [Value, Label, Lang])
  153        ;   rdf_has(URL, rdfs:label, literal(type(Type, Label))),
  154            nonvar(Type),
  155            \+ label_is_id(Label, Id)
  156        ->  (   rdf_global_id(TNS:TId, Type)
  157            ->      TVal = TNS:TId
  158            ;       TVal = Type
  159            ),
  160            format('~q/*"~w"^^~w*/', [Value, Label, TVal])
  161        ;   rdf_has(URL, rdfs:label, literal(Label)),
  162            atom(Label),
  163            Label \== Id
  164        ->  format('~q/*"~w"*/', [Value, Label])
  165        )
  166    ->  true
  167    ;   writeq(Value)
  168    ).
  169portray_url(prefix:label, URL) :-
  170    rdfs_ns_label(URL, Label),
  171    write(Label).
  172
  173label_is_id(_, Var) :-
  174    var(Var), !, fail.
  175label_is_id(Label, Label) :- !.
  176label_is_id(L0, L1) :-
  177    downcase_atom(L0, Lwr),
  178    downcase_atom(L1, Lwr)