[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.Map;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -735,17 +736,18 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
}
log.debug("Data property query string:\n" + query);
ResultSet results = getPropertyQueryResults(queryString);
List<DataProperty> properties = new ArrayList<DataProperty>();
while (results.hasNext()) {
QuerySolution sol = results.next();
Resource resource = sol.getResource("property");
String uri = resource.getURI();
DataProperty property = getDataPropertyByURI(uri);
if (property != null) {
properties.add(property);
final List<DataProperty> properties = new ArrayList<DataProperty>();
getPropertyQueryResults(queryString, new ResultSetConsumer() {
@Override
protected void processQuerySolution(QuerySolution qs) {
Resource resource = qs.getResource("property");
String uri = resource.getURI();
DataProperty property = getDataPropertyByURI(uri);
if (property != null) {
properties.add(property);
}
}
}
});
return properties;
}
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.List;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -175,12 +176,11 @@ public class IndividualDaoSDB extends IndividualDaoJena {
}
private List<Individual> getIndividualList(Resource theClass) {
List<Individual> ents = new ArrayList<Individual>();
final List<Individual> ents = new ArrayList<Individual>();
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
String query =
"SELECT DISTINCT ?ind ?label " +
"WHERE " +
@ -191,47 +191,53 @@ public class IndividualDaoSDB extends IndividualDaoJena {
" ?ind <" + RDFS.label.getURI() + "> ?label \n" +
"} \n" +
"} ORDER BY ?ind ?label";
RDFService rdfService = wadf.getRDFService();
InputStream in = null;
try {
in = rdfService.sparqlSelectQuery(
query, RDFService.ResultFormat.JSON);
rdfService.sparqlSelectQuery(
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) {
log.debug(e,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 {
dataset.getLock().leaveCriticalSection();
w.close();
@ -241,7 +247,7 @@ public class IndividualDaoSDB extends IndividualDaoJena {
private List<Individual> getGraphFilteredIndividualList(Resource theClass,
String filterStr) {
List<Individual> filteredIndividualList = new ArrayList<Individual>();
final List<Individual> filteredIndividualList = new ArrayList<Individual>();
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
@ -254,28 +260,26 @@ public class IndividualDaoSDB extends IndividualDaoJena {
" } \n" + filterStr +
"} ORDER BY ?ind";
RDFService rdfService = wadf.getRDFService();
InputStream in = null;
try {
in = rdfService.sparqlSelectQuery(
query, RDFService.ResultFormat.JSON);
rdfService.sparqlSelectQuery(
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) {
log.debug(e,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 {
dataset.getLock().leaveCriticalSection();
w.close();

View file

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

View file

@ -10,6 +10,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -1021,38 +1022,42 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
}
log.debug("Object property query:\n" + query);
ObjectProperty propRegister = new ObjectProperty();
propRegister.setURI("");
ResultSet results = getPropertyQueryResults(queryString);
List<ObjectProperty> properties = new ArrayList<ObjectProperty>();
while (results.hasNext()) {
QuerySolution soln = results.next();
Resource resource = soln.getResource("property");
String uri = resource.getURI();
Resource objType = soln.getResource("objType");
String objTypeUri = objType.getURI();
log.debug("Found populated object property " + uri +
" with object type " + objType);
ObjectProperty property = null;
if (uri.equals(propRegister.getURI())) {
property = propRegister.clone();
} else {
ObjectProperty newProperty = getObjectPropertyByURI(uri);
if (newProperty != null) {
propRegister = newProperty;
// 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();
final List<ObjectProperty> properties = new ArrayList<ObjectProperty>();
getPropertyQueryResults(queryString, new ResultSetConsumer() {
ObjectProperty propRegister = new ObjectProperty();
{
propRegister.setURI("");
}
@Override
protected void processQuerySolution(QuerySolution qs) {
Resource resource = qs.getResource("property");
String uri = resource.getURI();
Resource objType = qs.getResource("objType");
String objTypeUri = objType.getURI();
log.debug("Found populated object property " + uri + " with object type " + objType);
ObjectProperty property = null;
if (uri.equals(propRegister.getURI())) {
property = propRegister.clone();
} else {
ObjectProperty newProperty = getObjectPropertyByURI(uri);
if (newProperty != null) {
propRegister = newProperty;
// 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;
}

View file

@ -12,6 +12,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -279,12 +280,12 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri,
String propertyUri,
String objectKey, String domainUri, String rangeUri,
final String objectKey, String domainUri, String rangeUri,
String queryString,
Set<String> constructQueryStrings,
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();
@ -299,22 +300,30 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
if("desc".equalsIgnoreCase( sortDirection ) ){
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()) {
QuerySolution soln = results.nextSolution();
RDFNode node = soln.get(objectKey);
if (node != null && node.isURIResource()) {
list.add(QueryUtils.querySolutionToStringValueMap(soln));
}
}
if(log.isDebugEnabled()) {
long duration = System.currentTimeMillis() - start;
log.debug(duration + " to do list view for " +
propertyUri + " / " + domainUri + " / " + rangeUri);
ResultSetConsumer consumer = new ResultSetConsumer() {
@Override
protected void processQuerySolution(QuerySolution qs) {
RDFNode node = qs.get(objectKey);
if (node != null && node.isURIResource()) {
list.add(QueryUtils.querySolutionToStringValueMap(qs));
}
}
};
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) {
log.error("Error getting object property values for subject " + subjectUri + " and property " + propertyUri, e);
return Collections.emptyList();
@ -322,8 +331,8 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
return list;
}
private ResultSet selectFromRDFService(String queryString, String subjectUri,
String propertyUri, String domainUri, String rangeUri) {
private void selectFromRDFService(String queryString, String subjectUri,
String propertyUri, String domainUri, String rangeUri, ResultSetConsumer consumer) {
String[] part = queryString.split("[Ww][Hh][Ee][Rr][Ee]");
part[1] = part[1].replace("?subject", "<" + subjectUri + ">");
part[1] = part[1].replace("?property", "<" + propertyUri + ">");
@ -335,16 +344,15 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
}
queryString = part[0] + "WHERE" + part[1];
try {
return ResultSetFactory.fromJSON(
rdfService.sparqlSelectQuery(queryString, RDFService.ResultFormat.JSON));
rdfService.sparqlSelectQuery(queryString, consumer);
} catch (RDFServiceException e) {
throw new RuntimeException(e);
}
}
private ResultSet selectFromConstructedModel(String queryString,
String subjectUri, String propertyUri, String domainUri, String rangeUri,
Model constructedModel) {
private void selectFromConstructedModel(String queryString,
String subjectUri, String propertyUri, String domainUri, String rangeUri,
Model constructedModel, ResultSetConsumer consumer) {
Query query = null;
try {
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
@ -374,7 +382,7 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
try {
qexec = QueryExecutionFactory.create(
query, constructedModel, initialBindings);
return new ResultSetMem(qexec.execSelect());
consumer.processResultSet(qexec.execSelect());
} finally {
if (qexec != null) {
qexec.close();
@ -441,25 +449,7 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
w.close();
}
} else {
String parseFormat = "N3";
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);
rdfService.sparqlConstructQuery(queryString, constructedModel);
}
} catch (Exception e) {
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.Set;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -440,43 +441,23 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao {
return classSet;
}
protected ResultSet getPropertyQueryResults(String queryString) {
log.debug("SPARQL query:\n" + queryString);
protected void getPropertyQueryResults(String queryString, ResultSetConsumer consumer) {
log.debug("SPARQL query:\n" + queryString);
// 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
// but succeeds when the subject uri is concatenated into the query string.
//QuerySolutionMap subjectBinding = new QuerySolutionMap();
//subjectBinding.add("subject", ResourceFactory.createResource(subjectUri));
// 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
// but succeeds when the subject uri is concatenated into the query string.
//QuerySolutionMap subjectBinding = new QuerySolutionMap();
//subjectBinding.add("subject", ResourceFactory.createResource(subjectUri));
// Run the SPARQL query to get the properties
// Run the SPARQL query to get the properties
try {
return ResultSetFactory.fromJSON(
getRDFService().sparqlSelectQuery(
queryString, RDFService.ResultFormat.JSON));
} 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;
}
try {
getRDFService().sparqlSelectQuery(queryString, consumer);
} catch (RDFServiceException e) {
throw new RuntimeException(e);
}
}
/**
* 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.RDFServiceException;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import edu.cornell.mannlib.vitro.webapp.utils.logging.ToString;
public class RDFServiceDatasetGraph implements DatasetGraph {
@ -109,7 +110,7 @@ public class RDFServiceDatasetGraph implements DatasetGraph {
}
@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 (contains(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());
ResultSet rs = null;
final List<Quad> quadlist = new ArrayList<Quad>();
try {
rs = JSONInput.fromJSON(rdfService.sparqlSelectQuery(
findQuery.toString(), RDFService.ResultFormat.JSON));
rdfService.sparqlSelectQuery(findQuery.toString(), new ResultSetConsumer() {
@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) {
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");
return WrappedIterator.create(quadlist.iterator()); }

View file

@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -163,8 +164,10 @@ public class RDFServiceGraph implements GraphWithPerform {
containsQuery.append(" } \n");
}
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
@ -232,7 +235,7 @@ public class RDFServiceGraph implements GraphWithPerform {
}
@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 (contains(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();
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()) {
QuerySolution soln = rs.nextSolution();
Triple t = new Triple(isVar(subject) ? soln.get("?s").asNode() : subject,
isVar(predicate) ? soln.get("?p").asNode() : predicate,
isVar(object) ? soln.get("?o").asNode() : object);
isVar(predicate) ? soln.get("?p").asNode() : predicate,
isVar(object) ? soln.get("?o").asNode() : object);
//log.info(t);
triplist.add(t);
}
*/
//log.info(triplist.size() + " results");
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 {
return rdfService.sparqlAskQuery(queryStr);
} catch (RDFServiceException rdfse) {
throw new RuntimeException(rdfse);
}
}
private ResultSet execSelect(String queryStr) {
try {
return JSONInput.fromJSON(rdfService.sparqlSelectQuery(
queryStr, RDFService.ResultFormat.JSON));
rdfService.sparqlSelectQuery(queryStr, consumer);
} catch (RDFServiceException rdfse) {
throw new RuntimeException(rdfse);
}

View file

@ -8,6 +8,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ResultSetConsumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -168,8 +169,9 @@ public abstract class RDFServiceImpl implements RDFService {
}
protected boolean sparqlSelectQueryHasResults(String queryStr) throws RDFServiceException {
ResultSet rs = XMLInput.fromXML(sparqlSelectQuery(queryStr, ResultFormat.XML));
return rs.hasNext();
ResultSetConsumer.HasResult hasResult = new ResultSetConsumer.HasResult();
sparqlSelectQuery(queryStr, hasResult);
return hasResult.hasResult();
}
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 {
List<String> graphURIs = new ArrayList<String>();
final List<String> graphURIs = new ArrayList<String>();
try {
ResultSet rs = ResultSetFactory.fromJSON(
sparqlSelectQuery(queryString, RDFService.ResultFormat.JSON));
while (rs.hasNext()) {
QuerySolution qs = rs.nextSolution();
if (qs != null) { // no idea how this happens, but it seems to
RDFNode n = qs.getResource("g");
if (n != null && n.isResource()) {
graphURIs.add(((Resource) n).getURI());
}
}
}
sparqlSelectQuery(queryString, new ResultSetConsumer() {
@Override
protected void processQuerySolution(QuerySolution qs) {
if (qs != null) { // no idea how this happens, but it seems to
RDFNode n = qs.getResource("g");
if (n != null && n.isResource()) {
graphURIs.add(((Resource) n).getURI());
}
}
}
});
} catch (Exception e) {
throw new RDFServiceException("Unable to list graph URIs", e);
}