35
36:- module(rdf_portray,
37 [ rdf_portray_as/1, 38 rdf_portray_lang/1 39 ]). 40:- use_module(library(semweb/rdf_db)). 41:- use_module(library(semweb/rdfs)). 42:- use_module(library(error)).
61:- dynamic
62 style/1,
63 lang/1.
86rdf_portray_as(Style) :-
87 must_be(oneof([writeq, prefix:id, prefix:label, prefix:id=label]), Style),
88 retractall(style(_)),
89 assert(style(Style)).
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 sub_atom(URL, 0, _, _, 'http://'),
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
131portray_url(writeq, URL) :-
132 writeq(URL).
133portray_url(prefix:id, URL) :-
134 ( rdf_global_id(NS:Id, URL)
135 -> writeq(NS:Id)
136 ; writeq(URL)
137 ).
138portray_url(prefix:id=label, URL) :-
139 ( rdf_global_id(NS:Id, URL)
140 -> Value = NS:Id
141 ; Value = URL
142 ),
143 ( Id \== '',
144 ( ( try_lang(Lang),
145 rdf_has(URL, rdfs:label, literal(lang(Lang, Label)))
146 -> nonvar(Lang),
147 \+ label_is_id(Label, Id)
148 )
149 -> format('~q/*"~w"@~w*/', [Value, Label, Lang])
150 ; rdf_has(URL, rdfs:label, literal(type(Type, Label))),
151 nonvar(Type),
152 \+ label_is_id(Label, Id)
153 -> ( rdf_global_id(TNS:TId, Type)
154 -> TVal = TNS:TId
155 ; TVal = Type
156 ),
157 format('~q/*"~w"^^~w*/', [Value, Label, TVal])
158 ; rdf_has(URL, rdfs:label, literal(Label)),
159 atom(Label),
160 Label \== Id
161 -> format('~q/*"~w"*/', [Value, Label])
162 )
163 -> true
164 ; writeq(Value)
165 ).
166portray_url(prefix:label, URL) :-
167 rdfs_ns_label(URL, Label),
168 write(Label).
169
170label_is_id(_, Var) :-
171 var(Var), !, fail.
172label_is_id(Label, Label) :- !.
173label_is_id(L0, L1) :-
174 downcase_atom(L0, Lwr),
175 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: