NIHVIVO-1570 changes to support SPARQL queries with GRAPH filtering in non-SDB Jena implementation

This commit is contained in:
bjl23 2010-12-22 20:11:01 +00:00
parent 546b214a86
commit 4848117fc9
12 changed files with 168 additions and 71 deletions

View file

@ -64,11 +64,10 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
protected static final String dataPropertyQueryString =
PREFIXES + "\n" +
"SELECT DISTINCT ?property WHERE { \n" +
//" GRAPH ?g {\n" +
" ?subject ?property ?object . \n" +
" ?property rdf:type owl:DatatypeProperty . \n" +
" GRAPH ?g { ?subject ?property ?object } \n" +
" GRAPH ?h { ?property rdf:type owl:DatatypeProperty } \n" +
propertyFilters +
//" }\n" +
" }\n" +
"}";
static protected Query dataPropertyQuery;
@ -93,8 +92,9 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
}
}
public DataPropertyDaoJena(WebappDaoFactoryJena wadf) {
super(wadf);
public DataPropertyDaoJena(DatasetWrapperFactory dwf,
WebappDaoFactoryJena wadf) {
super(dwf, wadf);
}
public void deleteDataProperty(DataProperty dtp) {

View file

@ -10,6 +10,7 @@ import com.hp.hpl.jena.datatypes.TypeMapper;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
@ -39,11 +40,13 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualUpdateEvent;
public class DataPropertyStatementDaoJena extends JenaBaseDao implements DataPropertyStatementDao
{
private DatasetWrapperFactory dwf;
protected static final String dataPropertyValueQueryString =
"SELECT ?value WHERE { \n" +
//" GRAPH ?g {\n" +
" GRAPH ?g {\n" +
" ?subject ?property ?value . \n" +
//" }\n" +
" }\n" +
"}";
static protected Query dataPropertyValueQuery;
@ -56,8 +59,10 @@ public class DataPropertyStatementDaoJena extends JenaBaseDao implements DataPro
}
}
public DataPropertyStatementDaoJena(WebappDaoFactoryJena wadf) {
public DataPropertyStatementDaoJena(DatasetWrapperFactory dwf,
WebappDaoFactoryJena wadf) {
super(wadf);
this.dwf = dwf;
}
public void deleteDataPropertyStatement(DataPropertyStatement dataPropertyStmt) {
@ -295,16 +300,27 @@ public class DataPropertyStatementDaoJena extends JenaBaseDao implements DataPro
bindings.add("subject", ResourceFactory.createResource(subjectUri));
bindings.add("property", ResourceFactory.createResource(propertyUri));
// Run the SPARQL query to get the properties
QueryExecution qexec = QueryExecutionFactory.create(dataPropertyValueQuery, getOntModelSelector().getFullModel(), bindings);
ResultSet results = qexec.execSelect();
List<DataPropertyStatement> statements = new ArrayList<DataPropertyStatement>();
while (results.hasNext()) {
QuerySolution sol = results.next();
Literal value = sol.getLiteral("value");
DataPropertyStatement dps = new DataPropertyStatementImpl(subjectUri, propertyUri, value.getLexicalForm());
statements.add(dps);
// Run the SPARQL query to get the properties
List<DataPropertyStatement> statements =
new ArrayList<DataPropertyStatement>();
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
QueryExecution qexec = QueryExecutionFactory.create(
dataPropertyValueQuery, dataset, bindings);
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
QuerySolution sol = results.next();
Literal value = sol.getLiteral("value");
DataPropertyStatement dps = new DataPropertyStatementImpl(
subjectUri, propertyUri, value.getLexicalForm());
statements.add(dps);
}
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
return statements;
}

View file

@ -38,7 +38,7 @@ public class DataPropertyStatementDaoSDB extends DataPropertyStatementDaoJena
private DatasetWrapperFactory dwf;
public DataPropertyStatementDaoSDB(DatasetWrapperFactory datasetWrapperFactory, WebappDaoFactoryJena wadf) {
super (wadf);
super (datasetWrapperFactory, wadf);
this.dwf = datasetWrapperFactory;
}

View file

@ -55,11 +55,10 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
protected static final String objectPropertyQueryString =
PREFIXES + "\n" +
"SELECT DISTINCT ?property WHERE { \n" +
//" GRAPH ?g {\n" +
" ?subject ?property ?object . \n" +
" ?property rdf:type owl:ObjectProperty . \n" +
" GRAPH ?g { ?subject ?property ?object } \n" +
" GRAPH ?h { ?property rdf:type owl:ObjectProperty } \n" +
propertyFilters +
//" }\n" +
" }\n" +
"}";
static protected Query objectPropertyQuery;
@ -90,8 +89,9 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
Map<ObjectProperty, String> customListViewConfigFileMap = null;
public ObjectPropertyDaoJena(WebappDaoFactoryJena wadf) {
super(wadf);
public ObjectPropertyDaoJena(DatasetWrapperFactory dwf,
WebappDaoFactoryJena wadf) {
super(dwf, wadf);
}
@Override

View file

@ -11,6 +11,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
@ -38,8 +39,12 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
protected static final Log log = LogFactory.getLog(ObjectPropertyStatementDaoJena.class);
public ObjectPropertyStatementDaoJena(WebappDaoFactoryJena wadf) {
private DatasetWrapperFactory dwf;
public ObjectPropertyStatementDaoJena(DatasetWrapperFactory dwf,
WebappDaoFactoryJena wadf) {
super(wadf);
this.dwf = dwf;
}
@Override
@ -72,7 +77,7 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
else {
Map<String, ObjectProperty> uriToObjectProperty = new HashMap<String,ObjectProperty>();
ObjectPropertyDaoJena opDaoJena = new ObjectPropertyDaoJena(getWebappDaoFactory());
ObjectPropertyDaoJena opDaoJena = new ObjectPropertyDaoJena(dwf, getWebappDaoFactory());
OntModel ontModel = getOntModelSelector().getABoxModel();
ontModel.enterCriticalSection(Lock.READ);
@ -265,14 +270,25 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
bindings.add("subject", ResourceFactory.createResource(subjectUri));
bindings.add("property", ResourceFactory.createResource(propertyUri));
// Run the SPARQL query to get the properties
QueryExecution qexec = QueryExecutionFactory.create(query, getOntModelSelector().getFullModel(), bindings);
ResultSet results = qexec.execSelect();
// Run the SPARQL query to get the properties
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
while (results.hasNext()) {
QuerySolution soln = results.nextSolution();
list.add(QueryUtils.querySolutionToStringValueMap(soln));
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
QueryExecution qexec = QueryExecutionFactory.create(
query, dataset, bindings);
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
QuerySolution soln = results.nextSolution();
list.add(QueryUtils.querySolutionToStringValueMap(soln));
}
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
return list;
}

View file

@ -34,10 +34,10 @@ public class ObjectPropertyStatementDaoSDB extends
private DatasetWrapperFactory dwf;
public ObjectPropertyStatementDaoSDB(
DatasetWrapperFactory datasetWrapperFactory,
DatasetWrapperFactory dwf,
WebappDaoFactoryJena wadf) {
super (wadf);
this.dwf = datasetWrapperFactory;
super (dwf, wadf);
this.dwf = dwf;
}
@Override

View file

@ -15,6 +15,7 @@ import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
@ -69,8 +70,12 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao {
}
}
public PropertyDaoJena(WebappDaoFactoryJena wadf) {
private DatasetWrapperFactory dwf;
public PropertyDaoJena(DatasetWrapperFactory dwf,
WebappDaoFactoryJena wadf) {
super(wadf);
this.dwf = dwf;
}
@Override
@ -410,10 +415,21 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao {
log.debug("SPARQL query:\n" + query.toString());
// Bind the subject's uri to the ?subject query term
QuerySolutionMap subjectBinding = new QuerySolutionMap();
subjectBinding.add("subject", ResourceFactory.createResource(subjectUri));
subjectBinding.add("subject",
ResourceFactory.createResource(subjectUri));
// Run the SPARQL query to get the properties
QueryExecution qexec = QueryExecutionFactory.create(query, getOntModelSelector().getFullModel(), subjectBinding);
return qexec.execSelect();
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
QueryExecution qexec = QueryExecutionFactory.create(
query, dataset, subjectBinding);
return qexec.execSelect();
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
}
}

View file

@ -0,0 +1,17 @@
package edu.cornell.mannlib.vitro.webapp.dao.jena;
import com.hp.hpl.jena.query.Dataset;
public class StaticDatasetFactory implements DatasetWrapperFactory {
private Dataset _dataset;
public StaticDatasetFactory (Dataset dataset) {
_dataset = dataset;
}
public DatasetWrapper getDatasetWrapper() {
return new DatasetWrapper(_dataset);
}
}

View file

@ -16,6 +16,9 @@ import com.hp.hpl.jena.iri.Violation;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.query.DataSource;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
@ -59,6 +62,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
public class WebappDaoFactoryJena implements WebappDaoFactory {
@ -97,7 +101,9 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
protected boolean INCLUDE_TOP_CONCEPT = false;
protected boolean INCLUDE_BOTTOM_CONCEPT = false;
private Map<String,String> properties = new HashMap<String,String>();
private Map<String,String> properties = new HashMap<String,String>();
private DatasetWrapperFactory dwf;
// for temporary use to construct URIs for the things that still use integer IDs.
// As these objects get changed to support getURI(), this should become unnecessary.
@ -114,7 +120,12 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
this.flag2ClassLabelMap = base.flag2ClassLabelMap;
}
public WebappDaoFactoryJena(OntModelSelector ontModelSelector, String defaultNamespace, HashSet<String> nonuserNamespaces, String[] preferredLanguages, String userURI){
public WebappDaoFactoryJena(OntModelSelector ontModelSelector,
OntModelSelector inferenceOntModelSelector,
String defaultNamespace,
HashSet<String> nonuserNamespaces,
String[] preferredLanguages,
String userURI){
this.ontModelSelector = ontModelSelector;
@ -162,14 +173,43 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
if (languageUniversalsModel.size()>0) {
this.ontModelSelector.getTBoxModel().addSubModel(languageUniversalsModel);
}
DataSource dataset = DatasetFactory.create();
dataset.addNamedModel(JenaDataSourceSetupBase.JENA_DB_MODEL,
ontModelSelector.getFullModel());
if (inferenceOntModelSelector != null) {
dataset.addNamedModel(JenaDataSourceSetupBase.JENA_INF_MODEL,
inferenceOntModelSelector.getFullModel());
}
this.dwf = new StaticDatasetFactory(dataset);
}
public WebappDaoFactoryJena(OntModelSelector ontModelSelector,
String defaultNamespace,
HashSet<String> nonuserNamespaces,
String[] preferredLanguages,
String userURI){
this(ontModelSelector,
null,
defaultNamespace,
nonuserNamespaces,
preferredLanguages,
userURI);
}
public WebappDaoFactoryJena(OntModelSelector ontModelSelector, String defaultNamespace, HashSet<String> nonuserNamespaces, String[] preferredLanguages){
this(ontModelSelector, defaultNamespace, nonuserNamespaces, preferredLanguages, null);
}
public WebappDaoFactoryJena(OntModelSelector ontModelSelector, OntModelSelector inferenceOntModelSelector, String defaultNamespace, HashSet<String> nonuserNamespaces, String[] preferredLanguages){
this(ontModelSelector, inferenceOntModelSelector, defaultNamespace, nonuserNamespaces, preferredLanguages, null);
}
public WebappDaoFactoryJena(OntModelSelector ontModelSelector) {
this(ontModelSelector, null, null, null, null);
this(ontModelSelector, null, null, null, null, null);
}
public WebappDaoFactoryJena(OntModel ontModel, String defaultNamespace, HashSet<String> nonuserNamespaces, String[] preferredLanguages, String userURI){
@ -181,7 +221,7 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
}
public WebappDaoFactoryJena(OntModel ontModel) {
this(new SimpleOntModelSelector(ontModel), null, null, null, null);
this(new SimpleOntModelSelector(ontModel), null, null, null, null, null);
}
public OntModelSelector getOntModelSelector() {
@ -493,7 +533,8 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
DataPropertyStatementDao dataPropertyStatementDao = null;
public DataPropertyStatementDao getDataPropertyStatementDao() {
if( dataPropertyStatementDao == null )
dataPropertyStatementDao = new DataPropertyStatementDaoJena(this);
dataPropertyStatementDao = new DataPropertyStatementDaoJena(
dwf, this);
return dataPropertyStatementDao;
}
@ -507,7 +548,7 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
DataPropertyDao dataPropertyDao = null;
public DataPropertyDao getDataPropertyDao() {
if( dataPropertyDao == null )
dataPropertyDao = new DataPropertyDaoJena(this);
dataPropertyDao = new DataPropertyDaoJena(dwf, this);
return dataPropertyDao;
}
@ -528,7 +569,8 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
ObjectPropertyStatementDao objectPropertyStatementDao = null;
public ObjectPropertyStatementDao getObjectPropertyStatementDao() {
if( objectPropertyStatementDao == null )
objectPropertyStatementDao = new ObjectPropertyStatementDaoJena(this);
objectPropertyStatementDao = new ObjectPropertyStatementDaoJena(
dwf, this);
return objectPropertyStatementDao;
}
@ -542,7 +584,7 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
private ObjectPropertyDao objectPropertyDao = null;
public ObjectPropertyDao getObjectPropertyDao() {
if( objectPropertyDao == null )
objectPropertyDao = new ObjectPropertyDaoJena(this);
objectPropertyDao = new ObjectPropertyDaoJena(dwf, this);
return objectPropertyDao;
}
@ -595,4 +637,5 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
public DisplayModelDao getDisplayModelDao(){
return new DisplayModelDaoJena( this );
}
}

View file

@ -97,20 +97,6 @@ public class WebappDaoFactorySDB extends WebappDaoFactoryJena {
return vClassDao = new VClassDaoSDB(dwf, this);
}
private class StaticDatasetFactory implements DatasetWrapperFactory {
private Dataset _dataset;
public StaticDatasetFactory (Dataset dataset) {
_dataset = dataset;
}
public DatasetWrapper getDatasetWrapper() {
return new DatasetWrapper(_dataset);
}
}
private class ReconnectingDatasetFactory implements DatasetWrapperFactory {
private BasicDataSource _bds;

View file

@ -87,17 +87,20 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
checkForNamespaceMismatch( memModel, defaultNamespace );
sce.getServletContext().setAttribute("baseOntModel", memModel);
WebappDaoFactory baseWadf = new WebappDaoFactoryJena(baseOms, defaultNamespace, null, null);
WebappDaoFactory baseWadf = new WebappDaoFactoryJena(
baseOms, defaultNamespace, null, null);
sce.getServletContext().setAttribute("assertionsWebappDaoFactory",baseWadf);
sce.getServletContext().setAttribute("baseOntModelSelector", baseOms);
sce.getServletContext().setAttribute("inferenceOntModel", inferenceModel);
WebappDaoFactory infWadf = new WebappDaoFactoryJena(inferenceOms, defaultNamespace, null, null);
WebappDaoFactory infWadf = new WebappDaoFactoryJena(
inferenceOms, defaultNamespace, null, null);
sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf);
sce.getServletContext().setAttribute("inferenceOntModelSelector", inferenceOms);
sce.getServletContext().setAttribute("jenaOntModel", unionModel);
WebappDaoFactory wadf = new WebappDaoFactoryJena(unionOms, defaultNamespace, null, null);
WebappDaoFactory wadf = new WebappDaoFactoryJena(
baseOms, inferenceOms, defaultNamespace, null, null);
sce.getServletContext().setAttribute("webappDaoFactory",wadf);
sce.getServletContext().setAttribute("unionOntModelSelector", unionOms);

View file

@ -23,9 +23,9 @@
PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
SELECT ?object ?name ?moniker {
?subject ?property ?object .
OPTIONAL { ?object rdfs:label ?name . }
OPTIONAL { ?object vitro:moniker ?moniker . }
GRAPH ?g1 { ?subject ?property ?object }
OPTIONAL { GRAPH ?g2 { ?object rdfs:label ?name } }
OPTIONAL { GRAPH ?g3 { ?object vitro:moniker ?moniker } }
}
</query>
@ -34,4 +34,4 @@
<postprocessor>edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DefaultObjectPropertyDataPostprocessor</postprocessor>
<template>propStatement-default.ftl</template>
</list-view-config>
</list-view-config>