NIHVIVO-1843 KB Migration must happen in RDB mode
This commit is contained in:
parent
40de34558a
commit
304a1c7b1e
5 changed files with 110 additions and 50 deletions
|
@ -305,8 +305,8 @@ public class KnowledgeBaseUpdater {
|
|||
* @param filePath
|
||||
* @return the query string or null if file not found
|
||||
*/
|
||||
private String loadSparqlQuery(String filePath) throws IOException {
|
||||
File file = new File(settings.getAskQueryFile());
|
||||
public static String loadSparqlQuery(String filePath) throws IOException {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -257,13 +257,15 @@ public class TBoxUpdater {
|
|||
siteModel.remove(actualRetractions);
|
||||
record.recordRetractions(actualRetractions);
|
||||
|
||||
long numAdded = actualAdditions.size();
|
||||
long numRemoved = actualRetractions.size();
|
||||
|
||||
// log summary of changes
|
||||
if (actualAdditions.size() > 0) {
|
||||
if (numAdded > 0) {
|
||||
logger.log("Updated the default vitro annotation value for " +
|
||||
actualAdditions.size() + " statements in the knowledge base");
|
||||
numAdded + " statements in the knowledge base");
|
||||
}
|
||||
|
||||
long numRemoved = actualRetractions.size() - actualAdditions.size();
|
||||
if (numRemoved > 0) {
|
||||
logger.log("Removed " + numRemoved +
|
||||
" outdated vitro annotation property setting" + ((numRemoved > 1) ? "s" : "") + " from the knowledge base");
|
||||
|
|
|
@ -29,6 +29,7 @@ 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.SearchReindexingListener;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.SimpleOntModelSelector;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker;
|
||||
|
@ -45,22 +46,22 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
|
|||
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
|
||||
String tripleStoreTypeStr =
|
||||
ConfigurationProperties.getProperty(
|
||||
"VitroConnection.DataSource.tripleStoreType", "RDB");
|
||||
|
||||
if ("SDB".equals(tripleStoreTypeStr)) {
|
||||
(new JenaDataSourceSetupSDB()).contextInitialized(sce);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
String tripleStoreTypeStr =
|
||||
ConfigurationProperties.getProperty(
|
||||
"VitroConnection.DataSource.tripleStoreType", "RDB");
|
||||
|
||||
//FIXME improve
|
||||
if ("SDB".equals(tripleStoreTypeStr)) {
|
||||
(new JenaDataSourceSetupSDB()).contextInitialized(sce);
|
||||
return;
|
||||
}
|
||||
|
||||
OntModel memModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel");
|
||||
if (memModel == null) {
|
||||
memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
|
||||
|
@ -96,19 +97,19 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
|
|||
WebappDaoFactory baseWadf = new WebappDaoFactoryJena(
|
||||
baseOms, defaultNamespace, null, null);
|
||||
sce.getServletContext().setAttribute("assertionsWebappDaoFactory",baseWadf);
|
||||
sce.getServletContext().setAttribute("baseOntModelSelector", baseOms);
|
||||
ModelContext.setBaseOntModelSelector(baseOms, sce.getServletContext());
|
||||
|
||||
sce.getServletContext().setAttribute("inferenceOntModel", inferenceModel);
|
||||
WebappDaoFactory infWadf = new WebappDaoFactoryJena(
|
||||
inferenceOms, defaultNamespace, null, null);
|
||||
sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf);
|
||||
sce.getServletContext().setAttribute("inferenceOntModelSelector", inferenceOms);
|
||||
ModelContext.setInferenceOntModelSelector(inferenceOms, sce.getServletContext());
|
||||
|
||||
sce.getServletContext().setAttribute("jenaOntModel", unionModel);
|
||||
WebappDaoFactory wadf = new WebappDaoFactoryJena(
|
||||
unionOms, baseOms, inferenceOms, defaultNamespace, null, null);
|
||||
sce.getServletContext().setAttribute("webappDaoFactory",wadf);
|
||||
sce.getServletContext().setAttribute("unionOntModelSelector", unionOms);
|
||||
ModelContext.setUnionOntModelSelector(unionOms, sce.getServletContext());
|
||||
|
||||
ApplicationBean appBean = getApplicationBeanFromOntModel(memModel,wadf);
|
||||
if (appBean != null) {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
@ -51,6 +52,7 @@ 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.ontology.update.KnowledgeBaseUpdater;
|
||||
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;
|
||||
|
@ -73,6 +75,15 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
|
|||
// that it is not executed in a post-sdb-conversion environment.
|
||||
OntModel memModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel");
|
||||
|
||||
if ( updateRequired(sce.getServletContext(), memModel)) {
|
||||
log.error(getMigrationErrString());
|
||||
System.out.println(getMigrationErrString());
|
||||
// The rest of the application should not
|
||||
// start if this condition is encountered
|
||||
AbortStartup.abortStartup(sce.getServletContext());
|
||||
throw new MigrationRequiredError(getMigrationErrString());
|
||||
}
|
||||
|
||||
if (memModel == null) {
|
||||
memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
|
||||
log.warn("WARNING: no database connected. Changes will disappear after context restart.");
|
||||
|
@ -255,12 +266,12 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
|
|||
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);
|
||||
sce.getServletContext().setAttribute("webappDaoFactory",wadf);
|
||||
|
||||
sce.getServletContext().setAttribute("unionOntModelSelector", unionOms); //assertions and inferences
|
||||
sce.getServletContext().setAttribute("baseOntModelSelector", baseOms); //assertions
|
||||
sce.getServletContext().setAttribute("inferenceOntModelSelector", inferenceOms); //inferences
|
||||
ModelContext.setUnionOntModelSelector(unionOms, sce.getServletContext()); // assertions and inferences
|
||||
ModelContext.setBaseOntModelSelector(baseOms, sce.getServletContext()); // assertions
|
||||
ModelContext.setInferenceOntModelSelector(inferenceOms, sce.getServletContext()); // inferences
|
||||
|
||||
ApplicationBean appBean = getApplicationBeanFromOntModel(unionOms.getFullModel(),wadf);
|
||||
if (appBean != null) {
|
||||
sce.getServletContext().setAttribute("applicationBean", appBean);
|
||||
|
@ -302,6 +313,8 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
|
|||
|
||||
log.info("Model makers set up");
|
||||
|
||||
} catch (MigrationRequiredError mre) {
|
||||
throw new MigrationRequiredError(mre.getMessage());
|
||||
} catch (Throwable t) {
|
||||
log.error("Throwable in " + this.getClass().getName(), t);
|
||||
// printing the error because Tomcat doesn't print context listener
|
||||
|
@ -645,4 +658,46 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
|
|||
return (Store) ctx.getAttribute(STORE_ATTR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a SPARQL ASK query to determine whether the knowledge base
|
||||
* needs to be updated to conform to a new ontology version
|
||||
*/
|
||||
public boolean updateRequired(ServletContext ctx, OntModel m) throws IOException {
|
||||
|
||||
String sparqlQueryStr = KnowledgeBaseUpdater.loadSparqlQuery(UpdateKnowledgeBase.getAskQueryPath(ctx));
|
||||
if (sparqlQueryStr == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Query query = QueryFactory.create(sparqlQueryStr);
|
||||
QueryExecution qexec = QueryExecutionFactory.create(query, m);
|
||||
|
||||
// if the ASK query DOES have a solution (i.e. the assertions exist
|
||||
// showing that the update has already been performed), then the update
|
||||
// is NOT required.
|
||||
return !qexec.execAsk();
|
||||
|
||||
}
|
||||
|
||||
private String getMigrationErrString() {
|
||||
String errMessage = "\n*******************************************************************";
|
||||
errMessage += "\nA knowledge base migration is " +
|
||||
"required and this must be done in" +
|
||||
" RDB mode before converting to SDB. " +
|
||||
"Please change deploy.properties to" +
|
||||
" use RDB mode, redeploy, and restart. After " +
|
||||
"the knowledge base migration has completed " +
|
||||
"successfully, change deploy.properties to use " +
|
||||
"SDB mode, redeploy, and restart.\n";
|
||||
errMessage += "*******************************************************************";
|
||||
|
||||
return errMessage;
|
||||
}
|
||||
|
||||
private class MigrationRequiredError extends Error {
|
||||
public MigrationRequiredError(String string) {
|
||||
super(string);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,8 +29,8 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
|||
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDao;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.SimpleOntModelSelector;
|
||||
import edu.cornell.mannlib.vitro.webapp.ontology.update.UpdateSettings;
|
||||
import edu.cornell.mannlib.vitro.webapp.ontology.update.KnowledgeBaseUpdater;
|
||||
import edu.cornell.mannlib.vitro.webapp.ontology.update.UpdateSettings;
|
||||
import edu.cornell.mannlib.vitro.webapp.search.lucene.LuceneSetup;
|
||||
|
||||
/**
|
||||
|
@ -43,25 +43,25 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
|
||||
private final static Log log = LogFactory.getLog(UpdateKnowledgeBase.class);
|
||||
|
||||
private final String DATA_DIR = "/WEB-INF/ontologies/update/";
|
||||
private final String LOG_DIR = "logs/";
|
||||
private final String CHANGED_DATA_DIR = "changedData/";
|
||||
private final String ASK_QUERY_FILE = DATA_DIR + "ask.sparql";
|
||||
private final String SUCCESS_ASSERTIONS_FILE = DATA_DIR + "success.n3";
|
||||
private final String SUCCESS_RDF_FORMAT = "N3";
|
||||
private final String DIFF_FILE = DATA_DIR + "diff.tab.txt";
|
||||
private final String LOG_FILE = DATA_DIR + LOG_DIR + "knowledgeBaseUpdate.log";
|
||||
private final String ERROR_LOG_FILE = DATA_DIR + LOG_DIR + "knowledgeBaseUpdate.error.log";
|
||||
private final String REMOVED_DATA_FILE = DATA_DIR + CHANGED_DATA_DIR + "removedData.n3";
|
||||
private final String ADDED_DATA_FILE = DATA_DIR + CHANGED_DATA_DIR + "addedData.n3";
|
||||
private final String SPARQL_CONSTRUCT_ADDITIONS_DIR = DATA_DIR + "sparqlConstructs/additions/";
|
||||
private final String SPARQL_CONSTRUCT_ADDITIONS_PASS2_DIR = DATA_DIR + "sparqlConstructs/additions-pass2/";
|
||||
private final String SPARQL_CONSTRUCT_DELETIONS_DIR = DATA_DIR + "sparqlConstructs/deletions/";
|
||||
private final String MISC_REPLACEMENTS_FILE = DATA_DIR + "miscReplacements.rdf";
|
||||
private final String OLD_TBOX_MODEL_DIR = DATA_DIR + "oldVersion/";
|
||||
private final String NEW_TBOX_MODEL_DIR = "/WEB-INF/submodels/";
|
||||
private final String OLD_TBOX_ANNOTATIONS_DIR = DATA_DIR + "oldAnnotations/";
|
||||
private final String NEW_TBOX_ANNOTATIONS_DIR = "/WEB-INF/ontologies/user";
|
||||
private static final String DATA_DIR = "/WEB-INF/ontologies/update/";
|
||||
private static final String LOG_DIR = "logs/";
|
||||
private static final String CHANGED_DATA_DIR = "changedData/";
|
||||
private static final String ASK_QUERY_FILE = DATA_DIR + "ask.sparql";
|
||||
private static final String SUCCESS_ASSERTIONS_FILE = DATA_DIR + "success.n3";
|
||||
private static final String SUCCESS_RDF_FORMAT = "N3";
|
||||
private static final String DIFF_FILE = DATA_DIR + "diff.tab.txt";
|
||||
private static final String LOG_FILE = DATA_DIR + LOG_DIR + "knowledgeBaseUpdate.log";
|
||||
private static final String ERROR_LOG_FILE = DATA_DIR + LOG_DIR + "knowledgeBaseUpdate.error.log";
|
||||
private static final String REMOVED_DATA_FILE = DATA_DIR + CHANGED_DATA_DIR + "removedData.n3";
|
||||
private static final String ADDED_DATA_FILE = DATA_DIR + CHANGED_DATA_DIR + "addedData.n3";
|
||||
private static final String SPARQL_CONSTRUCT_ADDITIONS_DIR = DATA_DIR + "sparqlConstructs/additions/";
|
||||
private static final String SPARQL_CONSTRUCT_ADDITIONS_PASS2_DIR = DATA_DIR + "sparqlConstructs/additions-pass2/";
|
||||
private static final String SPARQL_CONSTRUCT_DELETIONS_DIR = DATA_DIR + "sparqlConstructs/deletions/";
|
||||
private static final String MISC_REPLACEMENTS_FILE = DATA_DIR + "miscReplacements.rdf";
|
||||
private static final String OLD_TBOX_MODEL_DIR = DATA_DIR + "oldVersion/";
|
||||
private static final String NEW_TBOX_MODEL_DIR = "/WEB-INF/submodels/";
|
||||
private static final String OLD_TBOX_ANNOTATIONS_DIR = DATA_DIR + "oldAnnotations/";
|
||||
private static final String NEW_TBOX_ANNOTATIONS_DIR = "/WEB-INF/ontologies/user";
|
||||
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
|
||||
|
@ -73,12 +73,10 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
|
||||
ServletContext ctx = sce.getServletContext();
|
||||
|
||||
OntModelSelector oms = new SimpleOntModelSelector(
|
||||
(OntModel) sce.getServletContext().getAttribute(
|
||||
JenaBaseDao.ASSERTIONS_ONT_MODEL_ATTRIBUTE_NAME));
|
||||
OntModelSelector oms = new SimpleOntModelSelector((OntModel) sce.getServletContext().getAttribute(JenaBaseDao.ASSERTIONS_ONT_MODEL_ATTRIBUTE_NAME));
|
||||
|
||||
UpdateSettings settings = new UpdateSettings();
|
||||
settings.setAskQueryFile(ctx.getRealPath(ASK_QUERY_FILE));
|
||||
settings.setAskQueryFile(getAskQueryPath(ctx));
|
||||
settings.setDataDir(ctx.getRealPath(DATA_DIR));
|
||||
settings.setSparqlConstructAdditionsDir(ctx.getRealPath(SPARQL_CONSTRUCT_ADDITIONS_DIR));
|
||||
settings.setSparqlConstructAdditionsPass2Dir(ctx.getRealPath(SPARQL_CONSTRUCT_ADDITIONS_PASS2_DIR));
|
||||
|
@ -238,4 +236,8 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
// nothing to do
|
||||
}
|
||||
|
||||
public static String getAskQueryPath(ServletContext ctx) {
|
||||
return ctx.getRealPath(ASK_QUERY_FILE);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue