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.graph.Triple;
011: import com.hp.hpl.jena.query.*;
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.sparql.core.Var;
014: import com.hp.hpl.jena.sparql.expr.E_Regex;
015: import com.hp.hpl.jena.sparql.expr.Expr;
016: import com.hp.hpl.jena.sparql.expr.ExprVar;
017: import com.hp.hpl.jena.sparql.syntax.ElementFilter;
018: import com.hp.hpl.jena.sparql.syntax.ElementGroup;
019: import com.hp.hpl.jena.sparql.util.IndentedWriter;
020: import com.hp.hpl.jena.vocabulary.DC;
021:
022: /** Example : Build a query with a filter programmatically.
023: * Note: it may be better to build and execute an algebra expression. See other examples.
024: *
025: * @author Andy Seaborne
026: */
027:
028: public class ExProg2 {
029: static public final String NL = System
030: .getProperty("line.separator");
031:
032: public static void main(String[] args) {
033: Model model = createModel();
034:
035: Query query = QueryFactory.make();
036: query.setQueryType(Query.QueryTypeSelect);
037:
038: // See also ExProg1
039:
040: ElementGroup elg = new ElementGroup();
041:
042: Var varTitle = Var.alloc("title");
043: Var varX = Var.alloc("x");
044:
045: Triple t1 = new Triple(varX, DC.title.asNode(), varTitle);
046: elg.addTriplePattern(t1);
047:
048: // Adds a filter. Need to wrap variable in a NodeVar.
049: Expr expr = new E_Regex(new ExprVar(varTitle), "sparql", "i");
050: ElementFilter filter = new ElementFilter(expr);
051: elg.addElementFilter(filter);
052:
053: // Attach the group to query.
054: query.setQueryPattern(elg);
055:
056: // Choose what we want - SELECT ?title
057: query.addResultVar(varTitle);
058:
059: // Print query with line numbers
060: // Prefix mapping just helps serialization
061: query.getPrefixMapping().setNsPrefix("dc", DC.getURI());
062: query.serialize(new IndentedWriter(System.out, true));
063: System.out.println();
064:
065: QueryExecution qexec = QueryExecutionFactory.create(query,
066: model);
067:
068: try {
069: // Assumption: it's a SELECT query.
070: ResultSet rs = qexec.execSelect();
071:
072: // The order of results is undefined.
073: System.out.println("Titles: ");
074: for (; rs.hasNext();) {
075: QuerySolution rb = rs.nextSolution();
076:
077: // Get title - variable names do not include the '?' (or '$')
078: RDFNode x = rb.get("title");
079:
080: // Check the type of the result value
081: if (x instanceof Literal) {
082: Literal titleStr = (Literal) x;
083: System.out.println(" " + titleStr);
084: } else
085: System.out.println("Strange - not a literal: " + x);
086:
087: }
088: } finally {
089: // QueryExecution objects should be closed to free any system resources
090: qexec.close();
091: }
092: }
093:
094: public static Model createModel() {
095: Model model = ModelFactory.createDefaultModel();
096:
097: Resource r1 = model.createResource("http://example.org/book#1");
098: Resource r2 = model.createResource("http://example.org/book#2");
099: Resource r3 = model.createResource("http://example.org/book#3");
100:
101: r1.addProperty(DC.title, "SPARQL - the book").addProperty(
102: DC.description, "A book about SPARQL");
103:
104: r2.addProperty(DC.title, "Advanced techniques for SPARQL");
105:
106: r3.addProperty(DC.title, "Jena - an RDF framework for Java")
107: .addProperty(DC.description, "A book about Jena");
108:
109: return model;
110: }
111: }
112:
113: /*
114: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
115: * All rights reserved.
116: *
117: * Redistribution and use in source and binary forms, with or without
118: * modification, are permitted provided that the following conditions
119: * are met:
120: * 1. Redistributions of source code must retain the above copyright
121: * notice, this list of conditions and the following disclaimer.
122: * 2. Redistributions in binary form must reproduce the above copyright
123: * notice, this list of conditions and the following disclaimer in the
124: * documentation and/or other materials provided with the distribution.
125: * 3. The name of the author may not be used to endorse or promote products
126: * derived from this software without specific prior written permission.
127: *
128: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
129: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
130: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
131: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
132: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
133: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
134: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
135: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
137: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
138: */
|