[VIVO-1031] Use consumer interface for most ResultSets

This commit is contained in:
grahamtriggs 2015-10-19 17:16:09 +01:00
parent 39b0d2e6bd
commit 34d78a9463
10 changed files with 268 additions and 278 deletions

View file

@ -12,6 +12,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -735,17 +736,18 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
} }
log.debug("Data property query string:\n" + query); log.debug("Data property query string:\n" + query);
ResultSet results = getPropertyQueryResults(queryString); final List<DataProperty> properties = new ArrayList<DataProperty>();
List<DataProperty> properties = new ArrayList<DataProperty>(); getPropertyQueryResults(queryString, new ResultSetConsumer() {
while (results.hasNext()) { @Override
QuerySolution sol = results.next(); protected void processQuerySolution(QuerySolution qs) {
Resource resource = sol.getResource("property"); Resource resource = qs.getResource("property");
String uri = resource.getURI(); String uri = resource.getURI();
DataProperty property = getDataPropertyByURI(uri); DataProperty property = getDataPropertyByURI(uri);
if (property != null) { if (property != null) {
properties.add(property); properties.add(property);
}
} }
} });
return properties; return properties;
} }
protected static final String LIST_VIEW_CONFIG_FILE_QUERY_STRING = protected static final String LIST_VIEW_CONFIG_FILE_QUERY_STRING =

View file

