VIVO-469 run the URL Finders from an RDFService, not an OntModel

This commit is contained in:
j2blake 2013-11-07 12:36:05 -05:00
parent 5af822f7ab
commit 5d19e746e1
6 changed files with 149 additions and 53 deletions

View file

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -116,7 +117,11 @@ public class QueryUtils {
return getQueryResults(queryStr, vreq.getRDFService());
}
public static ResultSet getLanguageNeutralQueryResults(String queryStr, VitroRequest vreq) {
public static ResultSet getQueryResults(String queryStr, QuerySolution initialBindings, RDFService rdfService) {
return getQueryResults(bindVariables(queryStr, initialBindings), rdfService);
}
public static ResultSet getLanguageNeutralQueryResults(String queryStr, VitroRequest vreq) {
return getQueryResults(queryStr, vreq.getUnfilteredRDFService());
}
@ -130,4 +135,38 @@ public class QueryUtils {
}
}
/**
* The RDFService interface doesn't support initial bindings, so do text
* substitutions instead.
*/
public static String bindVariables(String queryStr,
QuerySolution initialBindings) {
String bound = queryStr;
for (Iterator<String> it = initialBindings.varNames(); it.hasNext();) {
String name = it.next();
RDFNode node = initialBindings.get(name);
if (node.isLiteral()) {
bound = bound.replace('?' + name, literalToString(node.asLiteral()));
} else if (node.isURIResource()) {
bound = bound.replace('?' + name, '<'+node.asResource().getURI()+ '>');
}else {
log.warn("Failed to bind anonymous resource variable '" + name
+ "' to query '" + bound + "'");
}
}
return bound;
}
private static String literalToString(Literal l) {
StringBuilder buffer = new StringBuilder();
buffer.append('"').append(l.getLexicalForm()).append('"');
if (l.getDatatypeURI() != null) {
buffer.append("^^<").append(l.getDatatypeURI()).append(">");
} else if (StringUtils.isNotEmpty(l.getLanguage())) {
buffer.append("@").append(l.getLanguage());
}
return buffer.toString();
}
}

View file

@ -10,22 +10,17 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.QuerySolutionMap;
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.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vitro.webapp.dao.jena.QueryUtils;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.search.beans.StatementToURIsToUpdate;
/**
@ -38,13 +33,13 @@ import edu.cornell.mannlib.vitro.webapp.search.beans.StatementToURIsToUpdate;
public class AdditionalURIsForObjectProperties implements StatementToURIsToUpdate {
protected static final Log log = LogFactory.getLog(AdditionalURIsForObjectProperties.class);
protected Model model;
protected final RDFService rdfService;
public AdditionalURIsForObjectProperties( Model model){
this.model = model;
}
@Override
public AdditionalURIsForObjectProperties(RDFService rdfService) {
this.rdfService = rdfService;
}
@Override
public List<String> findAdditionalURIsToIndex(Statement stmt) {
if( stmt == null )
return Collections.emptyList();
@ -102,37 +97,27 @@ public class AdditionalURIsForObjectProperties implements StatementToURIsToUpdat
Resource uriResource = ResourceFactory.createResource(uri);
initialBinding.add("uri", uriResource);
Query sparqlQuery = QueryFactory.create( QUERY_FOR_RELATED );
model.getLock().enterCriticalSection(Lock.READ);
try{
QueryExecution qExec = QueryExecutionFactory.create(sparqlQuery, model, initialBinding);
try{
ResultSet results = qExec.execSelect();
while(results.hasNext()){
QuerySolution soln = results.nextSolution();
Iterator<String> iter = soln.varNames() ;
while( iter.hasNext()){
String name = iter.next();
RDFNode node = soln.get( name );
if( node != null ){
if( node.isURIResource() ){
additionalUris.add( node.as( Resource.class ).getURI() );
}else{
log.warn( "value from query for var " + name + " was not a URIResource, it was " + node);
}
}else{
log.warn("value for query for var " + name + " was null");
}
ResultSet results = QueryUtils.getQueryResults(QUERY_FOR_RELATED,
initialBinding, rdfService);
while(results.hasNext()){
QuerySolution soln = results.nextSolution();
Iterator<String> iter = soln.varNames() ;
while( iter.hasNext()){
String name = iter.next();
RDFNode node = soln.get( name );
if( node != null ){
if( node.isURIResource() ){
additionalUris.add( node.as( Resource.class ).getURI() );
}else{
log.warn( "value from query for var " + name + " was not a URIResource, it was " + node);
}
}
}catch(Throwable t){
log.error(t,t);
} finally{
qExec.close();
}
}finally{
model.getLock().leaveCriticalSection();
}else{
log.warn("value for query for var " + name + " was null");
}
}
}
return additionalUris;
}

View file

@ -5,9 +5,8 @@ package edu.cornell.mannlib.vitro.webapp.search.indexing;
import java.util.ArrayList;
import java.util.List;
import com.hp.hpl.jena.ontology.OntModel;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.search.beans.StatementToURIsToUpdate;
/**
@ -16,12 +15,12 @@ import edu.cornell.mannlib.vitro.webapp.search.beans.StatementToURIsToUpdate;
*/
public class AdditionalUriFinders {
public static List<StatementToURIsToUpdate> getList(OntModel jenaOntModel,
public static List<StatementToURIsToUpdate> getList(RDFService rdfService,
IndividualDao indDao) {
// TODO How many of these are only relevant to VIVO?
List<StatementToURIsToUpdate> uriFinders = new ArrayList<>();
uriFinders.add(new AdditionalURIsForDataProperties());
uriFinders.add(new AdditionalURIsForObjectProperties(jenaOntModel));
uriFinders.add(new AdditionalURIsForObjectProperties(rdfService));
uriFinders.add(new AdditionalURIsForTypeStatements());
uriFinders.add(new URIsForClassGroupChange(indDao));
return uriFinders;

View file

@ -23,6 +23,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.filtering.WebappDaoFactoryFiltering;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilterUtils;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
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.beans.StatementToURIsToUpdate;
@ -133,7 +134,8 @@ public class SolrSetup implements javax.servlet.ServletContextListener{
wadf = new WebappDaoFactoryFiltering(wadf, vf);
// make objects that will find additional URIs for context nodes etc
List<StatementToURIsToUpdate> uriFinders = AdditionalUriFinders.getList(jenaOntModel,wadf.getIndividualDao());
RDFService rdfService = RDFServiceUtils.getRDFServiceFactory(context).getRDFService();
List<StatementToURIsToUpdate> uriFinders = AdditionalUriFinders.getList(rdfService,wadf.getIndividualDao());
// Make the IndexBuilder
IndexBuilder builder = new IndexBuilder( solrIndexer, wadf, uriFinders );