Strabon

view scripts/strabon @ 646:300e958d034b

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