VIVO-871 Improve performance when getting VClasses().

This commit is contained in:
Jim Blake 2015-01-16 11:39:56 -05:00
parent 8d37fc0d18
commit 7b7ca268c8
4 changed files with 44 additions and 39 deletions

View file

@ -2,6 +2,8 @@
package edu.cornell.mannlib.vitro.webapp.dao.jena; package edu.cornell.mannlib.vitro.webapp.dao.jena;
import static edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.Collator; import java.text.Collator;
@ -817,8 +819,8 @@ public class IndividualSDB extends IndividualImpl implements Individual {
} }
@Override @Override
public List<VClass> getVClasses(boolean direct) { public List<VClass> getVClasses(boolean assertedOnly) {
if (direct) { if (assertedOnly) {
if (directVClasses != null) { if (directVClasses != null) {
return directVClasses; return directVClasses;
} else { } else {
@ -835,34 +837,19 @@ public class IndividualSDB extends IndividualImpl implements Individual {
} }
} }
private List<VClass> getMyVClasses(boolean direct) { private List<VClass> getMyVClasses(boolean assertedOnly) {
List<VClass> vClassList = new ArrayList<VClass>(); List<VClass> vClassList = new ArrayList<VClass>();
Model tempModel = null; Model tempModel = null;
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 {
String[] graphVars = { "?g" }; String getTypesQuery = buildMyVClassesQuery(assertedOnly);
String getTypes =
"CONSTRUCT{ <" + this.individualURI + "> <" + RDF.type +
"> ?types }\n" +
"WHERE{ { GRAPH ?g"
+ " { <" + this.individualURI +"> <" +RDF.type+ "> ?types } \n"
+ WebappDaoFactorySDB.getFilterBlock(
graphVars, (direct
? WebappDaoFactorySDB.SDBDatasetMode
.ASSERTIONS_ONLY
: datasetMode))
+ "} \n"
// GRAPH-less pattern to support retrieving inferred types
// from the unnamed base graph, as in Sesame and OWLIM
+ ((datasetMode.equals(WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY))
? "" : "UNION { <" + this.individualURI +"> <" +RDF.type+ "> ?types }" )
+ "} \n";
RDFService service = webappDaoFactory.getRDFService(); RDFService service = webappDaoFactory.getRDFService();
try { try {
tempModel = RDFServiceUtils.parseModel( tempModel = RDFServiceUtils.parseModel(
service.sparqlConstructQuery( service.sparqlConstructQuery(
getTypes, RDFService.ModelSerializationFormat.N3), getTypesQuery, RDFService.ModelSerializationFormat.N3),
RDFService.ModelSerializationFormat.N3); RDFService.ModelSerializationFormat.N3);
} catch (RDFServiceException e) { } catch (RDFServiceException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -884,7 +871,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
ArrayList<String> done = new ArrayList<String>(); ArrayList<String> done = new ArrayList<String>();
/* Loop for comparing starts here */ /* Loop for comparing starts here */
if(direct){ if(assertedOnly){
while(!directTypes){ while(!directTypes){
itr = list.listIterator(); itr = list.listIterator();
@ -943,6 +930,36 @@ public class IndividualSDB extends IndividualImpl implements Individual {
return vClassList; return vClassList;
} }
/**
* If we are restricting to asserted types, either by request or by dataset
* mode, then filter by graph and include a UNION clause to support
* retrieving inferred types from the unnamed base graph, as in Sesame and
* OWLIM.
*/
private String buildMyVClassesQuery(boolean assertedOnly) {
SDBDatasetMode queryMode = assertedOnly ? ASSERTIONS_ONLY : datasetMode;
String filterBlock = WebappDaoFactorySDB.getFilterBlock(new String[] { "?g" }, queryMode);
if (filterBlock.isEmpty()) {
return
"CONSTRUCT { <" + this.individualURI + "> " + "<" + RDF.type + "> ?types }\n" +
"WHERE { <" + this.individualURI +"> <" +RDF.type+ "> ?types } \n";
} else {
String unionBlock = (queryMode.equals(ASSERTIONS_ONLY)) ?
"" :
"UNION { <" + this.individualURI +"> <" +RDF.type+ "> ?types }";
return
"CONSTRUCT{ <" + this.individualURI + "> " + "<" + RDF.type + "> ?types }\n" +
"WHERE{ { GRAPH ?g"
+ " { <" + this.individualURI +"> <" +RDF.type+ "> ?types } \n"
+ filterBlock
+ "} \n"
+ unionBlock
+ "} \n";
}
}
/** /**
* The base method in {@link IndividualImpl} is adequate if the reasoner is * The base method in {@link IndividualImpl} is adequate if the reasoner is

View file

@ -129,12 +129,6 @@ public class WebappDaoFactorySDB extends WebappDaoFactoryJena {
.append("&& ").append(graphVars[i]).append(" != <") .append("&& ").append(graphVars[i]).append(" != <")
.append(ModelNames.TBOX_INFERENCES) .append(ModelNames.TBOX_INFERENCES)
.append(">") .append(">")
.append("&& ").append(graphVars[i]).append(" != <")
.append(SimpleReasonerSetup.JENA_INF_MODEL_REBUILD)
.append(">")
.append("&& ").append(graphVars[i]).append(" != <")
.append(SimpleReasonerSetup.JENA_INF_MODEL_SCRATCHPAD)
.append(">")
.append(") ) \n"); .append(") ) \n");
break; break;
case INFERENCES_ONLY : case INFERENCES_ONLY :

View file

@ -12,11 +12,8 @@ import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property; import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property;
/** /**
* Exclude individual from search index if it is a member of any of the the * Exclude individual from search index if it is a member of any of the
* types. * excluding types.
*
* @author bdc34
*
*/ */
public class ExcludeBasedOnType implements SearchIndexExcluder { public class ExcludeBasedOnType implements SearchIndexExcluder {
@ -34,13 +31,10 @@ public class ExcludeBasedOnType implements SearchIndexExcluder {
if (ind == null) { if (ind == null) {
return DONT_EXCLUDE; return DONT_EXCLUDE;
} }
if (typeURIinExcludeList(ind.getVClass())) {
return SKIP_MSG + ind.getVClass();
}
List<VClass> vclasses = new ArrayList<>(); List<VClass> vclasses = new ArrayList<>();
vclasses.add(ind.getVClass());
addToList(vclasses, ind.getVClasses()); addToList(vclasses, ind.getVClasses());
addToList(vclasses, ind.getVClasses(true));
for (VClass vclz : vclasses) { for (VClass vclz : vclasses) {
if (typeURIinExcludeList(vclz)) if (typeURIinExcludeList(vclz))

View file

@ -44,7 +44,7 @@ public class ExcludeBasedOnTypeTest {
vClassList.add( new VClass("http://example.com/Robot")); vClassList.add( new VClass("http://example.com/Robot"));
vClassList.add( new VClass("http://example.com/KillerRobot")); vClassList.add( new VClass("http://example.com/KillerRobot"));
vClassList.add( new VClass("http://example.com/Droid")); vClassList.add( new VClass("http://example.com/Droid"));
ind.setVClasses(vClassList, true); ind.setVClasses(vClassList, false);
String excludeResult = ebot.checkForExclusion(ind); String excludeResult = ebot.checkForExclusion(ind);
assertNotNull( excludeResult ); assertNotNull( excludeResult );
@ -79,7 +79,7 @@ public class ExcludeBasedOnTypeTest {
vClassList.add( new VClass("http://example.com/Robot")); vClassList.add( new VClass("http://example.com/Robot"));
vClassList.add( new VClass("http://example.com/KillerRobot")); vClassList.add( new VClass("http://example.com/KillerRobot"));
vClassList.add( new VClass("http://example.com/Droid")); vClassList.add( new VClass("http://example.com/Droid"));
ind.setVClasses(vClassList, true); ind.setVClasses(vClassList, false);
String excludeResult = ebot.checkForExclusion(ind); String excludeResult = ebot.checkForExclusion(ind);
assertNull( excludeResult ); assertNull( excludeResult );