001: /*
002: * (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package jena;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import arq.cmd.CmdException;
012: import arq.cmd.TerminationException;
013: import jena.cmdline.ArgDecl;
014: import jena.util.*;
015:
016: import com.hp.hpl.jena.query.*;
017: import com.hp.hpl.jena.rdf.model.Model;
018: import com.hp.hpl.jena.rdf.model.ModelFactory;
019: import com.hp.hpl.jena.rdf.model.RDFWriter;
020: import com.hp.hpl.jena.sparql.resultset.ResultsFormat;
021: import com.hp.hpl.jena.sparql.vocabulary.ResultSetGraphVocab;
022: import com.hp.hpl.jena.util.FileManager;
023:
024: public class dbquery extends DBcmd {
025:
026: // TODO Rewrite to use assemblers (and merge with arq.query?)
027: // Make DBcmd public -- actually, make an extension of CmdLineArgs that adds and handles the extra stuff.
028: // Execute an ARQ query against an old-style Jena database
029: static private Log log = LogFactory.getLog(dbquery.class);
030:
031: public static final String[] usage = new String[] {
032: "dbquery [--spec spec] | [db_description] [--model name] --query QueryFile",
033: " where db_description is",
034: " --db JDBC URL --dbType type",
035: " --dbUser user --dbPassword password" };
036:
037: public static void main(String[] argv) {
038: try {
039: main2(argv);
040: } catch (TerminationException ex) {
041: System.exit(ex.getCode());
042: } catch (CmdException ex) {
043: System.err.println(ex.getMessage());
044: if (ex.getCause() != null)
045: ex.getCause().printStackTrace(System.err);
046: }
047:
048: }
049:
050: public static void main2(String[] argv) {
051: dbquery db = new dbquery();
052: db.setUsage(usage);
053:
054: // add any new args
055: db.init(argv);
056: // do any additional test here
057:
058: // Action!
059: db.exec();
060: }
061:
062: private ArgDecl queryDecl = new ArgDecl(true, "query");
063: private ArgDecl queryTime = new ArgDecl(false, "time");
064: private ArgDecl repeatDecl = new ArgDecl(true, "repeat");
065: private boolean timing = false;
066:
067: public dbquery() {
068: super ("dbquery", false);
069: getCommandLine().add(queryDecl);
070: getCommandLine().add(queryTime);
071: getCommandLine().add(repeatDecl);
072: }
073:
074: //@Override
075: protected void exec0() {
076: if (!getCommandLine().contains(queryDecl)) {
077: System.err.println("No query");
078: return;
079: }
080: String queryFile = getCommandLine().getValue(queryDecl);
081: String queryString = FileManager.get().readWholeFileAsUTF8(
082: queryFile);
083: exec1(queryString);
084: }
085:
086: //@Override
087: protected boolean exec1(String arg) {
088: if (arg.startsWith("@"))
089: arg = FileManager.get().readWholeFileAsUTF8(
090: arg.substring(1));
091:
092: boolean timing = false;
093: if (getCommandLine().contains(queryTime))
094: timing = true;
095: // timing = ( getCommandLine().getArg(queryTime).getValue().equalsIgnoreCase("true") ||
096: // getCommandLine().getArg(queryTime).getValue().equalsIgnoreCase("on") ) ;
097:
098: // GraphRDB g = ((GraphRDB)getRDBModel().getGraph()) ;
099: // System.err.println("Reif: "+g.reificationBehavior()) ;
100: // System.err.println("OnlyReified: "+getRDBModel().getQueryOnlyReified()) ;
101: // System.err.println("FullReified: "+getRDBModel().getQueryFullReified()) ;
102: // System.err.println("QueryOnly: "+getRDBModel().getQueryOnlyAsserted()) ;
103:
104: // super.getRDBModel().setDoFastpath(true) ;
105: // super.getRDBModel().setQueryOnlyReified(false) ;
106: // super.getRDBModel().setQueryFullReified(false) ;
107: // super.getRDBModel().setQueryOnlyAsserted(true) ;
108:
109: //super.getRDBModel().setDoFastpath(true) ;
110: //System.err.println("Fastpath: "+getRDBModel().getDoFastpath()) ;
111:
112: long totalTime = 0;
113: long firstTime = 0;
114: ResultsFormat fmt = ResultsFormat.FMT_TEXT;
115: int repeat = 1;
116:
117: if (getCommandLine().contains(repeatDecl))
118: repeat = Integer.parseInt(getCommandLine().getValue(
119: repeatDecl));
120:
121: if (timing)
122: fmt = ResultsFormat.FMT_NONE;
123:
124: // Compile and execute once on empty model (get classes)
125: if (timing) {
126: long startTime = System.currentTimeMillis();
127: Query query = QueryFactory.create(arg);
128: Model m = ModelFactory.createDefaultModel();
129: QueryExecution qExec = QueryExecutionFactory.create(query,
130: m);
131: doQuery(query, qExec, ResultsFormat.FMT_NONE);
132: long finishTime = System.currentTimeMillis();
133: firstTime = (finishTime - startTime);
134: }
135:
136: //Query query = QueryFactory.create(arg) ;
137: // Creates model - does JDBC.
138: long startTimeJDBC = System.currentTimeMillis();
139: getRDBModel();
140: long jdbcTime = System.currentTimeMillis() - startTimeJDBC;
141:
142: for (int i = 0; i < repeat; i++) {
143: long startTime = System.currentTimeMillis();
144: //getRDBModel().begin() ;
145: // fastpath => one SQL query => autocommit is enough
146: Query query = QueryFactory.create(arg);
147: QueryExecution qExec = QueryExecutionFactory.create(query,
148: super .getRDBModel());
149: doQuery(query, qExec, fmt);
150: qExec.close();
151: //getRDBModel().commit() ;
152: long finishTime = System.currentTimeMillis();
153: long time = finishTime - startTime;
154: totalTime += time;
155: }
156:
157: if (timing) {
158: System.out.println("Query execution time: " + (totalTime)
159: / (repeat * 1000.0));
160: System.out.println("Setup: " + (firstTime)
161: / 1000.0);
162: System.out.println("JDBC setup: " + (jdbcTime)
163: / 1000.0);
164: }
165:
166: return false;
167: }
168:
169: public void doQuery(Query query, QueryExecution qe,
170: ResultsFormat outputFormat) {
171: if (query.isSelectType())
172: doSelectQuery(query, qe, outputFormat);
173: if (query.isDescribeType())
174: doDescribeQuery(query, qe, outputFormat);
175: if (query.isConstructType())
176: doConstructQuery(query, qe, outputFormat);
177: if (query.isAskType())
178: doAskQuery(query, qe, outputFormat);
179: qe.close();
180: }
181:
182: void doSelectQuery(Query query, QueryExecution qe,
183: ResultsFormat outputFormat) {
184: ResultSet results = qe.execSelect();
185:
186: // Force query to execute - until now it has merely been set up.
187: results = ResultSetFactory.makeRewindable(results);
188:
189: boolean done = false;
190:
191: // The non-display forms / uses a ResultSetFormatter
192:
193: if (outputFormat.equals(ResultsFormat.FMT_NONE)
194: || outputFormat.equals(ResultsFormat.FMT_COUNT)) {
195: int count = ResultSetFormatter.consume(results);
196: if (outputFormat.equals(ResultsFormat.FMT_COUNT)) {
197: System.out.println("Count = " + count);
198: }
199: done = true;
200: }
201:
202: if (outputFormat.equals(ResultsFormat.FMT_RS_RDF)
203: || outputFormat.equals(ResultsFormat.FMT_RDF_N3)) {
204: Model m = ResultSetFormatter.toModel(results);
205: m.setNsPrefixes(query.getPrefixMapping());
206: RDFWriter rdfw = m.getWriter("TURTLE");
207: m.setNsPrefix("rs", ResultSetGraphVocab.getURI());
208: rdfw.write(m, System.out, null);
209: done = true;
210: }
211:
212: if (outputFormat.equals(ResultsFormat.FMT_RS_XML)) {
213: ResultSetFormatter.outputAsXML(System.out, results);
214: done = true;
215: }
216:
217: if (outputFormat.equals(ResultsFormat.FMT_TEXT)) {
218: ResultSetFormatter.out(System.out, results, query
219: .getPrefixMapping());
220: done = true;
221: }
222: if (!done)
223: log.warn("Unknown format request: " + outputFormat);
224:
225: System.out.flush();
226: }
227:
228: void doDescribeQuery(Query query, QueryExecution qe,
229: ResultsFormat outputFormat) {
230: Model r = qe.execDescribe();
231: writeModel(query, r, outputFormat);
232: }
233:
234: void doConstructQuery(Query query, QueryExecution qe,
235: ResultsFormat outputFormat) {
236: Model r = qe.execConstruct();
237: writeModel(query, r, outputFormat);
238: }
239:
240: void writeModel(Query query, Model model, ResultsFormat outputFormat) {
241: if (outputFormat.equals(ResultsFormat.FMT_NONE))
242: return;
243:
244: if (outputFormat.equals(ResultsFormat.FMT_TEXT)) {
245: String qType = "";
246: if (query.isDescribeType())
247: qType = "DESCRIBE";
248: if (query.isConstructType())
249: qType = "CONSTRUCT";
250:
251: System.out.println("# ======== " + qType + " results ");
252: model.write(System.out, "N3", null); // Base is meaningless
253: System.out.println("# ======== ");
254: return;
255: }
256:
257: if (outputFormat.equals(ResultsFormat.FMT_RDF_XML)) {
258: model.write(System.out, "RDF/XML-ABBREV", null);
259: return;
260: }
261:
262: if (outputFormat.equals(ResultsFormat.FMT_RDF_N3)) {
263: model.write(System.out, "N3", null);
264: return;
265: }
266:
267: if (outputFormat.equals(ResultsFormat.FMT_RDF_NT)) {
268: model.write(System.out, "N_TRIPLES", null);
269: return;
270: }
271:
272: System.err.println("Unknown format: "
273: + outputFormat.getSymbol());
274: }
275:
276: void doAskQuery(Query query, QueryExecution qe,
277: ResultsFormat outputFormat) {
278: boolean b = qe.execAsk();
279:
280: if (outputFormat.equals(ResultsFormat.FMT_RS_XML)) {
281: ResultSetFormatter.outputAsXML(System.out, b);
282: return;
283: }
284:
285: if (outputFormat.equals(ResultsFormat.FMT_RDF_N3)
286: || outputFormat.equals(ResultsFormat.FMT_RDF_TTL)) {
287: ResultSetFormatter.outputAsRDF(System.out, "TURTLE", b);
288: System.out.flush();
289: return;
290: }
291:
292: if (outputFormat.equals(ResultsFormat.FMT_TEXT)) {
293: //ResultSetFormatter.out(System.out, b) ;
294: System.out.println("Ask => " + (b ? "Yes" : "No"));
295: return;
296: }
297:
298: System.err.println("Unknown format: "
299: + outputFormat.getSymbol());
300: }
301: }
302:
303: /*
304: * (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
305: * All rights reserved.
306: *
307: * Redistribution and use in source and binary forms, with or without
308: * modification, are permitted provided that the following conditions
309: * are met:
310: * 1. Redistributions of source code must retain the above copyright
311: * notice, this list of conditions and the following disclaimer.
312: * 2. Redistributions in binary form must reproduce the above copyright
313: * notice, this list of conditions and the following disclaimer in the
314: * documentation and/or other materials provided with the distribution.
315: * 3. The name of the author may not be used to endorse or promote products
316: * derived from this software without specific prior written permission.
317: *
318: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
319: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
320: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
321: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
322: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
323: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
324: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
325: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
326: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
327: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
328: */
|