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.OntModel;
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.Model;
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.sql.SDBConnection;
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.shared.Lock;
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.jena.JenaBaseDaoCon;
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.OntModelSelectorImpl;
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.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.jena.InitialJenaModelUtils;
import edu.cornell.mannlib.vitro.webapp.utils.jena.NamespaceMapperJena;
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) {
try {
// JenaPersistentDataSourceSetup should have already set this up - it just sets
// up things related to the DB.
// 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.
// JenaPersistentDataSourceSetup should have already set this up - it just sets
// up things related to the DB.
// 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.
OntModel memModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel");
if (memModel == null) {
memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
log.warn("WARNING: no database connected. Changes will disappear after context restart.");
sce.getServletContext().setAttribute("jenaOntModel",memModel);
memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
log.warn("WARNING: no database connected. Changes will disappear after context restart.");
sce.getServletContext().setAttribute("jenaOntModel",memModel);
}
memModel.addSubModel((new JenaBaseDaoCon()).getConstModel()); // add the vitro tbox to the model
OntModel inferenceModel = ontModelFromContextAttribute(sce.getServletContext(), "inferenceOntModel");
OntModel userAccountsModel = ontModelFromContextAttribute(sce.getServletContext(), "userAccountsOntModel");
if (userAccountsModel.size() == 0) {
checkMainModelForUserAccounts(memModel, userAccountsModel);
}
OntModel unionModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(memModel, inferenceModel));
checkMainModelForUserAccounts(memModel, userAccountsModel);
}
OntModelSelectorImpl baseOms = new OntModelSelectorImpl();
baseOms.setApplicationMetadataModel(memModel);
baseOms.setTBoxModel(memModel);
baseOms.setFullModel(memModel);
OntModelSelectorImpl inferenceOms = new OntModelSelectorImpl();
inferenceOms.setABoxModel(inferenceModel);
inferenceOms.setTBoxModel(inferenceModel);
inferenceOms.setFullModel(inferenceModel);
OntModelSelectorImpl unionOms = new OntModelSelectorImpl();
unionOms.setApplicationMetadataModel(unionModel);
unionOms.setTBoxModel(unionModel);
unionOms.setFullModel(unionModel);
baseOms.setUserAccountsModel(userAccountsModel);
inferenceOms.setUserAccountsModel(userAccountsModel);
unionOms.setUserAccountsModel(userAccountsModel);
baseOms.setUserAccountsModel(userAccountsModel);
inferenceOms.setUserAccountsModel(userAccountsModel);
unionOms.setUserAccountsModel(userAccountsModel);
OntModel displayModel = ontModelFromContextAttribute(sce.getServletContext(),"displayOntModel");
baseOms.setDisplayModel(displayModel);
inferenceOms.setDisplayModel(displayModel);
unionOms.setDisplayModel(displayModel);
checkForNamespaceMismatch( memModel, defaultNamespace );
OntModel displayModel = ontModelFromContextAttribute(sce.getServletContext(),"displayOntModel");
baseOms.setDisplayModel(displayModel);
inferenceOms.setDisplayModel(displayModel);
unionOms.setDisplayModel(displayModel);
checkForNamespaceMismatch( memModel, defaultNamespace );
// SDB initialization
// SDB initialization
String layoutStr = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.sdb.layout","layout2/hash");
String dbtypeStr = ConfigurationProperties.getProperty(
@ -115,141 +100,108 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
StoreDesc storeDesc = new StoreDesc(
LayoutType.fetch(layoutStr),
DatabaseType.fetch(dbtypeStr) );
sce.getServletContext().setAttribute("storeDesc", storeDesc);
BasicDataSource bds = makeDataSourceFromConfigurationProperties();
this.setApplicationDataSource(bds, sce.getServletContext());
SDBConnection conn = new SDBConnection(bds.getConnection()) ;
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("storeDesc", storeDesc);
BasicDataSource bds = makeDataSourceFromConfigurationProperties();
this.setApplicationDataSource(bds, sce.getServletContext());
SDBConnection conn = new SDBConnection(bds.getConnection()) ;
Store store = SDBFactory.connectStore(conn, storeDesc);
sce.getServletContext().setAttribute("kbStore", store);
//store.getTableFormatter().dropIndexes();
//store.getTableFormatter().addIndexes();
// Populate the three OntModelSelectors (BaseOntModel=assertions, InferenceOntModel=inferences
// and JenaOntModel=union of assertions and inferences) with the post-SDB-conversion models.
// ABox assertions
Model aboxAssertions = makeDBModel(bds, JenaDataSourceSetupBase.JENA_DB_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
Model listenableAboxAssertions = ModelFactory.createUnion(aboxAssertions, ModelFactory.createDefaultModel());
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
// ABox models do (and so don't need the extra step to make them listenable).
// TBox assertions
try {
Model tboxAssertionsDB = makeDBModel(bds, JENA_TBOX_ASSERTIONS_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
OntModel tboxAssertions = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
if (tboxAssertionsDB != null) {
long startTime = System.currentTimeMillis();
System.out.println("Copying cached tbox assertions into memory");
tboxAssertions.add(tboxAssertionsDB);
System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to load tbox assertions");
}
tboxAssertions.getBaseModel().register(new ModelSynchronizer(tboxAssertionsDB));
baseOms.setTBoxModel(tboxAssertions);
} catch (Throwable e) {
log.error("Unable to load tbox assertion cache from DB", e);
}
// TBox inferences
try {
Model tboxInferencesDB = makeDBModel(bds, JENA_TBOX_INF_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
OntModel tboxInferences = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
if (tboxInferencesDB != null) {
long startTime = System.currentTimeMillis();
System.out.println("Copying cached tbox inferences into memory");
tboxInferences.add(tboxInferencesDB);
System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to load tbox inferences");
}
tboxInferences.getBaseModel().register(new ModelSynchronizer(tboxInferencesDB));
inferenceOms.setTBoxModel(tboxInferences);
} catch (Throwable e) {
log.error("Unable to load tbox inference cache from DB", e);
if (!isSetUp(store)) {
log.debug("Non-SDB system detected. Setting up SDB store");
setupSDB(sce.getServletContext(), store, memModel, inferenceModel);
}
// union ABox
// 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.
// Populate the three OntModelSelectors (BaseOntModel=assertions, InferenceOntModel=inferences
// and JenaOntModel=union of assertions and inferences) with the post-SDB-conversion models.
// ABox assertions
Model aboxAssertions = makeDBModel(bds, JenaDataSourceSetupBase.JENA_DB_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
Model listenableAboxAssertions = ModelFactory.createUnion(aboxAssertions, ModelFactory.createDefaultModel());
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
// ABox models do (and so don't need the extra step to make them listenable).
// TBox assertions
try {
Model tboxAssertionsDB = makeDBModel(bds, JENA_TBOX_ASSERTIONS_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
OntModel tboxAssertions = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
if (tboxAssertionsDB != null) {
long startTime = System.currentTimeMillis();
System.out.println("Copying cached tbox assertions into memory");
tboxAssertions.add(tboxAssertionsDB);
System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to load tbox assertions");
}
tboxAssertions.getBaseModel().register(new ModelSynchronizer(tboxAssertionsDB));
baseOms.setTBoxModel(tboxAssertions);
} catch (Throwable e) {
log.error("Unable to load tbox assertion cache from DB", e);
}
// TBox inferences
try {
Model tboxInferencesDB = makeDBModel(bds, JENA_TBOX_INF_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB);
OntModel tboxInferences = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
if (tboxInferencesDB != null) {
long startTime = System.currentTimeMillis();
System.out.println("Copying cached tbox inferences into memory");
tboxInferences.add(tboxInferencesDB);
System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to load tbox inferences");
}
tboxInferences.getBaseModel().register(new ModelSynchronizer(tboxInferencesDB));
inferenceOms.setTBoxModel(tboxInferences);
} catch (Throwable e) {
log.error("Unable to load tbox inference cache from DB", e);
}
// union ABox
OntModel unionABoxModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(baseOms.getABoxModel(), inferenceOms.getABoxModel()));
unionOms.setABoxModel(unionABoxModel);
// union TBox
OntModel unionTBoxModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(baseOms.getTBoxModel(), inferenceOms.getTBoxModel()));
unionOms.setTBoxModel(unionTBoxModel);
// union TBox
OntModel unionTBoxModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(baseOms.getTBoxModel(), inferenceOms.getTBoxModel()));
unionOms.setTBoxModel(unionTBoxModel);
// add the vitro ontologies to the tbox models
OntModel vitroTBoxModel = (new JenaBaseDaoCon()).getConstModel();
baseOms.getTBoxModel().addSubModel(vitroTBoxModel);
inferenceOms.getTBoxModel().addSubModel(vitroTBoxModel);
unionOms.getTBoxModel().addSubModel(vitroTBoxModel);
Dataset dataset = DatasetStore.create(store);
//String queryStr = "CONSTRUCT { ?s ?p ?o } \n" +
// "WHERE { GRAPH ?g { ?s ?p ?o } } ";
//Query query = QueryFactory.create(queryStr);
//QueryExecution qe = QueryExecutionFactory.create(query, dataset);
//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, dataset, defaultNamespace, null, null);
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, dataset, defaultNamespace, null, null);
sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf);
OntModel masterUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
ModelFactory.createUnion(unionABoxModel, unionTBoxModel));
ModelFactory.createUnion(unionABoxModel, unionTBoxModel));
unionOms.setFullModel(masterUnion);
sce.getServletContext().setAttribute("jenaOntModel", masterUnion);
WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, bds, storeDesc, defaultNamespace, null, null);
//WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, dataset, defaultNamespace, null, null);
@ -259,38 +211,42 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
sce.getServletContext().setAttribute("baseOntModelSelector", baseOms); //assertions
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) {
sce.getServletContext().setAttribute("applicationBean", appBean);
sce.getServletContext().setAttribute("applicationBean", appBean);
}
if (isEmpty(memModel)) {
loadDataFromFilesystem(memModel, sce.getServletContext());
if (isEmpty(unionOms.getFullModel())) {
loadDataFromFilesystem(unionOms.getFullModel(), sce.getServletContext());
}
if (userAccountsModel.size() == 0) {
readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsModel);
if (userAccountsModel.size() == 0) {
createInitialAdminUser(userAccountsModel);
}
readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsModel);
if (userAccountsModel.size() == 0) {
createInitialAdminUser(userAccountsModel);
}
}
ensureEssentialInterfaceData(memModel, sce, wadf);
ensureEssentialInterfaceData(unionOms.getFullModel(), sce, wadf);
NamespaceMapper namespaceMapper = new NamespaceMapperJena(unionModel, unionModel, defaultNamespace);
sce.getServletContext().setAttribute("NamespaceMapper", namespaceMapper);
memModel.getBaseModel().register(namespaceMapper);
sce.getServletContext().setAttribute("defaultNamespace", defaultNamespace);
makeModelMakerFromConnectionProperties(TripleStoreType.RDB);
VitroJenaModelMaker vjmm = getVitroJenaModelMaker();
setVitroJenaModelMaker(vjmm,sce);
makeModelMakerFromConnectionProperties(TripleStoreType.SDB);
VitroJenaSDBModelMaker vsmm = getVitroJenaSDBModelMaker();
setVitroJenaSDBModelMaker(vsmm,sce);
NamespaceMapper namespaceMapper = new NamespaceMapperJena(masterUnion, masterUnion, defaultNamespace);
sce.getServletContext().setAttribute("NamespaceMapper", namespaceMapper);
unionOms.getFullModel().getBaseModel().register(namespaceMapper);
sce.getServletContext().setAttribute("defaultNamespace", defaultNamespace);
makeModelMakerFromConnectionProperties(TripleStoreType.RDB);
VitroJenaModelMaker vjmm = getVitroJenaModelMaker();
setVitroJenaModelMaker(vjmm,sce);
makeModelMakerFromConnectionProperties(TripleStoreType.SDB);
VitroJenaSDBModelMaker vsmm = getVitroJenaSDBModelMaker();
setVitroJenaSDBModelMaker(vsmm,sce);
} catch (Throwable t) {
log.error("Throwable in " + this.getClass().getName(), t);
// printing the error because Tomcat doesn't print context listener
@ -310,9 +266,9 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
List<String> portalURIs = new ArrayList<String>();
try {
model.enterCriticalSection(Lock.READ);
Iterator portalIt = model.listIndividuals(PORTAL);
Iterator<Individual> portalIt = model.listIndividuals(PORTAL);
while (portalIt.hasNext()) {
portalURIs.add( ((Individual)portalIt.next()).getURI() );
portalURIs.add( portalIt.next().getURI() );
}
} finally {
model.leaveCriticalSection();
@ -322,7 +278,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
if( portalUri != null && ! portalUri.startsWith(defaultNamespaceFromDeployProperites)){
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 " +
"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 " +
"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.");
@ -339,7 +295,8 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
}
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 {
if (appIt.hasNext()) {
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()));
} catch (Exception e) { /* ignore bad value */}
if( ! wadf.getApplicationDao().isFlag1Active() ){
appBean.setMaxPortalId(1);
appBean.setMaxPortalId(1);
}
return appBean;
} else {
return null;
return null;
}
} finally {
appIt.close();
@ -367,77 +324,78 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
private void ensureEssentialInterfaceData(OntModel memModel, ServletContextEvent sce, WebappDaoFactory wadf) {
Model essentialInterfaceData = null;
ClosableIterator portalIt = memModel.listIndividuals(memModel.getResource(VitroVocabulary.PORTAL));
ClosableIterator<Individual> portalIt = memModel.listIndividuals(
memModel.getResource(VitroVocabulary.PORTAL));
try {
if (!portalIt.hasNext()) {
log.debug("Loading initial site configuration");
essentialInterfaceData = InitialJenaModelUtils.loadInitialModel(sce.getServletContext(), defaultNamespace);
if (essentialInterfaceData.size() == 0) {
essentialInterfaceData = InitialJenaModelUtils.basicPortalAndRootTab(defaultNamespace);
essentialInterfaceData.add(InitialJenaModelUtils.basicClassgroup(wadf.getDefaultNamespace()));
}
//JenaModelUtils.makeClassGroupsFromRootClasses(wadf,memModel,essentialInterfaceData);
memModel.add(essentialInterfaceData);
} else {
//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
//config option is missing.
Individual portal = (Individual) portalIt.next();
if (portal.getNameSpace() != null) {
defaultNamespace = portal.getNameSpace();
}
}
if (!portalIt.hasNext()) {
log.debug("Loading initial site configuration");
essentialInterfaceData = InitialJenaModelUtils.loadInitialModel(sce.getServletContext(), defaultNamespace);
if (essentialInterfaceData.size() == 0) {
essentialInterfaceData = InitialJenaModelUtils.basicPortalAndRootTab(defaultNamespace);
essentialInterfaceData.add(InitialJenaModelUtils.basicClassgroup(wadf.getDefaultNamespace()));
}
//JenaModelUtils.makeClassGroupsFromRootClasses(wadf,memModel,essentialInterfaceData);
memModel.add(essentialInterfaceData);
} else {
//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
//config option is missing.
Individual portal = (Individual) portalIt.next();
if (portal.getNameSpace() != null) {
defaultNamespace = portal.getNameSpace();
}
}
} finally {
portalIt.close();
portalIt.close();
}
}
private void checkMainModelForUserAccounts(OntModel mainModel, OntModel userAccountsModel) {
Model extractedUserData = ((new JenaModelUtils()).extractUserAccountsData(mainModel));
if (extractedUserData.size() > 0) {
userAccountsModel.enterCriticalSection(Lock.WRITE);
try {
userAccountsModel.add(extractedUserData);
} finally {
userAccountsModel.leaveCriticalSection();
}
mainModel.enterCriticalSection(Lock.WRITE);
try {
mainModel.remove(extractedUserData);
} finally {
mainModel.leaveCriticalSection();
}
}
Model extractedUserData = ((new JenaModelUtils()).extractUserAccountsData(mainModel));
if (extractedUserData.size() > 0) {
userAccountsModel.enterCriticalSection(Lock.WRITE);
try {
userAccountsModel.add(extractedUserData);
} finally {
userAccountsModel.leaveCriticalSection();
}
mainModel.enterCriticalSection(Lock.WRITE);
try {
mainModel.remove(extractedUserData);
} finally {
mainModel.leaveCriticalSection();
}
}
}
private OntModel ontModelFromContextAttribute(ServletContext ctx, String attribute) {
OntModel ontModel;
Object attributeValue = ctx.getAttribute(attribute);
if (attributeValue != null && attributeValue instanceof OntModel) {
ontModel = (OntModel) attributeValue;
} else {
ontModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
ctx.setAttribute(attribute, ontModel);
}
return ontModel;
OntModel ontModel;
Object attributeValue = ctx.getAttribute(attribute);
if (attributeValue != null && attributeValue instanceof OntModel) {
ontModel = (OntModel) attributeValue;
} else {
ontModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
ctx.setAttribute(attribute, ontModel);
}
return ontModel;
}
private boolean isEmpty(Model model) {
ClosableIterator closeIt = model.listStatements();
try {
if (closeIt.hasNext()) {
return false;
} else {
return true;
}
} finally {
closeIt.close();
}
ClosableIterator<Statement> closeIt = model.listStatements();
try {
if (closeIt.hasNext()) {
return false;
} else {
return true;
}
} finally {
closeIt.close();
}
}
private void loadDataFromFilesystem(OntModel ontModel, ServletContext ctx) {
OntModel initialDataModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
Long startTime = System.currentTimeMillis();
OntModel initialDataModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
Long startTime = System.currentTimeMillis();
log.debug("Reading ontology files");
readOntologyFilesInPathSet(USERPATH, ctx, initialDataModel);
readOntologyFilesInPathSet(SYSTEMPATH, ctx, initialDataModel);
@ -445,9 +403,9 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
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 tempTBoxModel = modelUtils.extractTBox(tempModel);
@ -456,29 +414,92 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
StmtIterator iter = tempTBoxModel.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.next();
if (fullModel.contains(stmt)) {
tboxModel.add(stmt);
}
}
Statement stmt = iter.next();
if (fullModel.contains(stmt)) {
tboxModel.add(stmt);
}
}
return;
return;
}
/*
* 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();
while (iter.hasNext()) {
Statement stmt = iter.next();
if (!model2.contains(stmt)) {
model3.add(stmt);
}
}
Statement stmt = iter.next();
if (!model2.contains(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

@ -104,6 +104,23 @@ public class SimpleReasonerSetup implements ServletContextListener {
// 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);
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));
@ -119,5 +136,16 @@ public class SimpleReasonerSetup implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
// 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
WHERE
{
GRAPH ?g{
?geoLocation rdf:type core:GeographicLocation .
OPTIONAL { ?geoLocation rdfs:label ?label } .
GRAPH ?g {
?geoLocation rdf:type core:GeographicLocation
OPTIONAL { GRAPH ?h { ?geoLocation rdfs:label ?label } }
}
}
LIMIT 20