@ -12,6 +12,7 @@ import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -175,12 +176,11 @@ public class IndividualDaoSDB extends IndividualDaoJena {
} }
private List<Individual> getIndividualList(Resource theClass) { private List<Individual> getIndividualList(Resource theClass) {
List<Individual> ents = new ArrayList<Individual>(); final List<Individual> ents = new ArrayList<Individual>();
DatasetWrapper w = getDatasetWrapper(); DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset(); Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ); dataset.getLock().enterCriticalSection(Lock.READ);
try { try {
String query = String query =
"SELECT DISTINCT ?ind ?label " + "SELECT DISTINCT ?ind ?label " +
"WHERE " + "WHERE " +
@ -191,47 +191,53 @@ public class IndividualDaoSDB extends IndividualDaoJena {
" ?ind <" + RDFS.label.getURI() + "> ?label \n" + " ?ind <" + RDFS.label.getURI() + "> ?label \n" +
"} \n" + "} \n" +
"} ORDER BY ?ind ?label"; "} ORDER BY ?ind ?label";
RDFService rdfService = wadf.getRDFService(); RDFService rdfService = wadf.getRDFService();
InputStream in = null;
try { try {
in = rdfService.sparqlSelectQuery( rdfService.sparqlSelectQuery(
query, RDFService.ResultFormat.JSON); query, new ResultSetConsumer() {
String uri = null;
String label = null;
@Override
protected void processQuerySolution(QuerySolution qs) {
Resource currRes = qs.getResource("ind");
if (currRes.isAnon()) {
return;
}
if (uri != null && !uri.equals(currRes.getURI())) {
try {
ents.add(makeIndividual(uri, label));
} catch (IndividualNotFoundException e) {
// don't add
}
uri = currRes.getURI();
label = null;
} else if (uri == null) {
uri = currRes.getURI();
}
Literal labelLit = qs.getLiteral("label");
if (labelLit != null) {
label = labelLit.getLexicalForm();
}
}
@Override
protected void endProcessing() {
if (uri != null) {
try {
ents.add(makeIndividual(uri, label));
} catch (IndividualNotFoundException e) {
// don't add
}
}
}
});
} catch (RDFServiceException e) { } catch (RDFServiceException e) {
log.debug(e,e); log.debug(e,e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
ResultSet rs = ResultSetFactory.fromJSON(in);
String uri = null;
String label = null;
while (rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
Resource currRes = sol.getResource("ind");
if (currRes.isAnon()) {
continue;
}
if (uri != null && !uri.equals(currRes.getURI())) {
try {
ents.add(makeIndividual(uri, label));
} catch (IndividualNotFoundException e) {
// don't add
}
uri = currRes.getURI();
label = null;
} else if (uri == null) {
uri = currRes.getURI();
}
Literal labelLit = sol.getLiteral("label");
if (labelLit != null) {
label = labelLit.getLexicalForm();
}
if (!rs.hasNext()) {
try {
ents.add(makeIndividual(uri, label));
} catch (IndividualNotFoundException e) {
// don't add
}
}
}
} finally { } finally {
dataset.getLock().leaveCriticalSection(); dataset.getLock().leaveCriticalSection();
w.close(); w.close();
@ -241,7 +247,7 @@ public class IndividualDaoSDB extends IndividualDaoJena {
private List<Individual> getGraphFilteredIndividualList(Resource theClass, private List<Individual> getGraphFilteredIndividualList(Resource theClass,
String filterStr) { String filterStr) {
List<Individual> filteredIndividualList = new ArrayList<Individual>(); final List<Individual> filteredIndividualList = new ArrayList<Individual>();
DatasetWrapper w = getDatasetWrapper(); DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset(); Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ); dataset.getLock().enterCriticalSection(Lock.READ);
@ -254,28 +260,26 @@ public class IndividualDaoSDB extends IndividualDaoJena {
" } \n" + filterStr + " } \n" + filterStr +
"} ORDER BY ?ind"; "} ORDER BY ?ind";
RDFService rdfService = wadf.getRDFService(); RDFService rdfService = wadf.getRDFService();
InputStream in = null;
try { try {
in = rdfService.sparqlSelectQuery( rdfService.sparqlSelectQuery(
query, RDFService.ResultFormat.JSON); query, new ResultSetConsumer() {
@Override
protected void processQuerySolution(QuerySolution qs) {
Resource currRes = qs.getResource("ind");
if (!currRes.isAnon()) {
try {
filteredIndividualList.add(
makeIndividual(currRes.getURI(), null));
} catch (IndividualNotFoundException e) {
// don't add
}
}
}
});
} catch (RDFServiceException e) { } catch (RDFServiceException e) {
log.debug(e,e); log.debug(e,e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
ResultSet rs = ResultSetFactory.fromJSON(in);
while (rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
Resource currRes = sol.getResource("ind");
if (currRes.isAnon()) {
continue;
}
try {
filteredIndividualList.add(
makeIndividual(currRes.getURI(), null));
} catch (IndividualNotFoundException e) {
// don't add
}
}
} finally { } finally {
dataset.getLock().leaveCriticalSection(); dataset.getLock().leaveCriticalSection();
w.close(); w.close();

View file

@ -18,6 +18,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.hp.hpl.jena.graph.NodeFactory; import com.hp.hpl.jena.graph.NodeFactory;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -276,23 +277,23 @@ public class IndividualSDB extends IndividualImpl implements Individual {
@Override @Override
public List<String> getMostSpecificTypeURIs() { public List<String> getMostSpecificTypeURIs() {
List<String> typeURIs = new ArrayList<String>(); final List<String> typeURIs = new ArrayList<String>();
if (this.getURI() == null) { if (this.getURI() == null) {
return typeURIs; return typeURIs;
} else { } else {
String queryStr = "SELECT ?type WHERE { <" + this.getURI() + "> <" + String queryStr = "SELECT ?type WHERE { <" + this.getURI() + "> <" +
VitroVocabulary.MOST_SPECIFIC_TYPE + "> ?type }"; VitroVocabulary.MOST_SPECIFIC_TYPE + "> ?type }";
try { try {
InputStream json = webappDaoFactory.getRDFService().sparqlSelectQuery( webappDaoFactory.getRDFService().sparqlSelectQuery(queryStr, new ResultSetConsumer() {
queryStr, RDFService.ResultFormat.JSON); @Override
ResultSet rs = ResultSetFactory.fromJSON(json); protected void processQuerySolution(QuerySolution qs) {
while (rs.hasNext()) { RDFNode node = qs.get("type");
QuerySolution qsoln = rs.nextSolution(); if (node.isURIResource()) {
RDFNode node = qsoln.get("type"); typeURIs.add(node.asResource().getURI());
if (node.isURIResource()) { }
typeURIs.add(node.asResource().getURI()); }
} });
}
return typeURIs; return typeURIs;
} catch (RDFServiceException e) { } catch (RDFServiceException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -761,33 +762,35 @@ public class IndividualSDB extends IndividualImpl implements Individual {
} }
} }
private List<DataPropertyStatement> sparqlForDataPropertyStatements(String propertyUri) { private List<DataPropertyStatement> sparqlForDataPropertyStatements(final String propertyUri) {
List<DataPropertyStatement> stmts = new ArrayList<DataPropertyStatement>(); final List<DataPropertyStatement> stmts = new ArrayList<DataPropertyStatement>();
final IndividualSDB individualSDB = this;
String queryStr = "SELECT (str(?value) as ?valueString) WHERE { <" String queryStr = "SELECT (str(?value) as ?valueString) WHERE { <"
+ this.getURI() + "> <" + propertyUri + "> ?value }"; + this.getURI() + "> <" + propertyUri + "> ?value }";
try { try {
InputStream json = webappDaoFactory.getRDFService().sparqlSelectQuery( webappDaoFactory.getRDFService().sparqlSelectQuery(
queryStr, RDFService.ResultFormat.JSON); queryStr, new ResultSetConsumer() {
ResultSet rs = ResultSetFactory.fromJSON(json); @Override
while (rs.hasNext()) { protected void processQuerySolution(QuerySolution qs) {
QuerySolution qsoln = rs.nextSolution(); RDFNode node = qs.get("valueString");
RDFNode node = qsoln.get("valueString"); if (!node.isLiteral()) {
if (!node.isLiteral()) { log.debug("Ignoring non-literal value for " + node +
log.debug("Ignoring non-literal value for " + node + " for property " + propertyUri);
" for property " + propertyUri); } else {
} else { Literal lit = node.asLiteral();
Literal lit = node.asLiteral(); DataPropertyStatement stmt = new DataPropertyStatementImpl();
DataPropertyStatement stmt = new DataPropertyStatementImpl();
stmt.setData(lit.getLexicalForm()); stmt.setData(lit.getLexicalForm());
stmt.setDatatypeURI(lit.getDatatypeURI()); stmt.setDatatypeURI(lit.getDatatypeURI());
stmt.setLanguage(lit.getLanguage()); stmt.setLanguage(lit.getLanguage());
stmt.setDatapropURI(propertyUri); stmt.setDatapropURI(propertyUri);
stmt.setIndividualURI(this.getURI()); stmt.setIndividualURI(individualSDB.getURI());
stmt.setIndividual(this); stmt.setIndividual(individualSDB);
stmts.add(stmt); stmts.add(stmt);
} }
} }
});
} catch (RDFServiceException e) { } catch (RDFServiceException e) {
log.error(e,e); log.error(e,e);
throw new RuntimeException(e); throw new RuntimeException(e);
@ -842,14 +845,12 @@ public class IndividualSDB extends IndividualImpl implements Individual {
if (ind.getModel().contains((Resource) null, RDF.type, (RDFNode) null)){ if (ind.getModel().contains((Resource) null, RDF.type, (RDFNode) null)){
tempModel = ind.getModel(); tempModel = ind.getModel();
} else { } else {
tempModel = ModelFactory.createDefaultModel();
String getTypesQuery = buildMyVClassesQuery(assertedOnly); String getTypesQuery = buildMyVClassesQuery(assertedOnly);
RDFService service = webappDaoFactory.getRDFService(); RDFService service = webappDaoFactory.getRDFService();
try { try {
tempModel = RDFServiceUtils.parseModel( service.sparqlConstructQuery(getTypesQuery, tempModel);
service.sparqlConstructQuery(
getTypesQuery, RDFService.ModelSerializationFormat.N3),
RDFService.ModelSerializationFormat.N3);
} catch (RDFServiceException e) { } catch (RDFServiceException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View file

@ -10,6 +10,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -1021,38 +1022,42 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
} }
log.debug("Object property query:\n" + query); log.debug("Object property query:\n" + query);
ObjectProperty propRegister = new ObjectProperty();
propRegister.setURI("");
ResultSet results = getPropertyQueryResults(queryString); final List<ObjectProperty> properties = new ArrayList<ObjectProperty>();
List<ObjectProperty> properties = new ArrayList<ObjectProperty>(); getPropertyQueryResults(queryString, new ResultSetConsumer() {
while (results.hasNext()) { ObjectProperty propRegister = new ObjectProperty();
QuerySolution soln = results.next(); {
Resource resource = soln.getResource("property"); propRegister.setURI("");
String uri = resource.getURI(); }
Resource objType = soln.getResource("objType");
String objTypeUri = objType.getURI(); @Override
log.debug("Found populated object property " + uri + protected void processQuerySolution(QuerySolution qs) {
" with object type " + objType); Resource resource = qs.getResource("property");
ObjectProperty property = null; String uri = resource.getURI();
if (uri.equals(propRegister.getURI())) { Resource objType = qs.getResource("objType");
property = propRegister.clone(); String objTypeUri = objType.getURI();
} else { log.debug("Found populated object property " + uri + " with object type " + objType);
ObjectProperty newProperty = getObjectPropertyByURI(uri); ObjectProperty property = null;
if (newProperty != null) { if (uri.equals(propRegister.getURI())) {
propRegister = newProperty; property = propRegister.clone();
// add canonical instance of the property first in the list } else {
// before the range-changed versions ObjectProperty newProperty = getObjectPropertyByURI(uri);
properties.add(newProperty); if (newProperty != null) {
// isolate the canonical prop from what's about to happen next propRegister = newProperty;
property = newProperty.clone(); // add canonical instance of the property first in the list
// before the range-changed versions
properties.add(newProperty);
// isolate the canonical prop from what's about to happen next
property = newProperty.clone();
}
}
if (property != null) {
property.setRangeVClassURI(objTypeUri);
properties.add(property);
} }
} }
if (property != null) { });
property.setRangeVClassURI(objTypeUri);
properties.add(property);
}
}
return properties; return properties;
} }

View file

@ -12,6 +12,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -279,12 +280,12 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty( public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri, String subjectUri,
String propertyUri, String propertyUri,
String objectKey, String domainUri, String rangeUri, final String objectKey, String domainUri, String rangeUri,
String queryString, String queryString,
Set<String> constructQueryStrings, Set<String> constructQueryStrings,
String sortDirection) { String sortDirection) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>(); final List<Map<String, String>> list = new ArrayList<Map<String, String>>();
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
@ -299,22 +300,30 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
if("desc".equalsIgnoreCase( sortDirection ) ){ if("desc".equalsIgnoreCase( sortDirection ) ){
queryString = queryString.replaceAll(" ASC\\(", " DESC("); queryString = queryString.replaceAll(" ASC\\(", " DESC(");
} }
ResultSet results = (constructedModel == null) ? selectFromRDFService(
queryString, subjectUri, propertyUri, domainUri, rangeUri) : selectFromConstructedModel(
queryString, subjectUri, propertyUri, domainUri, rangeUri, constructedModel);
while (results.hasNext()) { ResultSetConsumer consumer = new ResultSetConsumer() {
QuerySolution soln = results.nextSolution(); @Override
RDFNode node = soln.get(objectKey); protected void processQuerySolution(QuerySolution qs) {
if (node != null && node.isURIResource()) { RDFNode node = qs.get(objectKey);
list.add(QueryUtils.querySolutionToStringValueMap(soln)); if (node != null && node.isURIResource()) {
} list.add(QueryUtils.querySolutionToStringValueMap(qs));
}
if(log.isDebugEnabled()) {
long duration = System.currentTimeMillis() - start;
log.debug(duration + " to do list view for " +
propertyUri + " / " + domainUri + " / " + rangeUri);
} }
}
};
if (constructedModel == null) {
selectFromRDFService(
queryString, subjectUri, propertyUri, domainUri, rangeUri, consumer);
} else {
selectFromConstructedModel(
queryString, subjectUri, propertyUri, domainUri, rangeUri, constructedModel, consumer);
}
if(log.isDebugEnabled()) {
long duration = System.currentTimeMillis() - start;
log.debug(duration + " to do list view for " +
propertyUri + " / " + domainUri + " / " + rangeUri);
}
} catch (Exception e) { } catch (Exception e) {
log.error("Error getting object property values for subject " + subjectUri + " and property " + propertyUri, e); log.error("Error getting object property values for subject " + subjectUri + " and property " + propertyUri, e);
return Collections.emptyList(); return Collections.emptyList();
@ -322,8 +331,8 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
return list; return list;
} }
private ResultSet selectFromRDFService(String queryString, String subjectUri, private void selectFromRDFService(String queryString, String subjectUri,
String propertyUri, String domainUri, String rangeUri) { String propertyUri, String domainUri, String rangeUri, ResultSetConsumer consumer) {
String[] part = queryString.split("[Ww][Hh][Ee][Rr][Ee]"); String[] part = queryString.split("[Ww][Hh][Ee][Rr][Ee]");
part[1] = part[1].replace("?subject", "<" + subjectUri + ">"); part[1] = part[1].replace("?subject", "<" + subjectUri + ">");
part[1] = part[1].replace("?property", "<" + propertyUri + ">"); part[1] = part[1].replace("?property", "<" + propertyUri + ">");
@ -335,16 +344,15 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
} }
queryString = part[0] + "WHERE" + part[1]; queryString = part[0] + "WHERE" + part[1];
try { try {
return ResultSetFactory.fromJSON( rdfService.sparqlSelectQuery(queryString, consumer);
rdfService.sparqlSelectQuery(queryString, RDFService.ResultFormat.JSON));
} catch (RDFServiceException e) { } catch (RDFServiceException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private ResultSet selectFromConstructedModel(String queryString, private void selectFromConstructedModel(String queryString,
String subjectUri, String propertyUri, String domainUri, String rangeUri, String subjectUri, String propertyUri, String domainUri, String rangeUri,
Model constructedModel) { Model constructedModel, ResultSetConsumer consumer) {
Query query = null; Query query = null;
try { try {
query = QueryFactory.create(queryString, Syntax.syntaxARQ); query = QueryFactory.create(queryString, Syntax.syntaxARQ);
@ -374,7 +382,7 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
try { try {
qexec = QueryExecutionFactory.create( qexec = QueryExecutionFactory.create(
query, constructedModel, initialBindings); query, constructedModel, initialBindings);
return new ResultSetMem(qexec.execSelect()); consumer.processResultSet(qexec.execSelect());
} finally { } finally {
if (qexec != null) { if (qexec != null) {
qexec.close(); qexec.close();
@ -441,25 +449,7 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
w.close(); w.close();
} }
} else { } else {
String parseFormat = "N3"; rdfService.sparqlConstructQuery(queryString, constructedModel);
RDFService.ModelSerializationFormat resultFormat = RDFService
.ModelSerializationFormat.N3;
/* If the test ObjectPropertyStatementDaoJenaTest.testN3WithSameAs()
* fails this code can be removed: */
if( OWL.sameAs.getURI().equals( propertyUri )){
// VIVO-111: owl:sameAs can be represented as = in n3 but
// Jena's parser does not recognize it.
// Switch to rdf/xml only for sameAs since it seems like n3
// would be faster the rest of the time.
parseFormat = "RDF/XML";
resultFormat = RDFService.ModelSerializationFormat.RDFXML;
}
/* end of removal */
InputStream is = rdfService.sparqlConstructQuery(
queryString, resultFormat);
constructedModel.read( is, null, parseFormat);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Error getting constructed model for subject " log.error("Error getting constructed model for subject "

View file

@ -13,6 +13,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -440,43 +441,23 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao {
return classSet; return classSet;
} }
protected ResultSet getPropertyQueryResults(String queryString) { protected void getPropertyQueryResults(String queryString, ResultSetConsumer consumer) {
log.debug("SPARQL query:\n" + queryString); log.debug("SPARQL query:\n" + queryString);
// RY Removing prebinding due to Jena bug: when isLiteral(?object) or // RY Removing prebinding due to Jena bug: when isLiteral(?object) or
// isURI(?object) is added to the query as a filter, the query fails with prebinding // isURI(?object) is added to the query as a filter, the query fails with prebinding
// but succeeds when the subject uri is concatenated into the query string. // but succeeds when the subject uri is concatenated into the query string.
//QuerySolutionMap subjectBinding = new QuerySolutionMap(); //QuerySolutionMap subjectBinding = new QuerySolutionMap();
//subjectBinding.add("subject", ResourceFactory.createResource(subjectUri)); //subjectBinding.add("subject", ResourceFactory.createResource(subjectUri));
// Run the SPARQL query to get the properties // Run the SPARQL query to get the properties
try { try {
return ResultSetFactory.fromJSON( getRDFService().sparqlSelectQuery(queryString, consumer);
getRDFService().sparqlSelectQuery( } catch (RDFServiceException e) {
queryString, RDFService.ResultFormat.JSON)); throw new RuntimeException(e);
} catch (RDFServiceException e) { }
throw new RuntimeException(e); }
}
// DatasetWrapper w = dwf.getDatasetWrapper();
// Dataset dataset = w.getDataset();
// dataset.getLock().enterCriticalSection(Lock.READ);
// ResultSet rs = null;
// try {
// QueryExecution qexec = QueryExecutionFactory.create(
// query, dataset); //, subjectBinding);
// try {
// rs = new ResultSetMem(qexec.execSelect());
// } finally {
// qexec.close();
// }
// } finally {
// dataset.getLock().leaveCriticalSection();
// w.close();
// }
// return rs;
}
/** /**
* requires SPARQL 1.1 (or ARQ) property path support * requires SPARQL 1.1 (or ARQ) property path support

View file

@ -24,6 +24,7 @@ import com.hp.hpl.jena.util.iterator.WrappedIterator;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import edu.cornell.mannlib.vitro.webapp.utils.logging.ToString; import edu.cornell.mannlib.vitro.webapp.utils.logging.ToString;
public class RDFServiceDatasetGraph implements DatasetGraph { public class RDFServiceDatasetGraph implements DatasetGraph {
@ -109,7 +110,7 @@ public class RDFServiceDatasetGraph implements DatasetGraph {
} }
@Override @Override
public Iterator<Quad> find(Node graph, Node subject, Node predicate, Node object) { public Iterator<Quad> find(final Node graph, final Node subject, final Node predicate, final Node object) {
if (!isVar(subject) && !isVar(predicate) && !isVar(object) &&!isVar(graph)) { if (!isVar(subject) && !isVar(predicate) && !isVar(object) &&!isVar(graph)) {
if (contains(subject, predicate, object, graph)) { if (contains(subject, predicate, object, graph)) {
return new SingletonIterator<Quad>(new Quad(subject, predicate, object, graph)); return new SingletonIterator<Quad>(new Quad(subject, predicate, object, graph));
@ -136,25 +137,23 @@ public class RDFServiceDatasetGraph implements DatasetGraph {
//log.info(findQuery.toString()); //log.info(findQuery.toString());
ResultSet rs = null; final List<Quad> quadlist = new ArrayList<Quad>();
try { try {
rs = JSONInput.fromJSON(rdfService.sparqlSelectQuery( rdfService.sparqlSelectQuery(findQuery.toString(), new ResultSetConsumer() {
findQuery.toString(), RDFService.ResultFormat.JSON)); @Override
protected void processQuerySolution(QuerySolution qs) {
Quad q = new Quad(isVar(graph) ? qs.get("?g").asNode() : graph,
isVar(subject) ? qs.get("?s").asNode() : subject,
isVar(predicate) ? qs.get("?p").asNode() : predicate,
isVar(object) ? qs.get("?o").asNode() : object);
quadlist.add(q);
}
});
} catch (RDFServiceException rdfse) { } catch (RDFServiceException rdfse) {
throw new RuntimeException(rdfse); throw new RuntimeException(rdfse);
} }
List<Quad> quadlist = new ArrayList<Quad>();
while (rs.hasNext()) {
QuerySolution soln = rs.nextSolution();
Quad q = new Quad(isVar(graph) ? soln.get("?g").asNode() : graph,
isVar(subject) ? soln.get("?s").asNode() : subject,
isVar(predicate) ? soln.get("?p").asNode() : predicate,
isVar(object) ? soln.get("?o").asNode() : object);
//log.info(t);
quadlist.add(q);
}
//log.info(triplist.size() + " results"); //log.info(triplist.size() + " results");
return WrappedIterator.create(quadlist.iterator()); } return WrappedIterator.create(quadlist.iterator()); }

View file

@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -163,8 +164,10 @@ public class RDFServiceGraph implements GraphWithPerform {
containsQuery.append(" } \n"); containsQuery.append(" } \n");
} }
containsQuery.append("} \nLIMIT 1 "); containsQuery.append("} \nLIMIT 1 ");
ResultSet result = execSelect(containsQuery.toString());
return result.hasNext(); ResultSetConsumer.HasResult consumer = new ResultSetConsumer.HasResult();
execSelect(containsQuery.toString(), consumer);
return consumer.hasResult();
} }
@Override @Override
@ -232,7 +235,7 @@ public class RDFServiceGraph implements GraphWithPerform {
} }
@Override @Override
public ExtendedIterator<Triple> find(Node subject, Node predicate, Node object) { public ExtendedIterator<Triple> find(final Node subject, final Node predicate, final Node object) {
if (!isVar(subject) && !isVar(predicate) && !isVar(object)) { if (!isVar(subject) && !isVar(predicate) && !isVar(object)) {
if (contains(subject, predicate, object)) { if (contains(subject, predicate, object)) {
return new SingletonIterator<Triple>(new Triple(subject, predicate, object)); return new SingletonIterator<Triple>(new Triple(subject, predicate, object));
@ -256,17 +259,30 @@ public class RDFServiceGraph implements GraphWithPerform {
String queryString = findQuery.toString(); String queryString = findQuery.toString();
ResultSet rs = execSelect(queryString); final List<Triple> triplist = new ArrayList<Triple>();
List<Triple> triplist = new ArrayList<Triple>(); execSelect(queryString, new ResultSetConsumer() {
@Override
protected void processQuerySolution(QuerySolution qs) {
Triple t = new Triple(isVar(subject) ? qs.get("?s").asNode() : subject,
isVar(predicate) ? qs.get("?p").asNode() : predicate,
isVar(object) ? qs.get("?o").asNode() : object);
//log.info(t);
triplist.add(t);
}
});
/*
ResultSet rs = execSelect(queryString);
while (rs.hasNext()) { while (rs.hasNext()) {
QuerySolution soln = rs.nextSolution(); QuerySolution soln = rs.nextSolution();
Triple t = new Triple(isVar(subject) ? soln.get("?s").asNode() : subject, Triple t = new Triple(isVar(subject) ? soln.get("?s").asNode() : subject,
isVar(predicate) ? soln.get("?p").asNode() : predicate, isVar(predicate) ? soln.get("?p").asNode() : predicate,
isVar(object) ? soln.get("?o").asNode() : object); isVar(object) ? soln.get("?o").asNode() : object);
//log.info(t); //log.info(t);
triplist.add(t); triplist.add(t);
} }
*/
//log.info(triplist.size() + " results"); //log.info(triplist.size() + " results");
return WrappedIterator.create(triplist.iterator()); return WrappedIterator.create(triplist.iterator());
} }
@ -389,18 +405,9 @@ public class RDFServiceGraph implements GraphWithPerform {
} }
}; };
private boolean execAsk(String queryStr) { private void execSelect(String queryStr, ResultSetConsumer consumer) {
try { try {
return rdfService.sparqlAskQuery(queryStr); rdfService.sparqlSelectQuery(queryStr, consumer);
} catch (RDFServiceException rdfse) {
throw new RuntimeException(rdfse);
}
}
private ResultSet execSelect(String queryStr) {
try {
return JSONInput.fromJSON(rdfService.sparqlSelectQuery(
queryStr, RDFService.ResultFormat.JSON));
} catch (RDFServiceException rdfse) { } catch (RDFServiceException rdfse) {
throw new RuntimeException(rdfse); throw new RuntimeException(rdfse);
} }

View file

@ -8,6 +8,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -168,8 +169,9 @@ public abstract class RDFServiceImpl implements RDFService {
} }
protected boolean sparqlSelectQueryHasResults(String queryStr) throws RDFServiceException { protected boolean sparqlSelectQueryHasResults(String queryStr) throws RDFServiceException {
ResultSet rs = XMLInput.fromXML(sparqlSelectQuery(queryStr, ResultFormat.XML)); ResultSetConsumer.HasResult hasResult = new ResultSetConsumer.HasResult();
return rs.hasNext(); sparqlSelectQuery(queryStr, hasResult);
return hasResult.hasResult();
} }
protected static String sparqlTriple(Triple triple) { protected static String sparqlTriple(Triple triple) {

View file

@ -411,20 +411,19 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
} }
private List<String> getGraphURIsFromSparqlQuery(String queryString) throws RDFServiceException { private List<String> getGraphURIsFromSparqlQuery(String queryString) throws RDFServiceException {
List<String> graphURIs = new ArrayList<String>(); final List<String> graphURIs = new ArrayList<String>();
try { try {
sparqlSelectQuery(queryString, new ResultSetConsumer() {
ResultSet rs = ResultSetFactory.fromJSON( @Override
sparqlSelectQuery(queryString, RDFService.ResultFormat.JSON)); protected void processQuerySolution(QuerySolution qs) {
while (rs.hasNext()) { if (qs != null) { // no idea how this happens, but it seems to
QuerySolution qs = rs.nextSolution(); RDFNode n = qs.getResource("g");
if (qs != null) { // no idea how this happens, but it seems to if (n != null && n.isResource()) {
RDFNode n = qs.getResource("g"); graphURIs.add(((Resource) n).getURI());
if (n != null && n.isResource()) { }
graphURIs.add(((Resource) n).getURI()); }
} }
} });
}
} catch (Exception e) { } catch (Exception e) {
throw new RDFServiceException("Unable to list graph URIs", e); throw new RDFServiceException("Unable to list graph URIs", e);
} }