36
37:- module(rdf_portray,
38 [ rdf_portray_as/1, 39 rdf_portray_lang/1 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]).
64:- dynamic
65 style/1,
66 lang/1.
89rdf_portray_as(Style) :-
90 must_be(oneof([writeq, prefix:id, prefix:label, prefix:id=label]), Style),
91 retractall(style(_)),
92 assert(style(Style)).
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)
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: