[VIVO-1031] Use consumer interface for most ResultSets
This commit is contained in:
parent
39b0d2e6bd
commit
34d78a9463
10 changed files with 268 additions and 278 deletions
|
@ -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");
|
||||
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 =
|
||||
|
|
|
@ -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,24 +191,21 @@ 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);
|
||||
} catch (RDFServiceException e) {
|
||||
log.debug(e,e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
ResultSet rs = ResultSetFactory.fromJSON(in);
|
||||
rdfService.sparqlSelectQuery(
|
||||
query, new ResultSetConsumer() {
|
||||
String uri = null;
|
||||
String label = null;
|
||||
while (rs.hasNext()) {
|
||||
QuerySolution sol = rs.nextSolution();
|
||||
Resource currRes = sol.getResource("ind");
|
||||
|
||||
@Override
|
||||
protected void processQuerySolution(QuerySolution qs) {
|
||||
Resource currRes = qs.getResource("ind");
|
||||
if (currRes.isAnon()) {
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
if (uri != null && !uri.equals(currRes.getURI())) {
|
||||
try {
|
||||
ents.add(makeIndividual(uri, label));
|
||||
|
@ -220,11 +217,15 @@ public class IndividualDaoSDB extends IndividualDaoJena {
|
|||
} else if (uri == null) {
|
||||
uri = currRes.getURI();
|
||||
}
|
||||
Literal labelLit = sol.getLiteral("label");
|
||||
Literal labelLit = qs.getLiteral("label");
|
||||
if (labelLit != null) {
|
||||
label = labelLit.getLexicalForm();
|
||||
}
|
||||
if (!rs.hasNext()) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void endProcessing() {
|
||||
if (uri != null) {
|
||||
try {
|
||||
ents.add(makeIndividual(uri, label));
|
||||
} catch (IndividualNotFoundException e) {
|
||||
|
@ -232,6 +233,11 @@ public class IndividualDaoSDB extends IndividualDaoJena {
|
|||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (RDFServiceException e) {
|
||||
log.debug(e,e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} 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,21 +260,13 @@ 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);
|
||||
} 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;
|
||||
}
|
||||
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));
|
||||
|
@ -276,6 +274,12 @@ public class IndividualDaoSDB extends IndividualDaoJena {
|
|||
// don't add
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (RDFServiceException e) {
|
||||
log.debug(e,e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} finally {
|
||||
dataset.getLock().leaveCriticalSection();
|
||||
w.close();
|
||||
|
|
|
@ -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");
|
||||
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,17 +762,18 @@ 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");
|
||||
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);
|
||||
|
@ -783,11 +785,12 @@ public class IndividualSDB extends IndividualImpl implements Individual {
|
|||
stmt.setDatatypeURI(lit.getDatatypeURI());
|
||||
stmt.setLanguage(lit.getLanguage());
|
||||
stmt.setDatapropURI(propertyUri);
|
||||
stmt.setIndividualURI(this.getURI());
|
||||
stmt.setIndividual(this);
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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,19 +1022,21 @@ 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");
|
||||
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 = soln.getResource("objType");
|
||||
Resource objType = qs.getResource("objType");
|
||||
String objTypeUri = objType.getURI();
|
||||
log.debug("Found populated object property " + uri +
|
||||
" with object type " + objType);
|
||||
log.debug("Found populated object property " + uri + " with object type " + objType);
|
||||
ObjectProperty property = null;
|
||||
if (uri.equals(propRegister.getURI())) {
|
||||
property = propRegister.clone();
|
||||
|
@ -1053,6 +1056,8 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
|
|||
properties.add(property);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return properties;
|
||||
}
|
||||
|
||||
|
|
|
@ -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,17 +300,25 @@ 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);
|
||||
ResultSetConsumer consumer = new ResultSetConsumer() {
|
||||
@Override
|
||||
protected void processQuerySolution(QuerySolution qs) {
|
||||
RDFNode node = qs.get(objectKey);
|
||||
if (node != null && node.isURIResource()) {
|
||||
list.add(QueryUtils.querySolutionToStringValueMap(soln));
|
||||
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 " +
|
||||
|
@ -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,
|
||||
private void selectFromConstructedModel(String queryString,
|
||||
String subjectUri, String propertyUri, String domainUri, String rangeUri,
|
||||
Model constructedModel) {
|
||||
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 "
|
||||
|
|
|
@ -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,7 +441,7 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao {
|
|||
return classSet;
|
||||
}
|
||||
|
||||
protected ResultSet getPropertyQueryResults(String 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
|
||||
|
@ -452,30 +453,10 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao {
|
|||
// Run the SPARQL query to get the properties
|
||||
|
||||
try {
|
||||
return ResultSetFactory.fromJSON(
|
||||
getRDFService().sparqlSelectQuery(
|
||||
queryString, RDFService.ResultFormat.JSON));
|
||||
getRDFService().sparqlSelectQuery(queryString, consumer);
|
||||
} 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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()); }
|
||||
|
||||
|
|
|
@ -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,9 +259,21 @@ 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,
|
||||
|
@ -267,6 +282,7 @@ public class RDFServiceGraph implements GraphWithPerform {
|
|||
//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);
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -411,13 +411,11 @@ 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();
|
||||
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()) {
|
||||
|
@ -425,6 +423,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
|||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
throw new RDFServiceException("Unable to list graph URIs", e);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue