Created
October 7, 2014 02:51
-
-
Save kinow/10875c79a94f4fd931c9 to your computer and use it in GitHub Desktop.
Jena-Text test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tupilabs.semweb.jena; | |
import java.util.Iterator; | |
import org.apache.jena.query.text.EntityDefinition; | |
import org.apache.jena.query.text.TextDatasetFactory; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.RAMDirectory; | |
import com.hp.hpl.jena.query.Dataset; | |
import com.hp.hpl.jena.query.DatasetFactory; | |
import com.hp.hpl.jena.query.Query; | |
import com.hp.hpl.jena.query.QueryExecution; | |
import com.hp.hpl.jena.query.QueryExecutionFactory; | |
import com.hp.hpl.jena.query.QueryFactory; | |
import com.hp.hpl.jena.query.QuerySolution; | |
import com.hp.hpl.jena.query.ReadWrite; | |
import com.hp.hpl.jena.query.ResultSet; | |
import com.hp.hpl.jena.rdf.model.InfModel; | |
import com.hp.hpl.jena.rdf.model.Model; | |
import com.hp.hpl.jena.rdf.model.ModelFactory; | |
import com.hp.hpl.jena.rdf.model.Resource; | |
import com.hp.hpl.jena.rdf.model.Statement; | |
import com.hp.hpl.jena.reasoner.Reasoner; | |
import com.hp.hpl.jena.reasoner.ReasonerRegistry; | |
import com.hp.hpl.jena.tdb.TDBFactory; | |
import com.hp.hpl.jena.vocabulary.RDFS; | |
public class JenaTextTests { | |
public static void main(String[] args) { | |
// From Phillip Rhodes e-mail to the dev-list on 06/Oct/2014. | |
Dataset dataset = TDBFactory.createDataset("jenastore"); | |
EntityDefinition entDef = new EntityDefinition("uri", "text", RDFS.label); | |
Directory dir = new RAMDirectory(); | |
Dataset ds = TextDatasetFactory.createLucene(dataset, dir, entDef); | |
ds.begin(ReadWrite.WRITE); | |
Model m = ds.getDefaultModel(); | |
Resource rSubject = m.createResource("http://ontology.fogbeam.com/example/TestResource1"); | |
Resource rSubject2 = m.createResource("http://ontology.fogbeam.com/example/TestResource2"); | |
try { | |
Statement s = m.createStatement(rSubject, RDFS.label, "This is a TEST Resource"); | |
m.add(s); | |
Statement s2 = m.createStatement(rSubject2, RDFS.label, "Bratwurst"); | |
m.add(s2); | |
ds.commit(); | |
String baseQueryString = | |
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " + | |
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + | |
"PREFIX dc: <http://purl.org/dc/elements/1.1/> " + | |
"PREFIX dcterm: <http://purl.org/dc/terms/> " + | |
"PREFIX owl: <http://www.w3.org/2002/07/owl#> " + | |
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + | |
"PREFIX text: <http://jena.apache.org/text#>"; | |
/* Do a SPARQL query using Jena-Text here... */ | |
String queryString = baseQueryString + "SELECT ?s { ?s text:query ('Flibble') ; rdfs:label ?label ; }"; | |
ds.begin(ReadWrite.READ); | |
Query query = QueryFactory.create(queryString); | |
// Reasoner reasoner = ReasonerRegistry.getOWLMiniReasoner(); | |
// InfModel inf = ModelFactory.createInfModel(reasoner, m); | |
QueryExecution qexec = QueryExecutionFactory.create(query, ds); // use our dataset with lucene | |
try { | |
ResultSet solutions = qexec.execSelect(); | |
for ( ; solutions.hasNext() ; ) { | |
QuerySolution soln = solutions.nextSolution(); | |
System.out.println(String.format("solution: %s", soln)); | |
Iterator<String> iter = soln.varNames(); | |
} | |
ds.commit(); | |
} finally { | |
qexec.close(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(System.err); | |
} finally { | |
if (ds != null) { | |
ds.end(); | |
} | |
} | |
System.out.println("done"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment