NIHVIVO-3797 routing context node fields queries through RDFService

This commit is contained in:
stellamit 2012-06-25 21:34:58 +00:00
parent ac3b58874f
commit 4602b4364d
2 changed files with 58 additions and 45 deletions

View file

@ -8,6 +8,11 @@ import java.io.UnsupportedEncodingException;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFactory;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.resultset.ResultSetFormat; import com.hp.hpl.jena.sparql.resultset.ResultSetFormat;
@ -16,7 +21,9 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService.ModelSerializationFormat; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService.ModelSerializationFormat;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService.ResultFormat; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService.ResultFormat;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory;
import edu.cornell.mannlib.vitro.webapp.search.solr.ContextNodeFields;
public class RDFServiceUtils { public class RDFServiceUtils {
@ -25,6 +32,7 @@ public class RDFServiceUtils {
private static final String RDFSERVICEFACTORY_FILTERING_ATTR = private static final String RDFSERVICEFACTORY_FILTERING_ATTR =
RDFServiceUtils.class.getName() + ".RDFServiceFactory.Filtering"; RDFServiceUtils.class.getName() + ".RDFServiceFactory.Filtering";
public static RDFServiceFactory getRDFServiceFactory(ServletContext context) { public static RDFServiceFactory getRDFServiceFactory(ServletContext context) {
Object o = context.getAttribute(RDFSERVICEFACTORY_ATTR); Object o = context.getAttribute(RDFSERVICEFACTORY_ATTR);
return (o instanceof RDFServiceFactory) ? (RDFServiceFactory) o : null; return (o instanceof RDFServiceFactory) ? (RDFServiceFactory) o : null;
@ -80,5 +88,20 @@ public class RDFServiceUtils {
return getRDFServiceFactory( return getRDFServiceFactory(
vreq.getSession().getServletContext()).getRDFService(); vreq.getSession().getServletContext()).getRDFService();
} }
public static ResultSet sparqlSelectQuery(String query, RDFService rdfService) {
ResultSet resultSet = null;
try {
InputStream resultStream = rdfService.sparqlSelectQuery(query, RDFService.ResultFormat.JSON);
resultSet = ResultSetFactory.fromJSON(resultStream);
return resultSet;
} catch (RDFServiceException e) {
Log log = LogFactory.getLog(ContextNodeFields.class);
log.error("error executing sparql select query: " + e.getMessage());
}
return resultSet;
}
} }

View file

@ -11,20 +11,16 @@ import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField; import org.apache.solr.common.SolrInputField;
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.QuerySolution;
import com.hp.hpl.jena.query.QuerySolutionMap; import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.shared.Lock;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames; import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames;
/** /**
@ -36,29 +32,21 @@ import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames;
* *
*/ */
public class ContextNodeFields implements DocumentModifier{ public class ContextNodeFields implements DocumentModifier{
protected Model model;
protected List<String> queries = new ArrayList<String>(); protected List<String> queries = new ArrayList<String>();
protected boolean shutdown = false; protected boolean shutdown = false;
protected Log log = LogFactory.getLog(ContextNodeFields.class); protected Log log = LogFactory.getLog(ContextNodeFields.class);
protected RDFServiceFactory rdfServiceFactory;
/** /**
* Construct this with a model to query when building Solr Documents and * Construct this with a model to query when building Solr Documents and
* a list of the SPARQL queries to run. * a list of the SPARQL queries to run.
*/ */
protected ContextNodeFields(Model model, List<String> queries){ protected ContextNodeFields(List<String> queries, RDFServiceFactory rdfServiceFactory){
this.model = model;
this.queries = queries; this.queries = queries;
this.rdfServiceFactory = rdfServiceFactory;
} }
/**
* Implement this method to get values that will be added to ALLTEXT
* field of solr Document for each individual.
*
* @param individual
* @return StringBuffer with text values to add to ALLTEXT field of solr Document.
*/
protected StringBuffer getValues( Individual individual ){ protected StringBuffer getValues( Individual individual ){
return executeQueryForValues( individual, queries ); return executeQueryForValues( individual, queries );
} }
@ -68,10 +56,10 @@ public class ContextNodeFields implements DocumentModifier{
if( individual == null ) if( individual == null )
return; return;
log.debug( "doing context nodes for: " + individual.getURI()); log.debug( "processing context nodes for: " + individual.getURI());
/* get text from the context nodes and add the to ALLTEXT */ /* get text from the context nodes and add the to ALLTEXT */
StringBuffer values = getValues( individual ); StringBuffer values = executeQueryForValues(individual, queries);
SolrInputField field = doc.getField(VitroSearchTermNames.ALLTEXT); SolrInputField field = doc.getField(VitroSearchTermNames.ALLTEXT);
if( field == null ){ if( field == null ){
@ -81,10 +69,17 @@ public class ContextNodeFields implements DocumentModifier{
} }
} }
/**
* this method gets values that will be added to ALLTEXT
* field of solr Document for each individual.
*
* @param individual
* @return StringBuffer with text values to add to ALLTEXT field of solr Document.
*/
protected StringBuffer executeQueryForValues( Individual individual, Collection<String> queries){ protected StringBuffer executeQueryForValues( Individual individual, Collection<String> queries){
/* execute all the queries on the list and concat the values to add to all text */ /* execute all the queries on the list and concat the values to add to all text */
RDFService rdfService = rdfServiceFactory.getRDFService();
StringBuffer allValues = new StringBuffer(""); StringBuffer allValues = new StringBuffer("");
QuerySolutionMap initialBinding = new QuerySolutionMap(); QuerySolutionMap initialBinding = new QuerySolutionMap();
@ -93,35 +88,30 @@ public class ContextNodeFields implements DocumentModifier{
for(String query : queries ){ for(String query : queries ){
StringBuffer valuesForQuery = new StringBuffer(); StringBuffer valuesForQuery = new StringBuffer();
Query sparqlQuery = QueryFactory.create( query, Syntax.syntaxARQ);
model.getLock().enterCriticalSection(Lock.READ);
try{ try{
QueryExecution qExec = ResultSet results = RDFServiceUtils.sparqlSelectQuery(query, rdfService);
QueryExecutionFactory.create(sparqlQuery, model, initialBinding);
try{ while(results.hasNext()){
ResultSet results = qExec.execSelect(); valuesForQuery.append(
while(results.hasNext()){ getTextForRow( results.nextSolution() ) ) ;
valuesForQuery.append( }
getTextForRow( results.nextSolution() ) ) ;
} }catch(Throwable t){
}catch(Throwable t){ if( ! shutdown )
if( ! shutdown ) log.error(t,t);
log.error(t,t); }
} finally{
qExec.close();
}
}finally{
model.getLock().leaveCriticalSection();
}
if(log.isDebugEnabled()){ if(log.isDebugEnabled()){
log.debug("query: '" + query + "'"); log.debug("query: '" + query + "'");
log.debug("text for query: '" + valuesForQuery.toString() + "'"); log.debug("text for query: '" + valuesForQuery.toString() + "'");
} }
allValues.append(valuesForQuery); allValues.append(valuesForQuery);
} }
rdfService.close();
return allValues; return allValues;
} }
protected String getTextForRow( QuerySolution row){ protected String getTextForRow( QuerySolution row){
if( row == null ) if( row == null )
return ""; return "";