NIHVIVO-2617 restored assertions-only filtering for individual lists

This commit is contained in:
brianjlowe 2011-06-20 18:25:50 +00:00
parent f49d3af301
commit 1edae6b651

View file

@ -10,6 +10,7 @@ import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
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;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -26,18 +27,14 @@ import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.AnonId; import com.hp.hpl.jena.rdf.model.AnonId;
import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.shared.Lock; import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.util.iterator.ClosableIterator; import com.hp.hpl.jena.util.iterator.ClosableIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS; import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
@ -105,66 +102,41 @@ public class IndividualDaoSDB extends IndividualDaoJena {
ents.addAll(getIndividualsByVClass(vc)); ents.addAll(getIndividualsByVClass(vc));
} }
} else { } else {
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset(); List<Individual> individualList;
dataset.getLock().enterCriticalSection(Lock.READ);
try { // Check if there is a graph filter.
String[] graphVars = {"?g", "?h", "?i"}; // If so, we will use it in a slightly strange way. Unfortunately,
String query = // performance is quite bad if we add several graph variables in
"SELECT DISTINCT ?ind ?label ?moniker " + // order to account for the fact that an individual's type
"WHERE " + // declaration may be in a different graph from its label or
"{ \n" + // moniker. Thus, we will run two queries: one with a single
"{ ?ind a <" + theClass.getURI() + "> } \n" + // graph variable to get the list of URIs, and a second against
"UNION { \n" + // the union graph to get individuals with their labels and
" ?ind a <" + theClass.getURI() + "> . \n" + // monikers. We will then toss out any individual in the second
" ?ind <" + RDFS.label.getURI() + "> ?label \n" + // list that is not also in the first list.
"} \n" + // Annoying, yes, but better than the alternative.
"UNION { \n" + // Note that both queries need to sort identically or
" ?ind a <" + theClass.getURI() + "> . \n" + // the results may be very strange.
" ?ind <" + VitroVocabulary.MONIKER + "> ?moniker \n" + String[] graphVars = {"?g"};
"} \n" + String filterStr = WebappDaoFactorySDB.getFilterBlock(
"} ORDER BY ?ind ?label"; graphVars, datasetMode);
ResultSet rs =QueryExecutionFactory.create( if (!StringUtils.isEmpty(filterStr)) {
QueryFactory.create(query), dataset) List<Individual> graphFilteredIndividualList =
.execSelect(); getGraphFilteredIndividualList(theClass, filterStr);
String uri = null; List<Individual> unfilteredIndividualList = getIndividualList(
String label = null; theClass);
String moniker = null; Iterator<Individual> unfilteredIt = unfilteredIndividualList
while (rs.hasNext()) { .iterator();
QuerySolution sol = rs.nextSolution(); for (Individual filt : graphFilteredIndividualList) {
Resource currRes = sol.getResource("ind"); Individual unfilt = unfilteredIt.next();
if (currRes.isAnon()) { while (!unfilt.getURI().equals(filt.getURI())) {
continue; unfilt = unfilteredIt.next();
} }
if (uri != null && !uri.equals(currRes.getURI())) { ents.add(unfilt);
Individual ent = makeIndividual(uri, label, moniker);
if (ent != null) {
ents.add(ent);
}
uri = currRes.getURI();
label = null;
moniker = null;
} else if (uri == null) {
uri = currRes.getURI();
}
Literal labelLit = sol.getLiteral("label");
if (labelLit != null) {
label = labelLit.getLexicalForm();
}
Literal monikerLit = sol.getLiteral("moniker");
if (monikerLit != null) {
moniker = monikerLit.getLexicalForm();
}
if (!rs.hasNext()) {
Individual ent = makeIndividual(uri, label, moniker);
if (ent != null) {
ents.add(ent);
}
}
} }
} finally { } else {
dataset.getLock().leaveCriticalSection(); ents = getIndividualList(theClass);
w.close();
} }
} }
@ -182,6 +154,105 @@ public class IndividualDaoSDB extends IndividualDaoJena {
} }
private List<Individual> getIndividualList(Resource theClass) {
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 ?moniker " +
"WHERE " +
"{ \n" +
"{ ?ind a <" + theClass.getURI() + "> } \n" +
"UNION { \n" +
" ?ind a <" + theClass.getURI() + "> . \n" +
" ?ind <" + RDFS.label.getURI() + "> ?label \n" +
"} \n" +
"UNION { \n" +
" ?ind a <" + theClass.getURI() + "> . \n" +
" ?ind <" + VitroVocabulary.MONIKER + "> ?moniker \n" +
"} \n" +
"} ORDER BY ?ind ?label";
ResultSet rs =QueryExecutionFactory.create(
QueryFactory.create(query), dataset)
.execSelect();
String uri = null;
String label = null;
String moniker = null;
while (rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
Resource currRes = sol.getResource("ind");
if (currRes.isAnon()) {
continue;
}
if (uri != null && !uri.equals(currRes.getURI())) {
Individual ent = makeIndividual(uri, label, moniker);
if (ent != null) {
ents.add(ent);
}
uri = currRes.getURI();
label = null;
moniker = null;
} else if (uri == null) {
uri = currRes.getURI();
}
Literal labelLit = sol.getLiteral("label");
if (labelLit != null) {
label = labelLit.getLexicalForm();
}
Literal monikerLit = sol.getLiteral("moniker");
if (monikerLit != null) {
moniker = monikerLit.getLexicalForm();
}
if (!rs.hasNext()) {
Individual ent = makeIndividual(uri, label, moniker);
if (ent != null) {
ents.add(ent);
}
}
}
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
return ents;
}
private List<Individual> getGraphFilteredIndividualList(Resource theClass,
String filterStr) {
List<Individual> filteredIndividualList = new ArrayList<Individual>();
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
String query =
"SELECT DISTINCT ?ind ?label ?moniker " +
"WHERE " +
"{ GRAPH ?g { \n" +
"{ ?ind a <" + theClass.getURI() + "> } \n" +
" } \n" + filterStr +
"} ORDER BY ?ind";
ResultSet rs =QueryExecutionFactory.create(
QueryFactory.create(query), dataset)
.execSelect();
while (rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
Resource currRes = sol.getResource("ind");
if (currRes.isAnon()) {
continue;
}
filteredIndividualList.add(
makeIndividual(currRes.getURI(), null, null));
}
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
return filteredIndividualList;
}
private Individual makeIndividual(String uri, String label, String moniker) { private Individual makeIndividual(String uri, String label, String moniker) {
Individual ent = new IndividualSDB(uri, Individual ent = new IndividualSDB(uri,
this.dwf, datasetMode, getWebappDaoFactory(), this.dwf, datasetMode, getWebappDaoFactory(),