Strabon

view scripts/strabon @ 317:ca965a7829ba

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