code to run two-stage CONSTRUCTs and SELECTs for list views

This commit is contained in:
bjl23 2011-01-31 18:24:00 +00:00
parent aa1ad284bf
commit bca2dbefcc
7 changed files with 109 additions and 15 deletions

View file

@ -4,6 +4,7 @@ package edu.cornell.mannlib.vitro.webapp.dao;
import java.util.List;
import java.util.Map;
import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
@ -31,4 +32,6 @@ public interface ObjectPropertyStatementDao {
int insertNewObjectPropertyStatement(ObjectPropertyStatement objPropertyStmt );
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(String subjectUri, String propertyUri, String query);
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(String subjectUri, String propertyUri, String query, Set<String> constructQueries);
}

View file

@ -5,6 +5,7 @@ package edu.cornell.mannlib.vitro.webapp.dao.filtering;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.jga.algorithms.Filter;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
@ -88,6 +89,14 @@ class ObjectPropertyStatementDaoFiltering extends BaseFiltering implements Objec
String subjectUri, String propertyUri, String query) {
return innerObjectPropertyStatementDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, query);
}
@Override
// RY What about filtering?
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri, String propertyUri, String query, Set<String> queryStrings) {
return innerObjectPropertyStatementDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, query, queryStrings);
}
// @Override

View file

@ -7,6 +7,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -21,6 +22,8 @@ import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
@ -247,6 +250,17 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
return 0;
}
@Override
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri,
String propertyUri,
String queryString) {
return getObjectPropertyStatementsForIndividualByProperty(
subjectUri, propertyUri, null);
}
@Override
/*
* SPARQL-based method for getting values related to a single object property.
@ -255,7 +269,14 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
* custom queries that could request any data in addition to just the object of the statement.
* However, we do need to get the object of the statement so that we have it to create editing links.
*/
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(String subjectUri, String propertyUri, String queryString) {
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri,
String propertyUri,
String queryString,
Set<String> constructQueryStrings ) {
Model constructedModel = constructModelForSelectQueries(
subjectUri, propertyUri, constructQueryStrings);
log.debug("Query string for object property " + propertyUri + ": " + queryString);
@ -267,7 +288,7 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
log.error(queryString);
return Collections.emptyList();
}
// RY One oddity here is that SDB adds the bound variables to the query select terms,
// even if they're not included in the query.
QuerySolutionMap initialBindings = new QuerySolutionMap();
@ -281,8 +302,13 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
dataset.getLock().enterCriticalSection(Lock.READ);
try {
QueryExecution qexec = QueryExecutionFactory.create(
query, dataset, initialBindings);
QueryExecution qexec = (constructedModel == null)
? QueryExecutionFactory.create(
query, dataset, initialBindings)
: QueryExecutionFactory.create(
query, constructedModel, initialBindings);
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
@ -297,4 +323,59 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
return list;
}
private Model constructModelForSelectQueries(String subjectUri,
String propertyUri,
Set<String> constructQueries) {
if (constructQueries == null) {
return null;
}
Model constructedModel = ModelFactory.createDefaultModel();
for (String queryString : constructQueries) {
log.debug("CONSTRUCT query string for object property " +
propertyUri + ": " + queryString);
Query query = null;
try {
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
} catch(Throwable th){
log.error("Could not create CONSTRUCT SPARQL query for query " +
"string. " + th.getMessage());
log.error(queryString);
return constructedModel;
}
QuerySolutionMap initialBindings = new QuerySolutionMap();
initialBindings.add(
"subject", ResourceFactory.createResource(subjectUri));
initialBindings.add(
"property", ResourceFactory.createResource(propertyUri));
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
QueryExecution qe = QueryExecutionFactory.create(
query, dataset, initialBindings);
try {
qe.execConstruct(constructedModel);
} finally {
qe.close();
}
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
}
return constructedModel;
}
}

View file

@ -53,15 +53,16 @@ public class ObjectPropertyStatementDaoSDB extends
String[] graphVars = { "?g", "?h", "?i", "?j" };
String query = "CONSTRUCT { \n" +
" <" + entity.getURI() + "> ?p ?o . \n" +
" ?o a ?oType . \n" +
// " ?o a ?oType . \n" +
" ?o <" + RDFS.label.getURI() + "> ?oLabel . \n" +
" ?o <" + VitroVocabulary.MONIKER + "> ?oMoniker \n" +
"} WHERE { GRAPH ?g { \n" +
" <" + entity.getURI() + "> ?p ?o \n" +
" OPTIONAL { GRAPH ?h { ?o a ?oType } } \n" +
" OPTIONAL { GRAPH ?i { ?o <" + RDFS.label.getURI() + "> ?oLabel } } \n" +
" OPTIONAL { GRAPH ?j { ?o <" + VitroVocabulary.MONIKER + "> ?oMoniker } } \n" +
"} \n" +
"} WHERE { \n" +
" { <" + entity.getURI() + "> ?p ?o } \n" +
// " UNION { <" + entity.getURI() + "> ?p ?o . ?o a ?oType } \n" +
" UNION { <" + entity.getURI() + "> ?p ?o . \n" +
" ?o <" + RDFS.label.getURI() + "> ?oLabel } \n" +
" UNION { <" + entity.getURI() + "> ?p ?o . \n " +
" ?o <" + VitroVocabulary.MONIKER + "> ?oMoniker } \n" +
WebappDaoFactorySDB.getFilterBlock(graphVars, datasetMode) +
"}";
long startTime = System.currentTimeMillis();

View file

@ -214,7 +214,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
inferenceOms.getTBoxModel().addSubModel(vitroTBoxModel);
unionOms.getTBoxModel().addSubModel(vitroTBoxModel);
log.error("Setting up union models and DAO factories");
log.info("Setting up union models and DAO factories");
// create TBox + ABox union models and set up webapp DAO factories
OntModel baseUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
@ -278,7 +278,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
ensureEssentialInterfaceData(unionOms.getApplicationMetadataModel(), sce, wadf);
//log.info("Setting up namespace mapper");
log.info("Setting up namespace mapper");
NamespaceMapper namespaceMapper = new NamespaceMapperJena(masterUnion, masterUnion, defaultNamespace);
sce.getServletContext().setAttribute("NamespaceMapper", namespaceMapper);

View file

@ -56,7 +56,7 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
String subjectUri = subject.getURI();
String propertyUri = op.getURI();
List<Map<String, String>> statementData =
opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getSelectQuery());
opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getSelectQuery(), getConstructQueries());
/* Apply post-processing */
postprocess(statementData, wdf);

View file

@ -32,7 +32,7 @@ public class UncollatedObjectPropertyTemplateModel extends ObjectPropertyTemplat
String subjectUri = subject.getURI();
String propertyUri = op.getURI();
List<Map<String, String>> statementData =
opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getSelectQuery());
opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getSelectQuery(), getConstructQueries());
/* Apply postprocessing */
postprocess(statementData, wdf);