Strabon

view scripts/strabon @ 308:7f1078abc31f

Added the Geonames parser
author Kostis Kyzirakos <kkyzir@di.uoa.gr>
date Wed Jun 20 19:03:13 2012 +0300 (2012-06-20)
parents eaef3863cd22
children ca965a7829ba
line source
1 #!/bin/bash
2 #
3 # Script for running the main classes of Strabon. The main classes of Strabon comprises
4 # QueryOp, , UpdateOp, and StoreOp.
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}"
51 # debug option for log4j configuration:
52 #-Dlog4j.debug
53 #-Dlog4j.configuration=\"${RUNTIME}/log4j.properties\"
55 # just print what shall be executed
56 DEBUG=false
58 # if true, DEBUG has been set in command line,
59 # so it must not be overidden
60 DEBUG_SAVE=false
62 # configuration file for the Strabon connection
63 STRABON_CONF="${HOME}/.strabon"
65 function help() {
66 echo "Usage: ${CMD} [OPTIONS] COMMAND ARGS"
67 echo
68 echo "Interface to execute the main classes of Strabon, such as QueryOp, StoreOp, UpdateOp, etc."
69 echo
70 echo " COMMAND : one of \`query', \`update', \`store', or \`help'"
71 echo " ARGS : arguments according to selected command"
72 echo
73 echo "OPTIONS can be any of the following (variable names and values are case sensitive)"
74 echo " -d : don't run, just print what shall be executed"
75 echo " Variable for configuration file: \`DEBUG'"
76 echo " Values: \`true' or \`false'"
77 echo " -e DATABASE : the database engine to connect (one of \`postgis' (default) or \`monetdb')"
78 echo " Variable for configuration file: \`DATABASE'"
79 echo " Values: \`postgis' or \`monetdb'"
80 echo " -db DB : the database to connect to (defaults to \`${DB}')"
81 echo " Variable for configuration file: \`DB'"
82 echo " -p PORT : the port to use for the database connection"
83 echo " : (defaults to 5432 for postgis and 50000 for monetdb)"
84 echo " Variable for configuration file: \`PORT'"
85 echo " -h HOSTNAME : the hostname to use for the database connection (defaults to \`${HOST}')"
86 echo " Variable for configuration file: \`HOST'"
87 echo " -u USERNAME : the username for the database connection"
88 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
89 echo " Variable for configuration file: \`DBUSER'"
90 echo " -pass PASS : the password for the database connection"
91 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
92 echo " Variable for configuration file: \`DBPASS'"
93 echo " -c FILE : configuration file to use for the connection (defaults to \`${STRABON_CONF}')."
94 echo " If the default configuration file exists, it is read. Options specified in the"
95 echo " command line override their values already set by the configuration file."
96 }
98 function help_query() {
99 echo "Usage: ${CMD} query SPARQL_QUERY [RESULT_FORMAT]"
100 echo
101 echo "Execute a SPARQL query on Strabon."
102 echo
103 echo " SPARQL_QUERY : the SPARQL query to execute or an alias name such as the following:"
104 echo " size: returns the number of triples"
105 echo " RESULT_FROM : the format of the result. Possible values are \`???' (default), \`xml'"
106 echo " \`html', \`kml', \`kmz', or \`geojson'"
107 }
109 function help_update() {
110 echo "Usage: ${CMD} update SPARQL_UPDATE"
111 echo
112 echo "Execute a SPARQL Update query on Strabon."
113 echo
114 echo " SPARQL_UPDATE : the SPARQL update query to execute or an alias name such as the"
115 echo " the following:"
116 echo " clear: deletes all triples"
117 }
119 function help_store() {
120 echo "Usage: ${CMD} store [OPTIONS] FILE..."
121 echo
122 echo "Store RDF documents in Strabon."
123 echo
124 echo " FILE : the file containing the RDF document to store. It can be a filename or a URL,"
125 echo " (i.e., file:///tmp/file.nt, http://www.example.org/file.nt,"
126 echo " ftp://www.example.org/file.nt, etc.)."
127 echo
128 echo "OPTIONS can be one of the following"
129 echo " -f FORMAT : the RDF format of the files to store. The format can be one of the following:"
130 echo " \`ntriples' (default), \`n3', \`rdfxml', or \`turtle'."
131 }
134 # read configuration script first
135 if test -e "${STRABON_CONF}"; then
136 . ${STRABON_CONF}
137 fi
139 if ${DEBUG}; then
140 echo "${CMD}: debug is ON (from configuration file ${STRABON_CONF})"
141 fi
143 # read script options
144 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
145 case "${1}" in
146 --help)
147 help
148 exit 0
149 ;;
150 -d)
151 shift
152 if ! ${DEBUG}; then
153 echo "${CMD}: debug is ON"
154 fi
155 DEBUG=true
156 DEBUG_SAVE=true
157 ;;
158 -e)
159 shift
160 if ! test $# -gt 1; then
161 help
162 exit 1
163 fi
165 DATABASE=${1}
166 case "${DATABASE}" in
167 monetdb|postgis)
168 shift
169 ;;
170 postgis)
171 shift
172 ;;
173 *)
174 echo "${CMD}: invalid database engine"
175 echo "${CMD}: valid database engines are \`postgis' or \`monetdb'"
176 exit 2
177 ;;
178 esac
179 ;;
180 -db)
181 shift
182 if ! test $# -gt 1; then
183 help
184 exit 1
185 fi
186 DB="${1}"
187 shift
188 ;;
189 -p)
190 shift
191 if ! test $# -gt 1; then
192 help
193 exit 1
194 fi
195 PORT=${1}
196 shift
197 ;;
198 -h)
199 shift
200 if ! test $# -gt 1; then
201 help
202 exit 1
203 fi
204 HOST=${1}
205 shift
206 ;;
207 -u)
208 shift
209 if ! test $# -gt 1; then
210 help
211 exit 1
212 fi
213 DBUSER=${1}
214 shift
215 ;;
216 -pass)
217 shift
218 if ! test $# -gt 1; then
219 help
220 exit 1
221 fi
222 DBPASS=${1}
223 shift
224 ;;
225 -c)
226 shift
227 if ! test $# -gt 1; then
228 help
229 exit 1
230 fi
232 STRABON_CONF="${1}"
233 if ! test -e "${STRABON_CONF}"; then
234 echo "${CMD}: configuration file \"${1}\" does not exist"
235 exit 1
236 else
237 # if true, then debug has been set ON in command line
238 if $DEBUG; then
239 echo "${CMD}: Reading configuration file \"${STRABON_CONF}\""
240 fi
242 . ${STRABON_CONF}
244 if $DEBUG_SAVE; then
245 # restore debug value set on command line
246 DEBUG=true
247 fi
248 shift
249 fi
250 ;;
251 -*)
252 echo "${CMD}: unknown option \"${1}\""
253 help
254 exit 1
255 ;;
256 esac
257 done
259 # set defaults
260 case "${DATABASE}" in
261 postgis)
262 if test -z "${PORT}";
263 then
264 PORT=5432
265 fi
267 if test -z "${DBUSER}";
268 then
269 DBUSER="postgres"
270 fi
272 if test -z "${DBPASS}";
273 then
274 DBPASS="postgres"
275 fi
276 ;;
277 monetdb)
278 if test -z "${PORT}";
279 then
280 PORT=50000
281 fi
283 if test -z "${DBUSER}";
284 then
285 DBUSER="monetdb"
286 fi
288 if test -z "${DBPASS}";
289 then
290 DBPASS="monetdb"
291 fi
292 ;;
293 esac
295 # print configuration/options
296 if ${DEBUG}; then
297 echo "${CMD}: printing database connection details"
298 echo " Database Engine : ${DATABASE}"
299 echo " Database Name : ${DB}"
300 echo " Hostname : ${HOST}"
301 echo " Port : ${PORT}"
302 echo " Username : ${DBUSER}"
303 echo " Password : ${DBPASS}"
304 fi
306 # determine command to execute
307 case "${1}" in
308 help)
309 shift
310 if test $# -ge 1; then
311 case "${1}" in
312 query)
313 help_query
314 ;;
315 update)
316 help_update
317 ;;
318 store)
319 help_store
320 ;;
321 *)
322 help
323 ;;
324 esac
325 exit 1
326 fi
327 help
328 exit 0
329 ;;
330 query)
331 CLASS="QueryOp"
332 shift
333 if ! test $# -ge 1; then
334 help_query
335 exit 1
336 fi
337 QUERY="${1}"
338 shift
340 # check for predefined queries
341 case "${QUERY}" in
342 size)
343 QUERY="${QUERY_SIZE}"
344 ;;
345 esac
347 # check for format of result
348 if test $# -gt 0; then
349 RESULT_FORMAT="${1}"
350 shift
351 case "${RESULT_FORMAT}" in
352 [xX][mM][lL]|[hH][tT][mM][lL]|[kK][mM][lL]|[kK][mM][zZ]|[gG][eE][oO][jJ][sS][oO][nN])
353 ;;
354 *)
355 echo "${CMD}: invalid result format \"${RESULT_FORMAT}\""
356 echo "${CMD}: valid formats are \`???' (default), \`xml', \`html', \`kml', \`kmz', or \`geojson'"
357 exit 2
358 ;;
359 esac
360 fi
361 ;;
362 update)
363 CLASS="UpdateOp"
364 shift
365 if ! test $# -eq 1; then
366 help_update
367 exit 1
368 fi
369 QUERY="${1}"
371 # check for predefined queries
372 case "${QUERY}" in
373 clear)
374 QUERY="${QUERY_DELETEALL}"
375 ;;
376 esac
377 ;;
378 store)
379 CLASS="StoreOp"
380 shift
381 if ! test $# -ge 1; then
382 help_store
383 exit 1
384 fi
385 # check whether format is specified
386 if test "${1}" = "-f"; then
387 shift
388 if ! test $# -ge 1; then
389 echo "${CMD}: Option -f requires an RDF format (\`ntriples', \`n3', \`rdfxml', or \`turtle')"
390 exit 2
391 else
392 FORMAT="${1}"
393 case "${FORMAT}" in
394 ntriples|n3|rdfxml|turtle)
395 shift
396 ;;
397 *)
398 echo "${CMD}: invalid RDF format \"${FORMAT}\"."
399 echo "${CMD}: valid RDF formats are \`ntriples', \`n3', \`rdfxml', or \`turtle'"
400 exit 2
401 ;;
402 esac
403 fi
404 fi
405 # if no files are given
406 if ! test $# -ge 1; then
407 help_store
408 exit 1
409 fi
410 # do not make an assignment of the files to QUERY
411 # handle the case of storing multiple files afterwards
412 # QUERY="${@}"
413 ;;
414 "")
415 help
416 exit 1
417 ;;
418 *)
419 help
420 echo
421 echo "${CMD}: unknown command \"${1}\"."
422 exit 1
423 ;;
424 esac
426 # compile command to execute
427 if test "${CLASS}" = "StoreOp"; then
428 STRABON_EXEC=
429 for file in "${@}"; do
430 STRABON_EXEC="${STRABON_EXEC}(cd ${RUNTIME} && java -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${file}\" ${FORMAT});
431 "
432 done
433 else
434 STRABON_EXEC="(cd ${RUNTIME} && java -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${QUERY}\" ${RESULT_FORMAT})"
435 fi
437 # execute command or debug
438 if ${DEBUG}; then
439 echo "${CMD}: printing command for execution"
440 echo "${STRABON_EXEC}"
441 else
442 eval ${STRABON_EXEC}
443 fi