NIHVIVO-3956 SPARQL-based implementations of getMostSpecificTypeURIs() getDataValue() and isVClass() to shorten render time of short views

This commit is contained in:
brianjlowe 2012-09-12 20:03:21 +00:00
parent b90d1ebefe
commit 2c83351eb6
6 changed files with 157 additions and 23 deletions

View file

@ -66,7 +66,9 @@ public interface Individual extends ResourceBean, Comparable<Individual> {
void setVClasses(List<VClass> vClassList, boolean direct); void setVClasses(List<VClass> vClassList, boolean direct);
/** Does the individual belong to this class? */ /** Does the individual belong to this class? */
boolean isVClass(String uri); boolean isVClass(String uri);
List<String> getMostSpecificTypeURIs();
void setObjectPropertyStatements(List<ObjectPropertyStatement> list); void setObjectPropertyStatements(List<ObjectPropertyStatement> list);
List<ObjectPropertyStatement> getObjectPropertyStatements(); List<ObjectPropertyStatement> getObjectPropertyStatements();

View file

@ -18,6 +18,7 @@ import java.util.TreeSet;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.filestorage.model.ImageInfo; import edu.cornell.mannlib.vitro.webapp.filestorage.model.ImageInfo;
import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch;
@ -208,6 +209,20 @@ public class IndividualImpl extends BaseResourceBean implements Individual, Comp
this.allVClasses = vClassList; this.allVClasses = vClassList;
} }
} }
@Override
public List<String> getMostSpecificTypeURIs() {
List<String> typeURIs = new ArrayList<String>();
List<ObjectPropertyStatement> stmts = getObjectPropertyStatements(
VitroVocabulary.MOST_SPECIFIC_TYPE);
for (ObjectPropertyStatement stmt : stmts) {
String objURI = stmt.getObjectURI();
if (objURI != null) {
typeURIs.add(objURI);
}
}
return typeURIs;
}
public void setObjectPropertyStatements(List<ObjectPropertyStatement> list) { public void setObjectPropertyStatements(List<ObjectPropertyStatement> list) {
objectPropertyStatements = list; objectPropertyStatements = list;

View file

@ -369,6 +369,11 @@ public class IndividualFiltering implements Individual {
return _innerIndividual.isVClass(uri); return _innerIndividual.isVClass(uri);
} }
@Override
public List<String> getMostSpecificTypeURIs() {
return _innerIndividual.getMostSpecificTypeURIs();
}
@Override @Override
public void setDataPropertyMap(Map<String, DataProperty> propertyMap) { public void setDataPropertyMap(Map<String, DataProperty> propertyMap) {
_innerIndividual.setDataPropertyMap(propertyMap); _innerIndividual.setDataPropertyMap(propertyMap);

View file

@ -2,6 +2,7 @@
package edu.cornell.mannlib.vitro.webapp.dao.jena; package edu.cornell.mannlib.vitro.webapp.dao.jena;
import java.io.InputStream;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.Collator; import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
@ -28,6 +29,7 @@ import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution; 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.query.ResultSetFactory;
import com.hp.hpl.jena.query.Syntax; import com.hp.hpl.jena.query.Syntax;
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.Model;
@ -43,6 +45,7 @@ import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement; import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatementImpl;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl; import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
@ -269,6 +272,32 @@ public class IndividualSDB extends IndividualImpl implements Individual {
return (clist.size() > 0) ? clist.get(0) : null ; return (clist.size() > 0) ? clist.get(0) : null ;
} }
} }
@Override
public List<String> getMostSpecificTypeURIs() {
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());
}
}
return typeURIs;
} catch (RDFServiceException e) {
throw new RuntimeException(e);
}
}
}
public Timestamp getModTime() { public Timestamp getModTime() {
if (modTime != null) { if (modTime != null) {
@ -676,7 +705,98 @@ public class IndividualSDB extends IndividualImpl implements Individual {
return map; return map;
} }
} }
@Override
public List<DataPropertyStatement> getDataPropertyStatements(String propertyUri) {
List<DataPropertyStatement> stmts = this.dataPropertyStatements;
if (stmts == null) {
return sparqlForDataPropertyStatements(propertyUri);
} else {
List<DataPropertyStatement> stmtsForProp = new ArrayList<DataPropertyStatement>();
for (DataPropertyStatement stmt : stmts) {
if (stmt.getDatapropURI().equals(propertyUri)) {
stmtsForProp.add(stmt);
}
}
return stmtsForProp;
}
}
@Override
public String getDataValue(String propertyUri) {
if (propertyUri == null) {
log.error("Cannot retrieve value for null property");
return null;
} else if (this.getURI() == null) {
log.error("Cannot retrieve value of property " + propertyUri +
" for anonymous individual");
return null;
} else {
List<DataPropertyStatement> stmts = sparqlForDataPropertyStatements(
propertyUri);
if (stmts != null && stmts.size() > 0) {
return stmts.get(0).getData();
}
}
return null; // not found
}
@Override
public List<String> getDataValues(String propertyUri) {
List<String> values = new ArrayList<String>();
if (propertyUri == null) {
log.error("Cannot retrieve value for null property");
return null;
} else if (this.getURI() == null) {
log.error("Cannot retrieve value of property " + propertyUri +
" for anonymous individual");
return null;
} else {
List<DataPropertyStatement> stmts = sparqlForDataPropertyStatements(
propertyUri);
if (stmts != null) {
for (DataPropertyStatement stmt : stmts) {
values.add(stmt.getData());
}
}
return values;
}
}
private List<DataPropertyStatement> sparqlForDataPropertyStatements(String propertyUri) {
List<DataPropertyStatement> stmts = new ArrayList<DataPropertyStatement>();
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();
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);
}
}
} catch (RDFServiceException e) {
log.error(e,e);
throw new RuntimeException(e);
}
return stmts;
}
public List<DataPropertyStatement> getExternalIds() { public List<DataPropertyStatement> getExternalIds() {
if (this.externalIds != null) { if (this.externalIds != null) {
return this.externalIds; return this.externalIds;
@ -830,25 +950,16 @@ public class IndividualSDB extends IndividualImpl implements Individual {
* any of the super classes of the direct classes will satisfy this request. * any of the super classes of the direct classes will satisfy this request.
*/ */
@Override @Override
public boolean isVClass(String uri) { public boolean isVClass(String uri) {
if (uri == null) { if (uri == null || this.getURI() == null) {
return false; return false;
} }
String queryString = "ASK { <" + this.getURI() + "> a <" + uri + "> }";
if (super.isVClass(uri)) { try {
return true; return webappDaoFactory.getRDFService().sparqlAskQuery(queryString);
} } catch (RDFServiceException e) {
throw new RuntimeException(e);
VClassDao vclassDao = webappDaoFactory.getVClassDao(); }
for (VClass vClass : getVClasses(true)) {
for (String superClassUri: vclassDao.getAllSuperClassURIs(
vClass.getURI())) {
if (uri.equals(superClassUri)) {
return true;
}
}
}
return false;
} }
/** /**

View file

@ -102,11 +102,7 @@ public class ShortViewServiceImpl implements ShortViewService {
/** Get most specific classes from Individual, sorted by alpha. */ /** Get most specific classes from Individual, sorted by alpha. */
private SortedSet<String> figureMostSpecificClassUris(Individual individual) { private SortedSet<String> figureMostSpecificClassUris(Individual individual) {
SortedSet<String> classUris = new TreeSet<String>(); SortedSet<String> classUris = new TreeSet<String>();
List<ObjectPropertyStatement> stmts = individual classUris.addAll(individual.getMostSpecificTypeURIs());
.getObjectPropertyStatements(VitroVocabulary.MOST_SPECIFIC_TYPE);
for (ObjectPropertyStatement stmt : stmts) {
classUris.add(stmt.getObjectURI());
}
return classUris; return classUris;
} }

View file

@ -259,6 +259,11 @@ public class IndividualStub implements Individual {
"Comparable<Individual>.compareTo() not implemented."); "Comparable<Individual>.compareTo() not implemented.");
} }
@Override
public List<String> getMostSpecificTypeURIs() {
throw new RuntimeException("Individual.getMostSpecificTypeURIs() not implemented.");
}
@Override @Override
public String getRdfsLabel() { public String getRdfsLabel() {
throw new RuntimeException("Individual.getRdfsLabel() not implemented."); throw new RuntimeException("Individual.getRdfsLabel() not implemented.");