forked from SWI-Prolog/packages-semweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdf_portray.pl
171 lines (148 loc) · 4.92 KB
/
rdf_portray.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2007-2012, University of Amsterdam
VU University Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module(rdf_portray,
[ rdf_portray_as/1, % +Style
rdf_portray_lang/1 % +Lang
]).
:- use_module(library(semweb/rdf_db)).
:- use_module(library(semweb/rdfs)).
:- use_module(library(error)).
/** <module> Portray RDF resources
This module defines rules for user: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:
* rdf_portray_as/1 defines the overall style
* rdf_portray_lang/1 selects languages for extracting label comments
@tbd Define alternate predicate to use for providing a comment
@tbd Use rdf:type if there is no meaningful label?
@tbd Smarter guess whether or not the local identifier might be
meaningful to the user without a comment. I.e. does it look
`word-like'?
*/
:- dynamic
style/1,
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.
rdf_portray_as(Style) :-
must_be(oneof([writeq, prefix:id, prefix:label, prefix:id=label]), Style),
retractall(style(_)),
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.
rdf_portray_lang(Lang) :-
( is_list(Lang)
-> must_be(list(atom), Lang),
retractall(lang(_)),
forall(member(L,Lang), assert(lang(L)))
; must_be(atom, Lang),
asserta(lang(Lang))
).
try_lang(L) :- lang(L).
try_lang(_).
:- multifile
user:portray/1.
user:portray(URL) :-
atom(URL),
sub_atom(URL, 0, _, _, 'http://'), !,
( style(Style)
-> true
; Style = prefix:id
),
portray_url(Style, URL).
user:portray(URL) :-
atom(URL),
atom_concat('__file://', URL2, URL),
sub_atom(URL2, S, _, A, #),
sub_atom(URL2, _, A, 0, Local),
sub_atom(URL2, 0, S, _, Path),
file_base_name(Path, Base),
format('__~w#~w', [Base, Local]).
portray_url(writeq, URL) :-
writeq(URL).
portray_url(prefix:id, URL) :-
( rdf_global_id(NS:Id, URL)
-> writeq(NS:Id)
; writeq(URL)
).
portray_url(prefix:id=label, URL) :-
( rdf_global_id(NS:Id, URL)
-> Value = NS:Id
; Value = URL
),
( Id \== '',
( ( try_lang(Lang),
rdf_has(URL, rdfs:label, literal(lang(Lang, Label)))
-> nonvar(Lang),
\+ label_is_id(Label, Id)
)
-> format('~q/*"~w"@~w*/', [Value, Label, Lang])
; rdf_has(URL, rdfs:label, literal(type(Type, Label))),
nonvar(Type),
\+ label_is_id(Label, Id)
-> ( rdf_global_id(TNS:TId, Type)
-> TVal = TNS:TId
; TVal = Type
),
format('~q/*"~w"^^~w*/', [Value, Label, TVal])
; rdf_has(URL, rdfs:label, literal(Label)),
atom(Label),
Label \== Id
-> format('~q/*"~w"*/', [Value, Label])
)
-> true
; writeq(Value)
).
portray_url(prefix:label, URL) :-
rdfs_ns_label(URL, Label),
write(Label).
label_is_id(_, Var) :-
var(Var), !, fail.
label_is_id(Label, Label) :- !.
label_is_id(L0, L1) :-
downcase_atom(L0, Lwr),
downcase_atom(L1, Lwr).