36
37:- module(rdf_label,
38 [ rdf_label/2, 39 rdf_display_label/2, 40 rdf_display_label/3, 41 literal_text/2, 42 truncate_atom/3, 43 label_property/1 44 ]). 45:- use_module(library(semweb/rdf_db)). 46:- use_module(library(semweb/rdf11), [rdf_lexical_form/2]). 47:- use_module(user(preferences)).
61:- multifile
62 label_property/1, 63 label_hook/2, 64 display_label_hook/3. 65
66:- rdf_meta
67 rdf_label(r,-),
68 rdf_display_label(r,-),
69 rdf_display_label(r,?,-),
70 label_property(r). 71
72 73:- rdf_register_ns(foaf, 'http://xmlns.com/foaf/0.1/').
81label_property(skos:prefLabel).
82label_property(foaf:name).
83label_property(dc:title).
84label_property(rdfs:label).
85label_property(skos:altLabel).
95rdf_label(R, Label) :-
96 ( label_hook(R, Label)
97 *-> true
98 ; label_property(P),
99 rdf_has(R, P, Label),
100 rdf_is_literal(Label)
101 ).
112rdf_display_label(R, Label) :-
113 rdf_display_label(R, _, Label).
121rdf_display_label(R, Lang, Label) :-
122 rdf_real_label(R, Lang, Label),
123 !.
124rdf_display_label(Resource, url, String) :-
125 ( after_char(Resource, '#', Local), Local \= ''
126 -> Label = Local
127 ; after_char(Resource, '/', Local), Local \= ''
128 -> Label = Local
129 ; Label = Resource
130 ),
131 atom_string(Label, String).
132
133
134rdf_real_label(R, Lang, Label) :-
135 136 display_label_hook(R, Lang, Label),
137 !.
138rdf_real_label(R, Lang, Label) :-
139 140 nonvar(Lang),
141 rdf_is_resource(R),
142 ( rdf_label(R, literal(lang(Lang, Label))) 143 -> true
144 ; rdf_label(R, Literal), 145 Literal = literal(lang(Lang0, Label)),
146 lang_matches(Lang0, Lang)
147 ),
148 !.
149 150
151rdf_real_label(R, Lang, Label) :-
152 153 var(Lang),
154 rdf_is_resource(R),
155 user_preference(user:lang, literal(Lang)),
156 rdf_real_label(R, Lang, Label).
157
158
159rdf_real_label(R, Lang, Label) :-
160 161 var(Lang),
162 rdf_is_resource(R),
163 rdf_label(R, Literal),
164 literal_lang(Literal, Lang),
165 literal_text(Literal, Label).
166
167rdf_real_label(BNode, Lang, Label) :-
168 rdf_has(BNode, rdf:value, Value),
169 rdf_real_label(Value, Lang, Label0),
170 !,
171 format(atom(Label), '[~a..]', Label0).
172rdf_real_label(Literal, Lang, Label) :-
173 rdf_is_literal(Literal),
174 !,
175 literal_lang(Literal, Lang),
176 literal_text(Literal, Label).
177
178after_char(Atom, Char, Rest) :-
179 State = last(-),
180 ( sub_atom(Atom, _, _, L, Char),
181 nb_setarg(1, State, L),
182 fail
183 ; arg(1, State, L),
184 L \== (-)
185 ),
186 sub_atom(Atom, _, L, 0, Rest).
187
188literal_lang(literal(Lang0, _), Lang) :-
189 !,
190 Lang = Lang0.
191
192literal_lang(literal(lang(Lang0, _)), Lang) :-
193 !,
194 Lang = Lang0.
195
196literal_lang(_, _).
206literal_text(Literal, Text) :-
207 ground(Literal),
208 ( atom(Literal)
209 -> Text = Literal
210 ; string(Literal)
211 -> Text = Literal
212 ; rdf_lexical_form(Literal, Lexical),
213 ( Lexical = @(Text, _Lang)
214 -> true
215 ; Lexical = ^^(Text, _Type)
216 )
217 ).
224truncate_atom(Atom, inf, All) :-
225 !,
226 All = Atom.
227truncate_atom(Atom, MaxLen, Truncated) :-
228 atom_length(Atom, Len),
229 ( Len =< MaxLen
230 -> Truncated = Atom
231 ; TLen is max(3, MaxLen-4),
232 sub_atom(Atom, 0, TLen, _, S0),
233 atom_concat(S0, ' ...', Truncated)
234 ).
235
236 239
240:- multifile
241 sandbox:safe_primitive/1. 242
243sandbox:safe_primitive(rdf_label:literal_text(_,_))
Generate labels for RDF objects
This library deals with a common problem in RDF applications: show labels for resources and display literals. There is no clear-cut answer to this problem because there are too many options. Think of e.g., language preferences, producing summaries, desired rdfs/owl/... reasoning. Therefore, this library provides the required APIs a default implementation and hooks that allow for dealing with the above mentioned issues. */