NIHVIVO-1735 and NIHVIVO-1753 bugfixes for baseOntModel setup and initial reasoning

This commit is contained in:
bjl23 2011-01-16 21:23:14 +00:00
parent 58027d5d95
commit 6883148154
3 changed files with 308 additions and 259 deletions

View file

@ -16,7 +16,6 @@ import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.Individual; import com.hp.hpl.jena.ontology.Individual;
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;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Literal;
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;
@ -28,7 +27,6 @@ import com.hp.hpl.jena.sdb.Store;
import com.hp.hpl.jena.sdb.StoreDesc; import com.hp.hpl.jena.sdb.StoreDesc;
import com.hp.hpl.jena.sdb.sql.SDBConnection; import com.hp.hpl.jena.sdb.sql.SDBConnection;
import com.hp.hpl.jena.sdb.store.DatabaseType; import com.hp.hpl.jena.sdb.store.DatabaseType;
import com.hp.hpl.jena.sdb.store.DatasetStore;
import com.hp.hpl.jena.sdb.store.LayoutType; import com.hp.hpl.jena.sdb.store.LayoutType;
import com.hp.hpl.jena.shared.Lock; import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.util.iterator.ClosableIterator; import com.hp.hpl.jena.util.iterator.ClosableIterator;
@ -41,73 +39,60 @@ import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDaoCon; import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDaoCon;
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils; import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer; import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer;
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelectorImpl; import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelectorImpl;
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker; import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker;
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaSDBModelMaker; import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaSDBModelMaker;
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB; import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase.TripleStoreType;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper; import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
import edu.cornell.mannlib.vitro.webapp.utils.jena.InitialJenaModelUtils; import edu.cornell.mannlib.vitro.webapp.utils.jena.InitialJenaModelUtils;
import edu.cornell.mannlib.vitro.webapp.utils.jena.NamespaceMapperJena; import edu.cornell.mannlib.vitro.webapp.utils.jena.NamespaceMapperJena;
public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements javax.servlet.ServletContextListener { public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements javax.servlet.ServletContextListener {
private static final Log log = LogFactory.getLog(JenaDataSourceSetup.class.getName()); private static final Log log = LogFactory.getLog(JenaDataSourceSetup.class.getName());
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
try { try {
// JenaPersistentDataSourceSetup should have already set this up - it just sets // JenaPersistentDataSourceSetup should have already set this up - it just sets
// up things related to the DB. // up things related to the DB.
// TODO: I would like to make this code (before the sdb try/catch conditional so // TODO: I would like to make this code (before the sdb try/catch conditional so
// that it is not executed in a post-sdb-conversion environment. // that it is not executed in a post-sdb-conversion environment.
OntModel memModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel"); OntModel memModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel");
if (memModel == null) { if (memModel == null) {
memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
log.warn("WARNING: no database connected. Changes will disappear after context restart."); log.warn("WARNING: no database connected. Changes will disappear after context restart.");
sce.getServletContext().setAttribute("jenaOntModel",memModel); sce.getServletContext().setAttribute("jenaOntModel",memModel);
} }
memModel.addSubModel((new JenaBaseDaoCon()).getConstModel()); // add the vitro tbox to the model
OntModel inferenceModel = ontModelFromContextAttribute(sce.getServletContext(), "inferenceOntModel"); OntModel inferenceModel = ontModelFromContextAttribute(sce.getServletContext(), "inferenceOntModel");
OntModel userAccountsModel = ontModelFromContextAttribute(sce.getServletContext(), "userAccountsOntModel"); OntModel userAccountsModel = ontModelFromContextAttribute(sce.getServletContext(), "userAccountsOntModel");
if (userAccountsModel.size() == 0) { if (userAccountsModel.size() == 0) {
checkMainModelForUserAccounts(memModel, userAccountsModel); checkMainModelForUserAccounts(memModel, userAccountsModel);
} }
OntModel unionModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(memModel, inferenceModel));
OntModelSelectorImpl baseOms = new OntModelSelectorImpl(); OntModelSelectorImpl baseOms = new OntModelSelectorImpl();
baseOms.setApplicationMetadataModel(memModel);
baseOms.setTBoxModel(memModel);
baseOms.setFullModel(memModel);
OntModelSelectorImpl inferenceOms = new OntModelSelectorImpl(); OntModelSelectorImpl inferenceOms = new OntModelSelectorImpl();
inferenceOms.setABoxModel(inferenceModel);
inferenceOms.setTBoxModel(inferenceModel);
inferenceOms.setFullModel(inferenceModel);
OntModelSelectorImpl unionOms = new OntModelSelectorImpl(); OntModelSelectorImpl unionOms = new OntModelSelectorImpl();
unionOms.setApplicationMetadataModel(unionModel);
unionOms.setTBoxModel(unionModel);
unionOms.setFullModel(unionModel);
baseOms.setUserAccountsModel(userAccountsModel); baseOms.setUserAccountsModel(userAccountsModel);
inferenceOms.setUserAccountsModel(userAccountsModel); inferenceOms.setUserAccountsModel(userAccountsModel);
unionOms.setUserAccountsModel(userAccountsModel); unionOms.setUserAccountsModel(userAccountsModel);
OntModel displayModel = ontModelFromContextAttribute(sce.getServletContext(),"displayOntModel"); OntModel displayModel = ontModelFromContextAttribute(sce.getServletContext(),"displayOntModel");
baseOms.setDisplayModel(displayModel); baseOms.setDisplayModel(displayModel);
inferenceOms.setDisplayModel(displayModel); inferenceOms.setDisplayModel(displayModel);
unionOms.setDisplayModel(displayModel); unionOms.setDisplayModel(displayModel);
checkForNamespaceMismatch( memModel, defaultNamespace ); checkForNamespaceMismatch( memModel, defaultNamespace );
// SDB initialization // SDB initialization
String layoutStr = ConfigurationProperties.getProperty( String layoutStr = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.sdb.layout","layout2/hash"); "VitroConnection.DataSource.sdb.layout","layout2/hash");
String dbtypeStr = ConfigurationProperties.getProperty( String dbtypeStr = ConfigurationProperties.getProperty(
@ -115,141 +100,108 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
StoreDesc storeDesc = new StoreDesc( StoreDesc storeDesc = new StoreDesc(
LayoutType.fetch(layoutStr), LayoutType.fetch(layoutStr),
DatabaseType.fetch(dbtypeStr) ); DatabaseType.fetch(dbtypeStr) );
sce.getServletContext().setAttribute("storeDesc", storeDesc); sce.getServletContext().setAttribute("storeDesc", storeDesc);
BasicDataSource bds = makeDataSourceFromConfigurationProperties(); BasicDataSource bds = makeDataSourceFromConfigurationProperties();
this.setApplicationDataSource(bds, sce.getServletContext()); this.setApplicationDataSource(bds, sce.getServletContext());
SDBConnection conn = new SDBConnection(bds.getConnection()) ; SDBConnection conn = new SDBConnection(bds.getConnection()) ;
Store store = SDBFactory.connectStore(conn, storeDesc); Store store = SDBFactory.connectStore(conn, storeDesc);
try {
// a test query to see if the store is formatted
SDBFactory.connectDefaultModel(store).contains(OWL.Thing, RDF.type, OWL.Nothing);
} catch (Exception e) { // unformatted store
log.debug("Non-SDB system detected. Setting up SDB store");
store.getTableFormatter().create();
store.getTableFormatter().truncate();
// This is a one-time copy of stored KB data - from a Jena RDB store
// to a Jena SDB store. In the process, we will also separate out the
// TBox from the Abox; these are in the same graph in pre 1.2 VIVO
// versions and will now be stored and maintained in separate models
// Access to the Jena RDB data is through the OntModelSelectors that have
// been set up earlier in the current session by
// JenaPersistentDataSourceSetup.java
// In the code below, note that the current getABoxModel() methods on
// the OntModelSelectors return a graph with both ABox and TBox data.
OntModel submodels = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
readOntologyFilesInPathSet(SUBMODELS, sce.getServletContext(), submodels);
Model tboxAssertions = SDBFactory.connectNamedModel(store, JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL);
// initially putting the results in memory so we have a
// cheaper way of computing the difference when we copy the ABox
Model memTboxAssertions = ModelFactory.createDefaultModel();
getTBoxModel(memModel, submodels, memTboxAssertions);
tboxAssertions.add(memTboxAssertions);
Model tboxInferences = SDBFactory.connectNamedModel(store, JenaDataSourceSetupBase.JENA_TBOX_INF_MODEL);
// initially putting the results in memory so we have a
// cheaper way of computing the difference when we copy the ABox
Model memTboxInferences = ModelFactory.createDefaultModel();
getTBoxModel(inferenceModel, submodels, memTboxInferences);
tboxInferences.add(memTboxInferences);
Model aboxAssertions = SDBFactory.connectNamedModel(store, JenaDataSourceSetupBase.JENA_DB_MODEL);
copyDifference(memModel, memTboxAssertions, aboxAssertions);
Model aboxInferences = SDBFactory.connectNamedModel(store, JenaDataSourceSetupBase.JENA_INF_MODEL);
copyDifference(inferenceModel, memTboxInferences, aboxInferences);
// The code below, which sets up the OntModelSelectors, controls whether each
// model is maintained in memory, in the DB, or both while the application
// is running.
}
sce.getServletContext().setAttribute("kbStore", store); sce.getServletContext().setAttribute("kbStore", store);
//store.getTableFormatter().dropIndexes(); if (!isSetUp(store)) {
//store.getTableFormatter().addIndexes(); log.debug("Non-SDB system detected. Setting up SDB store");
setupSDB(sce.getServletContext(), store, memModel, inferenceModel);
}
// Populate the three OntModelSelectors (BaseOntModel=assertions, InferenceOntModel=inferences // The code below, which sets up the OntModelSelectors, controls whether each
// and JenaOntModel=union of assertions and inferences) with the post-SDB-conversion models. // model is maintained in memory, in the DB, or both while the application
// is running.
// ABox assertions // Populate the three OntModelSelectors (BaseOntModel=assertions, InferenceOntModel=inferences
Model aboxAssertions = makeDBModel(bds, JenaDataSourceSetupBase.JENA_DB_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB); // and JenaOntModel=union of assertions and inferences) with the post-SDB-conversion models.
Model listenableAboxAssertions = ModelFactory.createUnion(aboxAssertions, ModelFactory.createDefaultModel());
baseOms.setABoxModel(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, listenableAboxAssertions));
// ABox inferences // ABox assertions
Model aboxInferences = makeDBModel(bds, JenaDataSourceSetupBase.JENA_INF_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB); Model aboxAssertions = makeDBModel(bds, JenaDataSourceSetupBase.JENA_DB_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
Model listenableAboxInferences = ModelFactory.createUnion(aboxInferences, ModelFactory.createDefaultModel()); Model listenableAboxAssertions = ModelFactory.createUnion(aboxAssertions, ModelFactory.createDefaultModel());
inferenceOms.setABoxModel(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, listenableAboxInferences)); baseOms.setABoxModel(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, listenableAboxAssertions));
// ABox inferences
Model aboxInferences = makeDBModel(bds, JenaDataSourceSetupBase.JENA_INF_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
Model listenableAboxInferences = ModelFactory.createUnion(aboxInferences, ModelFactory.createDefaultModel());
inferenceOms.setABoxModel(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, listenableAboxInferences));
// Since the TBox models are in memory, they do not have time out issues like the // Since the TBox models are in memory, they do not have time out issues like the
// ABox models do (and so don't need the extra step to make them listenable). // ABox models do (and so don't need the extra step to make them listenable).
// TBox assertions // TBox assertions
try { try {
Model tboxAssertionsDB = makeDBModel(bds, JENA_TBOX_ASSERTIONS_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB); Model tboxAssertionsDB = makeDBModel(bds, JENA_TBOX_ASSERTIONS_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
OntModel tboxAssertions = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); OntModel tboxAssertions = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
if (tboxAssertionsDB != null) { if (tboxAssertionsDB != null) {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
System.out.println("Copying cached tbox assertions into memory"); System.out.println("Copying cached tbox assertions into memory");
tboxAssertions.add(tboxAssertionsDB); tboxAssertions.add(tboxAssertionsDB);
System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to load tbox assertions"); System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to load tbox assertions");
} }
tboxAssertions.getBaseModel().register(new ModelSynchronizer(tboxAssertionsDB)); tboxAssertions.getBaseModel().register(new ModelSynchronizer(tboxAssertionsDB));
baseOms.setTBoxModel(tboxAssertions); baseOms.setTBoxModel(tboxAssertions);
} catch (Throwable e) { } catch (Throwable e) {
log.error("Unable to load tbox assertion cache from DB", e); log.error("Unable to load tbox assertion cache from DB", e);
} }
// TBox inferences // TBox inferences
try { try {
Model tboxInferencesDB = makeDBModel(bds, JENA_TBOX_INF_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB); Model tboxInferencesDB = makeDBModel(bds, JENA_TBOX_INF_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
OntModel tboxInferences = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); OntModel tboxInferences = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
if (tboxInferencesDB != null) { if (tboxInferencesDB != null) {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
System.out.println("Copying cached tbox inferences into memory"); System.out.println("Copying cached tbox inferences into memory");
tboxInferences.add(tboxInferencesDB); tboxInferences.add(tboxInferencesDB);
System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to load tbox inferences"); System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to load tbox inferences");
} }
tboxInferences.getBaseModel().register(new ModelSynchronizer(tboxInferencesDB)); tboxInferences.getBaseModel().register(new ModelSynchronizer(tboxInferencesDB));
inferenceOms.setTBoxModel(tboxInferences); inferenceOms.setTBoxModel(tboxInferences);
} catch (Throwable e) { } catch (Throwable e) {
log.error("Unable to load tbox inference cache from DB", e); log.error("Unable to load tbox inference cache from DB", e);
} }
// union ABox // union ABox
OntModel unionABoxModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(baseOms.getABoxModel(), inferenceOms.getABoxModel())); OntModel unionABoxModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(baseOms.getABoxModel(), inferenceOms.getABoxModel()));
unionOms.setABoxModel(unionABoxModel); unionOms.setABoxModel(unionABoxModel);
// union TBox // union TBox
OntModel unionTBoxModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(baseOms.getTBoxModel(), inferenceOms.getTBoxModel())); OntModel unionTBoxModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(baseOms.getTBoxModel(), inferenceOms.getTBoxModel()));
unionOms.setTBoxModel(unionTBoxModel); unionOms.setTBoxModel(unionTBoxModel);
Dataset dataset = DatasetStore.create(store); // add the vitro ontologies to the tbox models
//String queryStr = "CONSTRUCT { ?s ?p ?o } \n" + OntModel vitroTBoxModel = (new JenaBaseDaoCon()).getConstModel();
// "WHERE { GRAPH ?g { ?s ?p ?o } } "; baseOms.getTBoxModel().addSubModel(vitroTBoxModel);
//Query query = QueryFactory.create(queryStr); inferenceOms.getTBoxModel().addSubModel(vitroTBoxModel);
//QueryExecution qe = QueryExecutionFactory.create(query, dataset); unionOms.getTBoxModel().addSubModel(vitroTBoxModel);
//log.info("Test query returned " + qe.execConstruct().size() + " statements");
sce.getServletContext().setAttribute("baseOntModel", memModel); // create TBox + ABox union models and set up webapp DAO factories
OntModel baseUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
ModelFactory.createUnion(baseOms.getABoxModel(), baseOms.getTBoxModel()));
baseOms.setFullModel(baseUnion);
ModelContext.setBaseOntModel(baseOms.getFullModel(), sce.getServletContext());
WebappDaoFactory baseWadf = new WebappDaoFactorySDB(baseOms, bds, storeDesc, defaultNamespace, null, null); WebappDaoFactory baseWadf = new WebappDaoFactorySDB(baseOms, bds, storeDesc, defaultNamespace, null, null);
//WebappDaoFactory baseWadf = new WebappDaoFactorySDB(baseOms, dataset, defaultNamespace, null, null); //WebappDaoFactory baseWadf = new WebappDaoFactorySDB(baseOms, dataset, defaultNamespace, null, null);
sce.getServletContext().setAttribute("assertionsWebappDaoFactory",baseWadf); sce.getServletContext().setAttribute("assertionsWebappDaoFactory",baseWadf);
sce.getServletContext().setAttribute("inferenceOntModel", inferenceModel); OntModel inferenceUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
ModelFactory.createUnion(inferenceOms.getABoxModel(), inferenceOms.getTBoxModel()));
inferenceOms.setFullModel(inferenceUnion);
ModelContext.setInferenceOntModel(inferenceOms.getFullModel(), sce.getServletContext());
WebappDaoFactory infWadf = new WebappDaoFactorySDB(inferenceOms, bds, storeDesc, defaultNamespace, null, null); WebappDaoFactory infWadf = new WebappDaoFactorySDB(inferenceOms, bds, storeDesc, defaultNamespace, null, null);
//WebappDaoFactory infWadf = new WebappDaoFactorySDB(inferenceOms, dataset, defaultNamespace, null, null); //WebappDaoFactory infWadf = new WebappDaoFactorySDB(inferenceOms, dataset, defaultNamespace, null, null);
sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf); sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf);
OntModel masterUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, OntModel masterUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
ModelFactory.createUnion(unionABoxModel, unionTBoxModel)); ModelFactory.createUnion(unionABoxModel, unionTBoxModel));
unionOms.setFullModel(masterUnion);
sce.getServletContext().setAttribute("jenaOntModel", masterUnion); sce.getServletContext().setAttribute("jenaOntModel", masterUnion);
WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, bds, storeDesc, defaultNamespace, null, null); WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, bds, storeDesc, defaultNamespace, null, null);
//WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, dataset, defaultNamespace, null, null); //WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, dataset, defaultNamespace, null, null);
@ -259,37 +211,41 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
sce.getServletContext().setAttribute("baseOntModelSelector", baseOms); //assertions sce.getServletContext().setAttribute("baseOntModelSelector", baseOms); //assertions
sce.getServletContext().setAttribute("inferenceOntModelSelector", inferenceOms); //inferences sce.getServletContext().setAttribute("inferenceOntModelSelector", inferenceOms); //inferences
// use "full" models as the legacy application metadata models
baseOms.setApplicationMetadataModel(baseOms.getFullModel());
inferenceOms.setApplicationMetadataModel(inferenceOms.getFullModel());
unionOms.setApplicationMetadataModel(unionOms.getFullModel());
ApplicationBean appBean = getApplicationBeanFromOntModel(memModel,wadf); ApplicationBean appBean = getApplicationBeanFromOntModel(unionOms.getFullModel(),wadf);
if (appBean != null) { if (appBean != null) {
sce.getServletContext().setAttribute("applicationBean", appBean); sce.getServletContext().setAttribute("applicationBean", appBean);
} }
if (isEmpty(memModel)) { if (isEmpty(unionOms.getFullModel())) {
loadDataFromFilesystem(memModel, sce.getServletContext()); loadDataFromFilesystem(unionOms.getFullModel(), sce.getServletContext());
} }
if (userAccountsModel.size() == 0) { if (userAccountsModel.size() == 0) {
readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsModel); readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsModel);
if (userAccountsModel.size() == 0) { if (userAccountsModel.size() == 0) {
createInitialAdminUser(userAccountsModel); createInitialAdminUser(userAccountsModel);
} }
} }
ensureEssentialInterfaceData(memModel, sce, wadf); ensureEssentialInterfaceData(unionOms.getFullModel(), sce, wadf);
NamespaceMapper namespaceMapper = new NamespaceMapperJena(unionModel, unionModel, defaultNamespace); NamespaceMapper namespaceMapper = new NamespaceMapperJena(masterUnion, masterUnion, defaultNamespace);
sce.getServletContext().setAttribute("NamespaceMapper", namespaceMapper); sce.getServletContext().setAttribute("NamespaceMapper", namespaceMapper);
memModel.getBaseModel().register(namespaceMapper); unionOms.getFullModel().getBaseModel().register(namespaceMapper);
sce.getServletContext().setAttribute("defaultNamespace", defaultNamespace); sce.getServletContext().setAttribute("defaultNamespace", defaultNamespace);
makeModelMakerFromConnectionProperties(TripleStoreType.RDB); makeModelMakerFromConnectionProperties(TripleStoreType.RDB);
VitroJenaModelMaker vjmm = getVitroJenaModelMaker(); VitroJenaModelMaker vjmm = getVitroJenaModelMaker();
setVitroJenaModelMaker(vjmm,sce); setVitroJenaModelMaker(vjmm,sce);
makeModelMakerFromConnectionProperties(TripleStoreType.SDB); makeModelMakerFromConnectionProperties(TripleStoreType.SDB);
VitroJenaSDBModelMaker vsmm = getVitroJenaSDBModelMaker(); VitroJenaSDBModelMaker vsmm = getVitroJenaSDBModelMaker();
setVitroJenaSDBModelMaker(vsmm,sce); setVitroJenaSDBModelMaker(vsmm,sce);
} catch (Throwable t) { } catch (Throwable t) {
log.error("Throwable in " + this.getClass().getName(), t); log.error("Throwable in " + this.getClass().getName(), t);
@ -310,9 +266,9 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
List<String> portalURIs = new ArrayList<String>(); List<String> portalURIs = new ArrayList<String>();
try { try {
model.enterCriticalSection(Lock.READ); model.enterCriticalSection(Lock.READ);
Iterator portalIt = model.listIndividuals(PORTAL); Iterator<Individual> portalIt = model.listIndividuals(PORTAL);
while (portalIt.hasNext()) { while (portalIt.hasNext()) {
portalURIs.add( ((Individual)portalIt.next()).getURI() ); portalURIs.add( portalIt.next().getURI() );
} }
} finally { } finally {
model.leaveCriticalSection(); model.leaveCriticalSection();
@ -322,7 +278,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
if( portalUri != null && ! portalUri.startsWith(defaultNamespaceFromDeployProperites)){ if( portalUri != null && ! portalUri.startsWith(defaultNamespaceFromDeployProperites)){
log.error("Namespace mismatch between db and deploy.properties."); log.error("Namespace mismatch between db and deploy.properties.");
log.error("Vivo will not start up correctly because the default namespace specified in deploy.properties does not match the namespace of " + log.error("Vivo will not start up correctly because the default namespace specified in deploy.properties does not match the namespace of " +
"a portal in the database. Namespace from deploy.properties: \"" + defaultNamespaceFromDeployProperites + "a portal in the database. Namespace from deploy.properties: \"" + defaultNamespaceFromDeployProperites +
"\" Namespace from an existing portal: \"" + portalUri + "\" To get the application to start with this " + "\" Namespace from an existing portal: \"" + portalUri + "\" To get the application to start with this " +
"database change the default namespace in deploy.properties " + portalUri.substring(0, portalUri.lastIndexOf("/")+1) + "database change the default namespace in deploy.properties " + portalUri.substring(0, portalUri.lastIndexOf("/")+1) +
" Another possibility is that deploy.properties does not specify the intended database."); " Another possibility is that deploy.properties does not specify the intended database.");
@ -339,7 +295,8 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
} }
private ApplicationBean getApplicationBeanFromOntModel(OntModel ontModel,WebappDaoFactory wadf) { private ApplicationBean getApplicationBeanFromOntModel(OntModel ontModel,WebappDaoFactory wadf) {
ClosableIterator appIt = ontModel.listIndividuals(ResourceFactory.createResource(VitroVocabulary.APPLICATION)); ClosableIterator<Individual> appIt = ontModel.listIndividuals(
ResourceFactory.createResource(VitroVocabulary.APPLICATION));
try { try {
if (appIt.hasNext()) { if (appIt.hasNext()) {
Individual appInd = (Individual) appIt.next(); Individual appInd = (Individual) appIt.next();
@ -354,11 +311,11 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
appBean.setMaxSharedPortalId(Integer.decode( ((Literal)appInd.getPropertyValue(ResourceFactory.createProperty(VitroVocabulary.APPLICATION_MAXSHAREDPORTALID))).getLexicalForm())); appBean.setMaxSharedPortalId(Integer.decode( ((Literal)appInd.getPropertyValue(ResourceFactory.createProperty(VitroVocabulary.APPLICATION_MAXSHAREDPORTALID))).getLexicalForm()));
} catch (Exception e) { /* ignore bad value */} } catch (Exception e) { /* ignore bad value */}
if( ! wadf.getApplicationDao().isFlag1Active() ){ if( ! wadf.getApplicationDao().isFlag1Active() ){
appBean.setMaxPortalId(1); appBean.setMaxPortalId(1);
} }
return appBean; return appBean;
} else { } else {
return null; return null;
} }
} finally { } finally {
appIt.close(); appIt.close();
@ -367,77 +324,78 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
private void ensureEssentialInterfaceData(OntModel memModel, ServletContextEvent sce, WebappDaoFactory wadf) { private void ensureEssentialInterfaceData(OntModel memModel, ServletContextEvent sce, WebappDaoFactory wadf) {
Model essentialInterfaceData = null; Model essentialInterfaceData = null;
ClosableIterator portalIt = memModel.listIndividuals(memModel.getResource(VitroVocabulary.PORTAL)); ClosableIterator<Individual> portalIt = memModel.listIndividuals(
memModel.getResource(VitroVocabulary.PORTAL));
try { try {
if (!portalIt.hasNext()) { if (!portalIt.hasNext()) {
log.debug("Loading initial site configuration"); log.debug("Loading initial site configuration");
essentialInterfaceData = InitialJenaModelUtils.loadInitialModel(sce.getServletContext(), defaultNamespace); essentialInterfaceData = InitialJenaModelUtils.loadInitialModel(sce.getServletContext(), defaultNamespace);
if (essentialInterfaceData.size() == 0) { if (essentialInterfaceData.size() == 0) {
essentialInterfaceData = InitialJenaModelUtils.basicPortalAndRootTab(defaultNamespace); essentialInterfaceData = InitialJenaModelUtils.basicPortalAndRootTab(defaultNamespace);
essentialInterfaceData.add(InitialJenaModelUtils.basicClassgroup(wadf.getDefaultNamespace())); essentialInterfaceData.add(InitialJenaModelUtils.basicClassgroup(wadf.getDefaultNamespace()));
} }
//JenaModelUtils.makeClassGroupsFromRootClasses(wadf,memModel,essentialInterfaceData); //JenaModelUtils.makeClassGroupsFromRootClasses(wadf,memModel,essentialInterfaceData);
memModel.add(essentialInterfaceData); memModel.add(essentialInterfaceData);
} else { } else {
//Set the default namespace to the namespace of the first portal object we find. //Set the default namespace to the namespace of the first portal object we find.
//This will keep existing applications from dying when the default namespace //This will keep existing applications from dying when the default namespace
//config option is missing. //config option is missing.
Individual portal = (Individual) portalIt.next(); Individual portal = (Individual) portalIt.next();
if (portal.getNameSpace() != null) { if (portal.getNameSpace() != null) {
defaultNamespace = portal.getNameSpace(); defaultNamespace = portal.getNameSpace();
} }
} }
} finally { } finally {
portalIt.close(); portalIt.close();
} }
} }
private void checkMainModelForUserAccounts(OntModel mainModel, OntModel userAccountsModel) { private void checkMainModelForUserAccounts(OntModel mainModel, OntModel userAccountsModel) {
Model extractedUserData = ((new JenaModelUtils()).extractUserAccountsData(mainModel)); Model extractedUserData = ((new JenaModelUtils()).extractUserAccountsData(mainModel));
if (extractedUserData.size() > 0) { if (extractedUserData.size() > 0) {
userAccountsModel.enterCriticalSection(Lock.WRITE); userAccountsModel.enterCriticalSection(Lock.WRITE);
try { try {
userAccountsModel.add(extractedUserData); userAccountsModel.add(extractedUserData);
} finally { } finally {
userAccountsModel.leaveCriticalSection(); userAccountsModel.leaveCriticalSection();
} }
mainModel.enterCriticalSection(Lock.WRITE); mainModel.enterCriticalSection(Lock.WRITE);
try { try {
mainModel.remove(extractedUserData); mainModel.remove(extractedUserData);
} finally { } finally {
mainModel.leaveCriticalSection(); mainModel.leaveCriticalSection();
} }
} }
} }
private OntModel ontModelFromContextAttribute(ServletContext ctx, String attribute) { private OntModel ontModelFromContextAttribute(ServletContext ctx, String attribute) {
OntModel ontModel; OntModel ontModel;
Object attributeValue = ctx.getAttribute(attribute); Object attributeValue = ctx.getAttribute(attribute);
if (attributeValue != null && attributeValue instanceof OntModel) { if (attributeValue != null && attributeValue instanceof OntModel) {
ontModel = (OntModel) attributeValue; ontModel = (OntModel) attributeValue;
} else { } else {
ontModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); ontModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
ctx.setAttribute(attribute, ontModel); ctx.setAttribute(attribute, ontModel);
} }
return ontModel; return ontModel;
} }
private boolean isEmpty(Model model) { private boolean isEmpty(Model model) {
ClosableIterator closeIt = model.listStatements(); ClosableIterator<Statement> closeIt = model.listStatements();
try { try {
if (closeIt.hasNext()) { if (closeIt.hasNext()) {
return false; return false;
} else { } else {
return true; return true;
} }
} finally { } finally {
closeIt.close(); closeIt.close();
} }
} }
private void loadDataFromFilesystem(OntModel ontModel, ServletContext ctx) { private void loadDataFromFilesystem(OntModel ontModel, ServletContext ctx) {
OntModel initialDataModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); OntModel initialDataModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
Long startTime = System.currentTimeMillis(); Long startTime = System.currentTimeMillis();
log.debug("Reading ontology files"); log.debug("Reading ontology files");
readOntologyFilesInPathSet(USERPATH, ctx, initialDataModel); readOntologyFilesInPathSet(USERPATH, ctx, initialDataModel);
readOntologyFilesInPathSet(SYSTEMPATH, ctx, initialDataModel); readOntologyFilesInPathSet(SYSTEMPATH, ctx, initialDataModel);
@ -445,9 +403,9 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
ontModel.add(initialDataModel); ontModel.add(initialDataModel);
} }
private void getTBoxModel(Model fullModel, Model submodels, Model tboxModel) { private static void getTBoxModel(Model fullModel, Model submodels, Model tboxModel) {
JenaModelUtils modelUtils = new JenaModelUtils(); JenaModelUtils modelUtils = new JenaModelUtils();
Model tempModel = ModelFactory.createUnion(fullModel, submodels); Model tempModel = ModelFactory.createUnion(fullModel, submodels);
Model tempTBoxModel = modelUtils.extractTBox(tempModel); Model tempTBoxModel = modelUtils.extractTBox(tempModel);
@ -456,29 +414,92 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
StmtIterator iter = tempTBoxModel.listStatements(); StmtIterator iter = tempTBoxModel.listStatements();
while (iter.hasNext()) { while (iter.hasNext()) {
Statement stmt = iter.next(); Statement stmt = iter.next();
if (fullModel.contains(stmt)) { if (fullModel.contains(stmt)) {
tboxModel.add(stmt); tboxModel.add(stmt);
} }
} }
return; return;
} }
/* /*
* Copy all statements from model 1 that are not in model 2 to model 3. * Copy all statements from model 1 that are not in model 2 to model 3.
*/ */
private void copyDifference(Model model1, Model model2, Model model3) { private static void copyDifference(Model model1, Model model2, Model model3) {
StmtIterator iter = model1.listStatements(); StmtIterator iter = model1.listStatements();
while (iter.hasNext()) { while (iter.hasNext()) {
Statement stmt = iter.next(); Statement stmt = iter.next();
if (!model2.contains(stmt)) { if (!model2.contains(stmt)) {
model3.add(stmt); model3.add(stmt);
} }
} }
return; return;
} }
public static void setupSDB(ServletContext ctx,
Store store,
Model memModel,
Model inferenceModel) {
store.getTableFormatter().create();
store.getTableFormatter().truncate();
// This is a one-time copy of stored KB data - from a Jena RDB store
// to a Jena SDB store. In the process, we will also separate out the
// TBox from the Abox; these are in the same graph in pre 1.2 VIVO
// versions and will now be stored and maintained in separate models
// Access to the Jena RDB data is through the OntModelSelectors that have
// been set up earlier in the current session by
// JenaPersistentDataSourceSetup.java
// In the code below, note that the current getABoxModel() methods on
// the OntModelSelectors return a graph with both ABox and TBox data.
OntModel submodels = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
readOntologyFilesInPathSet(SUBMODELS, ctx, submodels);
Model tboxAssertions = SDBFactory.connectNamedModel(store, JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL);
// initially putting the results in memory so we have a
// cheaper way of computing the difference when we copy the ABox
Model memTboxAssertions = ModelFactory.createDefaultModel();
getTBoxModel(memModel, submodels, memTboxAssertions);
tboxAssertions.add(memTboxAssertions);
Model tboxInferences = SDBFactory.connectNamedModel(store, JenaDataSourceSetupBase.JENA_TBOX_INF_MODEL);
// initially putting the results in memory so we have a
// cheaper way of computing the difference when we copy the ABox
Model memTboxInferences = ModelFactory.createDefaultModel();
getTBoxModel(inferenceModel, submodels, memTboxInferences);
tboxInferences.add(memTboxInferences);
Model aboxAssertions = SDBFactory.connectNamedModel(store, JenaDataSourceSetupBase.JENA_DB_MODEL);
copyDifference(memModel, memTboxAssertions, aboxAssertions);
Model aboxInferences = SDBFactory.connectNamedModel(store, JenaDataSourceSetupBase.JENA_INF_MODEL);
copyDifference(inferenceModel, memTboxInferences, aboxInferences);
// Make sure the reasoner takes into account the newly-set-up data.
SimpleReasonerSetup.setRecomputeRequired(ctx);
}
/**
* Tests whether an SDB store has been formatted for use.
* @param store
* @return
*/
private boolean isSetUp(Store store) {
try {
// a test query to see if the store is formatted
SDBFactory.connectDefaultModel(store).contains(
OWL.Thing, RDF.type, OWL.Nothing);
return true;
} catch (Exception e) { // unformatted store
return false;
}
}
} }

View file

@ -105,6 +105,23 @@ public class SimpleReasonerSetup implements ServletContextListener {
// the simple reasoner will register itself as a listener to the ABox assertions // the simple reasoner will register itself as a listener to the ABox assertions
SimpleReasoner simpleReasoner = new SimpleReasoner(unionOms.getTBoxModel(), assertionsOms.getABoxModel(), inferencesOms.getABoxModel(), rebuildModel, scratchModel); SimpleReasoner simpleReasoner = new SimpleReasoner(unionOms.getTBoxModel(), assertionsOms.getABoxModel(), inferencesOms.getABoxModel(), rebuildModel, scratchModel);
if (isRecomputeRequired(sce.getServletContext())) {
log.info("ABox inference recompute required");
int sleeps = 0;
while (sleeps < 1000 && pelletListener.isReasoning()) {
if ((sleeps % 30) == 0) {
log.info("Waiting for initial TBox reasoning to complete");
}
Thread.sleep(100);
sleeps++;
}
simpleReasoner.recompute();
}
assertionsOms.getTBoxModel().register(new SimpleReasonerTBoxListener(simpleReasoner)); assertionsOms.getTBoxModel().register(new SimpleReasonerTBoxListener(simpleReasoner));
sce.getServletContext().setAttribute("simpleReasoner",simpleReasoner); sce.getServletContext().setAttribute("simpleReasoner",simpleReasoner);
@ -120,4 +137,15 @@ public class SimpleReasonerSetup implements ServletContextListener {
// nothing to do // nothing to do
} }
private static final String RECOMPUTE_REQUIRED_ATTR =
SimpleReasonerSetup.class.getName() + ".recomputeRequired";
public static void setRecomputeRequired(ServletContext ctx) {
ctx.setAttribute(RECOMPUTE_REQUIRED_ATTR, true);
}
private static boolean isRecomputeRequired(ServletContext ctx) {
return (ctx.getAttribute(RECOMPUTE_REQUIRED_ATTR) != null);
}
} }

View file

@ -38,9 +38,9 @@ PREFIX <%=prefixText%>: <<%=urlText%>><%}}%>
SELECT ?geoLocation ?label SELECT ?geoLocation ?label
WHERE WHERE
{ {
GRAPH ?g{ GRAPH ?g {
?geoLocation rdf:type core:GeographicLocation . ?geoLocation rdf:type core:GeographicLocation
OPTIONAL { ?geoLocation rdfs:label ?label } . OPTIONAL { GRAPH ?h { ?geoLocation rdfs:label ?label } }
} }
} }
LIMIT 20 LIMIT 20