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