Strabon

view scripts/strabon @ 919:6c8b5e31d7fe

addresses bug #31 (empty literals with wktLiteral datatype are regarded as empty geometries, i.e., MULTIPOLYGON EMPTY)
author Babis Nikolaou <charnik@di.uoa.gr>
date Mon Mar 25 00:04:09 2013 +0200 (2013-03-25)
parents 5a82ff362b8d
children a1e2561b7c2c 49d07fd6367e
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 running the main classes of Strabon. The main classes of Strabon comprises
15 # QueryOp, , UpdateOp, StoreOp, and DescribeOp.
16 #
17 # Author: Charalampos (Babis) Nikolaou <charnik@di.uoa.gr>
18 #
20 # command name
21 CMD="$(basename ${0})"
23 # absolute directory name of this command
24 LOC="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
26 RUNTIME="${LOC}/../runtime"
28 # runtime package
29 PKG="eu.earthobservatory.runtime"
31 # the underlying database to use (one of `postgis' or `monetdb')
32 DATABASE="postgis"
34 # the main class to run
35 CLASS=
37 # the hostname at which the database runs
38 HOST="localhost"
40 # the port at which the database listens
41 PORT=
43 # the database name to connect to
44 DB="strabon"
46 # the username for the database connection
47 DBUSER=
49 # the password for the database connection
50 DBPASS=
52 # the query to run
53 QUERY=
55 # the RDF format of the files to store (defaults to ntriples)
56 FORMAT="ntriples"
58 # true to force deletion of locked table, false otherwise
59 FORCE_DELETE="false"
61 # the URI of the named graph into which the RDF files shall be stored
62 NAMED_GRAPH=
64 # predefined queries
65 QUERY_SIZE="SELECT (COUNT(*) as ?C) WHERE {?s ?p ?o}"
66 QUERY_GETALL="SELECT * WHERE {?s ?p ?o}"
67 QUERY_DELETEALL="DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
68 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>}"
69 QUERY_EXPORT="CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}"
71 # debug option for log4j configuration:
72 #-Dlog4j.debug
73 #-Dlog4j.configuration=\"${RUNTIME}/log4j.properties\"
75 # further options for java
76 JAVA_OPTS=
78 # base max memory memory
79 BASE_MEMORY=1024
81 # more memory
82 MORE_MEMORY="-Xms512M -Xmx1024M"
84 # much more memory
85 MMORE_MEMORY="-Xms512M -Xmx1536M"
87 # flag for not setting the memory limit more than once
88 MEMORY_INCREASED=false
90 # just print what shall be executed
91 DEBUG=false
93 # if true, DEBUG has been set in command line,
94 # so it must not be overidden
95 DEBUG_SAVE=false
97 # Filename containing the prefixes to be included in SPARQL queries
98 PREFIXES_FILE="${LOC}/prefixes.sparql"
100 # configuration file for the Strabon connection
101 STRABON_CONF="${HOME}/.strabon"
103 function help() {
104 echo "Usage: ${CMD} [OPTIONS] COMMAND ARGS"
105 echo
106 echo "Interface to execute the main classes of Strabon, such as QueryOp, StoreOp, UpdateOp, DescribeOp, etc."
107 echo
108 echo " COMMAND : one of \`query', \`update', \`store', \`describe', or \`help'"
109 echo " ARGS : arguments according to selected command"
110 echo
111 echo "OPTIONS can be any of the following (variable names and values are case sensitive)"
112 echo " -d : don't run, just print what shall be executed"
113 echo " Variable for configuration file: \`DEBUG'"
114 echo " Values: \`true' or \`false'"
115 echo " -m : use more memory \`${MORE_MEMORY}' (useful in \"out of memory exceptions\")"
116 echo " -M : use much more memory \`${MMORE_MEMORY}' (useful in \"out of memory exceptions\")"
117 echo " -MM MULT : use MULT * ${BASE_MEMORY} MB of memory (useful in \"out of memory exceptions\")"
118 echo " -i : include URI prefixes in the SPARQL query. Prefixes are taken from file"
119 echo " \`prefixes.sparql'"
120 echo " -e DATABASE : the database engine to connect (one of \`postgis' (default) or \`monetdb')"
121 echo " Variable for configuration file: \`DATABASE'"
122 echo " Values: \`postgis' or \`monetdb'"
123 echo " -db DB : the database to connect to (defaults to \`${DB}')"
124 echo " Variable for configuration file: \`DB'"
125 echo " -p PORT : the port to use for the database connection"
126 echo " : (defaults to 5432 for postgis and 50000 for monetdb)"
127 echo " Variable for configuration file: \`PORT'"
128 echo " -h HOSTNAME : the hostname to use for the database connection (defaults to \`${HOST}')"
129 echo " Variable for configuration file: \`HOST'"
130 echo " -u USERNAME : the username for the database connection"
131 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
132 echo " Variable for configuration file: \`DBUSER'"
133 echo " -pass PASS : the password for the database connection"
134 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
135 echo " Variable for configuration file: \`DBPASS'"
136 echo " -c FILE : configuration file to use for the connection (defaults to \`${STRABON_CONF}')."
137 echo " If the default configuration file exists, it is read. Options specified in the"
138 echo " command line override their values already set by the configuration file."
139 }
141 function help_query() {
142 echo "Usage: ${CMD} query [OPTIONS] SPARQL_QUERY [RESULT_FORMAT]"
143 echo
144 echo "Execute a SPARQL query on Strabon."
145 echo
146 echo " SPARQL_QUERY : the SPARQL query to execute or an alias name such as the following:"
147 echo " size: returns the number of triples"
148 echo " all: returns all triples"
149 echo " hotspots: returns the number of hotspots"
150 echo " RESULT_FORMAT : the format of the result. Possible values are \`???' (default), \`xml'"
151 echo " \`html', \`kml', \`kmz', or \`geojson'"
152 echo
153 echo "OPTIONS can be one of the following"
154 echo " --force-delete : forces deletion of \"locked\" table (e.g., when Strabon has been"
155 echo " ungracefully shutdown)"
156 }
158 function help_update() {
159 echo "Usage: ${CMD} update SPARQL_UPDATE"
160 echo
161 echo "Execute a SPARQL Update query on Strabon."
162 echo
163 echo " SPARQL_UPDATE : the SPARQL update query to execute or an alias name such as the"
164 echo " the following:"
165 echo " clear: deletes all triples"
166 }
168 function help_store() {
169 echo "Usage: ${CMD} store [OPTIONS] FILE..."
170 echo
171 echo "Store RDF documents in Strabon."
172 echo
173 echo " FILE : the file containing the RDF document to store. It can be a filename or a URL,"
174 echo " (i.e., file:///tmp/file.nt, http://www.example.org/file.nt,"
175 echo " ftp://www.example.org/file.nt, etc.)."
176 echo
177 echo "OPTIONS can be one of the following"
178 echo " -f FORMAT : the RDF format of the files to store. The format can be one of the following:"
179 echo " \`ntriples' (default), \`n3', \`rdfxml', or \`turtle'."
180 echo " -g NAMED_GRAPH : the URI of the named graph into which the RDF files shall be stored"
181 echo " (defaults to the default graph)."
182 }
184 function help_describe() {
185 echo "Usage: ${CMD} describe DESCRIBE_QUERY [RESULT_FORMAT]"
186 echo
187 echo "Execute a SPARQL DESCRIBE query on Strabon."
188 echo
189 echo " DESCRIBE_QUERY : the SPARQL DESCRIBE query to execute or an alias name such as the following:"
190 echo " export: returns all triples stored in the database"
191 echo " RESULT_FORMAT : the format of the result. Possible values are \`N-Triples', "
192 echo " \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
193 echo " (defaults to N-Triples)"
194 }
196 # read configuration script first
197 if test -e "${STRABON_CONF}"; then
198 . ${STRABON_CONF}
199 fi
201 if ${DEBUG}; then
202 echo "${CMD}: debug is ON (from configuration file ${STRABON_CONF})"
203 fi
205 # read script options
206 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
207 case "${1}" in
208 --help)
209 help
210 exit 0
211 ;;
212 -d)
213 shift
214 if ! ${DEBUG}; then
215 echo "${CMD}: debug is ON"
216 fi
217 DEBUG=true
218 DEBUG_SAVE=true
219 ;;
220 -m)
221 if ! ${MEMORY_INCREASED}; then
222 JAVA_OPTS="${JAVA_OPTS} ${MORE_MEMORY}"
223 MEMORY_INCREASED=true
224 else
225 echo "${CMD}: memory has already been increased; option \`${1}' will be ignored."
226 fi
227 shift
228 ;;
229 -M)
230 if ! ${MEMORY_INCREASED}; then
231 JAVA_OPTS="${JAVA_OPTS} ${MMORE_MEMORY}"
232 MEMORY_INCREASED=true
233 else
234 echo "${CMD}: memory has already been increased; option \`${1}' will be ignored."
235 fi
236 shift
237 ;;
238 -MM)
239 shift
240 if ! test $# -gt 1; then
241 help
242 exit 1
243 fi
244 MULT=${1}
246 if ! ${MEMORY_INCREASED}; then
247 JAVA_OPTS="${JAVA_OPTS} -Xms512M -Xmx$((${MULT}*${BASE_MEMORY}))M"
248 MEMORY_INCREASED=true
249 else
250 echo "${CMD}: memory has already been increased; option \`${1}' will be ignored."
251 fi
252 shift
253 ;;
254 -i)
255 shift
256 PREFIXES="$(cat ${PREFIXES_FILE})
257 "
258 ;;
259 -e)
260 shift
261 if ! test $# -gt 1; then
262 help
263 exit 1
264 fi
266 DATABASE=${1}
267 case "${DATABASE}" in
268 monetdb|postgis)
269 shift
270 ;;
271 postgis)
272 shift
273 ;;
274 *)
275 echo "${CMD}: invalid database engine"
276 echo "${CMD}: valid database engines are \`postgis' or \`monetdb'"
277 exit 2
278 ;;
279 esac
280 ;;
281 -db)
282 shift
283 if ! test $# -gt 1; then
284 help
285 exit 1
286 fi
287 DB="${1}"
288 shift
289 ;;
290 -p)
291 shift
292 if ! test $# -gt 1; then
293 help
294 exit 1
295 fi
296 PORT=${1}
297 shift
298 ;;
299 -h)
300 shift
301 if ! test $# -gt 1; then
302 help
303 exit 1
304 fi
305 HOST=${1}
306 shift
307 ;;
308 -u)
309 shift
310 if ! test $# -gt 1; then
311 help
312 exit 1
313 fi
314 DBUSER=${1}
315 shift
316 ;;
317 -pass)
318 shift
319 if ! test $# -gt 1; then
320 help
321 exit 1
322 fi
323 DBPASS=${1}
324 shift
325 ;;
326 -c)
327 shift
328 if ! test $# -gt 1; then
329 help
330 exit 1
331 fi
333 STRABON_CONF="${1}"
334 if ! test -e "${STRABON_CONF}"; then
335 echo "${CMD}: configuration file \"${1}\" does not exist"
336 exit 1
337 else
338 # if true, then debug has been set ON in command line
339 if $DEBUG; then
340 echo "${CMD}: Reading configuration file \"${STRABON_CONF}\""
341 fi
343 . ${STRABON_CONF}
345 if $DEBUG_SAVE; then
346 # restore debug value set on command line
347 DEBUG=true
348 fi
349 shift
350 fi
351 ;;
352 -*)
353 echo "${CMD}: unknown option \"${1}\""
354 help
355 exit 1
356 ;;
357 esac
358 done
360 # set defaults
361 case "${DATABASE}" in
362 postgis)
363 if test -z "${PORT}";
364 then
365 PORT=5432
366 fi
368 if test -z "${DBUSER}";
369 then
370 DBUSER="postgres"
371 fi
373 if test -z "${DBPASS}";
374 then
375 DBPASS="postgres"
376 fi
377 ;;
378 monetdb)
379 if test -z "${PORT}";
380 then
381 PORT=50000
382 fi
384 if test -z "${DBUSER}";
385 then
386 DBUSER="monetdb"
387 fi
389 if test -z "${DBPASS}";
390 then
391 DBPASS="monetdb"
392 fi
393 ;;
394 esac
396 # print configuration/options
397 if ${DEBUG}; then
398 echo "${CMD}: printing database connection details"
399 echo " Database Engine : ${DATABASE}"
400 echo " Database Name : ${DB}"
401 echo " Hostname : ${HOST}"
402 echo " Port : ${PORT}"
403 echo " Username : ${DBUSER}"
404 echo " Password : ${DBPASS}"
405 fi
407 # determine command to execute
408 case "${1}" in
409 help)
410 shift
411 if test $# -ge 1; then
412 case "${1}" in
413 query)
414 help_query
415 ;;
416 update)
417 help_update
418 ;;
419 store)
420 help_store
421 ;;
422 describe)
423 help_describe
424 ;;
425 *)
426 help
427 ;;
428 esac
429 exit 1
430 fi
431 help
432 exit 0
433 ;;
434 query)
435 CLASS="QueryOp"
436 shift
437 if ! test $# -ge 1; then
438 help_query
439 exit 1
440 fi
442 # check whether force deletion of locked table has been specified
443 if test "${1}" = "--force-delete"; then
444 shift
445 FORCE_DELETE="true"
446 fi
448 QUERY="${1}"
449 shift
451 # check for predefined queries
452 case "${QUERY}" in
453 size)
454 QUERY="${QUERY_SIZE}"
455 ;;
456 hotspots)
457 QUERY="${QUERY_HOTSPOT_SIZE}"
458 ;;
459 all)
460 QUERY="${QUERY_GETALL}"
461 ;;
462 esac
464 # check for format of result
465 if test $# -gt 0; then
466 RESULT_FORMAT="${1}"
467 shift
468 case "${RESULT_FORMAT}" in
469 [xX][mM][lL]|[hH][tT][mM][lL]|[kK][mM][lL]|[kK][mM][zZ]|[gG][eE][oO][jJ][sS][oO][nN]|[tT][sS][vV]|[eE][xX][pP])
470 ;;
471 *)
472 echo "${CMD}: invalid result format \"${RESULT_FORMAT}\""
473 echo "${CMD}: valid formats are \`???' (default), \`xml', \`html', \`kml', \`kmz', or \`geojson'"
474 exit 2
475 ;;
476 esac
477 fi
478 ;;
479 update)
480 CLASS="UpdateOp"
481 shift
482 if ! test $# -eq 1; then
483 help_update
484 exit 1
485 fi
486 QUERY="${1}"
488 # check for predefined queries
489 case "${QUERY}" in
490 clear)
491 QUERY="${QUERY_DELETEALL}"
492 ;;
493 esac
494 ;;
495 store)
496 CLASS="StoreOp"
497 shift
498 if ! test $# -ge 1; then
499 help_store
500 exit 1
501 fi
503 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
504 # check whether format is specified
505 if test "${1}" = "-f"; then
506 shift
507 if ! test $# -ge 1; then
508 echo "${CMD}: Option -f requires an RDF format (\`ntriples', \`n3', \`rdfxml', or \`turtle')"
509 exit 2
510 else
511 FORMAT="${1}"
512 case "${FORMAT}" in
513 [nN][tT][rR][iI][pP][lL][eE][sS]|[nN]3|[rR][dD][fF][xX][mM][lL]|[tT][uU][rR][tT][lL][eE])
514 shift
515 ;;
516 *)
517 echo "${CMD}: invalid RDF format \"${FORMAT}\"."
518 echo "${CMD}: valid RDF formats are \`ntriples', \`n3', \`rdfxml', or \`turtle'"
519 exit 2
520 ;;
521 esac
522 fi
523 fi
525 # check whether a named graph is specified
526 if test "${1}" = "-g"; then
527 shift
528 if ! test $# -ge 1; then
529 echo "${CMD}: Option -g requires a URI argument"
530 exit 2
531 else
532 NAMED_GRAPH="-g ${1}"
533 shift
534 fi
535 fi
537 done
539 # if no files are given
540 if ! test $# -ge 1; then
541 help_store
542 exit 1
543 fi
545 # do not make an assignment of the files to QUERY
546 # handle the case of storing multiple files afterwards
547 # QUERY="${@}"
548 ;;
549 describe)
550 CLASS="DescribeOp"
551 shift
552 if ! test $# -ge 1; then
553 help_describe
554 exit 1
555 fi
556 QUERY="${1}"
557 shift
559 # check for predefined queries
560 case "${QUERY}" in
561 export)
562 QUERY="${QUERY_EXPORT}"
563 ;;
564 esac
566 # check for format of result
567 if test $# -gt 0; then
568 RESULT_FORMAT="${1}"
569 shift
570 case "${RESULT_FORMAT}" in
571 N-Triples|RDF/XML|N3|TURTLE|TRIG|TRIX|BinaryRDF)
572 ;;
573 *)
574 echo "${CMD}: invalid result format \"${RESULT_FORMAT}\""
575 echo "${CMD}: valid formats are \`N-Triples', \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
576 exit 2
577 ;;
578 esac
579 fi
580 ;;
581 "")
582 help
583 exit 1
584 ;;
585 *)
586 help
587 echo
588 echo "${CMD}: unknown command \"${1}\"."
589 exit 1
590 ;;
591 esac
593 # compile command to execute
594 if test "${CLASS}" = "StoreOp"; then
595 STRABON_EXEC=
596 for file in "${@}"; do
597 # check whether a relative path was given
598 if ! test "${file:0:7}" == "file://" -o "${file:0:7}" == "http://" -o "${file:0:1}" == "/"; then
599 file="$(pwd)/${file}"
600 fi
602 STRABON_EXEC="${STRABON_EXEC}(cd ${RUNTIME} && java ${JAVA_OPTS} -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${file}\" -f ${FORMAT} ${NAMED_GRAPH});
603 "
604 done
605 elif test "${CLASS}" = "QueryOp"; then
606 STRABON_EXEC="(cd ${RUNTIME} && java ${JAVA_OPTS} -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${PREFIXES}${QUERY}\" ${FORCE_DELETE} ${RESULT_FORMAT})"
607 else
608 STRABON_EXEC="(cd ${RUNTIME} && java ${JAVA_OPTS} -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${PREFIXES}${QUERY}\" ${RESULT_FORMAT})"
609 fi
611 # execute command or debug
612 if ${DEBUG}; then
613 echo "${CMD}: printing command for execution"
614 echo "${STRABON_EXEC}"
615 else
616 eval "${STRABON_EXEC}"
617 fi