Strabon

view scripts/strabon @ 865:152031be6487

merge
author George Garbis <ggarbis@di.uoa.gr>
date Wed Jan 23 12:50:41 2013 +0200 (2013-01-23)
parents af2605b62451
children 74ffcfcb691d cdfe73ee5c85
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 # the URI of the named graph into which the RDF files shall be stored
59 NAMED_GRAPH=
61 # predefined queries
62 QUERY_SIZE="SELECT (COUNT(*) as ?C) WHERE {?s ?p ?o}"
63 QUERY_DELETEALL="DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
64 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>}"
65 QUERY_EXPORT="CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}"
67 # debug option for log4j configuration:
68 #-Dlog4j.debug
69 #-Dlog4j.configuration=\"${RUNTIME}/log4j.properties\"
71 # further options for java
72 JAVA_OPTS=
74 # more memory
75 MORE_MEMORY="-Xms512M -Xmx1024M"
77 # much more memory
78 MMORE_MEMORY="-Xms512M -Xmx1536M"
80 # flag for not setting the memory limit more than once
81 MEMORY_INCREASED=false
83 # just print what shall be executed
84 DEBUG=false
86 # if true, DEBUG has been set in command line,
87 # so it must not be overidden
88 DEBUG_SAVE=false
90 # Filename containing the prefixes to be included in SPARQL queries
91 PREFIXES_FILE="${LOC}/prefixes.sparql"
93 # configuration file for the Strabon connection
94 STRABON_CONF="${HOME}/.strabon"
96 function help() {
97 echo "Usage: ${CMD} [OPTIONS] COMMAND ARGS"
98 echo
99 echo "Interface to execute the main classes of Strabon, such as QueryOp, StoreOp, UpdateOp, DescribeOp, etc."
100 echo
101 echo " COMMAND : one of \`query', \`update', \`store', \`describe', or \`help'"
102 echo " ARGS : arguments according to selected command"
103 echo
104 echo "OPTIONS can be any of the following (variable names and values are case sensitive)"
105 echo " -d : don't run, just print what shall be executed"
106 echo " Variable for configuration file: \`DEBUG'"
107 echo " Values: \`true' or \`false'"
108 echo " -m : use more memory \`${MORE_MEMORY}' (useful in \"out of memory exceptions\""
109 echo " -M : use much more memory \`${MMORE_MEMORY}' (useful in \"out of memory exceptions\""
110 echo " -i : include URI prefixes in the SPARQL query. Prefixes are taken from file"
111 echo " \`prefixes.sparql'"
112 echo " -e DATABASE : the database engine to connect (one of \`postgis' (default) or \`monetdb')"
113 echo " Variable for configuration file: \`DATABASE'"
114 echo " Values: \`postgis' or \`monetdb'"
115 echo " -db DB : the database to connect to (defaults to \`${DB}')"
116 echo " Variable for configuration file: \`DB'"
117 echo " -p PORT : the port to use for the database connection"
118 echo " : (defaults to 5432 for postgis and 50000 for monetdb)"
119 echo " Variable for configuration file: \`PORT'"
120 echo " -h HOSTNAME : the hostname to use for the database connection (defaults to \`${HOST}')"
121 echo " Variable for configuration file: \`HOST'"
122 echo " -u USERNAME : the username for the database connection"
123 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
124 echo " Variable for configuration file: \`DBUSER'"
125 echo " -pass PASS : the password for the database connection"
126 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
127 echo " Variable for configuration file: \`DBPASS'"
128 echo " -c FILE : configuration file to use for the connection (defaults to \`${STRABON_CONF}')."
129 echo " If the default configuration file exists, it is read. Options specified in the"
130 echo " command line override their values already set by the configuration file."
131 }
133 function help_query() {
134 echo "Usage: ${CMD} query SPARQL_QUERY [RESULT_FORMAT]"
135 echo
136 echo "Execute a SPARQL query on Strabon."
137 echo
138 echo " SPARQL_QUERY : the SPARQL query to execute or an alias name such as the following:"
139 echo " size: returns the number of triples"
140 echo " hotspots: returns the number of hotspots"
141 echo " RESULT_FORMAT : the format of the result. Possible values are \`???' (default), \`xml'"
142 echo " \`html', \`kml', \`kmz', or \`geojson'"
143 }
145 function help_update() {
146 echo "Usage: ${CMD} update SPARQL_UPDATE"
147 echo
148 echo "Execute a SPARQL Update query on Strabon."
149 echo
150 echo " SPARQL_UPDATE : the SPARQL update query to execute or an alias name such as the"
151 echo " the following:"
152 echo " clear: deletes all triples"
153 }
155 function help_store() {
156 echo "Usage: ${CMD} store [OPTIONS] FILE..."
157 echo
158 echo "Store RDF documents in Strabon."
159 echo
160 echo " FILE : the file containing the RDF document to store. It can be a filename or a URL,"
161 echo " (i.e., file:///tmp/file.nt, http://www.example.org/file.nt,"
162 echo " ftp://www.example.org/file.nt, etc.)."
163 echo
164 echo "OPTIONS can be one of the following"
165 echo " -f FORMAT : the RDF format of the files to store. The format can be one of the following:"
166 echo " \`ntriples' (default), \`n3', \`rdfxml', or \`turtle'."
167 echo " -g NAMED_GRAPH : the URI of the named graph into which the RDF files shall be stored"
168 echo " (defaults to the default graph)."
169 }
171 function help_describe() {
172 echo "Usage: ${CMD} describe DESCRIBE_QUERY [RESULT_FORMAT]"
173 echo
174 echo "Execute a SPARQL DESCRIBE query on Strabon."
175 echo
176 echo " DESCRIBE_QUERY : the SPARQL DESCRIBE query to execute or an alias name such as the following:"
177 echo " export: returns all triples stored in the database"
178 echo " RESULT_FORMAT : the format of the result. Possible values are \`N-Triples', "
179 echo " \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
180 echo " (defaults to N-Triples)"
181 }
183 # read configuration script first
184 if test -e "${STRABON_CONF}"; then
185 . ${STRABON_CONF}
186 fi
188 if ${DEBUG}; then
189 echo "${CMD}: debug is ON (from configuration file ${STRABON_CONF})"
190 fi
192 # read script options
193 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
194 case "${1}" in
195 --help)
196 help
197 exit 0
198 ;;
199 -d)
200 shift
201 if ! ${DEBUG}; then
202 echo "${CMD}: debug is ON"
203 fi
204 DEBUG=true
205 DEBUG_SAVE=true
206 ;;
207 -m)
208 if ! ${MEMORY_INCREASED}; then
209 JAVA_OPTS="${JAVA_OPTS} ${MORE_MEMORY}"
210 MEMORY_INCREASED=true
211 else
212 echo "${CMD}: memory has already been increased; option \`${1}' will be ignored."
213 fi
214 shift
215 ;;
216 -M)
217 if ! ${MEMORY_INCREASED}; then
218 JAVA_OPTS="${JAVA_OPTS} ${MMORE_MEMORY}"
219 MEMORY_INCREASED=true
220 else
221 echo "${CMD}: memory has already been increased; option \`${1}' will be ignored."
222 fi
223 shift
224 ;;
225 -i)
226 shift
227 PREFIXES="$(cat ${PREFIXES_FILE})
228 "
229 ;;
230 -e)
231 shift
232 if ! test $# -gt 1; then
233 help
234 exit 1
235 fi
237 DATABASE=${1}
238 case "${DATABASE}" in
239 monetdb|postgis)
240 shift
241 ;;
242 postgis)
243 shift
244 ;;
245 *)
246 echo "${CMD}: invalid database engine"
247 echo "${CMD}: valid database engines are \`postgis' or \`monetdb'"
248 exit 2
249 ;;
250 esac
251 ;;
252 -db)
253 shift
254 if ! test $# -gt 1; then
255 help
256 exit 1
257 fi
258 DB="${1}"
259 shift
260 ;;
261 -p)
262 shift
263 if ! test $# -gt 1; then
264 help
265 exit 1
266 fi
267 PORT=${1}
268 shift
269 ;;
270 -h)
271 shift
272 if ! test $# -gt 1; then
273 help
274 exit 1
275 fi
276 HOST=${1}
277 shift
278 ;;
279 -u)
280 shift
281 if ! test $# -gt 1; then
282 help
283 exit 1
284 fi
285 DBUSER=${1}
286 shift
287 ;;
288 -pass)
289 shift
290 if ! test $# -gt 1; then
291 help
292 exit 1
293 fi
294 DBPASS=${1}
295 shift
296 ;;
297 -c)
298 shift
299 if ! test $# -gt 1; then
300 help
301 exit 1
302 fi
304 STRABON_CONF="${1}"
305 if ! test -e "${STRABON_CONF}"; then
306 echo "${CMD}: configuration file \"${1}\" does not exist"
307 exit 1
308 else
309 # if true, then debug has been set ON in command line
310 if $DEBUG; then
311 echo "${CMD}: Reading configuration file \"${STRABON_CONF}\""
312 fi
314 . ${STRABON_CONF}
316 if $DEBUG_SAVE; then
317 # restore debug value set on command line
318 DEBUG=true
319 fi
320 shift
321 fi
322 ;;
323 -*)
324 echo "${CMD}: unknown option \"${1}\""
325 help
326 exit 1
327 ;;
328 esac
329 done
331 # set defaults
332 case "${DATABASE}" in
333 postgis)
334 if test -z "${PORT}";
335 then
336 PORT=5432
337 fi
339 if test -z "${DBUSER}";
340 then
341 DBUSER="postgres"
342 fi
344 if test -z "${DBPASS}";
345 then
346 DBPASS="postgres"
347 fi
348 ;;
349 monetdb)
350 if test -z "${PORT}";
351 then
352 PORT=50000
353 fi
355 if test -z "${DBUSER}";
356 then
357 DBUSER="monetdb"
358 fi
360 if test -z "${DBPASS}";
361 then
362 DBPASS="monetdb"
363 fi
364 ;;
365 esac
367 # print configuration/options
368 if ${DEBUG}; then
369 echo "${CMD}: printing database connection details"
370 echo " Database Engine : ${DATABASE}"
371 echo " Database Name : ${DB}"
372 echo " Hostname : ${HOST}"
373 echo " Port : ${PORT}"
374 echo " Username : ${DBUSER}"
375 echo " Password : ${DBPASS}"
376 fi
378 # determine command to execute
379 case "${1}" in
380 help)
381 shift
382 if test $# -ge 1; then
383 case "${1}" in
384 query)
385 help_query
386 ;;
387 update)
388 help_update
389 ;;
390 store)
391 help_store
392 ;;
393 describe)
394 help_describe
395 ;;
396 *)
397 help
398 ;;
399 esac
400 exit 1
401 fi
402 help
403 exit 0
404 ;;
405 query)
406 CLASS="QueryOp"
407 shift
408 if ! test $# -ge 1; then
409 help_query
410 exit 1
411 fi
412 QUERY="${1}"
413 shift
415 # check for predefined queries
416 case "${QUERY}" in
417 size)
418 QUERY="${QUERY_SIZE}"
419 ;;
420 hotspots)
421 QUERY="${QUERY_HOTSPOT_SIZE}"
422 ;;
423 esac
425 # check for format of result
426 if test $# -gt 0; then
427 RESULT_FORMAT="${1}"
428 shift
429 case "${RESULT_FORMAT}" in
430 [xX][mM][lL]|[hH][tT][mM][lL]|[kK][mM][lL]|[kK][mM][zZ]|[gG][eE][oO][jJ][sS][oO][nN]|[tT][sS][vV])
431 ;;
432 *)
433 echo "${CMD}: invalid result format \"${RESULT_FORMAT}\""
434 echo "${CMD}: valid formats are \`???' (default), \`xml', \`html', \`kml', \`kmz', or \`geojson'"
435 exit 2
436 ;;
437 esac
438 fi
439 ;;
440 update)
441 CLASS="UpdateOp"
442 shift
443 if ! test $# -eq 1; then
444 help_update
445 exit 1
446 fi
447 QUERY="${1}"
449 # check for predefined queries
450 case "${QUERY}" in
451 clear)
452 QUERY="${QUERY_DELETEALL}"
453 ;;
454 esac
455 ;;
456 store)
457 CLASS="StoreOp"
458 shift
459 if ! test $# -ge 1; then
460 help_store
461 exit 1
462 fi
464 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
465 # check whether format is specified
466 if test "${1}" = "-f"; then
467 shift
468 if ! test $# -ge 1; then
469 echo "${CMD}: Option -f requires an RDF format (\`ntriples', \`n3', \`rdfxml', or \`turtle')"
470 exit 2
471 else
472 FORMAT="${1}"
473 case "${FORMAT}" in
474 ntriples|n3|rdfxml|turtle)
475 shift
476 ;;
477 *)
478 echo "${CMD}: invalid RDF format \"${FORMAT}\"."
479 echo "${CMD}: valid RDF formats are \`ntriples', \`n3', \`rdfxml', or \`turtle'"
480 exit 2
481 ;;
482 esac
483 fi
484 fi
486 # check whether a named graph is specified
487 if test "${1}" = "-g"; then
488 shift
489 if ! test $# -ge 1; then
490 echo "${CMD}: Option -g requires a URI argument"
491 exit 2
492 else
493 NAMED_GRAPH="-g ${1}"
494 shift
495 fi
496 fi
498 done
500 # if no files are given
501 if ! test $# -ge 1; then
502 help_store
503 exit 1
504 fi
506 # do not make an assignment of the files to QUERY
507 # handle the case of storing multiple files afterwards
508 # QUERY="${@}"
509 ;;
510 describe)
511 CLASS="DescribeOp"
512 shift
513 if ! test $# -ge 1; then
514 help_describe
515 exit 1
516 fi
517 QUERY="${1}"
518 shift
520 # check for predefined queries
521 case "${QUERY}" in
522 export)
523 QUERY="${QUERY_EXPORT}"
524 ;;
525 esac
527 # check for format of result
528 if test $# -gt 0; then
529 RESULT_FORMAT="${1}"
530 shift
531 case "${RESULT_FORMAT}" in
532 N-Triples|RDF/XML|N3|TURTLE|TRIG|TRIX|BinaryRDF)
533 ;;
534 *)
535 echo "${CMD}: invalid result format \"${RESULT_FORMAT}\""
536 echo "${CMD}: valid formats are \`N-Triples', \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
537 exit 2
538 ;;
539 esac
540 fi
541 ;;
542 "")
543 help
544 exit 1
545 ;;
546 *)
547 help
548 echo
549 echo "${CMD}: unknown command \"${1}\"."
550 exit 1
551 ;;
552 esac
554 # compile command to execute
555 if test "${CLASS}" = "StoreOp"; then
556 STRABON_EXEC=
557 for file in "${@}"; do
558 # check whether a relative path was given
559 if ! test "${file:0:7}" == "file://" -o "${file:0:7}" == "http://" -o "${file:0:1}" == "/"; then
560 file="$(pwd)/${file}"
561 fi
563 STRABON_EXEC="${STRABON_EXEC}(cd ${RUNTIME} && java ${JAVA_OPTS} -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${file}\" -f ${FORMAT} ${NAMED_GRAPH});
564 "
565 done
566 else
567 STRABON_EXEC="(cd ${RUNTIME} && java ${JAVA_OPTS} -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${PREFIXES}${QUERY}\" ${RESULT_FORMAT})"
568 fi
570 # execute command or debug
571 if ${DEBUG}; then
572 echo "${CMD}: printing command for execution"
573 echo "${STRABON_EXEC}"
574 else
575 eval "${STRABON_EXEC}"
576 fi