1 5 6 7 package jena; 8 9 10 import java.io.* ; 11 import java.lang.reflect.Constructor ; 12 13 import com.hp.hpl.jena.util.* ; 14 import com.hp.hpl.jena.rdql.* ; 15 import com.hp.hpl.jena.rdf.model.* ; 16 import com.hp.hpl.jena.shared.*; 17 18 import com.hp.hpl.jena.vocabulary.ResultSet ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 26 52 53 56 public class rdfquery 57 { 58 static public boolean displayTime = false ; 59 static public int messageLevel = 0 ; 60 static public boolean debug = false ; 61 static public boolean dumpModel = false ; 62 63 static final int FMT_NONE = -1 ; 64 static final int FMT_TUPLES = 0 ; 65 static final int FMT_TEXT = 1 ; 66 static final int FMT_HTML = 2 ; 67 static final int FMT_DUMP = 3 ; 68 69 static public int outputFormat = FMT_TEXT ; 70 static String dbUser = "" ; 71 static String dbPassword = "" ; 72 static String dbType = "" ; 73 static String dbName = "" ; 74 static String dbDriver = null ; 75 76 static String vocabularyURI = null ; 79 static Model vocabulary = null ; 80 81 static boolean applyRDFS = false ; 82 83 static protected Log logger = LogFactory.getLog( rdfquery.class ); 84 85 public static void main (String [] argv) throws Exception 86 { 87 88 if ( argv.length == 0 ) 89 { 90 usage() ; 91 System.exit(0) ; 92 } 93 94 String dataURL = null ; 95 String language = null ; 96 String queryFile = null ; 97 displayTime = false ; 98 99 105 int argi = 0 ; 106 for ( ; argi < argv.length ; argi++ ) 107 { 108 String arg = argv[argi] ; 109 110 if ( ! arg.startsWith("-") ) 111 break ; 112 113 if ( !arg.startsWith("--") ) 115 arg = "-"+arg ; 116 117 if ( arg.equalsIgnoreCase("--test") ) 118 { 119 argi++ ; 120 if ( argi == argv.length ) 121 { 122 allTests() ; 124 System.exit(0) ; 125 } 126 else 127 { 128 doTests(argv[argi]) ; 129 System.exit(0) ; 130 } 131 132 continue ; 133 } 134 135 if ( arg.equalsIgnoreCase("--help") || arg.equalsIgnoreCase("--h") ) 136 { 137 usage() ; 138 System.exit(0) ; 139 continue ; 140 } 141 142 if ( arg.equalsIgnoreCase("--debug") ) 143 { 144 debug = true ; 145 continue ; 146 } 147 148 if ( arg.equalsIgnoreCase("--quiet") || arg.equalsIgnoreCase("--q") ) 149 { 150 messageLevel -- ; 151 continue ; 152 } 153 154 if ( arg.equalsIgnoreCase("--verbose") || arg.equalsIgnoreCase("--v") ) 155 { 156 messageLevel ++ ; 157 continue ; 158 } 159 160 if ( arg.equalsIgnoreCase("--format") || arg.equalsIgnoreCase("--fmt")) 161 { 162 argi ++ ; 163 if ( argi == argv.length ) 164 { 165 System.err.println("Error: no output format given" ); 166 System.exit(1) ; 167 } 168 169 arg = argv[argi] ; 170 if ( arg.equalsIgnoreCase("none") ) 171 outputFormat = FMT_NONE ; 172 else if ( arg.equalsIgnoreCase("tuples") ) 173 outputFormat = FMT_TUPLES ; 174 else if (arg.equalsIgnoreCase("tuple") ) 175 outputFormat = FMT_TUPLES ; 176 else if (arg.equalsIgnoreCase("text") ) 177 outputFormat = FMT_TEXT ; 178 else if (arg.equalsIgnoreCase("html") ) 179 outputFormat = FMT_HTML ; 180 else if (arg.equalsIgnoreCase("dump") ) 181 outputFormat = FMT_DUMP ; 182 else 183 { 184 System.err.println("Unrecognized output format: "+arg) ; 185 System.exit(1) ; 186 } 187 continue ; 188 } 189 190 if ( arg.equalsIgnoreCase("--vocabulary") || 191 arg.equalsIgnoreCase("--vocab") ) 192 { 193 argi++ ; 194 if ( argi == argv.length ) 195 { 196 System.err.println("Error: no vocabulary specified"); 197 System.exit(1) ; 198 } 199 vocabularyURI = argv[argi] ; 200 continue ; 201 } 202 203 204 if ( arg.equalsIgnoreCase("--rdfs")) 205 { 206 applyRDFS = true ; 207 continue ; 208 } 209 210 if ( arg.equalsIgnoreCase("--time") ) 211 { 212 displayTime = true ; 213 continue ; 214 } 215 216 if ( arg.equalsIgnoreCase("--xml" ) ) 217 { 218 language = FileUtils.langXML ; 219 continue ; 220 } 221 222 if ( arg.equalsIgnoreCase("--ntriple" ) ) 223 { 224 language = FileUtils.langNTriple ; 225 continue ; 226 } 227 228 if ( arg.equalsIgnoreCase("--n3" ) ) 229 { 230 language = FileUtils.langN3 ; 231 continue ; 232 } 233 234 if ( arg.equalsIgnoreCase("--bdb" ) ) 235 { 236 language = FileUtils.langBDB ; 237 continue ; 238 } 239 240 if ( arg.equalsIgnoreCase("--dbName" ) ) 241 { 242 argi++ ; 243 if ( argi == argv.length ) 244 { 245 System.err.println("Error: no database name specified"); 246 System.exit(1) ; 247 } 248 dbName = argv[argi] ; 249 continue ; 250 } 251 252 if ( arg.equalsIgnoreCase("--dbType" ) ) 253 { 254 argi++ ; 255 if ( argi == argv.length ) 256 { 257 System.err.println("Error: no database type specified"); 258 System.exit(1) ; 259 } 260 dbType = argv[argi] ; 261 continue ; 262 } 263 264 if ( arg.equalsIgnoreCase("--driver" ) ) 265 { 266 argi++ ; 267 if ( argi == argv.length ) 268 { 269 System.err.println("Error: no databse name specified"); 270 System.exit(1) ; 271 } 272 dbDriver = argv[argi] ; 273 continue ; 274 } 275 276 if ( arg.equalsIgnoreCase("--user" ) ) 277 { 278 argi++ ; 279 if ( argi == argv.length ) 280 { 281 System.err.println("Error: no user name specified"); 282 System.exit(1) ; 283 } 284 dbUser = argv[argi] ; 285 continue ; 286 } 287 288 if ( arg.equalsIgnoreCase("--password" ) ) 289 { 290 argi++ ; 291 if ( argi == argv.length ) 292 { 293 System.err.println("Error: no password specified"); 294 System.exit(1) ; 295 } 296 dbPassword = argv[argi] ; 297 continue ; 298 } 299 300 if ( arg.equalsIgnoreCase("--data") ) 301 { 302 if ( dataURL != null ) 303 { 304 System.err.println("Error: source already specified"); 305 System.exit(1) ; 306 } 307 308 argi++ ; 309 if ( argi == argv.length ) 310 { 311 System.err.println("Error: no data file specified"); 312 System.exit(1) ; 313 } 314 dataURL = argv[argi] ; 315 continue ; 316 } 317 318 if ( arg.equalsIgnoreCase("--query") ) 319 { 320 argi++ ; 321 if ( argi == argv.length ) 322 { 323 System.err.println("Error: no query file specified"); 324 System.exit(1) ; 325 } 326 327 queryFile = argv[argi] ; 328 continue ; 330 } 331 332 System.err.println("Unrecognized option: "+arg) ; 334 usage() ; 335 System.exit(1); 336 } 337 338 if ( messageLevel >= 3 ) 339 dumpModel = true ; 340 341 String queryString = null ; 342 343 if ( queryFile != null ) 344 { 345 try { 346 queryString = FileUtils.readWholeFileAsUTF8(queryFile) ; 347 } catch (Exception e) 348 { 349 System.err.println("Error: failed to read file: "+e) ; 350 System.exit(1) ; 351 } 352 } 353 else 354 { 355 if ( argi >= argv.length ) 356 { 357 System.err.println("Error: No query supplied") ; 358 System.exit(1) ; 359 } 360 queryString = argv[argi] ; 361 } 362 363 query(queryString, dataURL, language) ; 364 365 } 366 367 static void allTests() throws Exception 368 { 369 doTests( "-all" ); 370 } 371 372 static public void doTests(String testsFilename) throws Exception 375 { Class rdfparse = Class.forName( "jena.test.rdfquery" ); 377 Constructor constructor = rdfparse.getConstructor( new Class [] {String .class} ); 378 Command c = (Command) constructor.newInstance( new Object [] { testsFilename } ); 379 c.execute(); 380 389 } 390 391 392 393 static public void query(String s, String dataURL, String language) 395 { 396 try { 397 boolean doBlank = false ; 398 399 if ( messageLevel >= 2 ) 400 { 401 System.out.println("Query:") ; 402 System.out.println(s) ; 403 if ( ! s.endsWith("\n") ) 404 System.out.println() ; 405 doBlank = true ; 406 } 407 408 long startTime = System.currentTimeMillis(); 409 long loadTime = -1 ; 410 411 Query query = new Query(s) ; 412 if ( displayTime ) 413 query = new Query(s) ; 415 416 if ( messageLevel > 0 ) 417 { 418 System.out.println("Parsed query:") ; 419 String tmp = query.toString() ; 420 System.out.print(tmp) ; 421 if ( ! tmp.endsWith("\n") ) 422 System.out.println() ; 423 doBlank = true ; 424 } 425 426 if ( dataURL == null && query.getSourceURL() == null ) 427 { 428 System.err.println("RDQL: no data source"); 429 return ; 430 } 431 432 if ( dataURL != null ) 433 { 434 long startLoadTime = System.currentTimeMillis(); 435 query.setSource(ModelLoader.loadModel(dataURL, language, 436 dbUser, dbPassword, 437 dbName, dbType, dbDriver)) ; 438 Model m = query.getSource() ; 439 441 if ( applyRDFS ) 442 { 443 Model model = null ; 444 if ( vocabularyURI != null ) 445 { 446 vocabulary = FileManager.get().loadModel(vocabularyURI, null) ; 447 model = ModelFactory.createRDFSModel(m, vocabulary) ; 448 } 449 else 450 { 451 model = ModelFactory.createRDFSModel(m) ; 452 } 453 query.setSource(model) ; 454 } 455 loadTime = System.currentTimeMillis() - startLoadTime ; 456 query.loadTime = loadTime ; 457 } 458 459 QueryExecution qe = new QueryEngine(query) ; 460 qe.init() ; 461 if ( dumpModel ) 462 { 463 try { 464 if ( doBlank ) 465 System.out.println() ; 466 doBlank = true ; 467 Model model = query.getSource() ; 468 RDFWriter w = model.getWriter("N-TRIPLE") ; 469 PrintWriter pw = new PrintWriter(System.out) ; 470 pw.println("# Model --------------------------------------------------------------------------------") ; 471 w.write(model, pw, "http://unset/") ; 472 pw.println("# Model --------------------------------------------------------------------------------") ; 473 pw.flush() ; 474 } catch (JenaException refEx) { logger.error("rdfquery: Failed to write model") ; System.exit(1) ; } 475 } 476 QueryResults results = qe.exec() ; 477 QueryResultsFormatter fmt = new QueryResultsFormatter(results) ; 478 479 if ( outputFormat == FMT_NONE) 480 fmt.consume() ; 481 else 482 { 483 if ( doBlank ) System.out.println() ; 484 485 if ( outputFormat == FMT_DUMP ) 486 { 487 Model m = fmt.toModel() ; 488 RDFWriter rdfw = m.getWriter("N3") ; 489 m.setNsPrefix("rs", ResultSet.getURI()) ; 490 rdfw.write(m, System.out, null) ; 491 } 492 else 493 { 494 495 PrintWriter pw = new PrintWriter(System.out) ; 496 switch(outputFormat) 497 { 498 case FMT_TEXT: fmt.printAll(pw) ; break ; 499 case FMT_HTML: fmt.printHTML(pw) ; break ; 500 case FMT_TUPLES: fmt.dump(pw, true) ; break ; 501 default: break ; 502 } 503 pw.flush() ; 504 } 505 doBlank = true ; 506 } 507 508 fmt.close() ; 509 results.close() ; 510 qe.close() ; 511 512 long finishTime = System.currentTimeMillis(); 513 long totalTime = finishTime-startTime ; 514 515 if ( messageLevel > 0 ) 516 { 517 if ( doBlank ) System.out.println() ; 518 System.out.println("Results: "+fmt.numRows()) ; 519 doBlank = true ; 520 } 521 522 if ( displayTime ) 523 { 524 if ( doBlank ) System.out.println() ; 525 System.out.println("Query parse: "+formatlong(query.parseTime) +" ms") ; 526 System.out.println("Query build: "+formatlong(query.buildTime) +" ms") ; 527 System.out.println("Data load time: "+formatlong(query.loadTime) +" ms") ; 528 System.out.println("Query execute: "+formatlong(query.executeTime) +" ms") ; 529 System.out.println("Query misc: "+formatlong(totalTime-query.parseTime-query.buildTime-query.loadTime-query.executeTime)+" ms") ; 530 System.out.println("Query total: "+formatlong(totalTime) +" ms") ; 531 doBlank = true ; 532 } 533 534 if ( query.getSource() != null ) 535 query.getSource().close() ; 536 541 } 542 catch (QueryException qEx) 543 { 544 System.err.println(qEx.getMessage()) ; 545 System.exit(9) ; 546 } 547 } 548 549 static String formatlong(long x) 550 { 551 StringBuffer sbuff = new StringBuffer () ; 552 sbuff.append(Long.toString(x)) ; 553 for ( int i = sbuff.length() ; i < 4 ; i++ ) sbuff.append(" ") ; 554 return sbuff.toString() ; 555 } 556 557 static void usage() 558 { 559 System.out.println("Usage: [--rdfs] [--data URL] [queryString | --query file]") ; 560 System.out.println(" --query file Read one query from a file") ; 561 System.out.println(" --rdfs Use an RDFS reasoner around the data") ; 562 System.out.println(" --vocab URL | File Specify a separate vocabulary (may also be in the data)") ; 564 System.out.println(" --xml Data source is XML (default)") ; 565 System.out.println(" --ntriple Data source is n-triple") ; 566 System.out.println(" --n3 Data source is N3") ; 567 System.out.println(" --data URL Data source (can also be part of query)") ; 568 System.out.println(" --time Print some time information") ; 569 System.out.println(" --test [file] Run the test suite") ; 570 System.out.println(" --format FMT One of text, html, tuples, dump or none") ; 571 System.out.println(" --verbose Verbose - more messages") ; 572 System.out.println(" --quiet Quiet - less messages") ; 573 } 574 } 575 576 602 | Popular Tags |