merging SDB import/export changes to trunk with backward compatibility code

This commit is contained in:
bjl23 2010-11-22 19:46:38 +00:00
parent ef94f673d2
commit c149bc77d6
8 changed files with 183 additions and 93 deletions

View file

@ -132,6 +132,14 @@ public class VitroRequest implements HttpServletRequest {
return jenaOntModel;
}
public OntModel getInferenceOntModel() {
OntModel jenaOntModel = (OntModel)_req.getSession().getAttribute( JenaBaseDao.INFERENCE_ONT_MODEL_ATTRIBUTE_NAME );
if ( jenaOntModel == null ) {
jenaOntModel = (OntModel)_req.getSession().getServletContext().getAttribute( JenaBaseDao.INFERENCE_ONT_MODEL_ATTRIBUTE_NAME );
}
return jenaOntModel;
}
public Portal getPortal(){
return(Portal) getAttribute("portalBean");
}

View file

@ -14,13 +14,20 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vedit.controller.BaseEditController;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils;
public class JenaExportController extends BaseEditController {
@ -75,6 +82,7 @@ public class JenaExportController extends BaseEditController {
}
private void outputRDF( VitroRequest vreq, HttpServletResponse response ) {
Dataset dataset = vreq.getDataset();
JenaModelUtils xutil = new JenaModelUtils();
String formatParam = vreq.getParameter("format");
String subgraphParam = vreq.getParameter("subgraph");
@ -94,42 +102,32 @@ public class JenaExportController extends BaseEditController {
for(int i =0; i < uri.length-1;i++)
ontologyURI = ontologyURI + uri[i];
}
if ( "inferred".equals(assertedOrInferredParam) ) {
limitToInferred = true;
inferenceModel = getOntModelFromAttribute( INFERENCES_ONT_MODEL_ATTR, vreq );
model = inferenceModel;
} else if ( "full".equals(assertedOrInferredParam) ) {
model = getOntModelFromAttribute( FULL_ONT_MODEL_ATTR, vreq );
} else { // default
model = getOntModelFromAttribute( ASSERTIONS_ONT_MODEL_ATTR, vreq );
}
if ( "abox".equals(subgraphParam) ) {
if (limitToInferred) {
Model fullModel = getOntModelFromAttribute( FULL_ONT_MODEL_ATTR, vreq );
model = xutil.extractABox( fullModel );
try {
inferenceModel.enterCriticalSection(Lock.READ);
model = model.intersection(inferenceModel);
} finally {
inferenceModel.leaveCriticalSection();
}
} else {
model = xutil.extractABox( model );
if( "abox".equals(subgraphParam)){
model = ModelFactory.createDefaultModel();
if("inferred".equals(assertedOrInferredParam)){
model = xutil.extractABox(dataset, INFERENCE_GRAPH);
}
} else if ( "tbox".equals(subgraphParam) ) {
if (limitToInferred) {
Model fullModel = getOntModelFromAttribute( FULL_ONT_MODEL_ATTR, vreq );
model = xutil.extractTBox( fullModel, ontologyURI );
try {
inferenceModel.enterCriticalSection(Lock.READ);
model = model.intersection(inferenceModel);
} finally {
inferenceModel.leaveCriticalSection();
}
} else {
model = xutil.extractTBox( model, ontologyURI );
else if("full".equals(assertedOrInferredParam)){
model = xutil.extractABox(dataset, FULL_GRAPH);
}
else{
model = xutil.extractABox(dataset, ASSERTIONS_GRAPH);
}
}
else if("tbox".equals(subgraphParam)){
if("inferred".equals(assertedOrInferredParam)){
model = xutil.extractTBox(dataset, ontologyURI,INFERENCE_GRAPH);
}
else if("full".equals(assertedOrInferredParam)){
model = xutil.extractTBox(dataset, ontologyURI, FULL_GRAPH);
}
else{
model = xutil.extractTBox(dataset, ontologyURI, ASSERTIONS_GRAPH);
}
}
if ( formatParam == null ) {
@ -178,6 +176,7 @@ public class JenaExportController extends BaseEditController {
}
private OntModel getOntModelFromAttribute( String attributeName, VitroRequest vreq ) {
Object o = vreq.getAttribute( attributeName );
if ( (o != null) && (o instanceof OntModel) ) {
return (OntModel) o;
@ -194,6 +193,9 @@ public class JenaExportController extends BaseEditController {
static final String FULL_ONT_MODEL_ATTR = "jenaOntModel";
static final String ASSERTIONS_ONT_MODEL_ATTR = "baseOntModel";
static final String INFERENCES_ONT_MODEL_ATTR = "inferenceOntModel";
static final String FULL_GRAPH = "?g";
static final String ASSERTIONS_GRAPH = "<http://vitro.mannlib.cornell.edu/default/vitro-kb-2>";
static final String INFERENCE_GRAPH = "<http://vitro.mannlib.cornell.edu/default/vitro-kb-inf>";
static Map<String,String> formatToExtension;
static Map<String,String> formatToMimetype;

View file

@ -152,23 +152,42 @@ public class RDFUploadController extends BaseEditController {
}
}
/* ********** Do the model changes *********** */
long stmtCount = 0L;
long tboxstmtCount = 0L;
long aboxstmtCount = 0L;
if( tempModel != null ){
OntModel memModel=null;
JenaModelUtils xutil = new JenaModelUtils();
OntModel tboxModel=null;
OntModel aboxModel=null;
OntModel tboxChangeModel=null;
Model aboxChangeModel=null;
try {
memModel = ((OntModelSelector) request.getSession()
.getAttribute("unionOntModelSelector")).getABoxModel();
tboxModel = ((OntModelSelector) request.getSession()
.getAttribute("baseOntModelSelector")).getTBoxModel();
aboxModel = ((OntModelSelector) request.getSession()
.getAttribute("baseOntModelSelector")).getABoxModel();
} catch (Exception e) {}
if (memModel==null) {
memModel = ((OntModelSelector) getServletContext()
.getAttribute("unionOntModelSelector")).getABoxModel();
if (tboxModel==null) {
tboxModel = ((OntModelSelector) getServletContext()
.getAttribute("baseOntModelSelector")).getTBoxModel();
}
if (memModel != null) {
stmtCount = operateOnModel(request.getFullWebappDaoFactory(), memModel,tempModel,remove,makeClassgroups,portalArray,loginBean.getUserURI());
if (aboxModel==null) {
aboxModel = ((OntModelSelector) getServletContext()
.getAttribute("baseOntModelSelector")).getABoxModel();
}
if (tboxModel != null) {
tboxChangeModel = xutil.extractTBox(tempModel);
tboxstmtCount = operateOnModel(request.getFullWebappDaoFactory(), tboxModel,tboxChangeModel,remove,makeClassgroups,portalArray,loginBean.getUserURI());
}
if (aboxModel != null) {
aboxChangeModel = tempModel.remove(tboxChangeModel);
aboxstmtCount = operateOnModel(request.getFullWebappDaoFactory(), aboxModel,aboxChangeModel,remove,makeClassgroups,portalArray,loginBean.getUserURI());
}
}
request.setAttribute("uploadDesc", uploadDesc + ". " + verb + " " + stmtCount + " statements.");
request.setAttribute("uploadDesc", uploadDesc + ". " + verb + " " + (tboxstmtCount + aboxstmtCount) + " statements.");
RequestDispatcher rd = request.getRequestDispatcher(Controllers.BASIC_JSP);
request.setAttribute("bodyJsp","/templates/edit/specific/upload_rdf_result.jsp");

View file

@ -16,6 +16,8 @@ import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
@ -145,38 +147,40 @@ public class JenaModelUtils {
private final OntModelSpec DEFAULT_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
public OntModel extractTBox( Model inputModel ) {
return extractTBox( inputModel, null );
public OntModel extractTBox( Model inputModel) {
Dataset dataset = DatasetFactory.create(inputModel);
return extractTBox( dataset,null,null );
}
public OntModel extractTBox( Model inputModel, String namespace ) {
public OntModel extractTBox( Dataset dataset, String namespace, String graphURI ) {
System.out.println(namespace);
System.out.println(graphURI);
OntModel tboxModel = ModelFactory.createOntologyModel(DEFAULT_ONT_MODEL_SPEC);
List<String> queryStrList = new LinkedList<String>();
// Use SPARQL DESCRIBE queries to extract the RDF for named ontology entities
queryStrList.add( makeDescribeQueryStr( OWL.Class.getURI(), namespace ) );
queryStrList.add( makeDescribeQueryStr( OWL.ObjectProperty.getURI(), namespace ) );
queryStrList.add( makeDescribeQueryStr( OWL.DatatypeProperty.getURI(), namespace ) );
queryStrList.add( makeDescribeQueryStr( OWL.Class.getURI(), namespace, graphURI ) );
queryStrList.add( makeDescribeQueryStr( OWL.ObjectProperty.getURI(), namespace, graphURI ) );
queryStrList.add( makeDescribeQueryStr( OWL.DatatypeProperty.getURI(), namespace, graphURI ) );
// if we're using to a hash namespace, the URI of the Ontology resource will be
// that namespace minus the final hash mark.
if ( namespace != null && namespace.endsWith("#") ) {
queryStrList.add( makeDescribeQueryStr( OWL.Ontology.getURI(), namespace.substring(0,namespace.length()-2) ) );
queryStrList.add( makeDescribeQueryStr( OWL.Ontology.getURI(), namespace.substring(0,namespace.length()-2), graphURI ) );
} else {
queryStrList.add( makeDescribeQueryStr( OWL.Ontology.getURI(), namespace ) );
queryStrList.add( makeDescribeQueryStr( OWL.Ontology.getURI(), namespace, graphURI ) );
}
// Perform the SPARQL DESCRIBEs
for ( String queryStr : queryStrList ) {
Query tboxSparqlQuery = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(tboxSparqlQuery,inputModel);
QueryExecution qe = QueryExecutionFactory.create(tboxSparqlQuery,dataset);
try {
inputModel.enterCriticalSection(Lock.READ);
dataset.getLock().enterCriticalSection(Lock.READ);
qe.execDescribe(tboxModel);
} finally {
inputModel.leaveCriticalSection();
dataset.getLock().leaveCriticalSection();
}
}
@ -185,12 +189,23 @@ private final OntModelSpec DEFAULT_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
}
private String makeDescribeQueryStr( String typeURI, String namespace ) {
return makeDescribeQueryStr( typeURI, namespace, null );
}
private String makeDescribeQueryStr( String typeURI, String namespace, String graphURI ) {
StringBuffer describeQueryStrBuff = new StringBuffer()
.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n")
.append("PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#> \n")
.append("DESCRIBE ?res WHERE { \n")
.append(" ?res rdf:type <").append(typeURI).append("> . \n")
.append("DESCRIBE ?res WHERE { \n");
if (graphURI != null) {
describeQueryStrBuff
.append("GRAPH " + graphURI + "{ \n");
}
describeQueryStrBuff
.append(" ?res rdf:type <").append(typeURI).append("> . \n");
describeQueryStrBuff
.append(" FILTER (!isBlank(?res)) \n");
if (namespace == null) {
@ -216,6 +231,10 @@ private final OntModelSpec DEFAULT_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
.append(namespace)
.append("\") \n");
}
if (graphURI != null) {
describeQueryStrBuff
.append("} \n");
}
describeQueryStrBuff.append("} \n");
@ -223,7 +242,12 @@ private final OntModelSpec DEFAULT_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
}
public Model extractABox( Model inputModel ) {
public Model extractABox(Model inputModel){
Dataset dataset = DatasetFactory.create(inputModel);
return extractABox(dataset, null);
}
public Model extractABox( Dataset dataset, String graphURI ) {
Model aboxModel = ModelFactory.createDefaultModel();
@ -232,27 +256,48 @@ private final OntModelSpec DEFAULT_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
// reasoning model: we could then simply describe all instances of
// owl:Thing.
OntModel ontModel = ( inputModel instanceof OntModel )
? (OntModel)inputModel
: ModelFactory.createOntologyModel( DEFAULT_ONT_MODEL_SPEC, inputModel );
//OntModel ontModel = ( inputModel instanceof OntModel )
//? (OntModel)inputModel
//: ModelFactory.createOntologyModel( DEFAULT_ONT_MODEL_SPEC, inputModel );
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
Model model = ModelFactory.createDefaultModel();
String getStatements =
"CONSTRUCT " +
"{ ?p ?o ?a \n" +
"} WHERE {" +
"GRAPH ?g { \n" +
"{ ?p ?o ?a } \n" +
"} \n" +
"}";
try{
dataset.getLock().enterCriticalSection(Lock.READ);
model = QueryExecutionFactory.create(QueryFactory.create(getStatements), dataset).execConstruct();
}
finally{
dataset.getLock().leaveCriticalSection();
}
ontModel.add(model.listStatements());
try {
ontModel.enterCriticalSection(Lock.READ);
Iterator classIt = ontModel.listNamedClasses();
while ( classIt.hasNext() ) {
OntClass ontClass = (OntClass) classIt.next();
if ( !(ontClass.getNameSpace().startsWith(OWL.getURI()) )
&& !(ontClass.getNameSpace().startsWith(VitroVocabulary.vitroURI)) ) {
String queryStr = makeDescribeQueryStr( ontClass.getURI(), null );
String queryStr = makeDescribeQueryStr( ontClass.getURI(), null, graphURI );
Query aboxSparqlQuery = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(aboxSparqlQuery,inputModel);
QueryExecution qe = QueryExecutionFactory.create(aboxSparqlQuery,dataset);
try {
inputModel.enterCriticalSection(Lock.READ);
qe.execDescribe(aboxModel);
dataset.getLock().enterCriticalSection(Lock.READ);
qe.execDescribe(aboxModel); // puts the statements about each resource into aboxModel.
} finally {
inputModel.leaveCriticalSection();
dataset.getLock().leaveCriticalSection();
}
}

View file

@ -20,6 +20,10 @@ import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.query.DataSource;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.IdentifierBundle;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.SelfEditingIdentifierFactory;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.SelfEditingIdentifierFactory.SelfEditing;
@ -40,6 +44,7 @@ import edu.cornell.mannlib.vitro.webapp.flags.FlagException;
import edu.cornell.mannlib.vitro.webapp.flags.PortalFlag;
import edu.cornell.mannlib.vitro.webapp.flags.RequestToAuthFlag;
import edu.cornell.mannlib.vitro.webapp.flags.SunsetFlag;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
/**
* This sets up several objects in the Request scope for each
@ -195,6 +200,15 @@ public class VitroRequestPrep implements Filter {
if( log.isDebugEnabled() ) log.debug("setting role-based WebappDaoFactory filter for role " + role.toString());
vreq.setWebappDaoFactory(wdf);
// support for Dataset interface if using Jena in-memory model
if (vreq.getDataset() == null) {
DataSource dataset = DatasetFactory.create();
dataset.addNamedModel(JenaDataSourceSetupBase.JENA_DB_MODEL, vreq.getAssertionsOntModel());
dataset.addNamedModel(JenaDataSourceSetupBase.JENA_INF_MODEL, vreq.getInferenceOntModel());
vreq.setDataset(dataset);
}
request.setAttribute("VitroRequestPrep.setup", new Integer(1));
chain.doFilter(request, response);
}

View file

@ -80,27 +80,27 @@ public class WebappDaoFactorySDBPrep implements Filter {
SDBConnection conn = null;
try {
if (
request instanceof HttpServletRequest &&
_bds != null && _storeDesc != null && _oms != null) {
try {
conn = new SDBConnection(_bds.getConnection()) ;
} catch (SQLException sqe) {
throw new RuntimeException("Unable to connect to database", sqe);
if (
request instanceof HttpServletRequest &&
_bds != null && _storeDesc != null && _oms != null) {
try {
conn = new SDBConnection(_bds.getConnection()) ;
} catch (SQLException sqe) {
throw new RuntimeException("Unable to connect to database", sqe);
}
if (conn != null) {
Store store = SDBFactory.connectStore(conn, _storeDesc);
Dataset dataset = SDBFactory.connectDataset(store);
VitroRequest vreq = new VitroRequest((HttpServletRequest) request);
WebappDaoFactory wadf =
new WebappDaoFactorySDB(_oms, dataset, _defaultNamespace, null, null);
vreq.setWebappDaoFactory(wadf);
vreq.setFullWebappDaoFactory(wadf);
vreq.setDataset(dataset);
}
}
if (conn != null) {
Store store = SDBFactory.connectStore(conn, _storeDesc);
Dataset dataset = SDBFactory.connectDataset(store);
VitroRequest vreq = new VitroRequest((HttpServletRequest) request);
WebappDaoFactory wadf =
new WebappDaoFactorySDB(_oms, dataset, _defaultNamespace, null, null);
vreq.setWebappDaoFactory(wadf);
vreq.setFullWebappDaoFactory(wadf);
vreq.setDataset(dataset);
}
}
} catch (Throwable t) {
t.printStackTrace();
log.error("Unable to filter request to set up SDB connection", t);
}
request.setAttribute("WebappDaoFactorySDBPrep.setup", 1);

View file

@ -76,10 +76,12 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
sce.getServletContext().setAttribute("baseOntModel", memModel);
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);
sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf);
sce.getServletContext().setAttribute("inferenceOntModelSelector", inferenceOms);
sce.getServletContext().setAttribute("jenaOntModel", unionModel);
WebappDaoFactory wadf = new WebappDaoFactoryJena(unionOms, defaultNamespace, null, null);

View file

@ -56,12 +56,12 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
// (queries and updates) with the ABox data from the DB - this model is not maintained
// in memory. For query performance reasons, there won't be any submodels for the ABox data.
static final String JENA_DB_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-2";
public static final String JENA_DB_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-2";
// ABox inferences. This is ABox data that is inferred, using VIVO's native simple, specific-
// purpose reasoning based on the combination of the Abox (assertion and inferences) data
// and the TBox (assertions and inferences) data.
static final String JENA_INF_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-inf";
public static final String JENA_INF_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-inf";
// TBox assertions.
// Some of these (the local extensions) are stored and maintained in a Jena database and
@ -69,17 +69,17 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
// Other parts of the TBox, the 'VIVO Core', are also backed by a Jena DB, but they are
// read fresh from files each time the application starts. While the application is running,
// they are kept in memory, as submodels of the in memory copy of this named graph.
static final String JENA_TBOX_ASSERTIONS_MODEL = "http://vitro.mannlib.cornell.edu/default/asserted-tbox";
public static final String JENA_TBOX_ASSERTIONS_MODEL = "http://vitro.mannlib.cornell.edu/default/asserted-tbox";
// Inferred TBox. This is TBox data that is inferred from the combination of VIVO core TBox
// and any local extension TBox assertions. Pellet computes these inferences.
// These are stored in the DB.
static final String JENA_TBOX_INF_MODEL = "http://vitro.mannlib.cornell.edu/default/inferred-tbox";
public static final String JENA_TBOX_INF_MODEL = "http://vitro.mannlib.cornell.edu/default/inferred-tbox";
static final String JENA_AUDIT_MODEL = "http://vitro.mannlib.cornell.edu/ns/db/experimental/audit";
static final String JENA_USER_ACCOUNTS_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-userAccounts";
public static final String JENA_USER_ACCOUNTS_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-userAccounts";
// This model doesn't exist yet. It's a placeholder for the application ontology.