KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jena > rdfquery


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6
7 package jena;
8
9
10 import java.io.* ;
11 import java.lang.reflect.Constructor JavaDoc;
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 //import com.hp.hpl.jena.mem.ModelMem ;
24
//import com.hp.hpl.jena.reasoner.* ;
25

26 /** A program to execute queries from the command line.
27  *
28  * Queries can specify the source file so this can be used as a simple
29  * script engine for RDQL.
30  *
31  * <pre>
32  * Usage: [--xml|--ntriple] [--data URL] [queryString | --query file]") ;
33  * --query file Read one query from a file
34  * --rdfs Use an RDFS reasoner around the data
35  * --reasoner URI Set the reasoner URI explicitly.
36  * --vocab URL | File Specify a separate vocabulary (may also be in the data)
37  * --xml Data source is XML (default)
38  * --ntriple Data source is n-triple
39  * --n3 Data source is N3
40  * --data URL | File Data source (can also be part of query)
41  * --time Print some time information
42  * --test [file] Run the test suite
43  * --format FMT One of text, html, tuples, dump or none
44  * --verbose Verbose - more messages
45  * --quiet Quiet - less messages
46  * Options for data and format override the query specified source.
47  * </pre>
48  *
49  * @author Andy Seaborne
50  * @version $Id: rdfquery.java,v 1.24 2005/04/06 08:38:28 chris-dollin Exp $
51  */

52
53 // To do: formalise the use of variables and separate out the command line processor
54
// so the "query" routine can be used for testing from other main's.
55

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 JavaDoc dbUser = "" ;
71     static String JavaDoc dbPassword = "" ;
72     static String JavaDoc dbType = "" ;
73     static String JavaDoc dbName = "" ;
74     static String JavaDoc dbDriver = null ;
75
76     //static final String defaultReasonerURI = "http://www.hpl.hp.com/semweb/2003/RDFSReasoner1" ;
77
//static String reasonerURI = defaultReasonerURI ;
78
static String JavaDoc 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 JavaDoc [] argv) throws Exception JavaDoc
86     {
87
88         if ( argv.length == 0 )
89         {
90             usage() ;
91             System.exit(0) ;
92         }
93
94         String JavaDoc dataURL = null ;
95         String JavaDoc language = null ;
96         String JavaDoc queryFile = null ;
97         displayTime = false ;
98
99         // Flag processing.
100
// --X and -X are equivalent.
101
// Flags:
102
// --ntriple URL Get/read in a model in n-triple syntax
103
// --xml URL Get/read in a model in XML syntax
104

105         int argi = 0 ;
106         for ( ; argi < argv.length ; argi++ )
107         {
108             String JavaDoc arg = argv[argi] ;
109
110             if ( ! arg.startsWith("-") )
111                 break ;
112
113             // Canonical form: --LONG
114
if ( !arg.startsWith("--") )
115                 arg = "-"+arg ;
116
117             if ( arg.equalsIgnoreCase("--test") )
118             {
119                 argi++ ;
120                 if ( argi == argv.length )
121                 {
122                     // No control file.
123
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                 // read whole file later.
329
continue ;
330             }
331
332             // Unrecognized argument
333
System.err.println("Unrecognized option: "+arg) ;
334             usage() ;
335             System.exit(1);
336         }
337
338         if ( messageLevel >= 3 )
339             dumpModel = true ;
340
341         String JavaDoc queryString = null ;
342
343         if ( queryFile != null )
344         {
345             try {
346                 queryString = FileUtils.readWholeFileAsUTF8(queryFile) ;
347             } catch (Exception JavaDoc 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 JavaDoc
368     {
369         doTests( "-all" );
370     }
371
372     // Alternative invokation for command line use.
373
// Assumes it is in the tests directory
374
static public void doTests(String JavaDoc testsFilename) throws Exception JavaDoc
375     { // factored out to allow elimination of junit dependency.
376
Class JavaDoc rdfparse = Class.forName( "jena.test.rdfquery" );
377         Constructor JavaDoc constructor = rdfparse.getConstructor( new Class JavaDoc[] {String JavaDoc.class} );
378         Command c = (Command) constructor.newInstance( new Object JavaDoc[] { testsFilename } );
379         c.execute();
380         /*
381         // Fake the TestRunner : don't want all the dots.
382         TestResult r = new TestResult() ;
383         for ( Enumeration enum = suite.tests() ; enum.hasMoreElements() ; )
384         {
385             Test t = (Test)enum.nextElement() ;
386             t.run(r) ;
387         }
388         */

389     }
390
391
392
393     // Execute one query, with stats etc., print results
394
static public void query(String JavaDoc s, String JavaDoc dataURL, String JavaDoc 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                 // Do again after classloading has occured.
414
query = new Query(s) ;
415     
416             if ( messageLevel > 0 )
417             {
418                 System.out.println("Parsed query:") ;
419                 String JavaDoc 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                 // ------------
440

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             /*
537             PrintWriter pw = new PrintWriter(System.out) ;
538             resultsIter.print(pw) ;
539             pw.flush() ;
540             */

541         }
542         catch (QueryException qEx)
543         {
544             System.err.println(qEx.getMessage()) ;
545             System.exit(9) ;
546         }
547     }
548
549     static String JavaDoc formatlong(long x)
550     {
551         StringBuffer JavaDoc sbuff = new StringBuffer JavaDoc() ;
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(" --reasoner URI Set the reasoner URI explicitly.") ;
563
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 /*
577  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
578  * All rights reserved.
579  *
580  * Redistribution and use in source and binary forms, with or without
581  * modification, are permitted provided that the following conditions
582  * are met:
583  * 1. Redistributions of source code must retain the above copyright
584  * notice, this list of conditions and the following disclaimer.
585  * 2. Redistributions in binary form must reproduce the above copyright
586  * notice, this list of conditions and the following disclaimer in the
587  * documentation and/or other materials provided with the distribution.
588  * 3. The name of the author may not be used to endorse or promote products
589  * derived from this software without specific prior written permission.
590  *
591  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
592  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
593  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
594  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
595  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
596  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
597  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
598  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
599  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
600  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
601  */

602
Popular Tags