001: /*
002: * (c) Copyright 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: import com.hp.hpl.jena.query.*;
010: import com.hp.hpl.jena.query.larq.IndexBuilderString;
011: import com.hp.hpl.jena.query.larq.IndexLARQ;
012: import com.hp.hpl.jena.query.larq.LARQ;
013: import com.hp.hpl.jena.rdf.model.Model;
014: import com.hp.hpl.jena.rdf.model.ModelFactory;
015: import com.hp.hpl.jena.sparql.util.StringUtils;
016: import com.hp.hpl.jena.sparql.util.Utils;
017: import com.hp.hpl.jena.util.FileManager;
018:
019: /** Example code to load a model from a file, index all string literals,
020: * then execute a SPARQL query with a Lucene search in it.
021: *
022: * @author Andy Seaborne
023: */
024:
025: public class ExLucene1 {
026:
027: public static void main(String[] a) throws Exception {
028: System.out.println("ARQ Example: "
029: + Utils.classShortName(ExLucene1.class));
030: System.out.println("ARQ: " + ARQ.VERSION);
031: System.out.println();
032:
033: Model model = ModelFactory.createDefaultModel();
034: IndexLARQ index = buildIndex(model, "testing/LARQ/data-1.ttl");
035:
036: // Search for
037: String searchString = "+document";
038:
039: String queryString = StringUtils
040: .join(
041: "\n",
042: new String[] {
043: "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>",
044: "PREFIX : <http://example/>",
045: "PREFIX pf: <http://jena.hpl.hp.com/ARQ/property#>",
046: "SELECT * {",
047: " ?lit pf:textMatch '"
048: + searchString + "'.", "}" });
049:
050: // Two of three docuemnts should match.
051: performQuery(model, index, queryString);
052: index.close();
053: }
054:
055: static IndexLARQ buildIndex(Model model, String datafile) {
056: // ---- Read and index all literal strings.
057: IndexBuilderString larqBuilder = new IndexBuilderString();
058:
059: // Index statements as they are added to the model.
060: model.register(larqBuilder);
061:
062: // To just build the index, create a model that does not store statements
063: // Model model2 = ModelFactory.createModelForGraph(new GraphSink()) ;
064:
065: FileManager.get().readModel(model, datafile);
066:
067: // ---- Alternatively build the index after the model has been created.
068: // larqBuilder.indexStatements(model.listStatements()) ;
069:
070: // ---- Finish indexing
071: larqBuilder.closeWriter();
072: model.unregister(larqBuilder);
073:
074: // ---- Create the access index
075: IndexLARQ index = larqBuilder.getIndex();
076: return index;
077: }
078:
079: static void performQuery(Model model, IndexLARQ index,
080: String queryString) {
081: // Make globally available
082: LARQ.setDefaultIndex(index);
083:
084: Query query = QueryFactory.create(queryString);
085: query.serialize(System.out);
086: System.out.println();
087:
088: QueryExecution qExec = QueryExecutionFactory.create(query,
089: model);
090: //LARQ.setDefaultIndex(qExec.getContext(), index) ;
091: ResultSetFormatter.out(System.out, qExec.execSelect(), query);
092: qExec.close();
093: }
094:
095: }
096:
097: /*
098: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
099: * All rights reserved.
100: *
101: * Redistribution and use in source and binary forms, with or without
102: * modification, are permitted provided that the following conditions
103: * are met:
104: * 1. Redistributions of source code must retain the above copyright
105: * notice, this list of conditions and the following disclaimer.
106: * 2. Redistributions in binary form must reproduce the above copyright
107: * notice, this list of conditions and the following disclaimer in the
108: * documentation and/or other materials provided with the distribution.
109: * 3. The name of the author may not be used to endorse or promote products
110: * derived from this software without specific prior written permission.
111: *
112: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
113: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
114: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
115: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
116: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
117: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
118: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
119: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
120: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
121: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
122: */
|