Strabon

view scripts/endpoint @ 1071:93f5d7b2d3cb

Endpoint script also supports named graphs and inference.
author Panayiotis Smeros <psmeros@di.uoa.gr>
date Tue Apr 23 21:57:12 2013 +0300 (2013-04-23)
parents 52396ec09aae
children 866bf84309ae
line source
1 #!/bin/bash
3 #
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 #
8 # Copyright (C) 2010, 2011, 2012, Pyravlos Team
9 #
10 # http://www.strabon.di.uoa.gr/
11 #
13 #
14 # Script for executing SPARQL queries and SPARQL Update queries
15 # as well as storing RDF triples on a Strabon Endpoint.
16 #
17 # Author: Charalampos (Babis) Nikolaou <charnik@di.uoa.gr>
18 # Author: Panayiotis Smeros <psmeros@di.uoa.gr>
19 #
21 # absolute directory name of this command
22 LOC="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
24 # this command
25 CMD="$(basename ${0})"
27 # Filename containing the prefixes to be included in SPARQL queries
28 PREFIXES_FILE="${LOC}/prefixes.sparql"
30 function help() {
31 echo "Usage: ${CMD} [OPTIONS] COMMAND ENDPOINT ARGS"
32 echo
33 echo "Execute SPARQL and SPARQL Update queries as well as store RDF triples on a Strabon endpoint."
34 echo
35 echo " COMMAND : one of query, queryfile, update, store, describe, or help"
36 echo " ENDPOINT : the URL of the Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint)"
37 echo " ARGS : arguments according to selected command"
38 echo
39 echo "OPTIONS can be any of the following"
40 echo " -d : don't run, just print what shall be executed"
41 echo " -i : include URI prefixes in the SPARQL query. Prefixes are taken from file"
42 echo " \`prefixes.sparql'"
43 }
45 function help_query() {
46 echo "Usage: ${CMD} query ENDPOINT SPARQL_QUERY [RESULT_FORMAT]"
47 echo
48 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
49 echo " SPARQL_QUERY : the SPARQL query to execute or the alias name corresponding to a"
50 echo " predefined query such as:"
51 echo " \`size': SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
52 echo " RESULT_FORMAT : the format of the result. Possible values are \`XML' (default), \`KML', \`KMZ', \`GeoJSON', \`HTML', or \`TSV'"
53 }
55 function help_queryfile() {
56 echo "Usage: ${CMD} queryfile ENDPOINT SPARQL_QUERY_FILE [RESULT_FORMAT]"
57 echo
58 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
59 echo " SPARQL_QUERY_FILE : the file that contain the SPARQL query to execute"
60 echo " RESULT_FORMAT : the format of the result. Possible values are \`KMLMAP\, \`GEOJSON', "
61 echo " \`HTML', \`KMZMAP', \`XML' (default), or \`KML'."
62 }
64 function help_update() {
65 echo "Usage: ${CMD} update ENDPOINT SPARQL_QUERY"
66 echo
67 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
68 echo " SPARQL_QUERY : the SPARQL update query to execute or the alias name corresponding to a"
69 echo " predefined query such as:"
70 echo " \`clear': DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
71 }
73 function help_store() {
74 echo "Usage: ${CMD} store ENDPOINT FORMAT [-g NAMED_GRAPH] [--inference] -t TRIPLES|-u TRIPLES_URL"
75 echo
76 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
77 echo " FORMAT : the RDF format of the input (one of RDF/XML, N-Triples, Turtle, N3, TriX, TriG, or BinaryRDF)"
78 echo " TRIPLES : the RDF triples to store"
79 echo " TRIPLES_URL : the URL containing the RDF triples to store"
80 echo " NAMED_GRAPH : the URI of the named graph into which the RDF files shall be stored"
81 echo " (defaults to the default graph)."
82 echo " --inference : enables inference."
83 }
85 function help_describe() {
86 echo "Usage: ${CMD} describe ENDPOINT DESCRIBE_QUERY [RESULT_FORMAT]"
87 echo
88 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
89 echo " DESCRIBE_QUERY : the SPARQL DESCRIBE query to execute"
90 echo " RESULT_FORMAT : the format of the result. Possible values are \`N-Triples' (default)"
91 echo " \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
92 }
94 CURL_OPTS="-w HTTP_CODE='%{http_code}\n' -H \"Content-Type:application/x-www-form-urlencoded\" -H \"Accept:text/xml\""
96 # if set to 1, then only the command to be executed is printed
97 DEBUG=0
98 # set default limit
99 MAXLIMIT=0
100 case "${1}" in
101 -d)
102 shift
103 DEBUG=1
104 ;;
105 -i)
106 shift
107 PREFIXES="$(cat ${PREFIXES_FILE})
108 "
109 ;;
110 -l)
111 MAXLIMIT="${2}"
112 shift
113 shift
114 ;;
115 esac
117 case "${1}" in
118 help)
119 shift
120 if test $# -eq 1; then
121 case "${1}" in
122 query)
123 help_query
124 ;;
125 queryfile)
126 help_queryfile
127 ;;
128 update)
129 help_update
130 ;;
131 store)
132 help_store
133 ;;
134 describe)
135 help_describe
136 ;;
137 *)
138 help
139 ;;
140 esac
141 exit 1
142 fi
143 help
144 exit 0
145 ;;
146 query)
147 shift
148 if ! test $# -ge 2; then
149 help_query
150 exit 1
151 fi
152 URL="${1}/Query"
153 QUERY="${2}"
155 shift
156 shift
157 # predefined queries
158 case "${QUERY}" in
159 size)
160 QUERY="SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
161 ;;
162 esac
164 # set default format
165 FORMAT="XML"
166 if test $# -eq 1; then
167 FORMAT="${1}"
168 fi
170 case "${FORMAT}" in
171 [Xx][Mm][Ll])
172 MIME_TYPE="application/sparql-results+xml"
173 ;;
174 [Tt][sS][vV])
175 MIME_TYPE="text/tab-separated-values"
176 ;;
177 [Kk][Mm][Ll])
178 MIME_TYPE="application/vnd.google-earth.kml+xml"
179 ;;
180 [Kk][mM][Zz])
181 MIME_TYPE="application/vnd.google-earth.kmz"
182 ;;
183 [Hh][Tt][Mm][Ll])
184 MIME_TYPE="text/html"
185 ;;
186 [Gg][Ee][Oo][Jj][Ss][Oo][Nn])
187 MIME_TYPE="application/json"
188 ;;
189 *)
190 echo "${CMD}: unknown format \"${FORMAT}\"."
191 echo "${CMD}: possible values are \`KML', \`KMZ', \`GeoJSON', \`XML' (default), \`HTML', or \`TSV'"
192 exit 2
193 ;;
194 esac
196 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${QUERY}' --data maxLimit='${MAXLIMIT}' ${URL}"
197 ;;
198 queryfile)
199 shift
200 if ! test $# -ge 2; then
201 help_queryfile
202 exit 1
203 fi
205 if ! test -f ${2}; then
206 echo "${CMD}: File not found."
207 exit 2
208 fi
210 URL="${1}/Query"
211 QUERY=`cat ${2} | sed 's/\"/\\\"/g'`
213 shift
214 shift
216 # set default format
217 FORMAT="XML"
218 if test $# -eq 1; then
219 FORMAT="${1}"
220 fi
222 case "${FORMAT}" in
223 [Xx][Mm][Ll])
224 MIME_TYPE="application/sparql-results+xml"
225 ;;
226 [Tt][sS][vV])
227 MIME_TYPE="text/tab-separated-values"
228 ;;
229 [Kk][Mm][Ll])
230 MIME_TYPE="application/vnd.google-earth.kml+xml"
231 ;;
232 [Kk][mM][Zz])
233 MIME_TYPE="application/vnd.google-earth.kmz"
234 ;;
235 [Hh][Tt][Mm][Ll])
236 MIME_TYPE="text/html"
237 ;;
238 [Gg][Ee][Oo][Jj][Ss][Oo][Nn])
239 MIME_TYPE="application/json"
240 ;;
241 *)
242 echo "${CMD}: unknown format \"${FORMAT}\"."
243 echo "${CMD}: possible values are \`KML', \`KMZ', \`GeoJSON', \`XML' (default), \`HTML', or \`TSV'"
244 exit 2
245 ;;
246 esac
248 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
249 ;;
251 update)
252 shift
253 if ! test $# -eq 2; then
254 help_update
255 exit 1
256 fi
257 URL="${1}/Update"
258 QUERY="${2}"
260 # predefined queries
261 case "${QUERY}" in
262 clear)
263 QUERY="DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
264 ;;
265 esac
267 EXEC="curl -u endpoint:3ndpo1nt ${CURL_OPTS} --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
268 ;;
269 store)
270 shift
271 if ! test $# -ge 4; then
272 help_store
273 exit 1
274 fi
275 URL="${1}/Store"
276 shift
277 FORMAT="${1}"
278 shift
279 MIME_TYPE=
280 case "${FORMAT}" in
281 N-Triples)
282 MIME_TYPE="text/plain"
283 ;;
284 RDF/XML)
285 MIME_TYPE="application/rdf+xml"
286 ;;
287 N3)
288 MIME_TYPE="text/rdf+n3"
289 ;;
290 TURTLE)
291 MIME_TYPE="text/turtle"
292 ;;
293 TRIG)
294 MIME_TYPE="application/x-trig"
295 ;;
296 TRIX)
297 MIME_TYPE="application/trix"
298 ;;
299 BinaryRDF)
300 MIME_TYPE="application/x-binary-rdf"
301 ;;
302 *)
303 echo "${CMD}: unknown format \"${FORMAT}\"."
304 echo "${CMD}: possible values are \`N-Triples' (default), \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
305 exit 2
306 ;;
307 esac
309 GRAPH=
310 case "${1}" in
311 -g)
312 shift
313 if [ "${1}" == "--inference" ]; then
314 help_store
315 exit 1
316 fi
317 if ! test $# -ge 3; then
318 help_store
319 exit 1
320 fi
321 GRAPH="-d graph=${1}"
322 shift
323 esac
325 INFERENCE=
326 case "${1}" in
327 --inference)
328 shift
329 if ! test $# -ge 2; then
330 help_store
331 exit 1
332 fi
333 INFERENCE="-d inference=true"
334 esac
336 case "${1}" in
337 -t)
338 shift
339 if ! test $# -eq 1; then
340 help_store
341 exit 1
342 fi
343 TRIPLES="${1}"
344 EXEC="curl -u endpoint:3ndpo1nt -w '\nHTTP_CODE=%{http_code}\n' -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode data='${TRIPLES}' ${GRAPH} ${INFERENCE} ${URL}"
345 ;;
346 -u)
347 shift
348 if ! test $# -eq 1; then
349 help_store
350 exit 1
351 fi
352 URL_TRIPLES="${1}"
353 EXEC="curl -u endpoint:3ndpo1nt -w '\nHTTP_CODE=%{http_code}\n' -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode url='${URL_TRIPLES}' -d fromurl='' ${GRAPH} ${INFERENCE} ${URL}"
354 ;;
355 *)
356 help_store
357 exit 1
358 ;;
359 esac
360 ;;
361 describe)
362 shift
363 if ! test $# -ge 2; then
364 help_describe
365 exit 1
366 fi
367 URL="${1}/Describe"
368 QUERY="${2}"
370 shift
371 shift
373 # set default format
374 FORMAT="N-Triples"
375 if test $# -eq 1; then
376 FORMAT="${1}"
377 fi
379 MIME_TYPE=
380 case "${FORMAT}" in
381 N-Triples)
382 MIME_TYPE="text/plain"
383 ;;
384 RDF/XML)
385 MIME_TYPE="application/rdf+xml"
386 ;;
387 N3)
388 MIME_TYPE="text/rdf+n3"
389 ;;
390 TURTLE)
391 MIME_TYPE="text/turtle"
392 ;;
393 TRIG)
394 MIME_TYPE="application/x-trig"
395 ;;
396 TRIX)
397 MIME_TYPE="application/trix"
398 ;;
399 BinaryRDF)
400 MIME_TYPE="application/x-binary-rdf"
401 ;;
402 *)
403 echo "${CMD}: unknown format \"${FORMAT}\"."
404 echo "${CMD}: possible values are \`N-Triples' (default), \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
405 exit 2
406 ;;
407 esac
409 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
410 ;;
411 *)
412 help
413 echo
414 echo "${CMD}: unknown command \"${1}\"."
415 exit 1
416 ;;
417 esac
419 # execute or debug
420 if test $DEBUG -eq 1; then
421 echo "${CMD}: Debug is ON"
422 echo "${CMD}: Printing command for execution"
423 echo " $EXEC"
424 else
425 eval "${EXEC}"
426 fi