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,12 +39,12 @@ 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;
@ -70,8 +68,6 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
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");
@ -79,22 +75,11 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
checkMainModelForUserAccounts(memModel, userAccountsModel);
}
OntModel unionModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(memModel, inferenceModel));
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);
@ -120,56 +105,16 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
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
sce.getServletContext().setAttribute("kbStore", store);
if (!isSetUp(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);
setupSDB(sce.getServletContext(), store, memModel, inferenceModel);
}
// 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);
//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.
@ -231,25 +176,32 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
OntModel unionTBoxModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,ModelFactory.createUnion(baseOms.getTBoxModel(), inferenceOms.getTBoxModel()));
unionOms.setTBoxModel(unionTBoxModel);
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");
// 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);
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));
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,14 +211,18 @@ 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);
}
if (isEmpty(memModel)) {
loadDataFromFilesystem(memModel, sce.getServletContext());
if (isEmpty(unionOms.getFullModel())) {
loadDataFromFilesystem(unionOms.getFullModel(), sce.getServletContext());
}
if (userAccountsModel.size() == 0) {
@ -276,11 +232,11 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
}
}
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);
memModel.getBaseModel().register(namespaceMapper);
unionOms.getFullModel().getBaseModel().register(namespaceMapper);
sce.getServletContext().setAttribute("defaultNamespace", defaultNamespace);
@ -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();
@ -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();
@ -367,7 +324,8 @@ 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");
@ -423,7 +381,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
}
private boolean isEmpty(Model model) {
ClosableIterator closeIt = model.listStatements();
ClosableIterator<Statement> closeIt = model.listStatements();
try {
if (closeIt.hasNext()) {
return false;
@ -445,7 +403,7 @@ 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();
@ -468,7 +426,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
/*
* 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();
@ -481,4 +439,67 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
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
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));
sce.getServletContext().setAttribute("simpleReasoner",simpleReasoner);
@ -120,4 +137,15 @@ public class SimpleReasonerSetup implements ServletContextListener {
// 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

@ -39,8 +39,8 @@ SELECT ?geoLocation ?label
WHERE
{
GRAPH ?g {
?geoLocation rdf:type core:GeographicLocation .
OPTIONAL { ?geoLocation rdfs:label ?label } .
?geoLocation rdf:type core:GeographicLocation
OPTIONAL { GRAPH ?h { ?geoLocation rdfs:label ?label } }
}
}
LIMIT 20