/**
* org.bioversityinternational.sample.FollowYourNose
*
* Sample code copied from www.liyangyu.com under
* "A Developer's Guide to the Semantic Web"
* Chapter 14, List 14.10
*
**/
package org.bioversityinternational.model.rdf;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.bioversityinternational.Config;
import org.bioversityinternational.model.ModelException;
import org.bioversityinternational.model.general.Language;
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.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
public class SPARQLQuery {
public final static String NAME = "name" ;
public final static String DEFINITION = "definition" ;
private Boolean _VERBOSE = false ;
public void debug(Boolean flag) {
_VERBOSE = flag ;
}
private String SPARQLEndpoint =
"http://data.bioversityinternational.org/ontology/query";
private Model model = null ;
/**
* Constructor
* @throws IOException
*
*/
public SPARQLQuery() throws IOException {
Config.init();
}
/**
* Constructor
* @throws IOException
*
*/
public SPARQLQuery(Model model) throws IOException {
this();
this.model = model ;
}
/**
* Constructor
* @throws IOException
*
*/
public SPARQLQuery(String SPARQLEndpoint) throws IOException {
this() ;
if(SPARQLEndpoint != null && !SPARQLEndpoint.isEmpty())
this.SPARQLEndpoint = SPARQLEndpoint ;
}
private String language_code ;
private String language_name ;
/**
*
* Method
*
* @param language
*/
private void setLanguage(String language)
throws ModelException {
if(language == null) {
language_code = Language.getDefaultLanguageCode();
language_name = Language.getDefaultLanguage();
} else {
language_code = language.trim() ;
language_name = Language.getLanguage(language_code) ;
if(language_name == null) {
throw new ModelException("CropDescriptorQuery.setLanguage(): language code '"+
language_code+"' is unknown?") ;
}
}
}
protected String getLanguageCode()
throws ModelException {
if(language_code == null) {
setLanguage(null) ;
}
return language_code ;
}
protected String getLanguageName()
throws ModelException {
if(language_code == null) {
setLanguage(null) ;
}
return language_name ;
}
protected String getQueryString(String language_code) {
String queryStringtemplate =
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#>\n"+
"SELECT ?resource ?name ?definition\n"+
"WHERE {\n"+
"?resource a skos:Concept.\n"+
"optional {\n"+
"?resource skos:prefLabel ?name .\n"+
"FILTER(langMatches(lang(?name), \"%s\"))\n"+
"}\n"+
"optional {\n"+
"?resource skos:definition ?definition .\n"+
"FILTER(langMatches(lang(?definition), \"%s\"))\n"+
"}\n"+
"}" ;
return String.format(
new Locale(language_code),
queryStringtemplate,
language_code,
language_code
) ;
}
private Query query = null ;
private QueryExecution qe = null;
/**
*
* Method
*
* @param language is the ISO language code for the query: http://www.loc.gov/standards/iso639-2/englangn.html
*/
public void defaultQueryInLanguage(String language) {
try {
setLanguage(language);
create(this.getQueryString(language_code)) ;
} catch (ModelException e) {
// Fails non-fatally here...
e.printStackTrace();
}
}
/**
*
* Method
*/
public void defaultQuery() {
this.defaultQueryInLanguage(null);
}
/**
*
* Method
*
* @param queryString
*/
public void create(String queryString) {
query = QueryFactory.create(queryString);
if(model != null) {
qe = QueryExecutionFactory.create(query,model);
} else {
qe =
QueryExecutionFactory.sparqlService(
SPARQLEndpoint,
query
);
}
}
protected ResultSet results = null ;
public ResultSet getResults() {
return results ;
}
/**
*
* Method
*
*/
public void execSelect() {
if(qe != null) {
results = qe.execSelect();
}
}
/**
*
* Method
*
*/
public Model execDescribe() {
if(qe != null) {
return qe.execDescribe();
} else {
return null ;
}
}
/**
*
* Method
*
*/
public Model execConstruct() {
if(qe != null) {
return qe.execConstruct();
} else {
return null ;
}
}
/**
*
* Method
*
*/
public Boolean execAsk() {
if(qe != null) {
return qe.execAsk();
} else {
return false ;
}
}
public void close() {
if(qe != null) { qe.close(); }
}
protected String getNode(QuerySolution soln, String tag) {
String nodeValue = null ;
if(soln != null) {
RDFNode node = soln.get(tag) ;
if( node != null) {
if ( node.isLiteral() ) {
nodeValue = ((Literal)node).getLexicalForm() ;
if ( _VERBOSE ){ System.out.println( "\t"+tag+": "+nodeValue) ; }
} else if ( node.isResource() ) {
Resource res = (Resource)node;
if ( res.isAnon() == true ) {
nodeValue = res.getLocalName() ;
if ( _VERBOSE ) { System.out.println( "\t"+tag+": <" + nodeValue + ">"); }
} else {
nodeValue = res.getURI() ;
if (_VERBOSE ) { System.out.println( "\t"+tag+": <" + nodeValue + ">"); }
}
}
}
}
return nodeValue ;
}
protected Map<String,Map<String,String>> resultMap = null ;
protected void getNodes(QuerySolution soln, Map<String,String> nodeMap) {
String name = getNode(soln, NAME) ;
if(name != null) { nodeMap.put(NAME,name); }
String definition = getNode(soln, DEFINITION) ;
if(definition != null) { nodeMap.put(DEFINITION,definition); }
}
public Map<String,Map<String,String>> getResultMap() {
if(resultMap != null) {
return resultMap ;
} else {
resultMap = new HashMap<String,Map<String,String>>() ;
}
if (results != null) {
while ( results.hasNext() ) {
QuerySolution soln = results.nextSolution() ;
Resource res = (Resource)(soln.get("resource"));
String resName = res.getURI() ;
resultMap.put(resName, new HashMap<String,String>()) ;
if ( _VERBOSE ){System.out.println(" - <" + resName + "> : "); }
this.getNodes(soln, resultMap.get(resName));
}
}
return resultMap ;
}
/**
* Dumps current results to System.out
*/
public void dumpResults() {
Map<String,Map<String,String>> results = getResultMap();
for(String key : results.keySet()) {
System.out.println(key+":");
Map<String,String> annotation = results.get(key) ;
for(String tag : annotation.keySet()) {
System.out.printf( "\t%20s: %-255s\n",tag,annotation.get(tag));
}
System.out.println() ;
}
}
/**
* Method
*
* @param args
*/
public static void main(String[] args) {
SPARQLQuery q = null ;
try {
q = new SPARQLQuery();
/*
String bigQuery =
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#>\n"+
"SELECT *\n"+
"WHERE {\n"+
"?resource ?p ?o.\n"+
"}" ;
q.create(bigQuery);
*/
q.defaultQuery();
q.execSelect();
q.dumpResults();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(q != null) {
q.close() ;
}
}
}
}