30
31:- module(rdf_describe,
32 [ rdf_bounded_description/4, 33 rdf_bounded_description/5, 34 resource_CBD/3, 35 graph_CBD/3, 36 rdf_include_reifications/3, 37 rdf_include_labels/3, 38 lcbd_label/3 39 ]).
40:- use_module(library(semweb/rdf_db)).
41:- use_module(library(assoc)).
42:- use_module(library(lists)).
43
44
63
64:- meta_predicate
65 rdf_bounded_description(3, +, +, -),
66 resource_CBD(3, +, -),
67 graph_CBD(3, +, -),
68 rdf_include_reifications(3, +, -).
69
70
71 74
89
90rdf_bounded_description(Expand, Type, S, Graph) :-
91 rdf_bounded_description(Expand, Type, [], S, Graph).
92
93rdf_bounded_description(Expand, Type, Filter, S, Graph) :-
94 empty_assoc(Map0),
95 compile_pattern(Filter, Triple, Expand, Filter1),
96 expansion(Type, Expand, S, Triple, Filter1, Graph, BNG),
97 phrase(new_bnodes(Graph, Map0), BN),
98 phrase(r_bnodes(BN, Type, Expand, Map0, _Map), BNG).
99
100compile_pattern([], _, _, true).
101compile_pattern([rdf(S,P,O)], rdf(S,P,O), Expand,
102 call(Expand, S,P,O)) :- !.
103compile_pattern([rdf(S,P,O)|T], rdf(S,P,O), Expand,
104 ( call(Expand, S,P,O) ; More )) :-
105 compile_pattern(T, rdf(S,P,O), Expand, More).
106
107
108
109:- meta_predicate
110 expansion(+, 3, +, +, +, -, ?),
111 r_bnodes(+, +, 3, +, -, ?, ?).
112
113expansion(cbd, Expand, S, rdf(S,P,O), Filter, RDF, Tail) :-
114 findall(rdf(S,P,O), (call(Expand, S,P,O),Filter), RDF, Tail).
115expansion(scbd, Expand, S, rdf(S,P,O), Filter, RDF, Tail) :-
116 findall(rdf(S,P,O), (call(Expand, S,P,O),Filter), RDF, T0),
117 findall(rdf(O,P,S), (call(Expand, O,P,S),Filter), T0, Tail).
118
119r_bnodes([], _, _, Map, Map) -->
120 [].
121r_bnodes([H|T], Type, Expand, Map0, Map, Graph, Tail) :-
122 rdf_is_bnode(H), !,
123 put_assoc(H, Map0, true, Map1),
124 expansion(Type, Expand, H, _, true, Graph, Tail0),
125 phrase(new_bnodes(Graph, Map1), BN, T),
126 r_bnodes(BN, Type, Expand, Map1, Map, Tail0, Tail).
127r_bnodes([_|T], Type, Expand, Map0, Map) -->
128 r_bnodes(T, Type, Expand, Map0, Map).
129
130new_bnodes(Var, _) -->
131 { var(Var) }, !.
132new_bnodes([rdf(S,_,O)|RDF], Map) -->
133 new_bnode(S, Map),
134 new_bnode(O, Map),
135 new_bnodes(RDF, Map).
136
137new_bnode(S, Map) --> { rdf_is_bnode(S), \+ get_assoc(S, Map, _) }, !, [S].
138new_bnode(_, _) --> [].
139
140
152
153resource_CBD(Expand, S, Graph) :-
154 rdf_bounded_description(Expand, cbd, S, Graph).
155
156
157 160
165
166graph_CBD(Expand, Graph0, Graph) :-
167 empty_assoc(Map0),
168 must_be(list, Graph0),
169 phrase(gr_cbd(Graph0, Expand, Map0, _Map), Graph).
170
171:- meta_predicate
172 gr_cbd(+, 3, +, -, ?, ?).
173
174gr_cbd([], _, Map, Map) -->
175 [].
176gr_cbd([rdf(S,P,O)|T], Expand, Map0, Map) -->
177 { rdf_is_bnode(S)
178 ; rdf_is_bnode(O)
179 }, !,
180 [ rdf(S,P,O) ],
181 r_bnodes([S,O], cbd, Expand, Map0, Map1),
182 gr_cbd(T, Expand, Map1, Map).
183gr_cbd([Triple|T], Expand, Map0, Map) -->
184 [Triple],
185 gr_cbd(T, Expand, Map0, Map).
186
190
191rdf_include_reifications(Expand, Graph0, Graph) :-
192 phrase(reified_triples(Graph0, Expand), Statements),
193 ( Statements == []
194 -> Graph = Graph0
195 ; graph_CBD(Expand, Statements, Statements1),
196 rdf_include_reifications(Expand, Statements1, Graph1),
197 append(Graph0, Graph1, Graph)
198 ).
199
200:- meta_predicate
201 reified_triples(+, 3, ?, ?),
202 reification(?,?,?,3,-).
203
204reified_triples([], _) --> [].
205reified_triples([rdf(S,P,O)|T], Expand) -->
206 findall(T, reification(S,P,O,Expand,T)),
207 reified_triples(T, Expand).
208
209reification(S,P,O, Expand, Triple) :-
210 rdf_equal(SP, rdf:subject),
211 rdf_equal(PP, rdf:predicate),
212 rdf_equal(OP, rdf:object),
213 call(Expand, Stmt, SP, S),
214 call(Expand, Stmt, OP, O),
215 call(Expand, Stmt, PP, P),
216 ( Triple = rdf(Stmt, SP, S)
217 ; Triple = rdf(Stmt, PP, P)
218 ; Triple = rdf(Stmt, OP, O)
219 ).
220
231
232rdf_include_labels(Expand, Graph0, Graph) :-
233 phrase(label_triples(Graph0, Expand), LabelRDF),
234 ( LabelRDF == []
235 -> Graph = Graph0
236 ; append(Graph0, LabelRDF, Graph)
237 ).
238
239:- meta_predicate
240 label_triples(+, 3, ?, ?),
241 label_triple(+, 3, -).
242
243label_triples([], _) --> [].
244label_triples([rdf(_,_,O)|T], Expand) -->
245 findall(T, label_triple(O,Expand,T)),
246 label_triples(T, Expand).
247
248label_triple(O, Expand, Triple) :-
249 call(Expand, O, LP, Label),
250 Triple = rdf(O, LP, Label).
251
252:- rdf_meta
253 lcbd_property(r).
254
258
259lcbd_label(S, P, Label) :-
260 lcbd_property(P),
261 rdf_has(S, P, Label).
262
263lcbd_property(rdfs:label).
264lcbd_property(rdfs:comment).
265lcbd_property(rdfs:seeAlso).