NIHVIVO-3974 preventing reproduction of axioms with blank nodes in filegraphs and NIHVIVO-3958 applying query syntax leniency to RDFServiceSparql
This commit is contained in:
parent
daf3018ed8
commit
b9cd04f0fa
5 changed files with 47 additions and 37 deletions
|
@ -3,6 +3,7 @@
|
||||||
package edu.cornell.mannlib.vitro.webapp.rdfservice.impl;
|
package edu.cornell.mannlib.vitro.webapp.rdfservice.impl;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
@ -13,7 +14,11 @@ import org.apache.commons.logging.LogFactory;
|
||||||
import com.hp.hpl.jena.graph.Graph;
|
import com.hp.hpl.jena.graph.Graph;
|
||||||
import com.hp.hpl.jena.graph.Node;
|
import com.hp.hpl.jena.graph.Node;
|
||||||
import com.hp.hpl.jena.graph.Triple;
|
import com.hp.hpl.jena.graph.Triple;
|
||||||
|
import com.hp.hpl.jena.query.Query;
|
||||||
|
import com.hp.hpl.jena.query.QueryFactory;
|
||||||
|
import com.hp.hpl.jena.query.QueryParseException;
|
||||||
import com.hp.hpl.jena.query.ResultSet;
|
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.Model;
|
||||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||||
import com.hp.hpl.jena.rdf.model.Statement;
|
import com.hp.hpl.jena.rdf.model.Statement;
|
||||||
|
@ -31,7 +36,7 @@ public abstract class RDFServiceImpl implements RDFService {
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(RDFServiceImpl.class);
|
private static final Log log = LogFactory.getLog(RDFServiceImpl.class);
|
||||||
protected static final String BNODE_ROOT_QUERY =
|
protected static final String BNODE_ROOT_QUERY =
|
||||||
"SELECT ?s WHERE { ?s ?p ?o OPTIONAL { ?ss ?pp ?s } FILTER (!isBlank(?s) || !bound(?ss)) }";
|
"SELECT DISTINCT ?s WHERE { ?s ?p ?o OPTIONAL { ?ss ?pp ?s } FILTER (!isBlank(?s) || !bound(?ss)) }";
|
||||||
|
|
||||||
protected String defaultWriteGraphURI;
|
protected String defaultWriteGraphURI;
|
||||||
protected List<ChangeListener> registeredListeners = new CopyOnWriteArrayList<ChangeListener>();
|
protected List<ChangeListener> registeredListeners = new CopyOnWriteArrayList<ChangeListener>();
|
||||||
|
@ -261,4 +266,24 @@ public abstract class RDFServiceImpl implements RDFService {
|
||||||
result[1] = nonBlankNodeModel;
|
result[1] = nonBlankNodeModel;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Query createQuery(String queryString) {
|
||||||
|
List<Syntax> syntaxes = Arrays.asList(
|
||||||
|
Syntax.defaultQuerySyntax, Syntax.syntaxSPARQL_11,
|
||||||
|
Syntax.syntaxSPARQL_10, Syntax.syntaxSPARQL, Syntax.syntaxARQ);
|
||||||
|
Query q = null;
|
||||||
|
Iterator<Syntax> syntaxIt = syntaxes.iterator();
|
||||||
|
while (q == null) {
|
||||||
|
Syntax syntax = syntaxIt.next();
|
||||||
|
try {
|
||||||
|
q = QueryFactory.create(queryString, syntax);
|
||||||
|
} catch (QueryParseException e) {
|
||||||
|
if (!syntaxIt.hasNext()) {
|
||||||
|
throw(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -359,25 +359,6 @@ public abstract class RDFServiceJena extends RDFServiceImpl implements RDFServic
|
||||||
super.notifyListeners(triple, operation, graphURI);
|
super.notifyListeners(triple, operation, graphURI);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Query createQuery(String queryString) {
|
|
||||||
List<Syntax> syntaxes = Arrays.asList(
|
|
||||||
Syntax.defaultQuerySyntax, Syntax.syntaxSPARQL_11,
|
|
||||||
Syntax.syntaxSPARQL_10, Syntax.syntaxSPARQL, Syntax.syntaxARQ);
|
|
||||||
Query q = null;
|
|
||||||
Iterator<Syntax> syntaxIt = syntaxes.iterator();
|
|
||||||
while (q == null) {
|
|
||||||
Syntax syntax = syntaxIt.next();
|
|
||||||
try {
|
|
||||||
q = QueryFactory.create(queryString, syntax);
|
|
||||||
} catch (QueryParseException e) {
|
|
||||||
if (!syntaxIt.hasNext()) {
|
|
||||||
throw(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected QueryExecution createQueryExecution(String queryString, Query q, Dataset d) {
|
protected QueryExecution createQueryExecution(String queryString, Query q, Dataset d) {
|
||||||
return QueryExecutionFactory.create(q, d);
|
return QueryExecutionFactory.create(q, d);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,7 @@ import java.util.List;
|
||||||
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.HttpClient;
|
||||||
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
||||||
import org.apache.commons.httpclient.NameValuePair;
|
import org.apache.commons.httpclient.NameValuePair;
|
||||||
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
|
||||||
import org.apache.commons.httpclient.methods.PostMethod;
|
import org.apache.commons.httpclient.methods.PostMethod;
|
||||||
import org.apache.commons.httpclient.methods.RequestEntity;
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
@ -49,7 +47,6 @@ import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.ChangeSetImpl;
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.ChangeSetImpl;
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceImpl;
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceImpl;
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.ListeningGraph;
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.ListeningGraph;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* API to write, read, and update Vitro's RDF store, with support
|
* API to write, read, and update Vitro's RDF store, with support
|
||||||
|
@ -223,7 +220,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
RDFServiceImpl.ModelSerializationFormat resultFormat) throws RDFServiceException {
|
RDFServiceImpl.ModelSerializationFormat resultFormat) throws RDFServiceException {
|
||||||
|
|
||||||
Model model = ModelFactory.createDefaultModel();
|
Model model = ModelFactory.createDefaultModel();
|
||||||
Query query = QueryFactory.create(queryStr);
|
Query query = createQuery(queryStr);
|
||||||
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -253,7 +250,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
RDFServiceImpl.ModelSerializationFormat resultFormat) throws RDFServiceException {
|
RDFServiceImpl.ModelSerializationFormat resultFormat) throws RDFServiceException {
|
||||||
|
|
||||||
Model model = ModelFactory.createDefaultModel();
|
Model model = ModelFactory.createDefaultModel();
|
||||||
Query query = QueryFactory.create(queryStr);
|
Query query = createQuery(queryStr);
|
||||||
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -281,7 +278,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
@Override
|
@Override
|
||||||
public InputStream sparqlSelectQuery(String queryStr, RDFService.ResultFormat resultFormat) throws RDFServiceException {
|
public InputStream sparqlSelectQuery(String queryStr, RDFService.ResultFormat resultFormat) throws RDFServiceException {
|
||||||
|
|
||||||
Query query = QueryFactory.create(queryStr);
|
Query query = createQuery(queryStr);
|
||||||
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -323,7 +320,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
@Override
|
@Override
|
||||||
public boolean sparqlAskQuery(String queryStr) throws RDFServiceException {
|
public boolean sparqlAskQuery(String queryStr) throws RDFServiceException {
|
||||||
|
|
||||||
Query query = QueryFactory.create(queryStr);
|
Query query = createQuery(queryStr);
|
||||||
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -465,7 +462,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void executeUpdate(String updateString) throws RDFServiceException {
|
protected void executeUpdate(String updateString) throws RDFServiceException {
|
||||||
try {
|
try {
|
||||||
PostMethod meth = new PostMethod(updateEndpointURI);
|
PostMethod meth = new PostMethod(updateEndpointURI);
|
||||||
try {
|
try {
|
||||||
|
@ -582,7 +579,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
@Override
|
@Override
|
||||||
protected boolean sparqlSelectQueryHasResults(String queryStr) throws RDFServiceException {
|
protected boolean sparqlSelectQueryHasResults(String queryStr) throws RDFServiceException {
|
||||||
|
|
||||||
Query query = QueryFactory.create(queryStr);
|
Query query = createQuery(queryStr);
|
||||||
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -638,6 +635,8 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
Model blankNodeModel = ModelFactory.createDefaultModel();
|
Model blankNodeModel = ModelFactory.createDefaultModel();
|
||||||
blankNodeModel.add(blankNodeStatements);
|
blankNodeModel.add(blankNodeStatements);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
log.debug("update model size " + model.size());
|
log.debug("update model size " + model.size());
|
||||||
log.debug("blank node model size " + blankNodeModel.size());
|
log.debug("blank node model size " + blankNodeModel.size());
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ import javax.servlet.ServletContextListener;
|
||||||
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 com.hp.hpl.jena.ontology.OntDocumentManager;
|
|
||||||
import com.hp.hpl.jena.ontology.OntDocumentManager;
|
import com.hp.hpl.jena.ontology.OntDocumentManager;
|
||||||
import com.hp.hpl.jena.ontology.OntModel;
|
import com.hp.hpl.jena.ontology.OntModel;
|
||||||
import com.hp.hpl.jena.ontology.OntModelSpec;
|
import com.hp.hpl.jena.ontology.OntModelSpec;
|
||||||
|
@ -29,7 +28,8 @@ import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlGraph;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceModelMaker;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
|
||||||
|
|
||||||
// This ContextListener must run after the JenaDataSourceSetup ContextListener
|
// This ContextListener must run after the JenaDataSourceSetup ContextListener
|
||||||
|
|
||||||
|
@ -50,9 +50,11 @@ public class FileGraphSetup implements ServletContextListener {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
ServletContext ctx = sce.getServletContext();
|
||||||
OntDocumentManager.getInstance().setProcessImports(true);
|
OntDocumentManager.getInstance().setProcessImports(true);
|
||||||
baseOms = ModelContext.getBaseOntModelSelector(sce.getServletContext());
|
baseOms = ModelContext.getBaseOntModelSelector(sce.getServletContext());
|
||||||
Dataset dataset = JenaDataSourceSetupBase.getStartupDataset(sce.getServletContext());
|
Dataset dataset = JenaDataSourceSetupBase.getStartupDataset(sce.getServletContext());
|
||||||
|
RDFServiceModelMaker maker = new RDFServiceModelMaker(RDFServiceUtils.getRDFServiceFactory(ctx));
|
||||||
|
|
||||||
// ABox files
|
// ABox files
|
||||||
Set<String> pathSet = sce.getServletContext().getResourcePaths(PATH_ROOT + ABOX);
|
Set<String> pathSet = sce.getServletContext().getResourcePaths(PATH_ROOT + ABOX);
|
||||||
|
@ -61,7 +63,7 @@ public class FileGraphSetup implements ServletContextListener {
|
||||||
|
|
||||||
if (pathSet != null) {
|
if (pathSet != null) {
|
||||||
OntModel aboxBaseModel = baseOms.getABoxModel();
|
OntModel aboxBaseModel = baseOms.getABoxModel();
|
||||||
aboxChanged = readGraphs(sce, pathSet, dataset, ABOX, aboxBaseModel);
|
aboxChanged = readGraphs(sce, pathSet, maker, ABOX, aboxBaseModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TBox files
|
// TBox files
|
||||||
|
@ -71,7 +73,7 @@ public class FileGraphSetup implements ServletContextListener {
|
||||||
|
|
||||||
if (pathSet != null) {
|
if (pathSet != null) {
|
||||||
OntModel tboxBaseModel = baseOms.getTBoxModel();
|
OntModel tboxBaseModel = baseOms.getTBoxModel();
|
||||||
tboxChanged = readGraphs(sce, pathSet, dataset, TBOX, tboxBaseModel);
|
tboxChanged = readGraphs(sce, pathSet, maker, TBOX, tboxBaseModel);
|
||||||
}
|
}
|
||||||
} catch (ClassCastException cce) {
|
} catch (ClassCastException cce) {
|
||||||
String errMsg = "Unable to cast servlet context attribute to the appropriate type " + cce.getLocalizedMessage();
|
String errMsg = "Unable to cast servlet context attribute to the appropriate type " + cce.getLocalizedMessage();
|
||||||
|
@ -105,7 +107,7 @@ public class FileGraphSetup implements ServletContextListener {
|
||||||
* Note: no connection needs to be maintained between the in-memory copy of the
|
* Note: no connection needs to be maintained between the in-memory copy of the
|
||||||
* graph and the DB copy.
|
* graph and the DB copy.
|
||||||
*/
|
*/
|
||||||
public boolean readGraphs(ServletContextEvent sce, Set<String> pathSet, Dataset dataset, String type, OntModel baseModel) {
|
public boolean readGraphs(ServletContextEvent sce, Set<String> pathSet, RDFServiceModelMaker dataset, String type, OntModel baseModel) {
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
|
@ -169,10 +171,9 @@ public class FileGraphSetup implements ServletContextListener {
|
||||||
* Otherwise, if a graph with the given name is in the DB and is isomorphic with
|
* Otherwise, if a graph with the given name is in the DB and is isomorphic with
|
||||||
* the graph that was read from the files system, then do nothing.
|
* the graph that was read from the files system, then do nothing.
|
||||||
*/
|
*/
|
||||||
public boolean updateGraphInDB(Dataset dataset, Model fileModel, String type, String path) {
|
public boolean updateGraphInDB(RDFServiceModelMaker dataset, Model fileModel, String type, String path) {
|
||||||
|
|
||||||
String graphURI = pathToURI(path,type);
|
String graphURI = pathToURI(path,type);
|
||||||
Model dbModel = dataset.getNamedModel(graphURI);
|
Model dbModel = dataset.getModel(graphURI);
|
||||||
boolean modelChanged = false;
|
boolean modelChanged = false;
|
||||||
|
|
||||||
boolean isIsomorphic = dbModel.isIsomorphicWith(fileModel);
|
boolean isIsomorphic = dbModel.isIsomorphicWith(fileModel);
|
||||||
|
|
|
@ -28,6 +28,10 @@ public class JenaOutputUtils {
|
||||||
|
|
||||||
Map<String,String> prefixes = new HashMap<String,String>();
|
Map<String,String> prefixes = new HashMap<String,String>();
|
||||||
List<Ontology> ontologies = wadf.getOntologyDao().getAllOntologies();
|
List<Ontology> ontologies = wadf.getOntologyDao().getAllOntologies();
|
||||||
|
// apparently this is null if empty
|
||||||
|
if (ontologies == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
Iterator<Ontology> iter = ontologies.iterator();
|
Iterator<Ontology> iter = ontologies.iterator();
|
||||||
String namespace = null;
|
String namespace = null;
|
||||||
String prefix = null;
|
String prefix = null;
|
||||||
|
|
Loading…
Add table
Reference in a new issue