edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.SparqlEvaluateVTwo Maven / Gradle / Ivy
/* $This file is distributed under the terms of the license in LICENSE$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SparqlEvaluateVTwo {
private static Log log = LogFactory.getLog( SparqlEvaluateVTwo.class );
Model model;
public SparqlEvaluateVTwo(Model model){
if( model == null ) throw new Error("SparqlEvaluate must be passed a Model");
this.model = model;
}
public void evaluateForAdditionalUris( EditConfigurationVTwo editConfig ){
log.debug("Evaluating for Additional URIS");
Map> varsToUris = sparqlEvaluateForUris(editConfig, editConfig.getSparqlForAdditionalUrisInScope());
editConfig.getUrisInScope().putAll(varsToUris);
}
public void evalulateForAdditionalLiterals( EditConfigurationVTwo editConfig ){
log.debug("Evaluating for Additional Literals");
Map> varsToLiterals = sparqlEvaluateForLiterals(editConfig, editConfig.getSparqlForAdditionalLiteralsInScope());
editConfig.getLiteralsInScope().putAll(varsToLiterals);
}
public void evaluateForExistingUris( EditConfigurationVTwo editConfig){
log.debug("Evaluating for existing URIS");
Map> varsToUris = sparqlEvaluateForUris(editConfig, editConfig.getSparqlForExistingUris());
editConfig.getUrisInScope().putAll(varsToUris);
}
public void evaluateForExistingLiterals( EditConfigurationVTwo editConfig){
log.debug("Evaluating for existing literals");
Map> varsToLiterals = sparqlEvaluateForLiterals(editConfig, editConfig.getSparqlForExistingLiterals());
editConfig.getLiteralsInScope().putAll(varsToLiterals);
}
// public Map sparqlEvaluateForExistingToUris(Map varToSpqrql){
// Map varToUris = new HashMap();
// for(String var : varToSpqrql.keySet()){
// varToUris.put(var, queryToUri( varToSpqrql.get(var) ));
// }
// return varToUris;
// }
//
// public Map sparqlEvaluateForAdditionalLiterals(Map varToSpqrql){
// Map varToLiterals = new HashMap();
// for(String var : varToSpqrql.keySet()){
// varToLiterals.put(var, queryToLiteral( varToSpqrql.get(var) ));
// }
// return varToLiterals;
// }
// private Map sparqlEvaluateForExistingToUris( EditConfiguration editConfig ) {
// Map varToSpqrql = editConfig.getSparqlForExistingUris();
// Map uriScope = editConfig.getUrisInScope();
// Map literalScope = editConfig.getLiteralsInScope();
//
// Map varToUris = new HashMap();
//
// for(String var : varToSpqrql.keySet()){
// String query = varToSpqrql.get(var);
// List queryStrings = new ArrayList ();
// queryStrings.add(query);
// queryStrings= editConfig.getN3Generator().subInUris(uriScope, queryStrings);
// queryStrings = editConfig.getN3Generator().subInLiterals(literalScope,queryStrings);
// varToUris.put(var, queryToUri( queryStrings.get(0) )); //might result in (key -> null)
// }
//
// return varToUris;
// }
public Map> sparqlEvaluateForLiterals( EditConfigurationVTwo editConfig, Map varToSparql) {
Map> uriScope = editConfig.getUrisInScope();
Map> literalScope = editConfig.getLiteralsInScope();
Map> varToLiterals = new HashMap>();
for(String var : varToSparql.keySet()){
String query = varToSparql.get(var);
log.debug("Var name " + var + " and query = " + query);
/* skip if var set to use a system generated value */
if( query == null || EditConfigurationVTwo.USE_SYSTEM_VALUE.equals( query )) {
log.debug("Query is null or using system value so will not continue with rest of method");
continue;
}
List queryStrings = new ArrayList ();
queryStrings.add( query );
editConfig.getN3Generator().subInMultiUris(uriScope, queryStrings);
log.debug("Query after substituting uris in scope: " + queryStrings.toString());
editConfig.getN3Generator().subInMultiLiterals(literalScope,queryStrings);
log.debug("Query after substituting literals in scope: " + queryStrings.toString());
varToLiterals.put(var, queryToLiteral( queryStrings.get(0) )); //might result in (key -> null)
}
return varToLiterals;
}
public Map> sparqlEvaluateForUris( EditConfigurationVTwo editConfig, MapvarToSparql) {
Map> uriScope = editConfig.getUrisInScope();
Map> literalScope = editConfig.getLiteralsInScope();
Map> varToUris = new HashMap>();
for(String var : varToSparql.keySet()){
String query = varToSparql.get(var);
log.debug("Var name " + var + " and query = " + query);
/* skip if var set to use a system generated value */
if( query == null || EditConfigurationVTwo.USE_SYSTEM_VALUE.equals( query )) {
log.debug("Query is null or using system value so will not continue with rest of method");
continue;
}
List queryStrings = new ArrayList ();
queryStrings.add(query);
editConfig.getN3Generator().subInMultiUris(uriScope, queryStrings);
log.debug("Query after substituting uris in scope: " + queryStrings.toString());
editConfig.getN3Generator().subInMultiLiterals(literalScope,queryStrings);
log.debug("Query after substituting literals in scope: " + queryStrings.toString());
List uriFromQuery = queryToUri( queryStrings.get(0) );
if( uriFromQuery != null )
{
//Added parens and output
varToUris.put(var, uriFromQuery);
}
else
log.debug("sparqlEvaluateForUris(): for var " + var
+ " the following query evaluated to null:\n"+queryStrings.get(0)+"\n(end of query)\n");
}
return varToUris;
}
// public Map sparqlEvaluateForAdditionalLiterals( EditConfiguration editConfig) {
// Map varToSpqrql = editConfig.getSparqlForAdditionalLiteralsInScope();
// Map uriScope = editConfig.getUrisInScope();
// Map literalScope = editConfig.getLiteralsInScope();
//
// Map varToLiterals = new HashMap();
// for(String var : varToSpqrql.keySet()){
// String query = varToSpqrql.get(var);
// List queryStrings = new ArrayList ();
// queryStrings.add( query );
// queryStrings= editConfig.getN3Generator().subInUris(uriScope, queryStrings);
// queryStrings = editConfig.getN3Generator().subInLiterals(literalScope,queryStrings);
// Literal literalFromQuery = queryToLiteral( queryStrings.get(0) );
// if( literalFromQuery != null )
// varToLiterals.put(var, literalFromQuery );
// else
// log.debug("sparqlEvaluateForAdditionalLiterals(): for var " + var
// + "query evaluated to null. query: '" + queryStrings.get(0) +"'");
// }
//
// return varToLiterals;
// }
//now can return multiple uris
public List queryToUri(String querystr){
log.debug("Query string in queryToUri():" + querystr);
String value = null;
List values = new ArrayList();
QueryExecution qe = null;
try{
Query query = QueryFactory.create(querystr);
qe = QueryExecutionFactory.create(query, model);
if( query.isSelectType() ){
ResultSet results = null;
results = qe.execSelect();
if( results.hasNext()){
List vars = results.getResultVars();
if( vars == null )
throw new Error("sparql had no result variables");
for(String var: vars) {
QuerySolution qs = results.nextSolution();
Resource resource = qs.getResource(var);
value = resource.getURI();
values.add(value);
}
}else{
log.debug("Query had no results");
return null;
}
} else {
throw new Error("only SELECT type SPARQL queries are supported");
}
}catch(Exception ex){
throw new Error("could not parse SPARQL in queryToUri: \n" + querystr + '\n' + ex.getMessage());
}finally{
if( qe != null)
qe.close();
}
if( log.isDebugEnabled() ) log.debug("queryToUri() query: '"+ querystr +"'\nvalue: '" + values.toString() +"'");
return values;
}
public List queryToLiteral(String querystr){
log.debug("Executing query " + querystr);
Literal value = null;
List values = new ArrayList();
QueryExecution qe = null;
try{
Query query = QueryFactory.create(querystr);
qe = QueryExecutionFactory.create(query, model);
if( query.isSelectType() ){
ResultSet results = null;
results = qe.execSelect();
if( results.hasNext()){
List vars = results.getResultVars();
if( vars == null )
throw new Error("sparql had no result variables");
for(String var: vars){
QuerySolution qs = results.nextSolution();
value = qs.getLiteral(var);
values.add(value);
}
}else{
log.debug("Query had no results");
return null;
}
} else {
throw new Error("only SELECT type SPARQL queries are supported");
}
}catch(Exception ex){
throw new Error("could not parse SPARQL in queryToLiteral: \n" + querystr + '\n' + ex.getMessage());
}finally{
if( qe != null)
qe.close();
}
if( log.isDebugEnabled() ) log.debug("queryToLiteral() query: '"+ querystr +"'\nvalue: '" + values.toString() +"'");
return values;
}
}