Strabon

view scripts/strabon @ 241:6e556c0b0b77

fixed message when no options are given to strabon script
author Babis Nikolaou <charnik@di.uoa.gr>
date Sun Jun 03 17:57:55 2012 +0300 (2012-06-03)
parents 233f2ca0d7e3
children 6174ff9d242a
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"
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 }
107 function help_update() {
108 echo "Usage: ${CMD} update SPARQL_UPDATE"
109 echo
110 echo "Execute a SPARQL Update query on Strabon."
111 echo
112 echo " SPARQL_UPDATE : the SPARQL update query to execute or an alias name such as the"
113 echo " the following:"
114 echo " clear: deletes all triples"
115 }
117 function help_store() {
118 echo "Usage: ${CMD} store [OPTIONS] FILE..."
119 echo
120 echo "Store RDF documents in Strabon."
121 echo
122 echo " FILE : the file containing the RDF document to store. It can be a filename or a URL,"
123 echo " (i.e., file:///tmp/file.nt, http://www.example.org/file.nt,"
124 echo " ftp://www.example.org/file.nt, etc.)."
125 echo
126 echo "OPTIONS can be one of the following"
127 echo " -f FORMAT : the RDF format of the files to store. The format can be one of the following:"
128 echo " \`ntriples' (default), \`n3', \`rdfxml', or \`turtle'."
129 }
132 # read configuration script first
133 if test -e "${STRABON_CONF}"; then
134 echo "${CMD}: reading configuration file ${STRABON_CONF}"
135 . ${STRABON_CONF}
136 fi
138 if ${DEBUG}; then
139 echo "${CMD}: debug is ON (from configuration file ${STRABON_CONF})"
140 fi
142 # read script options
143 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
144 case "${1}" in
145 -d)
146 shift
147 if ! ${DEBUG}; then
148 echo "${CMD}: debug is ON"
149 fi
150 DEBUG=true
151 DEBUG_SAVE=true
152 ;;
153 -e)
154 shift
155 if ! test $# -gt 1; then
156 help
157 exit 1
158 fi
160 DATABASE=${1}
161 case "${DATABASE}" in
162 monetdb|postgis)
163 shift
164 ;;
165 postgis)
166 shift
167 ;;
168 *)
169 echo "${CMD}: invalid database engine"
170 echo "${CMD}: valid database engines are \`postgis' or \`monetdb'"
171 exit 2
172 ;;
173 esac
174 ;;
175 -db)
176 shift
177 if ! test $# -gt 1; then
178 help
179 exit 1
180 fi
181 DB="${1}"
182 shift
183 ;;
184 -p)
185 shift
186 if ! test $# -gt 1; then
187 help
188 exit 1
189 fi
190 PORT=${1}
191 shift
192 ;;
193 -h)
194 shift
195 if ! test $# -gt 1; then
196 help
197 exit 1
198 fi
199 HOST=${1}
200 shift
201 ;;
202 -u)
203 shift
204 if ! test $# -gt 1; then
205 help
206 exit 1
207 fi
208 DBUSER=${1}
209 shift
210 ;;
211 -pass)
212 shift
213 if ! test $# -gt 1; then
214 help
215 exit 1
216 fi
217 DBPASS=${1}
218 shift
219 ;;
220 -c)
221 shift
222 if ! test $# -gt 1; then
223 help
224 exit 1
225 fi
227 STRABON_CONF="${1}"
228 if ! test -e "${STRABON_CONF}"; then
229 echo "${CMD}: configuration file \"${1}\" does not exist"
230 exit 1
231 else
232 # if true, then debug has been set ON in command line
233 echo "${CMD}: Reading configuration file \"${STRABON_CONF}\""
235 . ${STRABON_CONF}
237 if $DEBUG_SAVE; then
238 # restore debug value set on command line
239 DEBUG=true
240 fi
241 shift
242 fi
243 ;;
244 -*)
245 echo "${CMD}: unknown option \"${1}\""
246 help
247 exit 1
248 ;;
249 esac
250 done
252 # set defaults
253 case "${DATABASE}" in
254 postgis)
255 if test -z "${PORT}";
256 then
257 PORT=5432
258 fi
260 if test -z "${DBUSER}";
261 then
262 DBUSER="postgres"
263 fi
265 if test -z "${DBPASS}";
266 then
267 DBPASS="postgres"
268 fi
269 ;;
270 monetdb)
271 if test -z "${PORT}";
272 then
273 PORT=50000
274 fi
276 if test -z "${DBUSER}";
277 then
278 DBUSER="monetdb"
279 fi
281 if test -z "${DBPASS}";
282 then
283 DBPASS="monetdb"
284 fi
285 ;;
286 esac
288 # print configuration/options
289 if ${DEBUG}; then
290 echo "${CMD}: printing database connection details"
291 echo " Database Engine : ${DATABASE}"
292 echo " Database Name : ${DB}"
293 echo " Hostname : ${HOST}"
294 echo " Port : ${PORT}"
295 echo " Username : ${DBUSER}"
296 echo " Password : ${DBPASS}"
297 fi
299 # determine command to execute
300 case "${1}" in
301 help)
302 shift
303 if test $# -ge 1; then
304 case "${1}" in
305 query)
306 help_query
307 ;;
308 update)
309 help_update
310 ;;
311 store)
312 help_store
313 ;;
314 *)
315 help
316 ;;
317 esac
318 exit 1
319 fi
320 help
321 exit 0
322 ;;
323 query)
324 CLASS="QueryOp"
325 shift
326 if ! test $# -eq 1; then
327 help_query
328 exit 1
329 fi
330 QUERY="${1}"
332 # check for predefined queries
333 case "${QUERY}" in
334 size)
335 QUERY="${QUERY_SIZE}"
336 ;;
337 esac
338 ;;
339 update)
340 CLASS="UpdateOp"
341 shift
342 if ! test $# -eq 1; then
343 help_update
344 exit 1
345 fi
346 QUERY="${1}"
348 # check for predefined queries
349 case "${QUERY}" in
350 clear)
351 QUERY="${QUERY_DELETEALL}"
352 ;;
353 esac
354 ;;
355 store)
356 CLASS="StoreOp"
357 shift
358 if ! test $# -ge 1; then
359 help_store
360 exit 1
361 fi
362 # check whether format is specified
363 if test "${1}" = "-f"; then
364 shift
365 if ! test $# -ge 1; then
366 echo "${CMD}: Option -f requires an RDF format (\`ntriples', \`n3', \`rdfxml', or \`turtle')"
367 exit 2
368 else
369 FORMAT="${1}"
370 case "${FORMAT}" in
371 ntriples|n3|rdfxml|turtle)
372 shift
373 ;;
374 *)
375 echo "${CMD}: invalid RDF format \"${FORMAT}\"."
376 echo "${CMD}: valid RDF formats are \`ntriples', \`n3', \`rdfxml', or \`turtle'"
377 exit 2
378 ;;
379 esac
380 fi
381 fi
382 # if no files are given
383 if ! test $# -ge 1; then
384 help_store
385 exit 1
386 fi
387 # do not make an assignment of the files to QUERY
388 # handle the case of storing multiple files afterwards
389 # QUERY="${@}"
390 ;;
391 "")
392 help
393 exit 1
394 ;;
395 *)
396 help
397 echo
398 echo "${CMD}: unknown command \"${1}\"."
399 exit 1
400 ;;
401 esac
403 # compile command to execute
404 if test "${CLASS}" = "StoreOp"; then
405 STRABON_EXEC=
406 for file in "${@}"; do
407 STRABON_EXEC="${STRABON_EXEC}(cd ${RUNTIME} && java -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${file}\" ${FORMAT});
408 "
409 done
410 else
411 STRABON_EXEC="(cd ${RUNTIME} && java -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${QUERY}\")"
412 fi
414 # execute command or debug
415 if ${DEBUG}; then
416 echo "${CMD}: printing command for execution"
417 echo "${STRABON_EXEC}"
418 else
419 eval ${STRABON_EXEC}
420 fi