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 java.io.StringReader;
010:
011: import com.hp.hpl.jena.query.ARQ;
012: import com.hp.hpl.jena.query.larq.IndexBuilderExt;
013: import com.hp.hpl.jena.query.larq.IndexLARQ;
014: import com.hp.hpl.jena.rdf.model.*;
015: import com.hp.hpl.jena.sparql.util.StringUtils;
016: import com.hp.hpl.jena.sparql.util.Utils;
017:
018: /** Example code to index subjects by some external content.
019: * Pattern 3.
020: *
021: * @author Andy Seaborne
022: */
023:
024: public class ExLucene5 {
025:
026: public static void main(String[] a) throws Exception {
027: System.out.println("ARQ Example: "
028: + Utils.classShortName(ExLucene5.class));
029: System.out.println("ARQ: " + ARQ.VERSION);
030: System.out.println();
031:
032: Model model = ModelFactory.createDefaultModel();
033:
034: IndexLARQ index = buildIndexExternalContent(model);
035:
036: // Search for string
037: String searchString = "+document";
038:
039: // This time, find documents with a matching DC title.
040: String queryString = StringUtils
041: .join(
042: "\n",
043: new String[] {
044: "PREFIX pf: <http://jena.hpl.hp.com/ARQ/property#>",
045: "SELECT ?doc {",
046: " ?doc pf:textMatch '"
047: + searchString + "'.", "}" });
048:
049: // Two of three docuemnts should match.
050: ExLucene1.performQuery(model, index, queryString);
051: index.close();
052: }
053:
054: static IndexLARQ buildIndexExternalContent(Model model) {
055: // ---- Create index builder
056: IndexBuilderExt larqBuilder = new IndexBuilderExt();
057:
058: Resource r1 = ResourceFactory
059: .createResource("http://example/r1");
060: Resource r2 = ResourceFactory
061: .createResource("http://example/r2");
062: Resource r3 = ResourceFactory
063: .createResource("http://example/r3");
064: Resource r4 = ResourceFactory
065: .createResource("http://example/r4");
066: Literal lit1 = ResourceFactory.createPlainLiteral("doc");
067:
068: // ---- Index based on some external content
069:
070: larqBuilder.index(r1, new StringReader("document")); // Just to show a Stringreader is possible
071: larqBuilder.index(r2, "document");
072: larqBuilder.index(r3, "slideshow");
073: larqBuilder.index(r4, "codebase");
074: larqBuilder.index(lit1, "document");
075:
076: // Note that the model is untouched - the index exists outside of any model statements.
077: // The application is responsible for keeping
078: // ----
079:
080: larqBuilder.closeWriter();
081: IndexLARQ index = larqBuilder.getIndex();
082:
083: // NodeIterator iter = index.searchModelByIndex(model, "document") ;
084: // for ( ; iter.hasNext() ; )
085: // System.out.println("Found: "+FmtUtils.stringForRDFNode((RDFNode)iter.next())) ;
086:
087: return index;
088: }
089:
090: }
091:
092: /*
093: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
094: * All rights reserved.
095: *
096: * Redistribution and use in source and binary forms, with or without
097: * modification, are permitted provided that the following conditions
098: * are met:
099: * 1. Redistributions of source code must retain the above copyright
100: * notice, this list of conditions and the following disclaimer.
101: * 2. Redistributions in binary form must reproduce the above copyright
102: * notice, this list of conditions and the following disclaimer in the
103: * documentation and/or other materials provided with the distribution.
104: * 3. The name of the author may not be used to endorse or promote products
105: * derived from this software without specific prior written permission.
106: *
107: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
108: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
109: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
110: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
111: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
112: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
113: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
114: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
115: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
116: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
117: */
|