001: /*
002: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package arq.examples;
008:
009: // The ARQ application API.
010: import com.hp.hpl.jena.sparql.util.IndentedWriter;
011: import com.hp.hpl.jena.query.*;
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.vocabulary.DC;
014:
015: /** Example 1 : Execute a simple SELECT query on a model
016: * to find the DC titles contained in a model.
017: *
018: * @author Andy Seaborne
019: */
020:
021: public class Ex1 {
022: static public final String NL = System
023: .getProperty("line.separator");
024:
025: public static void main(String[] args) {
026: // Create the data.
027: // This wil be the background (unnamed) graph in the dataset.
028: Model model = createModel();
029:
030: // First part or the query string
031: String prolog = "PREFIX dc: <" + DC.getURI() + ">";
032:
033: // Query string.
034: String queryString = prolog + NL
035: + "SELECT ?title WHERE {?x dc:title ?title}";
036:
037: Query query = QueryFactory.create(queryString);
038: // Print with line numbers
039: query.serialize(new IndentedWriter(System.out, true));
040: System.out.println();
041:
042: // Create a single execution of this query, apply to a model
043: // which is wrapped up as a Dataset
044:
045: QueryExecution qexec = QueryExecutionFactory.create(query,
046: model);
047: // Or QueryExecutionFactory.create(queryString, model) ;
048:
049: System.out.println("Titles: ");
050:
051: try {
052: // Assumption: it's a SELECT query.
053: ResultSet rs = qexec.execSelect();
054:
055: // The order of results is undefined.
056: for (; rs.hasNext();) {
057: QuerySolution rb = rs.nextSolution();
058:
059: // Get title - variable names do not include the '?' (or '$')
060: RDFNode x = rb.get("title");
061:
062: // Check the type of the result value
063: if (x.isLiteral()) {
064: Literal titleStr = (Literal) x;
065: System.out.println(" " + titleStr);
066: } else
067: System.out.println("Strange - not a literal: " + x);
068:
069: }
070: } finally {
071: // QueryExecution objects should be closed to free any system resources
072: qexec.close();
073: }
074: }
075:
076: public static Model createModel() {
077: Model m = ModelFactory.createDefaultModel();
078:
079: Resource r1 = m.createResource("http://example.org/book#1");
080: Resource r2 = m.createResource("http://example.org/book#2");
081:
082: r1.addProperty(DC.title, "SPARQL - the book").addProperty(
083: DC.description, "A book about SPARQL");
084:
085: r2.addProperty(DC.title, "Advanced techniques for SPARQL");
086:
087: return m;
088: }
089: }
090:
091: /*
092: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
093: * All rights reserved.
094: *
095: * Redistribution and use in source and binary forms, with or without
096: * modification, are permitted provided that the following conditions
097: * are met:
098: * 1. Redistributions of source code must retain the above copyright
099: * notice, this list of conditions and the following disclaimer.
100: * 2. Redistributions in binary form must reproduce the above copyright
101: * notice, this list of conditions and the following disclaimer in the
102: * documentation and/or other materials provided with the distribution.
103: * 3. The name of the author may not be used to endorse or promote products
104: * derived from this software without specific prior written permission.
105: *
106: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
107: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
108: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
109: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
110: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
111: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
112: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
113: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
114: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
115: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
116: */
|