Strabon

view scripts/strabon @ 611:66736f11dee8

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