35
36:- module(rdfql_queries,
37 [ query_form//1, 38 store_recall//2, 39 query_script//0, 40 store_query/3 41 ]). 42:- use_module(library(http/http_session)). 43:- use_module(library(http/html_write)). 44:- use_module(library(http/html_head)). 45:- use_module(basics). 46:- use_module(library(option)). 47:- use_module(library(settings)).
65query_form(Options) -->
66 html([ form([ class(query),
67 name(query),
68 action(location_by_id(evaluate_query)),
69 method('GET')
70 ],
71 [ \hidden(repository, default),
72 \hidden(serialization, rdfxml),
73 h3([ 'Interactive ',
74 \query_language(Options, Hidden),
75 ' query'
76 ]),
77 Hidden,
78 table([ class(query)
79 ],
80 [ \store_recall(_, 3-2),
81 tr([ td(colspan(5),
82 textarea(name(query), ''))
83 ]),
84 tr([ td([ span(class(label), 'Result format: '),
85 \result_format
86 ]),
87 td([ span(class(label), 'Resource: '),
88 \resource_menu
89 ]),
90 td([ span(class(label), 'Entailment: '),
91 \entailment
92 ]),
93 td(align(right),
94 [ input([ type(reset),
95 value('Clear')
96 ]),
97 input([ type(submit),
98 value('Go!')
99 ])
100 ])
101 ])
102 ])
103 ]),
104 \query_script
105 ]).
106
107
108result_format -->
109 html(select(name(resultFormat),
110 [ option([], xml),
111 option([selected], html),
112 option([], json),
113 option([], csv)
114 ])).
115
116query_language(Options, Hidden) -->
117 { option(query_languages(LangList), Options, ['SPARQL', 'SeRQL'])
118 },
119 ( { LangList = [Lang] }
120 -> html([Lang]),
121 { Hidden = \hidden(queryLanguage, Lang) }
122 ; { LangList = [DefLang|More] },
123 html(select(name(queryLanguage),
124 [ option([selected], DefLang)
125 | \options(More)
126 ])),
127 { Hidden = '' }
128 ).
129
130options([]) --> [].
131options([Value|T]) -->
132 html(option([], Value)),
133 options(T).
134
135
-->
137 html(select(name(resourceFormat),
138 [ option([value(plain)], plain),
139 option([value(ns), selected], 'ns:local'),
140 option([value(nslabel)], 'ns:label')
141 ])).
142
143entailment -->
144 { findall(E, cliopatria:entailment(E, _), Es)
145 },
146 html(select(name(entailment),
147 \entailments(Es))).
148
149entailments([]) -->
150 [].
151entailments([E|T]) -->
152 ( { setting(cliopatria:default_entailment, E)
153 }
154 -> html(option([selected], E))
155 ; html(option([], E))
156 ),
157 entailments(T).
168store_recall(Type, SL-SR) -->
169 { next_query_id(Id), !
170 },
171 html(tr([ td([ class(qstore),
172 colspan(SL)
173 ],
174 [ b('Remember as: '),
175 input([ id(qid),
176 name(storeAs),
177 size(30),
178 value(Id)
179 ])
180 ]),
181 td([ class(qrecall),
182 colspan(SR),
183 align(right)
184 ],
185 \recall(Type))
186 ])).
187store_recall(_, SL-SR) -->
188 { Span is SL+SR },
189 html(tr([ td([ class(qnostore),
190 colspan(Span)
191 ],
192 [ 'Login to enable save/restore of queries'
193 ])
194 ])).
195
196
197recall(Type) -->
198 { http_in_session(_),
199 findall(Name-Query, stored_query(Name, Type, Query), Pairs),
200 Pairs \== []
201 },
202 !,
203 html([ b('Recall: '),
204 select(name(recall),
205 [ option([selected], '')
206 | \stored_queries(Pairs)
207 ])
208 ]).
209recall(_) -->
210 [].
211
212:- thread_local
213 script_fragment/1. 214
215stored_queries([]) --> !.
216stored_queries(List) -->
217 stored_queries(List, 1),
218 { assert(script_fragment('\nf1();\n')) }.
219
220stored_queries([], _) -->
221 [].
222stored_queries([Name-Query|T], I) -->
223 { I2 is I + 1,
224 atom_concat(f, I, FName),
225 js_quoted(Query, QuotedQuery),
226 format(atom(Script),
227 'function ~w()\n\c
228 { document.query.query.value=\'~w\';\n \c
229 document.getElementById(\'qid\').value="~w";\n\c
230 }\n',
231 [ FName, QuotedQuery, Name ]),
232 assert(script_fragment(Script)),
233 format(atom(Call), '~w()', [FName])
234 },
235 html(option([onClick(Call)], Name)),
236 stored_queries(T, I2).
245query_script -->
246 { findall(S, retract(script_fragment(S)), Fragments),
247 Fragments \== []
248 },
249 !,
250 [ '\n<script language="JavaScript">\n'
251 ],
252 Fragments,
253 [ '\n</script>\n'
254 ].
255query_script -->
256 [].
263js_quoted(Raw, Quoted) :-
264 atom_codes(Raw, Codes),
265 phrase(js_quote_codes(Codes), QuotedCodes),
266 atom_codes(Quoted, QuotedCodes).
267
268js_quote_codes([]) -->
269 [].
270js_quote_codes([0'\r,0'\n|T]) -->
271 !,
272 "\\n",
273 js_quote_codes(T).
274js_quote_codes([H|T]) -->
275 js_quote_code(H),
276 js_quote_codes(T).
277
278js_quote_code(0'') -->
279 !,
280 "\\'".
281js_quote_code(0'\\) -->
282 !,
283 "\\\\".
284js_quote_code(0'\n) -->
285 !,
286 "\\n".
287js_quote_code(0'\r) -->
288 !,
289 "\\r".
290js_quote_code(0'\t) -->
291 !,
292 "\\t".
293js_quote_code(C) -->
294 [C].
295
296
297
306store_query(_, '', _) :- !.
307store_query(Type, As, Query) :-
308 http_in_session(_),
309 !,
310 set_high_id(As),
311 http_session_retractall(stored_query(As, Type, _)),
312 http_session_retractall(stored_query(_, Type, Query)),
313 http_session_asserta(stored_query(As, Type, Query)).
314store_query(_, _, _).
315
316stored_query(As, Type, Query) :-
317 http_session_data(stored_query(As, Type, Query)).
318
319set_high_id(Name) :-
320 http_in_session(_),
321 atom_concat('Q-', Id, Name),
322 catch(atom_number(Id, N), _, fail),
323 !,
324 ( http_session_data(qid(N0))
325 -> ( N > N0
326 -> http_session_retract(qid(_)),
327 http_session_assert(qid(N))
328 ; true
329 )
330 ; http_session_assert(qid(N))
331 ).
332set_high_id(_).
333
334
335next_query_id(Id) :-
336 http_in_session(_Session),
337 !,
338 ( http_session_data(qid(Id0))
339 -> Next is Id0+1
340 ; Next is 1
341 ),
342 atomic_list_concat(['Q-',Next], Id)
Forms for entering SPARQL and SeRQL queries.
This module implements the forms for entering SPARQL and SeRQL queries with a simple query-history mechanism for user-submitted SPARQL queries.