Strabon

view scripts/strabon @ 482:2e35628b10d0

fixed bug in strabon script when projecting all variables appearing in triples patterns using symbol *
author Babis Nikolaou <charnik@di.uoa.gr>
date Fri Jul 20 15:23:38 2012 +0300 (2012-07-20)
parents 7bad4b0576f9
children 23a9e69cb926
line source
1 #!/bin/bash
2 #
3 # Script for running the main classes of Strabon. The main classes of Strabon comprises
4 # QueryOp, , UpdateOp, StoreOp, and DescribeOp.
5 #
6 # Author: Charalampos (Babis) Nikolaou <charnik@di.uoa.gr>
7 #
9 # command name
10 CMD="$(basename ${0})"
12 # absolute directory name of this command
13 LOC="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15 RUNTIME="${LOC}/../runtime"
17 # runtime package
18 PKG="eu.earthobservatory.runtime"
20 # the underlying database to use (one of `postgis' or `monetdb')
21 DATABASE="postgis"
23 # the main class to run
24 CLASS=
26 # the hostname at which the database runs
27 HOST="localhost"
29 # the port at which the database listens
30 PORT=
32 # the database name to connect to
33 DB="strabon"
35 # the username for the database connection
36 DBUSER=
38 # the password for the database connection
39 DBPASS=
41 # the query to run
42 QUERY=
44 # the RDF format of the files to store (defaults to ntriples)
45 FORMAT="ntriples"
47 # predefined queries
48 QUERY_SIZE="SELECT (COUNT(*) as ?C) WHERE {?s ?p ?o}"
49 QUERY_DELETEALL="DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
50 QUERY_HOTSPOT_SIZE="SELECT (COUNT(*) as ?C) WHERE {?h <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://teleios.di.uoa.gr/ontologies/noaOntology.owl#Hotspot>}"
52 # debug option for log4j configuration:
53 #-Dlog4j.debug
54 #-Dlog4j.configuration=\"${RUNTIME}/log4j.properties\"
56 # just print what shall be executed
57 DEBUG=false
59 # if true, DEBUG has been set in command line,
60 # so it must not be overidden
61 DEBUG_SAVE=false
63 # configuration file for the Strabon connection
64 STRABON_CONF="${HOME}/.strabon"
66 function help() {
67 echo "Usage: ${CMD} [OPTIONS] COMMAND ARGS"
68 echo
69 echo "Interface to execute the main classes of Strabon, such as QueryOp, StoreOp, UpdateOp, DescribeOp, etc."
70 echo
71 echo " COMMAND : one of \`query', \`update', \`store', \`describe', or \`help'"
72 echo " ARGS : arguments according to selected command"
73 echo
74 echo "OPTIONS can be any of the following (variable names and values are case sensitive)"
75 echo " -d : don't run, just print what shall be executed"
76 echo " Variable for configuration file: \`DEBUG'"
77 echo " Values: \`true' or \`false'"
78 echo " -e DATABASE : the database engine to connect (one of \`postgis' (default) or \`monetdb')"
79 echo " Variable for configuration file: \`DATABASE'"
80 echo " Values: \`postgis' or \`monetdb'"
81 echo " -db DB : the database to connect to (defaults to \`${DB}')"
82 echo " Variable for configuration file: \`DB'"
83 echo " -p PORT : the port to use for the database connection"
84 echo " : (defaults to 5432 for postgis and 50000 for monetdb)"
85 echo " Variable for configuration file: \`PORT'"
86 echo " -h HOSTNAME : the hostname to use for the database connection (defaults to \`${HOST}')"
87 echo " Variable for configuration file: \`HOST'"
88 echo " -u USERNAME : the username for the database connection"
89 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
90 echo " Variable for configuration file: \`DBUSER'"
91 echo " -pass PASS : the password for the database connection"
92 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
93 echo " Variable for configuration file: \`DBPASS'"
94 echo " -c FILE : configuration file to use for the connection (defaults to \`${STRABON_CONF}')."
95 echo " If the default configuration file exists, it is read. Options specified in the"
96 echo " command line override their values already set by the configuration file."
97 }
99 function help_query() {
100 echo "Usage: ${CMD} query SPARQL_QUERY [RESULT_FORMAT]"
101 echo
102 echo "Execute a SPARQL query on Strabon."
103 echo
104 echo " SPARQL_QUERY : the SPARQL query to execute or an alias name such as the following:"
105 echo " size: returns the number of triples"
106 echo " hotspots: returns the number of hotspots"
107 echo " RESULT_FORMAT : the format of the result. Possible values are \`???' (default), \`xml'"
108 echo " \`html', \`kml', \`kmz', or \`geojson'"
109 }
111 function help_update() {
112 echo "Usage: ${CMD} update SPARQL_UPDATE"
113 echo
114 echo "Execute a SPARQL Update query on Strabon."
115 echo
116 echo " SPARQL_UPDATE : the SPARQL update query to execute or an alias name such as the"
117 echo " the following:"
118 echo " clear: deletes all triples"
119 }
121 function help_store() {
122 echo "Usage: ${CMD} store [OPTIONS] FILE..."
123 echo
124 echo "Store RDF documents in Strabon."
125 echo
126 echo " FILE : the file containing the RDF document to store. It can be a filename or a URL,"
127 echo " (i.e., file:///tmp/file.nt, http://www.example.org/file.nt,"
128 echo " ftp://www.example.org/file.nt, etc.)."
129 echo
130 echo "OPTIONS can be one of the following"
131 echo " -f FORMAT : the RDF format of the files to store. The format can be one of the following:"
132 echo " \`ntriples' (default), \`n3', \`rdfxml', or \`turtle'."
133 }
135 function help_describe() {
136 echo "Usage: ${CMD} describe DESCRIBE_QUERY [RESULT_FORMAT]"
137 echo
138 echo "Execute a SPARQL DESCRIBE query on Strabon."
139 echo
140 echo " DESCRIBE_QUERY : the SPARQL DESCRIBE query to execute"
141 echo " RESULT_FORMAT : the format of the result. Possible values are \`N-Triples', "
142 echo " \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
143 echo " (defaults to N-Triples)"
144 }
146 # read configuration script first
147 if test -e "${STRABON_CONF}"; then
148 . ${STRABON_CONF}
149 fi
151 if ${DEBUG}; then
152 echo "${CMD}: debug is ON (from configuration file ${STRABON_CONF})"
153 fi
155 # read script options
156 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
157 case "${1}" in
158 --help)
159 help
160 exit 0
161 ;;
162 -d)
163 shift
164 if ! ${DEBUG}; then
165 echo "${CMD}: debug is ON"
166 fi
167 DEBUG=true
168 DEBUG_SAVE=true
169 ;;
170 -e)
171 shift
172 if ! test $# -gt 1; then
173 help
174 exit 1
175 fi
177 DATABASE=${1}
178 case "${DATABASE}" in
179 monetdb|postgis)
180 shift
181 ;;
182 postgis)
183 shift
184 ;;
185 *)
186 echo "${CMD}: invalid database engine"
187 echo "${CMD}: valid database engines are \`postgis' or \`monetdb'"
188 exit 2
189 ;;
190 esac
191 ;;
192 -db)
193 shift
194 if ! test $# -gt 1; then
195 help
196 exit 1
197 fi
198 DB="${1}"
199 shift
200 ;;
201 -p)
202 shift
203 if ! test $# -gt 1; then
204 help
205 exit 1
206 fi
207 PORT=${1}
208 shift
209 ;;
210 -h)
211 shift
212 if ! test $# -gt 1; then
213 help
214 exit 1
215 fi
216 HOST=${1}
217 shift
218 ;;
219 -u)
220 shift
221 if ! test $# -gt 1; then
222 help
223 exit 1
224 fi
225 DBUSER=${1}
226 shift
227 ;;
228 -pass)
229 shift
230 if ! test $# -gt 1; then
231 help
232 exit 1
233 fi
234 DBPASS=${1}
235 shift
236 ;;
237 -c)
238 shift
239 if ! test $# -gt 1; then
240 help
241 exit 1
242 fi
244 STRABON_CONF="${1}"
245 if ! test -e "${STRABON_CONF}"; then
246 echo "${CMD}: configuration file \"${1}\" does not exist"
247 exit 1
248 else
249 # if true, then debug has been set ON in command line
250 if $DEBUG; then
251 echo "${CMD}: Reading configuration file \"${STRABON_CONF}\""
252 fi
254 . ${STRABON_CONF}
256 if $DEBUG_SAVE; then
257 # restore debug value set on command line
258 DEBUG=true
259 fi
260 shift
261 fi
262 ;;
263 -*)
264 echo "${CMD}: unknown option \"${1}\""
265 help
266 exit 1
267 ;;
268 esac
269 done
271 # set defaults
272 case "${DATABASE}" in
273 postgis)
274 if test -z "${PORT}";
275 then
276 PORT=5432
277 fi
279 if test -z "${DBUSER}";
280 then
281 DBUSER="postgres"
282 fi
284 if test -z "${DBPASS}";
285 then
286 DBPASS="postgres"
287 fi
288 ;;
289 monetdb)
290 if test -z "${PORT}";
291 then
292 PORT=50000
293 fi
295 if test -z "${DBUSER}";
296 then
297 DBUSER="monetdb"
298 fi
300 if test -z "${DBPASS}";
301 then
302 DBPASS="monetdb"
303 fi
304 ;;
305 esac
307 # print configuration/options
308 if ${DEBUG}; then
309 echo "${CMD}: printing database connection details"
310 echo " Database Engine : ${DATABASE}"
311 echo " Database Name : ${DB}"
312 echo " Hostname : ${HOST}"
313 echo " Port : ${PORT}"
314 echo " Username : ${DBUSER}"
315 echo " Password : ${DBPASS}"
316 fi
318 # determine command to execute
319 case "${1}" in
320 help)
321 shift
322 if test $# -ge 1; then
323 case "${1}" in
324 query)
325 help_query
326 ;;
327 update)
328 help_update
329 ;;
330 store)
331 help_store
332 ;;
333 describe)
334 help_describe
335 ;;
336 *)
337 help
338 ;;
339 esac
340 exit 1
341 fi
342 help
343 exit 0
344 ;;
345 query)
346 CLASS="QueryOp"
347 shift
348 if ! test $# -ge 1; then
349 help_query
350 exit 1
351 fi
352 QUERY="${1}"
353 shift
355 # check for predefined queries
356 case "${QUERY}" in
357 size)
358 QUERY="${QUERY_SIZE}"
359 ;;
360 hotspots)
361 QUERY="${QUERY_HOTSPOT_SIZE}"
362 ;;
363 esac
365 # check for format of result
366 if test $# -gt 0; then
367 RESULT_FORMAT="${1}"
368 shift
369 case "${RESULT_FORMAT}" in
370 [xX][mM][lL]|[hH][tT][mM][lL]|[kK][mM][lL]|[kK][mM][zZ]|[gG][eE][oO][jJ][sS][oO][nN])
371 ;;
372 *)
373 echo "${CMD}: invalid result format \"${RESULT_FORMAT}\""
374 echo "${CMD}: valid formats are \`???' (default), \`xml', \`html', \`kml', \`kmz', or \`geojson'"
375 exit 2
376 ;;
377 esac
378 fi
379 ;;
380 update)
381 CLASS="UpdateOp"
382 shift
383 if ! test $# -eq 1; then
384 help_update
385 exit 1
386 fi
387 QUERY="${1}"
389 # check for predefined queries
390 case "${QUERY}" in
391 clear)
392 QUERY="${QUERY_DELETEALL}"
393 ;;
394 esac
395 ;;
396 store)
397 CLASS="StoreOp"
398 shift
399 if ! test $# -ge 1; then
400 help_store
401 exit 1
402 fi
403 # check whether format is specified
404 if test "${1}" = "-f"; then
405 shift
406 if ! test $# -ge 1; then
407 echo "${CMD}: Option -f requires an RDF format (\`ntriples', \`n3', \`rdfxml', or \`turtle')"
408 exit 2
409 else
410 FORMAT="${1}"
411 case "${FORMAT}" in
412 ntriples|n3|rdfxml|turtle)
413 shift
414 ;;
415 *)
416 echo "${CMD}: invalid RDF format \"${FORMAT}\"."
417 echo "${CMD}: valid RDF formats are \`ntriples', \`n3', \`rdfxml', or \`turtle'"
418 exit 2
419 ;;
420 esac
421 fi
422 fi
423 # if no files are given
424 if ! test $# -ge 1; then
425 help_store
426 exit 1
427 fi
428 # do not make an assignment of the files to QUERY
429 # handle the case of storing multiple files afterwards
430 # QUERY="${@}"
431 ;;
432 describe)
433 CLASS="DescribeOp"
434 shift
435 if ! test $# -ge 1; then
436 help_describe
437 exit 1
438 fi
439 QUERY="${1}"
440 shift
442 # check for format of result
443 if test $# -gt 0; then
444 RESULT_FORMAT="${1}"
445 shift
446 case "${RESULT_FORMAT}" in
447 N-Triples|RDF/XML|N3|TURTLE|TRIG|TRIX|BinaryRDF)
448 ;;
449 *)
450 echo "${CMD}: invalid result format \"${RESULT_FORMAT}\""
451 echo "${CMD}: valid formats are \`N-Triples', \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
452 exit 2
453 ;;
454 esac
455 fi
456 ;;
457 "")
458 help
459 exit 1
460 ;;
461 *)
462 help
463 echo
464 echo "${CMD}: unknown command \"${1}\"."
465 exit 1
466 ;;
467 esac
469 # compile command to execute
470 if test "${CLASS}" = "StoreOp"; then
471 STRABON_EXEC=
472 for file in "${@}"; do
473 # check whether a relative path was given
474 if ! test "${file:0:7}" == "file://" -o "${file:0:7}" == "http://" -o "${file:0:1}" == "/"; then
475 file="$(pwd)/${file}"
476 fi
478 STRABON_EXEC="${STRABON_EXEC}(cd ${RUNTIME} && java -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${file}\" ${FORMAT});
479 "
480 done
481 else
482 STRABON_EXEC="(cd ${RUNTIME} && java -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${QUERY}\" ${RESULT_FORMAT})"
483 fi
485 # execute command or debug
486 if ${DEBUG}; then
487 echo "${CMD}: printing command for execution"
488 echo "${STRABON_EXEC}"
489 else
490 eval "${STRABON_EXEC}"
491 fi